diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-01-13 15:10:27 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-01-13 15:10:27 +0300 |
commit | 39c1496527de559d5d3a5c3b53d11575f435a4dc (patch) | |
tree | 51ed818b49752bb27d8bc7a13e9efcb3e5192c1f /lib/atlassian/jira_connect | |
parent | ab9c1dbb2dc0e591a6ce4466e15766d99f4abf4b (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/atlassian/jira_connect')
-rw-r--r-- | lib/atlassian/jira_connect/client.rb | 16 | ||||
-rw-r--r-- | lib/atlassian/jira_connect/serializers/feature_flag_entity.rb | 83 |
2 files changed, 99 insertions, 0 deletions
diff --git a/lib/atlassian/jira_connect/client.rb b/lib/atlassian/jira_connect/client.rb index 76211eba0bb..5f7550bdbb4 100644 --- a/lib/atlassian/jira_connect/client.rb +++ b/lib/atlassian/jira_connect/client.rb @@ -17,12 +17,14 @@ module Atlassian dev_info = args.slice(:commits, :branches, :merge_requests) build_info = args.slice(:pipelines) deploy_info = args.slice(:deployments) + ff_info = args.slice(:feature_flags) responses = [] responses << store_dev_info(**common, **dev_info) if dev_info.present? responses << store_build_info(**common, **build_info) if build_info.present? responses << store_deploy_info(**common, **deploy_info) if deploy_info.present? + responses << store_ff_info(**common, **ff_info) if ff_info.present? raise ArgumentError, 'Invalid arguments' if responses.empty? responses.compact @@ -30,6 +32,20 @@ module Atlassian private + def store_ff_info(project:, feature_flags:, **opts) + return unless Feature.enabled?(:jira_sync_feature_flags, project) + + items = feature_flags.map { |flag| Serializers::FeatureFlagEntity.represent(flag, opts) } + items.reject! { |item| item.issue_keys.empty? } + + return if items.empty? + + post('/rest/featureflags/0.1/bulk', { + flags: items, + properties: { projectId: "project-#{project.id}" } + }) + end + def store_deploy_info(project:, deployments:, **opts) return unless Feature.enabled?(:jira_sync_deployments, project) diff --git a/lib/atlassian/jira_connect/serializers/feature_flag_entity.rb b/lib/atlassian/jira_connect/serializers/feature_flag_entity.rb new file mode 100644 index 00000000000..e17c150aacb --- /dev/null +++ b/lib/atlassian/jira_connect/serializers/feature_flag_entity.rb @@ -0,0 +1,83 @@ +# frozen_string_literal: true + +module Atlassian + module JiraConnect + module Serializers + class FeatureFlagEntity < Grape::Entity + include Gitlab::Routing + + alias_method :flag, :object + + format_with(:string, &:to_s) + + expose :schema_version, as: :schemaVersion + expose :id, format_with: :string + expose :name, as: :key + expose :update_sequence_id, as: :updateSequenceId + expose :name, as: :displayName + expose :summary + expose :details + expose :issue_keys, as: :issueKeys + + def issue_keys + @issue_keys ||= JiraIssueKeyExtractor.new(flag.description).issue_keys + end + + def schema_version + '1.0' + end + + def update_sequence_id + options[:update_sequence_id] || Client.generate_update_sequence_id + end + + STRATEGY_NAMES = { + ::Operations::FeatureFlags::Strategy::STRATEGY_DEFAULT => 'All users', + ::Operations::FeatureFlags::Strategy::STRATEGY_GITLABUSERLIST => 'User List', + ::Operations::FeatureFlags::Strategy::STRATEGY_GRADUALROLLOUTUSERID => 'Percent of users', + ::Operations::FeatureFlags::Strategy::STRATEGY_FLEXIBLEROLLOUT => 'Percent rollout', + ::Operations::FeatureFlags::Strategy::STRATEGY_USERWITHID => 'User IDs' + }.freeze + + private + + # The summary does not map very well to our FeatureFlag model. + # + # We allow feature flags to have multiple strategies, depending + # on the environment. Jira expects a single rollout strategy. + # + # Also, we don't actually support showing a single flag, so we use the + # edit path as an interim solution. + def summary(strategies = flag.strategies) + { + url: project_url(flag.project) + "/-/feature_flags/#{flag.id}/edit", + lastUpdated: flag.updated_at.iso8601, + status: { + enabled: flag.active, + defaultValue: '', + rollout: { + percentage: strategies.map do |s| + s.parameters['rollout'] || s.parameters['percentage'] + end.compact.first&.to_f, + text: strategies.map { |s| STRATEGY_NAMES[s.name] }.compact.join(', ') + }.compact + } + } + end + + def details + envs = flag.strategies.flat_map do |s| + s.scopes.map do |es| + env_type = es.environment_scope.scan(/development|testing|staging|production/).first + [es.environment_scope, env_type, s] + end + end + + envs.map do |env_name, env_type, strat| + summary([strat]).merge(environment: { name: env_name, type: env_type }.compact) + end + end + end + end + end +end |