Commit 5884f970 authored by Jonas Hahnfeld's avatar Jonas Hahnfeld

Expose merge request diff variables

Introduce the following variables:
 - CI_MERGE_REQUEST_DIFF_ID
 - CI_MERGE_REQUEST_DIFF_BASE_SHA
behind a feature flag, ci_mr_diff_variables.

For testing, remove the context 'when source project does not exist'
because the factory creates the pipeline in the source project. Also
differentiate between detached and merged results pipelines.

Closes https://gitlab.com/gitlab-org/gitlab/-/issues/273387
parent e1d7aca7
...@@ -774,6 +774,15 @@ module Ci ...@@ -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_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_SOURCE_BRANCH_SHA', value: source_sha.to_s)
variables.append(key: 'CI_MERGE_REQUEST_TARGET_BRANCH_SHA', value: target_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) variables.concat(merge_request.predefined_variables)
end end
...@@ -1131,6 +1140,22 @@ module Ci ...@@ -1131,6 +1140,22 @@ module Ci
Gitlab::DataBuilder::Pipeline.build(self) Gitlab::DataBuilder::Pipeline.build(self)
end 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 def push_details
strong_memoize(:push_details) do strong_memoize(:push_details) do
Gitlab::Git::Push.new(project, before_sha, sha, git_ref) 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
...@@ -745,8 +745,12 @@ RSpec.describe Ci::Pipeline, :mailer, factory_default: :keep do ...@@ -745,8 +745,12 @@ RSpec.describe Ci::Pipeline, :mailer, factory_default: :keep do
end end
context 'when pipeline is merge request' do context 'when pipeline is merge request' do
before do
stub_feature_flags(ci_mr_diff_variables: false)
end
let(:pipeline) do let(:pipeline) do
create(:ci_pipeline, merge_request: merge_request) create(:ci_pipeline, :detached_merge_request_pipeline, merge_request: merge_request)
end end
let(:merge_request) do let(:merge_request) do
...@@ -774,30 +778,32 @@ RSpec.describe Ci::Pipeline, :mailer, factory_default: :keep do ...@@ -774,30 +778,32 @@ RSpec.describe Ci::Pipeline, :mailer, factory_default: :keep do
'CI_MERGE_REQUEST_PROJECT_PATH' => merge_request.project.full_path, 'CI_MERGE_REQUEST_PROJECT_PATH' => merge_request.project.full_path,
'CI_MERGE_REQUEST_PROJECT_URL' => merge_request.project.web_url, '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_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_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_PATH' => merge_request.source_project.full_path,
'CI_MERGE_REQUEST_SOURCE_PROJECT_URL' => merge_request.source_project.web_url, '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_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_TITLE' => merge_request.title,
'CI_MERGE_REQUEST_ASSIGNEES' => merge_request.assignee_username_list, 'CI_MERGE_REQUEST_ASSIGNEES' => merge_request.assignee_username_list,
'CI_MERGE_REQUEST_MILESTONE' => milestone.title, 'CI_MERGE_REQUEST_MILESTONE' => milestone.title,
'CI_MERGE_REQUEST_LABELS' => labels.map(&:title).sort.join(','), '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 end
context 'when source project does not exist' do context 'when feature flag ci_mr_diff_variables is enabled' do
before do before do
merge_request.update_column(:source_project_id, nil) stub_feature_flags(ci_mr_diff_variables: true)
end end
it 'does not expose source project related variables' do it 'exposes diff variables' do
expect(subject.to_hash.keys).not_to include( expect(subject.to_hash)
%w[CI_MERGE_REQUEST_SOURCE_PROJECT_ID .to include(
CI_MERGE_REQUEST_SOURCE_PROJECT_PATH 'CI_MERGE_REQUEST_DIFF_ID' => merge_request.merge_request_diff.id.to_s,
CI_MERGE_REQUEST_SOURCE_PROJECT_URL 'CI_MERGE_REQUEST_DIFF_BASE_SHA' => merge_request.merge_request_diff.base_commit_sha)
CI_MERGE_REQUEST_SOURCE_BRANCH_NAME])
end end
end end
...@@ -824,6 +830,51 @@ RSpec.describe Ci::Pipeline, :mailer, factory_default: :keep do ...@@ -824,6 +830,51 @@ RSpec.describe Ci::Pipeline, :mailer, factory_default: :keep do
expect(subject.to_hash.keys).not_to include('CI_MERGE_REQUEST_LABELS') expect(subject.to_hash.keys).not_to include('CI_MERGE_REQUEST_LABELS')
end end
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 end
context 'when source is external pull request' do 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