Commit fab30c11 authored by Shinya Maeda's avatar Shinya Maeda

Expose merge request pipeline variables

Introduce the following variables

- CI_MERGE_REQUEST_ID
- CI_MERGE_REQUEST_IID
- CI_MERGE_REQUEST_REF_PATH
- CI_MERGE_REQUEST_PROJECT_ID
- CI_MERGE_REQUEST_PROJECT_PATH
- CI_MERGE_REQUEST_PROJECT_URL
- CI_MERGE_REQUEST_TARGET_BRANCH_NAME
- CI_MERGE_REQUEST_SOURCE_PROJECT_ID
- CI_MERGE_REQUEST_SOURCE_PROJECT_PATH
- CI_MERGE_REQUEST_SOURCE_PROJECT_URL
- CI_MERGE_REQUEST_SOURCE_BRANCH_NAME
parent ec2c268b
......@@ -605,13 +605,18 @@ module Ci
end
def predefined_variables
Gitlab::Ci::Variables::Collection.new
.append(key: 'CI_PIPELINE_IID', value: iid.to_s)
.append(key: 'CI_CONFIG_PATH', value: ci_yaml_file_path)
.append(key: 'CI_PIPELINE_SOURCE', value: source.to_s)
.append(key: 'CI_COMMIT_MESSAGE', value: git_commit_message.to_s)
.append(key: 'CI_COMMIT_TITLE', value: git_commit_full_title.to_s)
.append(key: 'CI_COMMIT_DESCRIPTION', value: git_commit_description.to_s)
Gitlab::Ci::Variables::Collection.new.tap do |variables|
variables.append(key: 'CI_PIPELINE_IID', value: iid.to_s)
variables.append(key: 'CI_CONFIG_PATH', value: ci_yaml_file_path)
variables.append(key: 'CI_PIPELINE_SOURCE', value: source.to_s)
variables.append(key: 'CI_COMMIT_MESSAGE', value: git_commit_message.to_s)
variables.append(key: 'CI_COMMIT_TITLE', value: git_commit_full_title.to_s)
variables.append(key: 'CI_COMMIT_DESCRIPTION', value: git_commit_description.to_s)
if merge_request? && merge_request
variables.concat(merge_request.predefined_variables)
end
end
end
def queued_duration
......
......@@ -1070,6 +1070,42 @@ class MergeRequest < ActiveRecord::Base
actual_head_pipeline&.has_test_reports?
end
def predefined_variables
Gitlab::Ci::Variables::Collection.new.tap do |variables|
variables.append(key: 'CI_MERGE_REQUEST_ID', value: id.to_s)
variables.append(key: 'CI_MERGE_REQUEST_IID', value: iid.to_s)
variables.append(key: 'CI_MERGE_REQUEST_REF_PATH',
value: ref_path.to_s)
variables.append(key: 'CI_MERGE_REQUEST_PROJECT_ID',
value: project.id.to_s)
variables.append(key: 'CI_MERGE_REQUEST_PROJECT_PATH',
value: project.full_path)
variables.append(key: 'CI_MERGE_REQUEST_PROJECT_URL',
value: project.web_url)
variables.append(key: 'CI_MERGE_REQUEST_TARGET_BRANCH_NAME',
value: target_branch.to_s)
if source_project
variables.append(key: 'CI_MERGE_REQUEST_SOURCE_PROJECT_ID',
value: source_project.id.to_s)
variables.append(key: 'CI_MERGE_REQUEST_SOURCE_PROJECT_PATH',
value: source_project.full_path)
variables.append(key: 'CI_MERGE_REQUEST_SOURCE_PROJECT_URL',
value: source_project.web_url)
variables.append(key: 'CI_MERGE_REQUEST_SOURCE_BRANCH_NAME',
value: source_branch.to_s)
end
end
end
# rubocop: disable CodeReuse/ServiceClass
def compare_test_reports
unless has_test_reports?
......
---
title: Expose merge request pipeline variables
merge_request: 23398
author:
type: changed
......@@ -350,6 +350,50 @@ describe Ci::Pipeline, :mailer do
CI_COMMIT_TITLE
CI_COMMIT_DESCRIPTION]
end
context 'when source is merge request' do
let(:pipeline) do
create(:ci_pipeline, source: :merge_request, merge_request: merge_request)
end
let(:merge_request) do
create(:merge_request,
source_project: project,
source_branch: 'feature',
target_project: project,
target_branch: 'master')
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_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)
end
context 'when source project does not exist' do
before do
merge_request.update_column(:source_project_id, nil)
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])
end
end
end
end
describe '#protected_ref?' 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