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

changed-feature-flags « scripts - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 8c1b219e5a6b1644a5d66173062964320e5e1de0 (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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
#!/usr/bin/env ruby
# frozen_string_literal: true

require 'yaml'
require 'optparse'
require 'pathname'
require_relative 'api/default_options'

# This script returns the desired feature flag state as a comma-separated string for the feature flags in the specified files.
# Each desired feature flag state is specified as 'feature-flag=state'. This allows us to run package-and-qa with the
# feature flag set to the desired state.
#
# For example, if the specified files included `config/feature_flags/development/ci_awesome_feature.yml` and the desired
# state as specified by the second argument was enabled, the value returned would be `ci_awesome_feature=enabled`

class GetFeatureFlagsFromFiles
  def initialize(options)
    @files = options.delete(:files)
    @state = options.delete(:state)

    abort("ERROR: Please specify the directory containing MR diffs.") if @files.to_s.empty?
  end

  # Gets feature flags from definition files or diffs of deleted defition files
  #
  # @return [String] a comma-separated list of feature flags and their desired state
  def extracted_flags
    flags_list = diffs_dir.glob('**/*').each_with_object([]) do |file_path, flags|
      ff_yaml = ff_yaml_for_file(file_path)
      next if ff_yaml.nil?
      break [] if ff_yaml.empty?

      flags << ff_yaml['name']
    end
    flags_list = flags_list.map { |flag| "#{flag}=#{state}" } unless state.to_s.empty?
    flags_list.join(',')
  end

  # Loads the YAML feature flag definition based on a diff of the definition file. The definition is loaded from the
  # definition file itself, or from a diff of the deleted definition file.
  #
  # @param [Pathname] path the path to the diff
  # @return [Hash] a hash containing the YAML data for the feature flag definition
  def ff_yaml_for_file(path)
    return unless File.expand_path(path).to_s =~ %r{/feature_flags/(development|ops)/.*\.yml}

    if path.to_s.end_with?('yml.deleted.diff')
      # Ignore deleted feature flag definitions if we want to enable/disable existing flags.
      return if state != 'deleted'

      yaml_from_deleted_diff(path)
    else
      # If we want deleted definition files but find one that wasn't deleted, we return immediately to
      # because non-deleted flags are tested in separate jobs from deleted flags, so we don't need to run
      # a job with just deleted flags.
      return [] if state == 'deleted'

      yaml_from_file(path, diffs_dir)
    end
  end

  private

  attr_reader :files, :state

  # The absolute path to the directory of diffs
  #
  # @return [String]
  def diffs_dir
    @diffs_dir ||= Pathname.new(files).expand_path
  end

  # Loads the YAML feature flag definition from a file corresponding to a diff of the definition file.
  #
  # @param [Pathname] file_path the path to the diff
  # @param [Pathname] diffs_dir the path to the diffs directory
  # @return [Hash] a hash containing the YAML data from the feature flag definition file corresponding to the diff
  def yaml_from_file(file_path, diffs_dir)
    real_file_path = File.join(Dir.pwd, file_path.to_s.delete_prefix(diffs_dir.to_s)).delete_suffix('.diff')
    YAML.safe_load(File.read(real_file_path))
  end

  # Loads the YAML feature flag definition from a diff of the deleted feature flag definition file.
  #
  # @param [Pathname] file_path the path of the diff
  # @return [Hash] a hash containing the YAML data for the feature flag definition from the diff
  def yaml_from_deleted_diff(file_path)
    cleaned_diff = File.read(file_path).gsub(/^[^a-z]+/, '')
    YAML.safe_load(cleaned_diff)
  end
end

if $PROGRAM_NAME == __FILE__
  options = API::DEFAULT_OPTIONS.dup

  OptionParser.new do |opts|
    opts.on("-f", "--files FILES", String, "A directory containing diffs including feature flag definition change diffs") do |value|
      options[:files] = value
    end

    opts.on("-s", "--state STATE", String,
      "The desired state of the feature flags (enabled or disabled). If not specified the output will only list the feature flags."
    ) do |value|
      options[:state] = value
    end

    opts.on("-h", "--help", "Prints this help") do
      puts opts
      exit
    end
  end.parse!

  puts GetFeatureFlagsFromFiles.new(options).extracted_flags
end