blob: 67e5263383385bd944e1e870eff8445a1cd82e49 (
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
|
# frozen_string_literal: true
require "yaml"
module QA
module Tools
module Ci
class FfChanges
include Helpers
def initialize(mr_diff)
@mr_diff = mr_diff
end
# Return list of feature flags changed in mr with inverse or deleted state
#
# @return [String]
def fetch
logger.info("Detecting feature flag changes")
ff_toggles = mr_diff.map do |change|
ff_yaml = ff_yaml_for_file(change)
next unless ff_yaml
state = if ff_yaml[:deleted]
"deleted"
else
ff_yaml[:default_enabled] ? 'disabled' : 'enabled'
end
logger.info(" found changes in feature flag '#{ff_yaml[:name]}'")
"#{ff_yaml[:name]}=#{state}"
end.compact
if ff_toggles.empty?
logger.info(" no changes to feature flags detected, skipping!")
return
end
logger.info(" constructed feature flag states: '#{ff_toggles}'")
ff_toggles.join(",")
end
private
attr_reader :mr_diff
# Loads the YAML feature flag definition based on changed files in merge requests.
# The definition is loaded from the definition file itself.
#
# @param [Hash] change mr file change
# @return [Hash] a hash containing the YAML data for the feature flag definition
def ff_yaml_for_file(change)
return unless change[:path] =~ %r{/feature_flags/(development|ops)/.*\.yml}
if change[:deleted_file]
return { name: change[:path].split("/").last.gsub(/\.(yml|yaml)/, ""), deleted: true }
end
YAML.safe_load(
File.read(File.expand_path("../#{change[:path]}", QA::Runtime::Path.qa_root)),
symbolize_names: true
)
end
end
end
end
end
|