2017-05-04 08:09:21 +00:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 14:24:02 +00:00
|
|
|
describe MergeRequests::CreateFromIssueService do
|
2017-05-04 08:09:21 +00:00
|
|
|
let(:project) { create(:project, :repository) }
|
2017-08-18 14:34:26 +00:00
|
|
|
let(:user) { create(:user) }
|
2017-08-17 14:44:37 +00:00
|
|
|
let(:label_ids) { create_pair(:label, project: project).map(&:id) }
|
|
|
|
let(:milestone_id) { create(:milestone, project: project).id }
|
2017-08-18 14:34:26 +00:00
|
|
|
let(:issue) { create(:issue, project: project, milestone_id: milestone_id) }
|
2017-11-25 11:33:05 +00:00
|
|
|
let(:custom_source_branch) { 'custom-source-branch' }
|
2017-08-17 14:44:37 +00:00
|
|
|
|
2017-05-04 08:09:21 +00:00
|
|
|
subject(:service) { described_class.new(project, user, issue_iid: issue.iid) }
|
2017-11-25 11:33:05 +00:00
|
|
|
subject(:service_with_custom_source_branch) { described_class.new(project, user, issue_iid: issue.iid, branch_name: custom_source_branch) }
|
2017-05-04 08:09:21 +00:00
|
|
|
|
|
|
|
before do
|
|
|
|
project.add_developer(user)
|
|
|
|
end
|
|
|
|
|
|
|
|
describe '#execute' do
|
|
|
|
it 'returns an error with invalid issue iid' do
|
|
|
|
result = described_class.new(project, user, issue_iid: -1).execute
|
|
|
|
|
2017-11-25 11:33:05 +00:00
|
|
|
expect(result[:status]).to eq(:error)
|
|
|
|
expect(result[:message]).to eq('Invalid issue iid')
|
2017-05-04 08:09:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'delegates issue search to IssuesFinder' do
|
|
|
|
expect_any_instance_of(IssuesFinder).to receive(:execute).once.and_call_original
|
|
|
|
|
|
|
|
described_class.new(project, user, issue_iid: -1).execute
|
|
|
|
end
|
|
|
|
|
2017-08-17 14:44:37 +00:00
|
|
|
it "inherits labels" do
|
|
|
|
issue.assign_attributes(label_ids: label_ids)
|
|
|
|
|
|
|
|
result = service.execute
|
2017-08-10 18:44:15 +00:00
|
|
|
|
2017-08-17 14:44:37 +00:00
|
|
|
expect(result[:merge_request].label_ids).to eq(label_ids)
|
2017-08-10 18:44:15 +00:00
|
|
|
end
|
|
|
|
|
2017-08-17 14:44:37 +00:00
|
|
|
it "inherits milestones" do
|
|
|
|
result = service.execute
|
2017-08-10 18:44:15 +00:00
|
|
|
|
2017-08-17 14:44:37 +00:00
|
|
|
expect(result[:merge_request].milestone_id).to eq(milestone_id)
|
2017-08-10 18:44:15 +00:00
|
|
|
end
|
|
|
|
|
2017-05-04 08:09:21 +00:00
|
|
|
it 'delegates the branch creation to CreateBranchService' do
|
|
|
|
expect_any_instance_of(CreateBranchService).to receive(:execute).once.and_call_original
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a branch based on issue title' do
|
|
|
|
service.execute
|
|
|
|
|
|
|
|
expect(project.repository.branch_exists?(issue.to_branch_name)).to be_truthy
|
|
|
|
end
|
|
|
|
|
2017-11-25 11:33:05 +00:00
|
|
|
it 'creates a branch using passed name' do
|
|
|
|
service_with_custom_source_branch.execute
|
|
|
|
|
|
|
|
expect(project.repository.branch_exists?(custom_source_branch)).to be_truthy
|
|
|
|
end
|
|
|
|
|
2017-05-04 08:09:21 +00:00
|
|
|
it 'creates a system note' do
|
|
|
|
expect(SystemNoteService).to receive(:new_issue_branch).with(issue, project, user, issue.to_branch_name)
|
|
|
|
|
|
|
|
service.execute
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'creates a merge request' do
|
|
|
|
expect { service.execute }.to change(project.merge_requests, :count).by(1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the merge request title to: "WIP: Resolves "$issue-title"' do
|
|
|
|
result = service.execute
|
|
|
|
|
|
|
|
expect(result[:merge_request].title).to eq("WIP: Resolve \"#{issue.title}\"")
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the merge request author to current user' do
|
|
|
|
result = service.execute
|
|
|
|
|
2017-11-25 11:33:05 +00:00
|
|
|
expect(result[:merge_request].author).to eq(user)
|
2017-05-04 08:09:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the merge request source branch to the new issue branch' do
|
|
|
|
result = service.execute
|
|
|
|
|
2017-11-25 11:33:05 +00:00
|
|
|
expect(result[:merge_request].source_branch).to eq(issue.to_branch_name)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the merge request source branch to the passed branch name' do
|
|
|
|
result = service_with_custom_source_branch.execute
|
|
|
|
|
|
|
|
expect(result[:merge_request].source_branch).to eq(custom_source_branch)
|
2017-05-04 08:09:21 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the merge request target branch to the project default branch' do
|
|
|
|
result = service.execute
|
|
|
|
|
2017-11-25 11:33:05 +00:00
|
|
|
expect(result[:merge_request].target_branch).to eq(project.default_branch)
|
2017-05-04 08:09:21 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|