Commit f62dc67d authored by Shinya Maeda's avatar Shinya Maeda

Add suffix for merge request event

Fix

ok

Add spec

Fix

ok

Fix

Add changelog

Fix

Add memoization

a

fix
parent dc5fc283
......@@ -46,7 +46,7 @@ module Ci
delegate :terminal_specification, to: :runner_session, allow_nil: true
delegate :gitlab_deploy_token, to: :project
delegate :trigger_short_token, to: :trigger_request, allow_nil: true
delegate :merge_request?, to: :pipeline
delegate :merge_request_event?, to: :pipeline
##
# Since Gitlab 11.5, deployments records started being created right after
......
......@@ -60,9 +60,9 @@ module Ci
validates :sha, presence: { unless: :importing? }
validates :ref, presence: { unless: :importing? }
validates :merge_request, presence: { if: :merge_request? }
validates :merge_request, absence: { unless: :merge_request? }
validates :tag, inclusion: { in: [false], if: :merge_request? }
validates :merge_request, presence: { if: :merge_request_event? }
validates :merge_request, absence: { unless: :merge_request_event? }
validates :tag, inclusion: { in: [false], if: :merge_request_event? }
validates :status, presence: { unless: :importing? }
validate :valid_commit_sha, unless: :importing?
validates :source, exclusion: { in: %w(unknown), unless: :importing? }, on: :create
......@@ -179,7 +179,7 @@ module Ci
scope :sort_by_merge_request_pipelines, -> do
sql = 'CASE ci_pipelines.source WHEN (?) THEN 0 ELSE 1 END, ci_pipelines.id DESC'
query = ActiveRecord::Base.send(:sanitize_sql_array, [sql, sources[:merge_request]]) # rubocop:disable GitlabSecurity/PublicSend
query = ActiveRecord::Base.send(:sanitize_sql_array, [sql, sources[:merge_request_event]]) # rubocop:disable GitlabSecurity/PublicSend
order(query)
end
......@@ -196,7 +196,7 @@ module Ci
end
scope :triggered_by_merge_request, -> (merge_request) do
where(source: :merge_request, merge_request: merge_request)
where(source: :merge_request_event, merge_request: merge_request)
end
scope :detached_merge_request_pipelines, -> (merge_request) do
......@@ -639,7 +639,7 @@ module Ci
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
if merge_request_event? && merge_request
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.concat(merge_request.predefined_variables)
......@@ -669,7 +669,7 @@ module Ci
# All the merge requests for which the current pipeline runs/ran against
def all_merge_requests
@all_merge_requests ||=
if merge_request?
if merge_request_event?
MergeRequest.where(id: merge_request_id)
else
MergeRequest.where(source_project_id: project_id, source_branch: ref)
......@@ -714,7 +714,7 @@ module Ci
# * nil: Modified path can not be evaluated
def modified_paths
strong_memoize(:modified_paths) do
if merge_request?
if merge_request_event?
merge_request.modified_paths
elsif branch_updated?
push_details.modified_paths
......@@ -727,7 +727,7 @@ module Ci
end
def triggered_by_merge_request?
merge_request? && merge_request_id.present?
merge_request_event? && merge_request_id.present?
end
def detached_merge_request_pipeline?
......@@ -773,7 +773,7 @@ module Ci
end
def git_ref
if merge_request?
if merge_request_event?
##
# In the future, we're going to change this ref to
# merge request's merged reference, such as "refs/merge-requests/:iid/merge".
......
......@@ -23,7 +23,7 @@ module Ci
api: 5,
external: 6,
chat: 8,
merge_request: 10
merge_request_event: 10
}
end
......
......@@ -4,7 +4,7 @@ module HasRef
extend ActiveSupport::Concern
def branch?
!tag? && !merge_request?
!tag? && !merge_request_event?
end
def git_ref
......
......@@ -35,7 +35,7 @@ module Ci
specs = []
if git_depth > 0
specs << refspec_for_branch(ref) if branch? || merge_request?
specs << refspec_for_branch(ref) if branch? || merge_request_event?
specs << refspec_for_tag(ref) if tag?
else
specs << refspec_for_branch
......
......@@ -23,7 +23,7 @@ class PipelineEntity < Grape::Entity
expose :latest?, as: :latest
expose :stuck?, as: :stuck
expose :auto_devops_source?, as: :auto_devops
expose :merge_request?, as: :merge_request
expose :merge_request_event?, as: :merge_request
expose :has_yaml_errors?, as: :yaml_errors
expose :can_retry?, as: :retryable
expose :can_cancel?, as: :cancelable
......@@ -49,7 +49,7 @@ class PipelineEntity < Grape::Entity
expose :tag?, as: :tag
expose :branch?, as: :branch
expose :merge_request?, as: :merge_request
expose :merge_request_event?, as: :merge_request
end
expose :commit, using: CommitEntity
......
......@@ -67,7 +67,7 @@ module MergeRequests
Ci::CreatePipelineService
.new(merge_request.source_project, user, ref: merge_request.source_branch)
.execute(:merge_request,
.execute(:merge_request_event,
ignore_skip_ci: true,
save_on_errors: false,
merge_request: merge_request)
......
......@@ -48,7 +48,7 @@
content: "<a class='autodevops-link' href='#{popover_content_url}' target='_blank' rel='noopener noreferrer nofollow'>#{popover_content_text}</a>",
} }
Auto DevOps
- if @pipeline.merge_request?
- if @pipeline.merge_request_event?
%span.js-pipeline-url-mergerequest.badge.badge-info.has-tooltip{ title: "This pipeline is run in a merge request context" }
merge request
- if @pipeline.stuck?
......
---
title: Add suffix (`_event`) to merge request source
merge_request: 25508
author:
type: other
......@@ -29,8 +29,8 @@ module Gitlab
def matches_pattern?(pattern, pipeline)
return true if pipeline.tag? && pattern == 'tags'
return true if pipeline.branch? && pattern == 'branches'
return true if pipeline.source == pattern
return true if pipeline.source&.pluralize == pattern
return true if sanitized_source_name(pipeline) == pattern
return true if sanitized_source_name(pipeline)&.pluralize == pattern
# patterns can be matched only when branch or tag is used
# the pattern matching does not work for merge requests pipelines
......@@ -42,6 +42,10 @@ module Gitlab
end
end
end
def sanitized_source_name(pipeline)
@sanitized_source_name ||= pipeline&.source&.delete_suffix('_event')
end
end
end
end
......
......@@ -104,7 +104,7 @@ FactoryBot.define do
trait :with_merge_request_pipeline do
after(:build) do |merge_request|
merge_request.merge_request_pipelines << build(:ci_pipeline,
source: :merge_request,
source: :merge_request_event,
merge_request: merge_request,
project: merge_request.source_project)
end
......
......@@ -49,7 +49,7 @@ describe 'Merge request > User sees merge request pipelines', :js do
let!(:merge_request_pipeline) do
Ci::CreatePipelineService.new(project, user, ref: 'feature')
.execute(:merge_request, merge_request: merge_request)
.execute(:merge_request_event, merge_request: merge_request)
end
before do
......@@ -81,7 +81,7 @@ describe 'Merge request > User sees merge request pipelines', :js do
let!(:merge_request_pipeline_2) do
Ci::CreatePipelineService.new(project, user, ref: 'feature')
.execute(:merge_request, merge_request: merge_request)
.execute(:merge_request_event, merge_request: merge_request)
end
before do
......@@ -220,7 +220,7 @@ describe 'Merge request > User sees merge request pipelines', :js do
let!(:merge_request_pipeline) do
Ci::CreatePipelineService.new(forked_project, user2, ref: 'feature')
.execute(:merge_request, merge_request: merge_request)
.execute(:merge_request_event, merge_request: merge_request)
end
let(:forked_project) { fork_project(project, user2, repository: true) }
......@@ -263,7 +263,7 @@ describe 'Merge request > User sees merge request pipelines', :js do
let!(:merge_request_pipeline_2) do
Ci::CreatePipelineService.new(forked_project, user2, ref: 'feature')
.execute(:merge_request, merge_request: merge_request)
.execute(:merge_request_event, merge_request: merge_request)
end
before do
......
......@@ -666,7 +666,7 @@ describe 'Pipeline', :js do
let(:pipeline) do
create(:ci_pipeline,
source: :merge_request,
source: :merge_request_event,
project: merge_request.source_project,
ref: 'feature',
sha: merge_request.diff_head_sha,
......
......@@ -133,7 +133,7 @@ describe Gitlab::Ci::Build::Policy::Changes do
let(:seed) { double('build seed', to_resource: ci_build) }
context 'when source is merge request' do
let(:source) { :merge_request }
let(:source) { :merge_request_event }
let(:merge_request) do
create(:merge_request,
......
......@@ -68,6 +68,20 @@ describe Gitlab::Ci::Build::Policy::Refs do
expect(described_class.new(%w[triggers]))
.not_to be_satisfied_by(pipeline)
end
context 'when source is merge_request_event' do
let(:pipeline) { build_stubbed(:ci_pipeline, source: :merge_request_event) }
it 'is satisfied with only: merge_request' do
expect(described_class.new(%w[merge_requests]))
.to be_satisfied_by(pipeline)
end
it 'is not satisfied with only: merge_request_event' do
expect(described_class.new(%w[merge_request_events]))
.not_to be_satisfied_by(pipeline)
end
end
end
context 'when matching a ref by a regular expression' do
......
......@@ -96,7 +96,7 @@ describe Gitlab::Ci::Pipeline::Chain::Build do
context 'when pipeline is running for a merge request' do
let(:command) do
Gitlab::Ci::Pipeline::Chain::Command.new(
source: :merge_request,
source: :merge_request_event,
origin_ref: 'feature',
checkout_sha: project.commit.id,
after_sha: nil,
......@@ -117,7 +117,7 @@ describe Gitlab::Ci::Pipeline::Chain::Build do
end
it 'correctly indicated that this is a merge request pipeline' do
expect(pipeline).to be_merge_request
expect(pipeline).to be_merge_request_event
expect(pipeline.merge_request).to eq(merge_request)
end
......
......@@ -115,7 +115,7 @@ describe Gitlab::Ci::Pipeline::Chain::Validate::Config do
let(:pipeline) { build_stubbed(:ci_pipeline, project: project) }
let(:merge_request_pipeline) do
build(:ci_pipeline, source: :merge_request, project: project)
build(:ci_pipeline, source: :merge_request_event, project: project)
end
let(:chain) { described_class.new(merge_request_pipeline, command).tap(&:perform!) }
......
......@@ -23,7 +23,7 @@ describe Ci::Build do
it { is_expected.to validate_presence_of(:ref) }
it { is_expected.to respond_to(:has_trace?) }
it { is_expected.to respond_to(:trace) }
it { is_expected.to delegate_method(:merge_request?).to(:pipeline) }
it { is_expected.to delegate_method(:merge_request_event?).to(:pipeline) }
it { is_expected.to be_a(ArtifactMigratable) }
......
......@@ -80,11 +80,11 @@ describe Ci::Pipeline, :mailer do
context 'when merge request pipelines exist' do
let!(:merge_request_pipeline_1) do
create(:ci_pipeline, source: :merge_request, merge_request: merge_request)
create(:ci_pipeline, source: :merge_request_event, merge_request: merge_request)
end
let!(:merge_request_pipeline_2) do
create(:ci_pipeline, source: :merge_request, merge_request: merge_request)
create(:ci_pipeline, source: :merge_request_event, merge_request: merge_request)
end
let(:merge_request) do
......@@ -106,11 +106,11 @@ describe Ci::Pipeline, :mailer do
let!(:branch_pipeline_2) { create(:ci_pipeline, source: :push) }
let!(:merge_request_pipeline_1) do
create(:ci_pipeline, source: :merge_request, merge_request: merge_request)
create(:ci_pipeline, source: :merge_request_event, merge_request: merge_request)
end
let!(:merge_request_pipeline_2) do
create(:ci_pipeline, source: :merge_request, merge_request: merge_request)
create(:ci_pipeline, source: :merge_request_event, merge_request: merge_request)
end
let(:merge_request) do
......@@ -134,7 +134,7 @@ describe Ci::Pipeline, :mailer do
subject { described_class.detached_merge_request_pipelines(merge_request) }
let!(:pipeline) do
create(:ci_pipeline, source: :merge_request, merge_request: merge_request, target_sha: target_sha)
create(:ci_pipeline, source: :merge_request_event, merge_request: merge_request, target_sha: target_sha)
end
let(:merge_request) { create(:merge_request) }
......@@ -157,7 +157,7 @@ describe Ci::Pipeline, :mailer do
subject { pipeline.detached_merge_request_pipeline? }
let!(:pipeline) do
create(:ci_pipeline, source: :merge_request, merge_request: merge_request, target_sha: target_sha)
create(:ci_pipeline, source: :merge_request_event, merge_request: merge_request, target_sha: target_sha)
end
let(:merge_request) { create(:merge_request) }
......@@ -176,7 +176,7 @@ describe Ci::Pipeline, :mailer do
subject { described_class.merge_request_pipelines(merge_request) }
let!(:pipeline) do
create(:ci_pipeline, source: :merge_request, merge_request: merge_request, target_sha: target_sha)
create(:ci_pipeline, source: :merge_request_event, merge_request: merge_request, target_sha: target_sha)
end
let(:merge_request) { create(:merge_request) }
......@@ -199,7 +199,7 @@ describe Ci::Pipeline, :mailer do
subject { pipeline.merge_request_pipeline? }
let!(:pipeline) do
create(:ci_pipeline, source: :merge_request, merge_request: merge_request, target_sha: target_sha)
create(:ci_pipeline, source: :merge_request_event, merge_request: merge_request, target_sha: target_sha)
end
let(:merge_request) { create(:merge_request) }
......@@ -218,7 +218,7 @@ describe Ci::Pipeline, :mailer do
subject { described_class.mergeable_merge_request_pipelines(merge_request) }
let!(:pipeline) do
create(:ci_pipeline, source: :merge_request, merge_request: merge_request, target_sha: target_sha)
create(:ci_pipeline, source: :merge_request_event, merge_request: merge_request, target_sha: target_sha)
end
let(:merge_request) { create(:merge_request) }
......@@ -241,7 +241,7 @@ describe Ci::Pipeline, :mailer do
subject { pipeline.mergeable_merge_request_pipeline? }
let!(:pipeline) do
create(:ci_pipeline, source: :merge_request, merge_request: merge_request, target_sha: target_sha)
create(:ci_pipeline, source: :merge_request_event, merge_request: merge_request, target_sha: target_sha)
end
let(:merge_request) { create(:merge_request) }
......@@ -256,11 +256,11 @@ describe Ci::Pipeline, :mailer do
end
end
describe '.merge_request' do
subject { described_class.merge_request }
describe '.merge_request_event' do
subject { described_class.merge_request_event }
context 'when there is a merge request pipeline' do
let!(:pipeline) { create(:ci_pipeline, source: :merge_request, merge_request: merge_request) }
let!(:pipeline) { create(:ci_pipeline, source: :merge_request_event, merge_request: merge_request) }
let(:merge_request) { create(:merge_request) }
it 'returns merge request pipeline first' do
......@@ -281,7 +281,7 @@ describe Ci::Pipeline, :mailer do
let(:pipeline) { build(:ci_pipeline, source: source, merge_request: merge_request) }
context 'when source is merge request' do
let(:source) { :merge_request }
let(:source) { :merge_request_event }
context 'when merge request is specified' do
let(:merge_request) { create(:merge_request, source_project: project, source_branch: 'feature', target_project: project, target_branch: 'master') }
......@@ -505,7 +505,7 @@ describe Ci::Pipeline, :mailer do
context 'when source is merge request' do
let(:pipeline) do
create(:ci_pipeline, source: :merge_request, merge_request: merge_request)
create(:ci_pipeline, source: :merge_request_event, merge_request: merge_request)
end
let(:merge_request) do
......@@ -1097,7 +1097,7 @@ describe Ci::Pipeline, :mailer do
context 'when source is merge request' do
let(:pipeline) do
create(:ci_pipeline, source: :merge_request, merge_request: merge_request)
create(:ci_pipeline, source: :merge_request_event, merge_request: merge_request)
end
let(:merge_request) do
......@@ -1147,7 +1147,7 @@ describe Ci::Pipeline, :mailer do
context 'when ref is merge request' do
let(:pipeline) do
create(:ci_pipeline,
source: :merge_request,
source: :merge_request_event,
merge_request: merge_request)
end
......@@ -1310,7 +1310,7 @@ describe Ci::Pipeline, :mailer do
context 'when source is merge request' do
let(:pipeline) do
create(:ci_pipeline, source: :merge_request, merge_request: merge_request)
create(:ci_pipeline, source: :merge_request_event, merge_request: merge_request)
end
let(:merge_request) do
......@@ -2266,7 +2266,7 @@ describe Ci::Pipeline, :mailer do
let!(:pipeline) do
create(:ci_pipeline,
source: :merge_request,
source: :merge_request_event,
project: pipeline_project,
ref: source_branch,
merge_request: merge_request)
......@@ -2289,7 +2289,7 @@ describe Ci::Pipeline, :mailer do
let!(:pipeline_2) do
create(:ci_pipeline,
source: :merge_request,
source: :merge_request_event,
project: pipeline_project,
ref: source_branch,
merge_request: merge_request_2)
......
......@@ -1333,7 +1333,7 @@ describe MergeRequest do
let!(:merge_request_pipeline) do
create(:ci_pipeline,
source: :merge_request,
source: :merge_request_event,
project: project,
ref: source_ref,
sha: shas.second,
......@@ -1372,7 +1372,7 @@ describe MergeRequest do
let!(:merge_request_pipeline_2) do
create(:ci_pipeline,
source: :merge_request,
source: :merge_request_event,
project: project,
ref: source_ref,
sha: shas.first,
......@@ -1399,7 +1399,7 @@ describe MergeRequest do
let!(:merge_request_pipeline_2) do
create(:ci_pipeline,
source: :merge_request,
source: :merge_request_event,
project: project,
ref: source_ref,
sha: shas.first,
......
......@@ -549,7 +549,7 @@ describe API::Runner, :clean_gitlab_redis_shared_state do
end
context 'when job is made for merge request' do
let(:pipeline) { create(:ci_pipeline_without_jobs, source: :merge_request, project: project, ref: 'feature', merge_request: merge_request) }
let(:pipeline) { create(:ci_pipeline_without_jobs, source: :merge_request_event, project: project, ref: 'feature', merge_request: merge_request) }
let!(:job) { create(:ci_build, pipeline: pipeline, name: 'spinach', ref: 'feature', stage: 'test', stage_idx: 0) }
let(:merge_request) { create(:merge_request) }
......
......@@ -701,7 +701,7 @@ describe Ci::CreatePipelineService do
let(:target_sha) { nil }
context 'when source is merge request' do
let(:source) { :merge_request }
let(:source) { :merge_request_event }
context "when config has merge_requests keywords" do
let(:config) do
......@@ -734,7 +734,7 @@ describe Ci::CreatePipelineService do
it 'creates a merge request pipeline' do
expect(pipeline).to be_persisted
expect(pipeline).to be_merge_request
expect(pipeline).to be_merge_request_event
expect(pipeline.merge_request).to eq(merge_request)
expect(pipeline.builds.order(:stage_id).map(&:name)).to eq(%w[test])
end
......
......@@ -194,7 +194,7 @@ describe MergeRequests::CreateService do
merge_request.reload
expect(merge_request.merge_request_pipelines.count).to eq(1)
expect(merge_request.actual_head_pipeline).to be_merge_request
expect(merge_request.actual_head_pipeline).to be_merge_request_event
end
context 'when there are no commits between source branch and target branch' do
......@@ -226,7 +226,7 @@ describe MergeRequests::CreateService do
end
it 'sets the latest merge request pipeline as the head pipeline' do
expect(merge_request.actual_head_pipeline).to be_merge_request
expect(merge_request.actual_head_pipeline).to be_merge_request_event
end
end
......
......@@ -173,12 +173,12 @@ describe MergeRequests::RefreshService do
it 'sets the latest merge request pipeline as a head pipeline' do
@merge_request.reload
expect(@merge_request.actual_head_pipeline).to be_merge_request
expect(@merge_request.actual_head_pipeline).to be_merge_request_event
end
it 'returns pipelines in correct order' do
@merge_request.reload
expect(@merge_request.all_pipelines.first).to be_merge_request
expect(@merge_request.all_pipelines.first).to be_merge_request_event
expect(@merge_request.all_pipelines.second).to be_push
end
end
......
......@@ -39,7 +39,7 @@ describe UpdateHeadPipelineForMergeRequestWorker do
let!(:merge_request_pipeline) do
create(:ci_pipeline,
project: project,
source: :merge_request,
source: :merge_request_event,
sha: latest_sha,
merge_request: merge_request)
end
......
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