summaryrefslogtreecommitdiff
path: root/danger/feature_flag/Dangerfile
blob: 5fe9d42a7a1c4bf341388fcdf1aa25fbb33e7036 (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
# frozen_string_literal: true
# rubocop:disable Style/SignalException

SEE_DOC = "See the [feature flag documentation](https://docs.gitlab.com/ee/development/feature_flags#feature-flag-definition-and-validation)."
FEATURE_FLAG_LABEL = "feature flag"

SUGGEST_MR_COMMENT = <<~SUGGEST_COMMENT
```suggestion
group: "%<group>s"
```

#{SEE_DOC}
SUGGEST_COMMENT

def check_feature_flag_yaml(feature_flag)
  mr_group_label = helper.group_label

  if feature_flag.group.nil?
    message_for_feature_flag_missing_group!(feature_flag: feature_flag, mr_group_label: mr_group_label)
  else
    message_for_feature_flag_with_group!(feature_flag: feature_flag, mr_group_label: mr_group_label)
  end
rescue Psych::Exception
  # YAML could not be parsed, fail the build.
  fail "#{gitlab.html_link(feature_flag.path)} isn't valid YAML! #{SEE_DOC}"
rescue StandardError => e
  warn "There was a problem trying to check the Feature Flag file. Exception: #{e.class.name} - #{e.message}"
end

def message_for_feature_flag_missing_group!(feature_flag:, mr_group_label:)
  if mr_group_label.nil?
    warn "Consider setting `group` in #{gitlab.html_link(feature_flag.path)}. #{SEE_DOC}"
  else
    mr_line = feature_flag.raw.lines.find_index("group:\n")

    if mr_line
      markdown(format(SUGGEST_MR_COMMENT, group: mr_group_label), file: feature_flag.path, line: mr_line.succ)
    else
      warn %(Consider setting `group: "#{mr_group_label}"` in #{gitlab.html_link(feature_flag.path)}. #{SEE_DOC})
    end
  end
end

def message_for_global_rollout(feature_flag)
  return unless feature_flag.default_enabled == true

  message = <<~SUGGEST_COMMENT
  You're about to [release the feature with the feature flag](https://gitlab.com/gitlab-org/gitlab/-/blob/master/.gitlab/issue_templates/Feature%20Flag%20Roll%20Out.md#optional-release-the-feature-with-the-feature-flag).
  This process can only be done **after** the [global rollout on production](https://gitlab.com/gitlab-org/gitlab/-/blob/master/.gitlab/issue_templates/Feature%20Flag%20Roll%20Out.md#global-rollout-on-production).
  Please make sure in [the rollout issue](#{feature_flag.rollout_issue_url}) that the preliminary steps have already been done. Otherwise, changing the YAML definition might not have the desired effect.
  SUGGEST_COMMENT

  mr_line = feature_flag.raw.lines.find_index { |l| l.include?('default_enabled:') }
  markdown(message, file: feature_flag.path, line: mr_line.succ)
end

def message_for_feature_flag_with_group!(feature_flag:, mr_group_label:)
  return if feature_flag.group_match_mr_label?(mr_group_label)

  if mr_group_label.nil?
    helper.labels_to_add << feature_flag.group
  else
    fail %(`group` is set to ~"#{feature_flag.group}" in #{gitlab.html_link(feature_flag.path)}, which does not match ~"#{mr_group_label}" set on the MR!)
  end
end

def feature_flag_file_added?
  feature_flag.feature_flag_files(change_type: :added).any?
end

def feature_flag_file_added_or_removed?
  feature_flag_file_added? || feature_flag.feature_flag_files(change_type: :deleted).any?
end

feature_flag.feature_flag_files(change_type: :added).each do |feature_flag|
  check_feature_flag_yaml(feature_flag)
end

feature_flag.feature_flag_files(change_type: :modified).each do |feature_flag|
  message_for_global_rollout(feature_flag)
end

if helper.security_mr? && feature_flag_file_added?
  fail "Feature flags are discouraged from security merge requests. Read the [security documentation](https://gitlab.com/gitlab-org/release/docs/-/blob/master/general/security/utilities/feature_flags.md) for details."
end