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-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 )
mr_group_label = helper . group_label ( gitlab . mr_labels )
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_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?
gitlab . api . update_merge_request ( gitlab . mr_json [ 'project_id' ] ,
gitlab . mr_json [ 'iid' ] ,
add_labels : 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
2021-02-08 01:09:18 -05:00
def feature_flag_file_added_or_removed?
feature_flag . feature_flag_files ( change_type : :added ) . any? || feature_flag . feature_flag_files ( change_type : :deleted ) . any?
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-02-08 01:09:18 -05:00
if feature_flag_file_added_or_removed?
2021-02-04 01:09:22 -05:00
new_mr_title = helper . mr_title . dup
new_mr_title << ' [RUN ALL RSPEC]' unless helper . run_all_rspec_mr?
new_mr_title << ' [RUN AS-IF-FOSS]' unless helper . run_as_if_foss_mr?
if new_mr_title != helper . mr_title
gitlab . api . update_merge_request (
gitlab . mr_json [ 'project_id' ] ,
gitlab . mr_json [ 'iid' ] ,
title : new_mr_title
)
gitlab . api . post ( " /projects/ #{ gitlab . mr_json [ 'project_id' ] } /merge_requests/ #{ gitlab . mr_json [ 'iid' ] } /pipelines " )
2021-02-08 01:09:18 -05:00
else
message " You're adding or removing a feature flag, your MR title needs to include `[RUN ALL RSPEC] [RUN AS-IF-FOSS]` (we may have updated it automatically for you and started a new MR pipeline) to ensure everything is covered. "
2021-02-04 01:09:22 -05:00
end
end