diff --git a/scripts/trigger-build.rb b/scripts/trigger-build.rb index 98ca8112d6268291ce94911fdd4ad88df19b032a..19b39ce7023133f42e456395c5b4aaed14728c2d 100755 --- a/scripts/trigger-build.rb +++ b/scripts/trigger-build.rb @@ -25,6 +25,7 @@ def self.variables_for_env_file(variables) class Base # Can be overridden + STABLE_BRANCH_REGEX = /^[\d-]+-stable(-ee|-jh)?$/ def self.access_token ENV['PROJECT_TOKEN_FOR_CI_SCRIPTS_API_USAGE'] end @@ -113,21 +114,33 @@ def trigger_stable_branch_if_detected? end def stable_branch? - ENV['CI_COMMIT_REF_NAME'] =~ /^[\d-]+-stable(-ee|-jh)?$/ + ENV['CI_COMMIT_REF_NAME'] =~ STABLE_BRANCH_REGEX + end + + def mr_target_stable_branch? + ENV['CI_MERGE_REQUEST_TARGET_BRANCH_NAME'] =~ STABLE_BRANCH_REGEX end def fallback_ref - if trigger_stable_branch_if_detected? && stable_branch? - if ENV['CI_PROJECT_NAMESPACE'] == 'gitlab-cn' - ENV['CI_COMMIT_REF_NAME'].delete_suffix('-jh') - elsif ENV['CI_PROJECT_NAMESPACE'] == 'gitlab-org' - ENV['CI_COMMIT_REF_NAME'].delete_suffix('-ee') - end + return primary_ref unless trigger_stable_branch_if_detected? + + if stable_branch? + normalize_stable_branch_name(ENV['CI_COMMIT_REF_NAME']) + elsif mr_target_stable_branch? + normalize_stable_branch_name(ENV['CI_MERGE_REQUEST_TARGET_BRANCH_NAME']) else primary_ref end end + def normalize_stable_branch_name(branch_name) + if ENV['CI_PROJECT_NAMESPACE'] == 'gitlab-cn' + branch_name.delete_suffix('-jh') + elsif ENV['CI_PROJECT_NAMESPACE'] == 'gitlab-org' + branch_name.delete_suffix('-ee') + end + end + def ref ENV.fetch(ref_param_name, fallback_ref) end diff --git a/spec/scripts/trigger-build_spec.rb b/spec/scripts/trigger-build_spec.rb index f46adb1a9f1f1dd4b4287125d0423075f47d56d2..a1bedd19ed37b655d8a1b08cf122a78986369109 100644 --- a/spec/scripts/trigger-build_spec.rb +++ b/spec/scripts/trigger-build_spec.rb @@ -236,14 +236,62 @@ def ref_param_name describe "TRIGGER_BRANCH" do context 'when CNG_BRANCH is not set' do - it 'sets TRIGGER_BRANCH to master' do - stub_env('CI_PROJECT_NAMESPACE', 'gitlab-org') - expect(subject.variables['TRIGGER_BRANCH']).to eq('master') + context 'with gitlab-org' do + before do + stub_env('CI_PROJECT_NAMESPACE', 'gitlab-org') + end + + it 'sets TRIGGER_BRANCH to master if the commit ref is master' do + stub_env('CI_COMMIT_REF_NAME', 'master') + stub_env('CI_MERGE_REQUEST_TARGET_BRANCH_NAME', nil) + expect(subject.variables['TRIGGER_BRANCH']).to eq('master') + end + + it 'sets the TRIGGER_BRANCH to master if the commit is part of an MR targeting master' do + stub_env('CI_COMMIT_REF_NAME', 'feature_branch') + stub_env('CI_MERGE_REQUEST_TARGET_BRANCH_NAME', 'master') + expect(subject.variables['TRIGGER_BRANCH']).to eq('master') + end + + it 'sets TRIGGER_BRANCH to stable branch if the commit ref is a stable branch' do + stub_env('CI_COMMIT_REF_NAME', '16-6-stable-ee') + expect(subject.variables['TRIGGER_BRANCH']).to eq('16-6-stable') + end + + it 'sets the TRIGGER_BRANCH to stable branch if the commit is part of an MR targeting stable branch' do + stub_env('CI_COMMIT_REF_NAME', 'feature_branch') + stub_env('CI_MERGE_REQUEST_TARGET_BRANCH_NAME', '16-6-stable-ee') + expect(subject.variables['TRIGGER_BRANCH']).to eq('16-6-stable') + end end - it 'sets TRIGGER_BRANCH to main-jh on JH side' do - stub_env('CI_PROJECT_NAMESPACE', 'gitlab-cn') - expect(subject.variables['TRIGGER_BRANCH']).to eq('main-jh') + context 'with gitlab-cn' do + before do + stub_env('CI_PROJECT_NAMESPACE', 'gitlab-cn') + end + + it 'sets TRIGGER_BRANCH to main-jh if commit ref is main-jh' do + stub_env('CI_COMMIT_REF_NAME', 'main-jh') + stub_env('CI_MERGE_REQUEST_TARGET_BRANCH_NAME', nil) + expect(subject.variables['TRIGGER_BRANCH']).to eq('main-jh') + end + + it 'sets the TRIGGER_BRANCH to main-jh if the commit is part of an MR targeting main-jh' do + stub_env('CI_COMMIT_REF_NAME', 'feature_branch') + stub_env('CI_MERGE_REQUEST_TARGET_BRANCH_NAME', 'main-jh') + expect(subject.variables['TRIGGER_BRANCH']).to eq('main-jh') + end + + it 'sets TRIGGER_BRANCH to 16-6-stable if commit ref is a stable branch' do + stub_env('CI_COMMIT_REF_NAME', '16-6-stable-jh') + expect(subject.variables['TRIGGER_BRANCH']).to eq('16-6-stable') + end + + it 'sets the TRIGGER_BRANCH to 16-6-stable if the commit is part of an MR targeting 16-6-stable-jh' do + stub_env('CI_COMMIT_REF_NAME', 'feature_branch') + stub_env('CI_MERGE_REQUEST_TARGET_BRANCH_NAME', '16-6-stable-jh') + expect(subject.variables['TRIGGER_BRANCH']).to eq('16-6-stable') + end end end