debian-mirror-gitlab/spec/services/merge_requests/create_service_spec.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

579 lines
19 KiB
Ruby
Raw Permalink Normal View History

2019-07-31 22:56:46 +05:30
# frozen_string_literal: true
2014-09-02 18:07:02 +05:30
require 'spec_helper'
2023-04-23 21:23:45 +05:30
RSpec.describe MergeRequests::CreateService, :clean_gitlab_redis_shared_state, feature_category: :code_review_workflow do
2018-05-09 12:01:36 +05:30
include ProjectForksHelper
2023-06-20 00:43:36 +05:30
include AfterNextHelpers
2018-05-09 12:01:36 +05:30
2017-08-17 22:00:37 +05:30
let(:project) { create(:project, :repository) }
2014-09-02 18:07:02 +05:30
let(:user) { create(:user) }
2020-11-24 15:15:51 +05:30
let(:user2) { create(:user) }
2014-09-02 18:07:02 +05:30
2016-08-24 12:49:21 +05:30
describe '#execute' do
2015-04-26 12:48:37 +05:30
context 'valid params' do
let(:opts) do
{
2014-09-02 18:07:02 +05:30
title: 'Awesome merge_request',
description: 'please fix',
2015-09-25 12:07:36 +05:30
source_branch: 'feature',
2016-06-02 11:05:42 +05:30
target_branch: 'master',
force_remove_source_branch: '1'
2014-09-02 18:07:02 +05:30
}
2015-04-26 12:48:37 +05:30
end
2016-04-02 18:10:28 +05:30
2021-06-08 01:23:25 +05:30
let(:service) { described_class.new(project: project, current_user: user, params: opts) }
2018-03-17 18:26:18 +05:30
let(:merge_request) { service.execute }
2015-04-26 12:48:37 +05:30
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2020-11-24 15:15:51 +05:30
project.add_developer(user2)
2014-09-02 18:07:02 +05:30
end
2017-09-10 17:25:29 +05:30
it 'creates an MR' do
2018-03-17 18:26:18 +05:30
expect(merge_request).to be_valid
2022-07-23 23:45:48 +05:30
expect(merge_request.draft?).to be(false)
2018-03-17 18:26:18 +05:30
expect(merge_request.title).to eq('Awesome merge_request')
2019-07-31 22:56:46 +05:30
expect(merge_request.assignees).to be_empty
2018-03-17 18:26:18 +05:30
expect(merge_request.merge_params['force_remove_source_branch']).to eq('1')
2017-09-10 17:25:29 +05:30
end
2015-04-26 12:48:37 +05:30
2023-07-09 08:55:56 +05:30
it 'does not execute hooks' do
expect(project).not_to receive(:execute_hooks)
service.execute
2018-03-17 18:26:18 +05:30
end
it 'refreshes the number of open merge requests', :use_clean_rails_memory_store_caching do
2023-05-27 22:25:52 +05:30
expect do
service.execute
BatchLoader::Executor.clear_current
end.to change { project.open_merge_requests_count }.from(0).to(1)
2015-04-26 12:48:37 +05:30
end
2016-04-02 18:10:28 +05:30
2019-12-26 22:10:19 +05:30
it 'creates exactly 1 create MR event', :sidekiq_might_not_need_inline do
2017-09-10 17:25:29 +05:30
attributes = {
2020-06-23 00:09:42 +05:30
action: :created,
2018-03-17 18:26:18 +05:30
target_id: merge_request.id,
target_type: merge_request.class.name
2017-09-10 17:25:29 +05:30
}
expect(Event.where(attributes).count).to eq(1)
end
2021-04-29 21:17:54 +05:30
it 'sets the merge_status to preparing' do
expect(merge_request.reload).to be_preparing
end
2022-04-04 11:22:00 +05:30
describe 'when marked with /draft' do
2018-03-27 19:54:05 +05:30
context 'in title and in description' do
let(:opts) do
{
2022-04-04 11:22:00 +05:30
title: 'Draft: Awesome merge_request',
description: "well this is not done yet\n/draft",
2018-03-27 19:54:05 +05:30
source_branch: 'feature',
target_branch: 'master',
2020-11-24 15:15:51 +05:30
assignees: [user2]
2018-03-27 19:54:05 +05:30
}
end
2022-04-04 11:22:00 +05:30
it 'sets MR to draft' do
2022-07-23 23:45:48 +05:30
expect(merge_request.draft?).to be(true)
2018-03-27 19:54:05 +05:30
end
end
context 'in description only' do
let(:opts) do
{
title: 'Awesome merge_request',
2021-09-04 01:27:46 +05:30
description: "well this is not done yet\n/draft",
2018-03-27 19:54:05 +05:30
source_branch: 'feature',
target_branch: 'master',
2020-11-24 15:15:51 +05:30
assignees: [user2]
2018-03-27 19:54:05 +05:30
}
end
2022-04-04 11:22:00 +05:30
it 'sets MR to draft' do
2022-07-23 23:45:48 +05:30
expect(merge_request.draft?).to be(true)
2018-03-27 19:54:05 +05:30
end
end
end
2016-04-02 18:10:28 +05:30
context 'when merge request is assigned to someone' do
let(:opts) do
{
title: 'Awesome merge_request',
description: 'please fix',
source_branch: 'feature',
target_branch: 'master',
2022-11-25 23:54:43 +05:30
assignee_ids: [user2.id]
2016-04-02 18:10:28 +05:30
}
end
2020-11-24 15:15:51 +05:30
it { expect(merge_request.assignees).to eq([user2]) }
2016-04-02 18:10:28 +05:30
end
2017-09-10 17:25:29 +05:30
2020-11-24 15:15:51 +05:30
context 'when reviewer is assigned' do
let(:opts) do
{
title: 'Awesome merge_request',
description: 'please fix',
source_branch: 'feature',
target_branch: 'master',
reviewers: [user2]
}
end
it { expect(merge_request.reviewers).to eq([user2]) }
2021-03-08 18:12:59 +05:30
it 'invalidates counter cache for reviewers', :use_clean_rails_memory_store_caching do
expect { merge_request }
.to change { user2.review_requested_open_merge_requests_count }
.by(1)
end
2020-11-24 15:15:51 +05:30
end
2020-04-08 14:13:33 +05:30
context 'when head pipelines already exist for merge request source branch', :sidekiq_inline do
2019-02-15 15:39:39 +05:30
let(:shas) { project.repository.commits(opts[:source_branch], limit: 2).map(&:id) }
let!(:pipeline_1) { create(:ci_pipeline, project: project, ref: opts[:source_branch], project_id: project.id, sha: shas[1]) }
let!(:pipeline_2) { create(:ci_pipeline, project: project, ref: opts[:source_branch], project_id: project.id, sha: shas[0]) }
2017-09-10 17:25:29 +05:30
let!(:pipeline_3) { create(:ci_pipeline, project: project, ref: "other_branch", project_id: project.id) }
before do
2020-06-23 00:09:42 +05:30
# rubocop: disable Cop/DestroyAll
2017-09-10 17:25:29 +05:30
project.merge_requests
.where(source_branch: opts[:source_branch], target_branch: opts[:target_branch])
.destroy_all
2020-06-23 00:09:42 +05:30
# rubocop: enable Cop/DestroyAll
2017-09-10 17:25:29 +05:30
end
it 'sets head pipeline' do
merge_request = service.execute
2019-02-15 15:39:39 +05:30
expect(merge_request.reload.head_pipeline).to eq(pipeline_2)
2017-09-10 17:25:29 +05:30
expect(merge_request).to be_persisted
end
2019-02-15 15:39:39 +05:30
context 'when the new pipeline is associated with an old sha' do
let!(:pipeline_1) { create(:ci_pipeline, project: project, ref: opts[:source_branch], project_id: project.id, sha: shas[0]) }
let!(:pipeline_2) { create(:ci_pipeline, project: project, ref: opts[:source_branch], project_id: project.id, sha: shas[1]) }
2017-09-10 17:25:29 +05:30
2019-02-15 15:39:39 +05:30
it 'sets an old pipeline with associated with the latest sha as the head pipeline' do
2017-09-10 17:25:29 +05:30
merge_request = service.execute
2019-02-15 15:39:39 +05:30
expect(merge_request.reload.head_pipeline).to eq(pipeline_1)
2017-09-10 17:25:29 +05:30
expect(merge_request).to be_persisted
end
end
2019-02-15 15:39:39 +05:30
context 'when there are no pipelines with the diff head sha' do
let!(:pipeline_1) { create(:ci_pipeline, project: project, ref: opts[:source_branch], project_id: project.id, sha: shas[1]) }
let!(:pipeline_2) { create(:ci_pipeline, project: project, ref: opts[:source_branch], project_id: project.id, sha: shas[1]) }
it 'does not set the head pipeline' do
merge_request = service.execute
expect(merge_request.reload.head_pipeline).to be_nil
expect(merge_request).to be_persisted
end
end
end
2020-04-08 14:13:33 +05:30
describe 'Pipelines for merge requests', :sidekiq_inline do
2019-02-15 15:39:39 +05:30
before do
2020-04-08 14:13:33 +05:30
stub_ci_pipeline_yaml_file(config)
2019-02-15 15:39:39 +05:30
end
context "when .gitlab-ci.yml has merge_requests keywords" do
let(:config) do
2020-04-08 14:13:33 +05:30
YAML.dump({
2019-02-15 15:39:39 +05:30
test: {
stage: 'test',
script: 'echo',
only: ['merge_requests']
}
2020-04-08 14:13:33 +05:30
})
2019-02-15 15:39:39 +05:30
end
2019-07-07 11:18:12 +05:30
it 'creates a detached merge request pipeline and sets it as a head pipeline' do
2019-02-15 15:39:39 +05:30
expect(merge_request).to be_persisted
merge_request.reload
2019-07-31 22:56:46 +05:30
expect(merge_request.pipelines_for_merge_request.count).to eq(1)
2019-07-07 11:18:12 +05:30
expect(merge_request.actual_head_pipeline).to be_detached_merge_request_pipeline
end
context 'when merge request is submitted from forked project' do
let(:target_project) { fork_project(project, nil, repository: true) }
let(:opts) do
{
title: 'Awesome merge_request',
source_branch: 'feature',
target_branch: 'master',
target_project_id: target_project.id
}
end
before do
2020-11-24 15:15:51 +05:30
target_project.add_developer(user2)
2019-07-07 11:18:12 +05:30
target_project.add_maintainer(user)
end
2020-07-28 23:09:34 +05:30
it 'create detached merge request pipeline for fork merge request' do
2020-04-08 14:13:33 +05:30
merge_request.reload
2020-07-28 23:09:34 +05:30
head_pipeline = merge_request.actual_head_pipeline
expect(head_pipeline).to be_detached_merge_request_pipeline
expect(head_pipeline.project).to eq(target_project)
2019-07-07 11:18:12 +05:30
end
end
2019-03-02 22:35:43 +05:30
context 'when there are no commits between source branch and target branch' do
let(:opts) do
{
title: 'Awesome merge_request',
description: 'please fix',
source_branch: 'not-merged-branch',
target_branch: 'master'
}
end
2019-07-07 11:18:12 +05:30
it 'does not create a detached merge request pipeline' do
2019-03-02 22:35:43 +05:30
expect(merge_request).to be_persisted
merge_request.reload
2019-07-31 22:56:46 +05:30
expect(merge_request.pipelines_for_merge_request.count).to eq(0)
2019-03-02 22:35:43 +05:30
end
end
2019-02-15 15:39:39 +05:30
context "when branch pipeline was created before a merge request pipline has been created" do
before do
2023-07-09 08:55:56 +05:30
create(
:ci_pipeline,
project: merge_request.source_project,
sha: merge_request.diff_head_sha,
ref: merge_request.source_branch,
tag: false
)
2019-02-15 15:39:39 +05:30
merge_request
end
2019-07-07 11:18:12 +05:30
it 'sets the latest detached merge request pipeline as the head pipeline' do
2020-04-08 14:13:33 +05:30
merge_request.reload
2019-07-07 11:18:12 +05:30
expect(merge_request.actual_head_pipeline).to be_merge_request_event
2019-02-15 15:39:39 +05:30
end
end
end
context "when .gitlab-ci.yml does not have merge_requests keywords" do
let(:config) do
2020-04-08 14:13:33 +05:30
YAML.dump({
2019-02-15 15:39:39 +05:30
test: {
stage: 'test',
script: 'echo'
}
2020-04-08 14:13:33 +05:30
})
2019-02-15 15:39:39 +05:30
end
2019-07-07 11:18:12 +05:30
it 'does not create a detached merge request pipeline' do
2019-02-15 15:39:39 +05:30
expect(merge_request).to be_persisted
merge_request.reload
2019-07-31 22:56:46 +05:30
expect(merge_request.pipelines_for_merge_request.count).to eq(0)
2019-02-15 15:39:39 +05:30
end
end
2020-04-08 14:13:33 +05:30
context 'when .gitlab-ci.yml is invalid' do
let(:config) { 'invalid yaml file' }
it 'persists a pipeline with config error' do
expect(merge_request).to be_persisted
merge_request.reload
expect(merge_request.pipelines_for_merge_request.count).to eq(1)
expect(merge_request.pipelines_for_merge_request.last).to be_failed
expect(merge_request.pipelines_for_merge_request.last).to be_config_error
end
end
2017-09-10 17:25:29 +05:30
end
2019-12-04 20:38:33 +05:30
2020-03-13 15:44:24 +05:30
context 'after_save callback to store_mentions' do
let(:labels) { create_pair(:label, project: project) }
let(:milestone) { create(:milestone, project: project) }
let(:req_opts) { { source_branch: 'feature', target_branch: 'master' } }
context 'when mentionable attributes change' do
let(:opts) { { title: 'Title', description: "Description with #{user.to_reference}" }.merge(req_opts) }
it 'saves mentions' do
expect_next_instance_of(MergeRequest) do |instance|
expect(instance).to receive(:store_mentions!).and_call_original
end
expect(merge_request.user_mentions.count).to eq 1
end
end
context 'when mentionable attributes do not change' do
let(:opts) { { label_ids: labels.map(&:id), milestone_id: milestone.id }.merge(req_opts) }
it 'does not call store_mentions' do
expect_next_instance_of(MergeRequest) do |instance|
expect(instance).not_to receive(:store_mentions!).and_call_original
end
expect(merge_request.valid?).to be false
expect(merge_request.user_mentions.count).to eq 0
end
end
context 'when save fails' do
let(:opts) { { label_ids: labels.map(&:id), milestone_id: milestone.id } }
it 'does not call store_mentions' do
expect_next_instance_of(MergeRequest) do |instance|
expect(instance).not_to receive(:store_mentions!).and_call_original
end
expect(merge_request.valid?).to be false
end
end
end
2020-11-24 15:15:51 +05:30
2023-06-20 00:43:36 +05:30
context 'with a milestone' do
let(:milestone) { create(:milestone, project: project) }
let(:opts) { { title: 'Awesome merge_request', source_branch: 'feature', target_branch: 'master', milestone_id: milestone.id } }
it 'deletes the cache key for milestone merge request counter' do
expect_next(Milestones::MergeRequestsCountService, milestone)
.to receive(:delete_cache).and_call_original
expect(merge_request).to be_persisted
end
end
2020-11-24 15:15:51 +05:30
it_behaves_like 'reviewer_ids filter' do
let(:execute) { service.execute }
end
2023-01-13 00:05:48 +05:30
context 'when called in a transaction' do
it 'does not raise an error' do
expect { MergeRequest.transaction { described_class.new(project: project, current_user: user, params: opts).execute } }.not_to raise_error
end
end
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
2020-07-28 23:09:34 +05:30
it_behaves_like 'issuable record that supports quick actions' do
2016-09-13 17:45:13 +05:30
let(:default_params) do
{
source_branch: 'feature',
target_branch: 'master'
}
end
2020-10-24 23:57:45 +05:30
2021-06-08 01:23:25 +05:30
let(:issuable) { described_class.new(project: project, current_user: user, params: params).execute }
2016-09-13 17:45:13 +05:30
end
2016-09-29 09:46:39 +05:30
2017-09-10 17:25:29 +05:30
context 'Quick actions' do
2017-08-17 22:00:37 +05:30
context 'with assignee and milestone in params and command' do
2021-06-08 01:23:25 +05:30
let(:merge_request) { described_class.new(project: project, current_user: user, params: opts).execute }
2017-08-17 22:00:37 +05:30
let(:milestone) { create(:milestone, project: project) }
let(:opts) do
{
2019-07-31 22:56:46 +05:30
assignee_ids: create(:user).id,
2017-08-17 22:00:37 +05:30
milestone_id: 1,
title: 'Title',
2020-11-24 15:15:51 +05:30
description: %(/assign @#{user2.username}\n/milestone %"#{milestone.name}"),
2017-08-17 22:00:37 +05:30
source_branch: 'feature',
target_branch: 'master'
}
end
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2020-11-24 15:15:51 +05:30
project.add_maintainer(user2)
2017-08-17 22:00:37 +05:30
end
it 'assigns and sets milestone to issuable from command' do
expect(merge_request).to be_persisted
2020-11-24 15:15:51 +05:30
expect(merge_request.assignees).to eq([user2])
2017-08-17 22:00:37 +05:30
expect(merge_request.milestone).to eq(milestone)
end
end
end
context 'merge request create service' do
context 'asssignee_id' do
2020-11-24 15:15:51 +05:30
let(:user2) { create(:user) }
2017-08-17 22:00:37 +05:30
2017-09-10 17:25:29 +05:30
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2017-09-10 17:25:29 +05:30
end
2017-08-17 22:00:37 +05:30
it 'removes assignee_id when user id is invalid' do
2019-07-31 22:56:46 +05:30
opts = { title: 'Title', description: 'Description', assignee_ids: [-1] }
2017-08-17 22:00:37 +05:30
2021-06-08 01:23:25 +05:30
merge_request = described_class.new(project: project, current_user: user, params: opts).execute
2017-08-17 22:00:37 +05:30
2019-07-31 22:56:46 +05:30
expect(merge_request.assignee_ids).to be_empty
2017-08-17 22:00:37 +05:30
end
it 'removes assignee_id when user id is 0' do
2019-07-31 22:56:46 +05:30
opts = { title: 'Title', description: 'Description', assignee_ids: [0] }
2017-08-17 22:00:37 +05:30
2021-06-08 01:23:25 +05:30
merge_request = described_class.new(project: project, current_user: user, params: opts).execute
2017-08-17 22:00:37 +05:30
2019-07-31 22:56:46 +05:30
expect(merge_request.assignee_ids).to be_empty
2017-08-17 22:00:37 +05:30
end
it 'saves assignee when user id is valid' do
2020-11-24 15:15:51 +05:30
project.add_maintainer(user2)
opts = { title: 'Title', description: 'Description', assignee_ids: [user2.id] }
2017-08-17 22:00:37 +05:30
2021-06-08 01:23:25 +05:30
merge_request = described_class.new(project: project, current_user: user, params: opts).execute
2017-08-17 22:00:37 +05:30
2020-11-24 15:15:51 +05:30
expect(merge_request.assignees).to eq([user2])
2017-08-17 22:00:37 +05:30
end
context 'when assignee is set' do
let(:opts) do
{
title: 'Title',
description: 'Description',
2020-11-24 15:15:51 +05:30
assignee_ids: [user2.id],
2017-08-17 22:00:37 +05:30
source_branch: 'feature',
target_branch: 'master'
}
end
2023-06-20 00:43:36 +05:30
before do
2020-11-24 15:15:51 +05:30
project.add_maintainer(user2)
2023-06-20 00:43:36 +05:30
end
2017-08-17 22:00:37 +05:30
2023-06-20 00:43:36 +05:30
it 'invalidates open merge request counter for assignees when merge request is assigned' do
2021-06-08 01:23:25 +05:30
described_class.new(project: project, current_user: user, params: opts).execute
2017-08-17 22:00:37 +05:30
2020-11-24 15:15:51 +05:30
expect(user2.assigned_open_merge_requests_count).to eq 1
2017-08-17 22:00:37 +05:30
end
2023-06-20 00:43:36 +05:30
it 'records the assignee assignment event', :sidekiq_inline do
mr = described_class.new(project: project, current_user: user, params: opts).execute.reload
expect(mr.assignment_events).to match([have_attributes(user_id: user2.id, action: 'add')])
end
2017-08-17 22:00:37 +05:30
end
context "when issuable feature is private" do
before do
2023-07-09 08:55:56 +05:30
project.project_feature.update!(
issues_access_level: ProjectFeature::PRIVATE,
merge_requests_access_level: ProjectFeature::PRIVATE
)
2017-08-17 22:00:37 +05:30
end
levels = [Gitlab::VisibilityLevel::INTERNAL, Gitlab::VisibilityLevel::PUBLIC]
levels.each do |level|
it "removes not authorized assignee when project is #{Gitlab::VisibilityLevel.level_name(level)}" do
2020-11-24 15:15:51 +05:30
project.update!(visibility_level: level)
opts = { title: 'Title', description: 'Description', assignee_ids: [user2.id] }
2017-08-17 22:00:37 +05:30
2021-06-08 01:23:25 +05:30
merge_request = described_class.new(project: project, current_user: user, params: opts).execute
2017-08-17 22:00:37 +05:30
expect(merge_request.assignee_id).to be_nil
end
end
end
end
end
2022-05-07 20:08:51 +05:30
shared_examples 'when source and target projects are different' do
2018-05-09 12:01:36 +05:30
let(:target_project) { fork_project(project, nil, repository: true) }
2018-03-17 18:26:18 +05:30
let(:opts) do
{
title: 'Awesome merge_request',
source_branch: 'feature',
target_branch: 'master',
target_project_id: target_project.id
}
end
context 'when user can not access source project' do
before do
2020-11-24 15:15:51 +05:30
target_project.add_developer(user2)
2018-11-18 11:00:15 +05:30
target_project.add_maintainer(user)
2018-03-17 18:26:18 +05:30
end
it 'raises an error' do
2021-06-08 01:23:25 +05:30
expect { described_class.new(project: project, current_user: user, params: opts).execute }
2018-03-17 18:26:18 +05:30
.to raise_error Gitlab::Access::AccessDeniedError
end
end
context 'when user can not access target project' do
before do
2020-11-24 15:15:51 +05:30
target_project.add_developer(user2)
2018-11-18 11:00:15 +05:30
target_project.add_maintainer(user)
2018-03-17 18:26:18 +05:30
end
it 'raises an error' do
2021-06-08 01:23:25 +05:30
expect { described_class.new(project: project, current_user: user, params: opts).execute }
2018-03-17 18:26:18 +05:30
.to raise_error Gitlab::Access::AccessDeniedError
end
end
2018-05-09 12:01:36 +05:30
context 'when the user has access to both projects' do
before do
target_project.add_developer(user)
project.add_developer(user)
end
2023-04-23 21:23:45 +05:30
it 'creates the merge request', :sidekiq_inline do
merge_request = described_class.new(project: project, current_user: user, params: opts).execute
2023-01-13 00:05:48 +05:30
2023-04-23 21:23:45 +05:30
expect(merge_request).to be_persisted
expect(merge_request.iid).to be > 0
expect(merge_request.merge_request_diff).not_to be_empty
2018-05-09 12:01:36 +05:30
end
it 'does not create the merge request when the target project is archived' do
target_project.update!(archived: true)
2021-06-08 01:23:25 +05:30
expect { described_class.new(project: project, current_user: user, params: opts).execute }
2018-05-09 12:01:36 +05:30
.to raise_error Gitlab::Access::AccessDeniedError
end
end
2018-03-17 18:26:18 +05:30
end
2022-05-07 20:08:51 +05:30
it_behaves_like 'when source and target projects are different'
2018-03-17 18:26:18 +05:30
context 'when user sets source project id' do
let(:another_project) { create(:project) }
let(:opts) do
{
title: 'Awesome merge_request',
source_branch: 'feature',
target_branch: 'master',
source_project_id: another_project.id
}
end
before do
2020-11-24 15:15:51 +05:30
project.add_developer(user2)
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2018-03-17 18:26:18 +05:30
end
it 'ignores source_project_id' do
2021-06-08 01:23:25 +05:30
merge_request = described_class.new(project: project, current_user: user, params: opts).execute
2018-03-17 18:26:18 +05:30
expect(merge_request.source_project_id).to eq(project.id)
end
end
2014-09-02 18:07:02 +05:30
end
end