Handle MR branches in is_regular_branch? check

Signed-off-by: Balasankar "Balu" C <balasankar@gitlab.com>
This commit is contained in:
Balasankar "Balu" C 2023-11-17 12:43:45 +05:30
parent 34fdcdd4cb
commit cd27448441
No known key found for this signature in database
GPG Key ID: B77D2E2E23735427
4 changed files with 46 additions and 6 deletions

View File

@ -7,7 +7,7 @@ module Build
class CI
class << self
def branch_name
Gitlab::Util.get_env('CI_COMMIT_BRANCH')
Gitlab::Util.get_env('CI_COMMIT_BRANCH') || Gitlab::Util.get_env('CI_MERGE_REQUEST_SOURCE_BRANCH_NAME')
end
def tag_name

View File

@ -269,6 +269,16 @@ RSpec.describe Build::Check do
end
end
context 'when on a feature branch MR pipeline' do
before do
stub_mr_branch('my-feature-branch')
end
it 'returns true' do
expect(described_class.on_regular_branch?).to be_truthy
end
end
context 'when on a stable branch' do
before do
stub_branch('15-6-stable')

View File

@ -26,6 +26,7 @@ RSpec.describe Build::Info::Git do
context 'not in CI' do
before do
stub_env_var('CI_COMMIT_BRANCH', '')
stub_env_var('CI_MERGE_REQUEST_SOURCE_BRANCH_NAME', '')
allow(Gitlab::Util).to receive(:shellout_stdout).with('git rev-parse --abbrev-ref HEAD').and_return('HEAD')
end
@ -37,12 +38,26 @@ RSpec.describe Build::Info::Git do
context 'in branches' do
context 'in CI' do
before do
stub_env_var('CI_COMMIT_BRANCH', 'my-feature-branch')
context 'in MR pipelines' do
before do
stub_env_var('CI_COMMIT_BRANCH', '')
stub_env_var('CI_MERGE_REQUEST_SOURCE_BRANCH_NAME', 'my-feature-branch')
end
it 'returns branch name from CI variable' do
expect(described_class.branch_name).to eq('my-feature-branch')
end
end
it 'returns branch name from CI variable' do
expect(described_class.branch_name).to eq('my-feature-branch')
context 'in regular branch pipelines' do
before do
stub_env_var('CI_COMMIT_BRANCH', 'my-feature-branch')
stub_env_var('CI_MERGE_REQUEST_SOURCE_BRANCH_NAME', '')
end
it 'returns branch name from CI variable' do
expect(described_class.branch_name).to eq('my-feature-branch')
end
end
end
@ -50,6 +65,7 @@ RSpec.describe Build::Info::Git do
before do
stub_env_var('CI_COMMIT_BRANCH', '')
stub_env_var('CI_COMMIT_TAG', '')
stub_env_var('CI_MERGE_REQUEST_SOURCE_BRANCH_NAME', '')
allow(Gitlab::Util).to receive(:shellout_stdout).with('git rev-parse --abbrev-ref HEAD').and_return('my-feature-branch')
end

View File

@ -52,11 +52,23 @@ module GitlabSpec
end
def stub_branch(branch)
stub_env_var('CI_MERGE_REQUEST_SOURCE_BRANCH_NAME', '')
stub_env_var('CI_COMMIT_BRANCH', branch)
allow(Gitlab::Util).to receive(:shellout_stdout).with('git rev-parse --abbrev-ref HEAD').and_return(branch)
stub_env_var('CI_COMMIT_TAG', '')
allow(Gitlab::Util).to receive(:shellout_stdout).with('git describe --tags --exact-match').and_raise(Gitlab::Util::ShellOutExecutionError.new("git describe --tags --exact-match", 128, "", "fatal: no tag exactly matches 'foobar'"))
allow(Gitlab::Util).to receive(:shellout_stdout).with('git describe --tags --exact-match').and_raise(Gitlab::Util::ShellOutExecutionError.new("git describe --tags --exact-match", 128, "", "fatal: no tag exactly matches '#{branch}'"))
end
def stub_mr_branch(branch)
stub_env_var('CI_MERGE_REQUEST_SOURCE_BRANCH_NAME', branch)
stub_env_var('CI_COMMIT_BRANCH', '')
allow(Gitlab::Util).to receive(:shellout_stdout).with('git rev-parse --abbrev-ref HEAD').and_return('HEAD')
stub_env_var('CI_COMMIT_TAG', '')
allow(Gitlab::Util).to receive(:shellout_stdout).with('git describe --tags --exact-match').and_raise(Gitlab::Util::ShellOutExecutionError.new("git describe --tags --exact-match", 128, "", "fatal: no tag exactly matches '#{branch}'"))
end
def stub_tag(tag)
@ -65,6 +77,8 @@ module GitlabSpec
stub_env_var('CI_COMMIT_BRANCH', '')
allow(Gitlab::Util).to receive(:shellout_stdout).with('git rev-parse --abbrev-ref HEAD').and_return('HEAD')
stub_env_var('CI_MERGE_REQUEST_SOURCE_BRANCH_NAME', '')
end
def stub_is_package_version(package, value = nil)