2021-01-26 13:09:30 -05:00
# frozen_string_literal: true
# rubocop:disable Style/SignalException
2021-03-11 16:09:09 -05:00
SEE_DOC = " See the [feature flag documentation](https://docs.gitlab.com/ee/development/feature_flags # feature-flag-definition-and-validation). "
2021-04-08 11:09:06 -04:00
FEATURE_FLAG_LABEL = " feature flag "
2021-01-26 13:09:30 -05:00
SUGGEST_MR_COMMENT = << ~ SUGGEST_COMMENT
` ` ` suggestion
group : " %<group>s "
` ` `
#{SEE_DOC}
SUGGEST_COMMENT
def check_feature_flag_yaml ( feature_flag )
2021-05-05 08:10:33 -04:00
mr_group_label = helper . group_label
2021-01-26 13:09:30 -05:00
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.
2022-03-23 20:07:27 -04:00
fail " #{ helper . html_link ( feature_flag . path ) } isn't valid YAML! #{ SEE_DOC } "
2021-01-26 13:09:30 -05:00
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?
2022-03-23 20:07:27 -04:00
warn " Consider setting `group` in #{ helper . html_link ( feature_flag . path ) } . #{ SEE_DOC } "
2021-01-26 13:09:30 -05:00
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
2022-03-23 20:07:27 -04:00
warn %( Consider setting `group: " #{ mr_group_label } "` in #{ helper . html_link ( feature_flag . path ) } . #{ SEE_DOC } )
2021-01-26 13:09:30 -05:00
end
end
end
2021-06-28 05:08:19 -04:00
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 % 20 Flag % 20 Roll % 20 Out . 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 % 20 Flag % 20 Roll % 20 Out . 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
2021-01-26 13:09:30 -05:00
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?
2022-02-28 13:14:03 -05:00
helper . labels_to_add << feature_flag . group
2021-01-26 13:09:30 -05:00
else
2022-03-23 20:07:27 -04:00
fail %( `group` is set to ~" #{ feature_flag . group } " in #{ helper . html_link ( feature_flag . path ) } , which does not match ~" #{ mr_group_label } " set on the MR! )
2021-01-26 13:09:30 -05:00
end
end
2021-06-09 08:10:27 -04:00
def feature_flag_file_added?
feature_flag . feature_flag_files ( change_type : :added ) . any?
end
2021-02-08 01:09:18 -05:00
def feature_flag_file_added_or_removed?
2021-06-09 08:10:27 -04:00
feature_flag_file_added? || feature_flag . feature_flag_files ( change_type : :deleted ) . any?
2021-02-08 01:09:18 -05:00
end
2021-02-04 01:09:22 -05:00
feature_flag . feature_flag_files ( change_type : :added ) . each do | feature_flag |
2021-01-26 13:09:30 -05:00
check_feature_flag_yaml ( feature_flag )
end
2021-02-04 01:09:22 -05:00
2021-06-28 05:08:19 -04:00
feature_flag . feature_flag_files ( change_type : :modified ) . each do | feature_flag |
message_for_global_rollout ( feature_flag )
end
2021-06-09 08:10:27 -04:00
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