Welcome to mirror list, hosted at ThFree Co, Russian Federation.

feature_flag_entity.rb « serializers « jira_connect « atlassian « lib - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: e17c150aacb76049db3e7d816e1a6a93355edca6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
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