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

388 lines
12 KiB
Ruby
Raw Normal View History

2014-09-02 18:07:02 +05:30
require 'spec_helper'
2017-09-10 17:25:29 +05:30
describe MergeRequests::CreateService do
2018-05-09 12:01:36 +05:30
include ProjectForksHelper
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) }
2016-04-02 18:10:28 +05:30
let(:assignee) { 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
2016-09-13 17:45:13 +05:30
let(:service) { described_class.new(project, user, 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)
2018-03-17 18:26:18 +05:30
project.add_developer(assignee)
2015-04-26 12:48:37 +05:30
allow(service).to receive(:execute_hooks)
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
2018-03-27 19:54:05 +05:30
expect(merge_request.work_in_progress?).to be(false)
2018-03-17 18:26:18 +05:30
expect(merge_request.title).to eq('Awesome merge_request')
expect(merge_request.assignee).to be_nil
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
2016-09-13 17:45:13 +05:30
it 'executes hooks with default action' do
2018-03-17 18:26:18 +05:30
expect(service).to have_received(:execute_hooks).with(merge_request)
end
it 'refreshes the number of open merge requests', :use_clean_rails_memory_store_caching do
expect { service.execute }
.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
it 'does not creates todos' do
attributes = {
project: project,
2018-03-17 18:26:18 +05:30
target_id: merge_request.id,
target_type: merge_request.class.name
2016-04-02 18:10:28 +05:30
}
expect(Todo.where(attributes).count).to be_zero
end
2017-09-10 17:25:29 +05:30
it 'creates exactly 1 create MR event' do
attributes = {
action: Event::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
2018-03-27 19:54:05 +05:30
describe 'when marked with /wip' do
context 'in title and in description' do
let(:opts) do
{
title: 'WIP: Awesome merge_request',
description: "well this is not done yet\n/wip",
source_branch: 'feature',
target_branch: 'master',
assignee: assignee
}
end
it 'sets MR to WIP' do
expect(merge_request.work_in_progress?).to be(true)
end
end
context 'in description only' do
let(:opts) do
{
title: 'Awesome merge_request',
description: "well this is not done yet\n/wip",
source_branch: 'feature',
target_branch: 'master',
assignee: assignee
}
end
it 'sets MR to WIP' do
expect(merge_request.work_in_progress?).to be(true)
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',
assignee: assignee
}
end
2018-03-17 18:26:18 +05:30
it { expect(merge_request.assignee).to eq assignee }
2016-04-02 18:10:28 +05:30
it 'creates a todo for new assignee' do
attributes = {
project: project,
author: user,
user: assignee,
2018-03-17 18:26:18 +05:30
target_id: merge_request.id,
target_type: merge_request.class.name,
2016-04-02 18:10:28 +05:30
action: Todo::ASSIGNED,
state: :pending
}
expect(Todo.where(attributes).count).to eq 1
end
end
2017-09-10 17:25:29 +05:30
context 'when head pipelines already exist for merge request source branch' do
let(:sha) { project.commit(opts[:source_branch]).id }
let!(:pipeline_1) { create(:ci_pipeline, project: project, ref: opts[:source_branch], project_id: project.id, sha: sha) }
let!(:pipeline_2) { create(:ci_pipeline, project: project, ref: opts[:source_branch], project_id: project.id, sha: sha) }
let!(:pipeline_3) { create(:ci_pipeline, project: project, ref: "other_branch", project_id: project.id) }
before do
2018-11-20 20:47:30 +05:30
# rubocop: disable 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
2018-11-20 20:47:30 +05:30
# rubocop: enable DestroyAll
2017-09-10 17:25:29 +05:30
end
it 'sets head pipeline' do
merge_request = service.execute
expect(merge_request.head_pipeline).to eq(pipeline_2)
expect(merge_request).to be_persisted
end
context 'when merge request head commit sha does not match pipeline sha' do
it 'sets the head pipeline correctly' do
pipeline_2.update(sha: 1234)
merge_request = service.execute
expect(merge_request.head_pipeline).to eq(pipeline_1)
expect(merge_request).to be_persisted
end
end
end
2014-09-02 18:07:02 +05:30
end
2016-09-13 17:45:13 +05:30
2017-09-10 17:25:29 +05:30
it_behaves_like 'new 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
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
let(:merge_request) { described_class.new(project, user, opts).execute }
let(:milestone) { create(:milestone, project: project) }
let(:opts) do
{
assignee_id: create(:user).id,
milestone_id: 1,
title: 'Title',
description: %(/assign @#{assignee.username}\n/milestone %"#{milestone.name}"),
source_branch: 'feature',
target_branch: 'master'
}
end
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
project.add_maintainer(assignee)
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
expect(merge_request.assignee).to eq(assignee)
expect(merge_request.milestone).to eq(milestone)
end
end
end
context 'merge request create service' do
context 'asssignee_id' do
let(:assignee) { create(:user) }
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
opts = { title: 'Title', description: 'Description', assignee_id: -1 }
merge_request = described_class.new(project, user, opts).execute
expect(merge_request.assignee_id).to be_nil
end
it 'removes assignee_id when user id is 0' do
opts = { title: 'Title', description: 'Description', assignee_id: 0 }
merge_request = described_class.new(project, user, opts).execute
expect(merge_request.assignee_id).to be_nil
end
it 'saves assignee when user id is valid' do
2018-11-18 11:00:15 +05:30
project.add_maintainer(assignee)
2017-08-17 22:00:37 +05:30
opts = { title: 'Title', description: 'Description', assignee_id: assignee.id }
merge_request = described_class.new(project, user, opts).execute
expect(merge_request.assignee).to eq(assignee)
end
context 'when assignee is set' do
let(:opts) do
{
title: 'Title',
description: 'Description',
assignee_id: assignee.id,
source_branch: 'feature',
target_branch: 'master'
}
end
it 'invalidates open merge request counter for assignees when merge request is assigned' do
2018-11-18 11:00:15 +05:30
project.add_maintainer(assignee)
2017-08-17 22:00:37 +05:30
described_class.new(project, user, opts).execute
expect(assignee.assigned_open_merge_requests_count).to eq 1
end
end
context "when issuable feature is private" do
before do
project.project_feature.update(issues_access_level: ProjectFeature::PRIVATE,
merge_requests_access_level: ProjectFeature::PRIVATE)
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
project.update(visibility_level: level)
opts = { title: 'Title', description: 'Description', assignee_id: assignee.id }
merge_request = described_class.new(project, user, opts).execute
expect(merge_request.assignee_id).to be_nil
end
end
end
end
end
2016-09-29 09:46:39 +05:30
context 'while saving references to issues that the created merge request closes' do
let(:first_issue) { create(:issue, project: project) }
let(:second_issue) { create(:issue, project: project) }
let(:opts) do
{
title: 'Awesome merge_request',
source_branch: 'feature',
target_branch: 'master',
force_remove_source_branch: '1'
}
end
before do
2018-11-18 11:00:15 +05:30
project.add_maintainer(user)
2018-03-17 18:26:18 +05:30
project.add_developer(assignee)
2016-09-29 09:46:39 +05:30
end
it 'creates a `MergeRequestsClosingIssues` record for each issue' do
issue_closing_opts = opts.merge(description: "Closes #{first_issue.to_reference} and #{second_issue.to_reference}")
service = described_class.new(project, user, issue_closing_opts)
allow(service).to receive(:execute_hooks)
merge_request = service.execute
issue_ids = MergeRequestsClosingIssues.where(merge_request: merge_request).pluck(:issue_id)
expect(issue_ids).to match_array([first_issue.id, second_issue.id])
end
end
2018-03-17 18:26:18 +05:30
context '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
target_project.add_developer(assignee)
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
expect { described_class.new(project, user, opts).execute }
.to raise_error Gitlab::Access::AccessDeniedError
end
end
context 'when user can not access target project' do
before do
target_project.add_developer(assignee)
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
expect { described_class.new(project, user, opts).execute }
.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
it 'creates the merge request' do
merge_request = described_class.new(project, user, opts).execute
expect(merge_request).to be_persisted
end
it 'does not create the merge request when the target project is archived' do
target_project.update!(archived: true)
expect { described_class.new(project, user, opts).execute }
.to raise_error Gitlab::Access::AccessDeniedError
end
end
2018-03-17 18:26:18 +05:30
end
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
project.add_developer(assignee)
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
merge_request = described_class.new(project, user, opts).execute
expect(merge_request.source_project_id).to eq(project.id)
end
end
2014-09-02 18:07:02 +05:30
end
end