Commit 88afb1de authored by Fabio Pitino's avatar Fabio Pitino

Merge branch '273387-ci-expose-version' into 'master'

Expose merge request diff variables

See merge request gitlab-org/gitlab!46621
parents 66649862 5884f970
......@@ -774,6 +774,15 @@ module Ci
variables.append(key: 'CI_MERGE_REQUEST_EVENT_TYPE', value: merge_request_event_type.to_s)
variables.append(key: 'CI_MERGE_REQUEST_SOURCE_BRANCH_SHA', value: source_sha.to_s)
variables.append(key: 'CI_MERGE_REQUEST_TARGET_BRANCH_SHA', value: target_sha.to_s)
if Feature.enabled?(:ci_mr_diff_variables, project)
diff = self.merge_request_diff
if diff.present?
variables.append(key: 'CI_MERGE_REQUEST_DIFF_ID', value: diff.id.to_s)
variables.append(key: 'CI_MERGE_REQUEST_DIFF_BASE_SHA', value: diff.base_commit_sha)
end
end
variables.concat(merge_request.predefined_variables)
end
......@@ -1139,6 +1148,22 @@ module Ci
Gitlab::DataBuilder::Pipeline.build(self)
end
def merge_request_diff_sha
return unless merge_request?
if merge_request_pipeline?
source_sha
else
sha
end
end
def merge_request_diff
return unless merge_request?
merge_request.merge_request_diff_for(merge_request_diff_sha)
end
def push_details
strong_memoize(:push_details) do
Gitlab::Git::Push.new(project, before_sha, sha, git_ref)
......
---
name: ci_mr_diff_variables
introduced_by_url: https://gitlab.com/gitlab-org/gitlab/-/merge_requests/46621
rollout_issue_url: https://gitlab.com/gitlab-org/gitlab/-/issues/285468
milestone: '13.7'
type: development
group: group::pipeline authoring
default_enabled: false
......@@ -755,8 +755,12 @@ RSpec.describe Ci::Pipeline, :mailer, factory_default: :keep do
end
context 'when pipeline is merge request' do
before do
stub_feature_flags(ci_mr_diff_variables: false)
end
let(:pipeline) do
create(:ci_pipeline, merge_request: merge_request)
create(:ci_pipeline, :detached_merge_request_pipeline, merge_request: merge_request)
end
let(:merge_request) do
......@@ -784,30 +788,32 @@ RSpec.describe Ci::Pipeline, :mailer, factory_default: :keep do
'CI_MERGE_REQUEST_PROJECT_PATH' => merge_request.project.full_path,
'CI_MERGE_REQUEST_PROJECT_URL' => merge_request.project.web_url,
'CI_MERGE_REQUEST_TARGET_BRANCH_NAME' => merge_request.target_branch.to_s,
'CI_MERGE_REQUEST_TARGET_BRANCH_SHA' => pipeline.target_sha.to_s,
'CI_MERGE_REQUEST_TARGET_BRANCH_SHA' => '',
'CI_MERGE_REQUEST_SOURCE_PROJECT_ID' => merge_request.source_project.id.to_s,
'CI_MERGE_REQUEST_SOURCE_PROJECT_PATH' => merge_request.source_project.full_path,
'CI_MERGE_REQUEST_SOURCE_PROJECT_URL' => merge_request.source_project.web_url,
'CI_MERGE_REQUEST_SOURCE_BRANCH_NAME' => merge_request.source_branch.to_s,
'CI_MERGE_REQUEST_SOURCE_BRANCH_SHA' => pipeline.source_sha.to_s,
'CI_MERGE_REQUEST_SOURCE_BRANCH_SHA' => '',
'CI_MERGE_REQUEST_TITLE' => merge_request.title,
'CI_MERGE_REQUEST_ASSIGNEES' => merge_request.assignee_username_list,
'CI_MERGE_REQUEST_MILESTONE' => milestone.title,
'CI_MERGE_REQUEST_LABELS' => labels.map(&:title).sort.join(','),
'CI_MERGE_REQUEST_EVENT_TYPE' => pipeline.merge_request_event_type.to_s)
'CI_MERGE_REQUEST_EVENT_TYPE' => 'detached')
expect(subject.to_hash.keys).not_to include(
%w[CI_MERGE_REQUEST_DIFF_ID
CI_MERGE_REQUEST_DIFF_BASE_SHA])
end
context 'when source project does not exist' do
context 'when feature flag ci_mr_diff_variables is enabled' do
before do
merge_request.update_column(:source_project_id, nil)
stub_feature_flags(ci_mr_diff_variables: true)
end
it 'does not expose source project related variables' do
expect(subject.to_hash.keys).not_to include(
%w[CI_MERGE_REQUEST_SOURCE_PROJECT_ID
CI_MERGE_REQUEST_SOURCE_PROJECT_PATH
CI_MERGE_REQUEST_SOURCE_PROJECT_URL
CI_MERGE_REQUEST_SOURCE_BRANCH_NAME])
it 'exposes diff variables' do
expect(subject.to_hash)
.to include(
'CI_MERGE_REQUEST_DIFF_ID' => merge_request.merge_request_diff.id.to_s,
'CI_MERGE_REQUEST_DIFF_BASE_SHA' => merge_request.merge_request_diff.base_commit_sha)
end
end
......@@ -834,6 +840,51 @@ RSpec.describe Ci::Pipeline, :mailer, factory_default: :keep do
expect(subject.to_hash.keys).not_to include('CI_MERGE_REQUEST_LABELS')
end
end
context 'with merged results' do
let(:pipeline) do
create(:ci_pipeline, :merged_result_pipeline, merge_request: merge_request)
end
it 'exposes merge request pipeline variables' do
expect(subject.to_hash)
.to include(
'CI_MERGE_REQUEST_ID' => merge_request.id.to_s,
'CI_MERGE_REQUEST_IID' => merge_request.iid.to_s,
'CI_MERGE_REQUEST_REF_PATH' => merge_request.ref_path.to_s,
'CI_MERGE_REQUEST_PROJECT_ID' => merge_request.project.id.to_s,
'CI_MERGE_REQUEST_PROJECT_PATH' => merge_request.project.full_path,
'CI_MERGE_REQUEST_PROJECT_URL' => merge_request.project.web_url,
'CI_MERGE_REQUEST_TARGET_BRANCH_NAME' => merge_request.target_branch.to_s,
'CI_MERGE_REQUEST_TARGET_BRANCH_SHA' => merge_request.target_branch_sha,
'CI_MERGE_REQUEST_SOURCE_PROJECT_ID' => merge_request.source_project.id.to_s,
'CI_MERGE_REQUEST_SOURCE_PROJECT_PATH' => merge_request.source_project.full_path,
'CI_MERGE_REQUEST_SOURCE_PROJECT_URL' => merge_request.source_project.web_url,
'CI_MERGE_REQUEST_SOURCE_BRANCH_NAME' => merge_request.source_branch.to_s,
'CI_MERGE_REQUEST_SOURCE_BRANCH_SHA' => merge_request.source_branch_sha,
'CI_MERGE_REQUEST_TITLE' => merge_request.title,
'CI_MERGE_REQUEST_ASSIGNEES' => merge_request.assignee_username_list,
'CI_MERGE_REQUEST_MILESTONE' => milestone.title,
'CI_MERGE_REQUEST_LABELS' => labels.map(&:title).sort.join(','),
'CI_MERGE_REQUEST_EVENT_TYPE' => 'merged_result')
expect(subject.to_hash.keys).not_to include(
%w[CI_MERGE_REQUEST_DIFF_ID
CI_MERGE_REQUEST_DIFF_BASE_SHA])
end
context 'when feature flag ci_mr_diff_variables is enabled' do
before do
stub_feature_flags(ci_mr_diff_variables: true)
end
it 'exposes diff variables' do
expect(subject.to_hash)
.to include(
'CI_MERGE_REQUEST_DIFF_ID' => merge_request.merge_request_diff.id.to_s,
'CI_MERGE_REQUEST_DIFF_BASE_SHA' => merge_request.merge_request_diff.base_commit_sha)
end
end
end
end
context 'when source is external pull request' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment