2014-04-03 11:16:05 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2015-12-09 05:55:49 -05:00
|
|
|
describe MergeRequests::CreateService, services: true do
|
2017-03-27 17:14:01 -04:00
|
|
|
let(:project) { create(:project, :repository) }
|
2014-04-03 11:16:05 -04:00
|
|
|
let(:user) { create(:user) }
|
2016-02-16 18:01:14 -05:00
|
|
|
let(:assignee) { create(:user) }
|
2014-04-03 11:16:05 -04:00
|
|
|
|
2016-07-11 18:12:31 -04:00
|
|
|
describe '#execute' do
|
2015-01-17 19:34:34 -05:00
|
|
|
context 'valid params' do
|
|
|
|
let(:opts) do
|
|
|
|
{
|
2014-04-03 11:16:05 -04:00
|
|
|
title: 'Awesome merge_request',
|
|
|
|
description: 'please fix',
|
2015-08-11 08:33:31 -04:00
|
|
|
source_branch: 'feature',
|
2016-02-12 15:41:31 -05:00
|
|
|
target_branch: 'master',
|
|
|
|
force_remove_source_branch: '1'
|
2014-04-03 11:16:05 -04:00
|
|
|
}
|
2015-01-17 19:34:34 -05:00
|
|
|
end
|
2016-02-16 18:01:14 -05:00
|
|
|
|
2016-06-30 11:34:19 -04:00
|
|
|
let(:service) { described_class.new(project, user, opts) }
|
2015-01-17 19:34:34 -05:00
|
|
|
|
|
|
|
before do
|
|
|
|
project.team << [user, :master]
|
2016-02-16 18:01:14 -05:00
|
|
|
project.team << [assignee, :developer]
|
2015-02-12 13:17:35 -05:00
|
|
|
allow(service).to receive(:execute_hooks)
|
2014-04-03 11:16:05 -04:00
|
|
|
|
2015-01-17 19:34:34 -05:00
|
|
|
@merge_request = service.execute
|
2014-04-03 11:16:05 -04:00
|
|
|
end
|
|
|
|
|
2017-05-15 05:39:56 -04:00
|
|
|
it 'creates an MR' do
|
|
|
|
expect(@merge_request).to be_valid
|
|
|
|
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')
|
|
|
|
end
|
2015-01-17 19:34:34 -05:00
|
|
|
|
2016-08-01 11:00:44 -04:00
|
|
|
it 'executes hooks with default action' do
|
2015-01-17 19:34:34 -05:00
|
|
|
expect(service).to have_received(:execute_hooks).with(@merge_request)
|
|
|
|
end
|
2016-02-16 18:01:14 -05:00
|
|
|
|
2016-02-20 08:59:59 -05:00
|
|
|
it 'does not creates todos' do
|
2016-02-16 18:01:14 -05:00
|
|
|
attributes = {
|
|
|
|
project: project,
|
2016-02-20 08:59:59 -05:00
|
|
|
target_id: @merge_request.id,
|
|
|
|
target_type: @merge_request.class.name
|
2016-02-16 18:01:14 -05:00
|
|
|
}
|
|
|
|
|
2016-02-20 08:59:59 -05:00
|
|
|
expect(Todo.where(attributes).count).to be_zero
|
2016-02-16 18:01:14 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
it { expect(@merge_request.assignee).to eq assignee }
|
|
|
|
|
2016-02-20 08:59:59 -05:00
|
|
|
it 'creates a todo for new assignee' do
|
2016-02-16 18:01:14 -05:00
|
|
|
attributes = {
|
|
|
|
project: project,
|
|
|
|
author: user,
|
|
|
|
user: assignee,
|
2016-02-20 08:59:59 -05:00
|
|
|
target_id: @merge_request.id,
|
|
|
|
target_type: @merge_request.class.name,
|
|
|
|
action: Todo::ASSIGNED,
|
2016-02-16 18:01:14 -05:00
|
|
|
state: :pending
|
|
|
|
}
|
|
|
|
|
2016-02-20 08:59:59 -05:00
|
|
|
expect(Todo.where(attributes).count).to eq 1
|
2016-02-16 18:01:14 -05:00
|
|
|
end
|
|
|
|
end
|
2017-05-25 17:14:40 -04:00
|
|
|
|
|
|
|
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
|
2017-06-21 09:48:12 -04:00
|
|
|
project.merge_requests
|
|
|
|
.where(source_branch: opts[:source_branch], target_branch: opts[:target_branch])
|
|
|
|
.destroy_all
|
2017-05-25 17:14:40 -04:00
|
|
|
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-04-03 11:16:05 -04:00
|
|
|
end
|
2016-06-30 11:34:19 -04:00
|
|
|
|
2017-05-31 01:50:53 -04:00
|
|
|
it_behaves_like 'new issuable record that supports quick actions' do
|
2016-06-30 11:34:19 -04:00
|
|
|
let(:default_params) do
|
|
|
|
{
|
|
|
|
source_branch: 'feature',
|
|
|
|
target_branch: 'master'
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2016-09-19 03:26:25 -04:00
|
|
|
|
2017-05-31 01:50:53 -04:00
|
|
|
context 'Quick actions' do
|
2017-05-04 08:11:15 -04:00
|
|
|
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
|
|
|
|
project.team << [user, :master]
|
|
|
|
project.team << [assignee, :master]
|
|
|
|
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-06-14 14:18:56 -04:00
|
|
|
before do
|
|
|
|
project.team << [user, :master]
|
|
|
|
end
|
2017-05-04 08:11:15 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
project.team << [assignee, :master]
|
|
|
|
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
|
|
|
|
|
2017-05-10 16:54:10 -04:00
|
|
|
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
|
|
|
|
project.team << [assignee, :master]
|
|
|
|
|
|
|
|
described_class.new(project, user, opts).execute
|
|
|
|
|
|
|
|
expect(assignee.assigned_open_merge_requests_count).to eq 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-05-04 08:11:15 -04:00
|
|
|
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-12-14 16:39:53 -05:00
|
|
|
|
2016-09-19 03:26:25 -04:00
|
|
|
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
|
|
|
|
project.team << [user, :master]
|
|
|
|
project.team << [assignee, :developer]
|
|
|
|
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
|
|
|
|
|
2016-09-20 16:45:02 -04:00
|
|
|
issue_ids = MergeRequestsClosingIssues.where(merge_request: merge_request).pluck(:issue_id)
|
|
|
|
expect(issue_ids).to match_array([first_issue.id, second_issue.id])
|
2016-09-19 03:26:25 -04:00
|
|
|
end
|
|
|
|
end
|
2014-04-03 11:16:05 -04:00
|
|
|
end
|
|
|
|
end
|