2016-04-28 05:45:45 -04:00
|
|
|
require 'spec_helper'
|
|
|
|
|
2017-07-10 10:24:02 -04:00
|
|
|
describe MergeRequests::BuildService do
|
2016-04-28 05:45:45 -04:00
|
|
|
include RepoHelpers
|
|
|
|
|
2017-03-27 17:14:01 -04:00
|
|
|
let(:project) { create(:project, :repository) }
|
2017-04-03 14:47:14 -04:00
|
|
|
let(:source_project) { nil }
|
|
|
|
let(:target_project) { nil }
|
2016-04-28 05:45:45 -04:00
|
|
|
let(:user) { create(:user) }
|
2016-04-28 07:12:03 -04:00
|
|
|
let(:issue_confidential) { false }
|
|
|
|
let(:issue) { create(:issue, project: project, title: 'A bug', confidential: issue_confidential) }
|
2016-04-28 05:45:45 -04:00
|
|
|
let(:description) { nil }
|
|
|
|
let(:source_branch) { 'feature-branch' }
|
|
|
|
let(:target_branch) { 'master' }
|
|
|
|
let(:merge_request) { service.execute }
|
|
|
|
let(:compare) { double(:compare, commits: commits) }
|
|
|
|
let(:commit_1) { double(:commit_1, safe_message: "Initial commit\n\nCreate the app") }
|
|
|
|
let(:commit_2) { double(:commit_2, safe_message: 'This is a bad commit message!') }
|
|
|
|
let(:commits) { nil }
|
|
|
|
|
|
|
|
let(:service) do
|
2017-07-25 13:09:00 -04:00
|
|
|
described_class.new(project, user,
|
2016-04-28 05:45:45 -04:00
|
|
|
description: description,
|
|
|
|
source_branch: source_branch,
|
2017-04-03 14:47:14 -04:00
|
|
|
target_branch: target_branch,
|
|
|
|
source_project: source_project,
|
|
|
|
target_project: target_project)
|
2016-04-28 05:45:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
before do
|
2016-11-22 05:25:04 -05:00
|
|
|
project.team << [user, :guest]
|
|
|
|
|
2016-04-28 05:45:45 -04:00
|
|
|
allow(CompareService).to receive_message_chain(:new, :execute).and_return(compare)
|
2016-10-06 21:09:15 -04:00
|
|
|
allow(project).to receive(:commit).and_return(commit_1)
|
|
|
|
allow(project).to receive(:commit).and_return(commit_2)
|
2016-04-28 05:45:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
describe 'execute' do
|
|
|
|
context 'missing source branch' do
|
|
|
|
let(:source_branch) { '' }
|
|
|
|
|
|
|
|
it 'forbids the merge request from being created' do
|
|
|
|
expect(merge_request.can_be_created).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds an error message to the merge request' do
|
|
|
|
expect(merge_request.errors).to contain_exactly('You must select source and target branch')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-15 10:01:07 -05:00
|
|
|
context 'when target branch is missing' do
|
|
|
|
let(:target_branch) { nil }
|
|
|
|
let(:commits) { Commit.decorate([commit_1], project) }
|
2016-04-28 05:45:45 -04:00
|
|
|
|
2017-02-15 10:01:07 -05:00
|
|
|
it 'creates compare object with target branch as default branch' do
|
|
|
|
expect(merge_request.compare).to be_present
|
|
|
|
expect(merge_request.target_branch).to eq(project.default_branch)
|
2016-04-28 05:45:45 -04:00
|
|
|
end
|
2017-03-17 09:12:16 -04:00
|
|
|
|
|
|
|
it 'allows the merge request to be created' do
|
|
|
|
expect(merge_request.can_be_created).to eq(true)
|
|
|
|
end
|
2016-04-28 05:45:45 -04:00
|
|
|
end
|
|
|
|
|
2016-09-13 17:05:16 -04:00
|
|
|
context 'same source and target branch' do
|
|
|
|
let(:source_branch) { 'master' }
|
2016-04-28 05:45:45 -04:00
|
|
|
|
|
|
|
it 'forbids the merge request from being created' do
|
|
|
|
expect(merge_request.can_be_created).to eq(false)
|
|
|
|
end
|
2016-09-13 17:05:16 -04:00
|
|
|
|
|
|
|
it 'adds an error message to the merge request' do
|
|
|
|
expect(merge_request.errors).to contain_exactly('You must select different branches')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'no commits in the diff' do
|
|
|
|
let(:commits) { [] }
|
|
|
|
|
|
|
|
it 'allows the merge request to be created' do
|
|
|
|
expect(merge_request.can_be_created).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds a WIP prefix to the merge request title' do
|
|
|
|
expect(merge_request.title).to eq('WIP: Feature branch')
|
|
|
|
end
|
2016-04-28 05:45:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'one commit in the diff' do
|
2016-07-27 07:09:52 -04:00
|
|
|
let(:commits) { Commit.decorate([commit_1], project) }
|
2016-04-28 05:45:45 -04:00
|
|
|
|
|
|
|
it 'allows the merge request to be created' do
|
|
|
|
expect(merge_request.can_be_created).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses the title of the commit as the title of the merge request' do
|
|
|
|
expect(merge_request.title).to eq(commit_1.safe_message.split("\n").first)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses the description of the commit as the description of the merge request' do
|
|
|
|
expect(merge_request.description).to eq(commit_1.safe_message.split(/\n+/, 2).last)
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'merge request already has a description set' do
|
|
|
|
let(:description) { 'Merge request description' }
|
|
|
|
|
|
|
|
it 'keeps the description from the initial params' do
|
|
|
|
expect(merge_request.description).to eq(description)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'commit has no description' do
|
2016-07-27 07:09:52 -04:00
|
|
|
let(:commits) { Commit.decorate([commit_2], project) }
|
2016-04-28 05:45:45 -04:00
|
|
|
|
|
|
|
it 'uses the title of the commit as the title of the merge request' do
|
|
|
|
expect(merge_request.title).to eq(commit_2.safe_message)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'sets the description to nil' do
|
|
|
|
expect(merge_request.description).to be_nil
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'branch starts with issue IID followed by a hyphen' do
|
|
|
|
let(:source_branch) { "#{issue.iid}-fix-issue" }
|
|
|
|
|
|
|
|
it 'appends "Closes #$issue-iid" to the description' do
|
2016-09-01 10:52:43 -04:00
|
|
|
expect(merge_request.description).to eq("#{commit_1.safe_message.split(/\n+/, 2).last}\n\nCloses ##{issue.iid}")
|
2016-04-28 05:45:45 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'merge request already has a description set' do
|
|
|
|
let(:description) { 'Merge request description' }
|
|
|
|
|
|
|
|
it 'appends "Closes #$issue-iid" to the description' do
|
2016-09-01 10:52:43 -04:00
|
|
|
expect(merge_request.description).to eq("#{description}\n\nCloses ##{issue.iid}")
|
2016-04-28 05:45:45 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'commit has no description' do
|
2016-07-27 07:09:52 -04:00
|
|
|
let(:commits) { Commit.decorate([commit_2], project) }
|
2016-04-28 05:45:45 -04:00
|
|
|
|
|
|
|
it 'sets the description to "Closes #$issue-iid"' do
|
|
|
|
expect(merge_request.description).to eq("Closes ##{issue.iid}")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'more than one commit in the diff' do
|
2016-07-27 07:09:52 -04:00
|
|
|
let(:commits) { Commit.decorate([commit_1, commit_2], project) }
|
2016-04-28 05:45:45 -04:00
|
|
|
|
|
|
|
it 'allows the merge request to be created' do
|
|
|
|
expect(merge_request.can_be_created).to eq(true)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses the title of the branch as the merge request title' do
|
|
|
|
expect(merge_request.title).to eq('Feature branch')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not add a description' do
|
|
|
|
expect(merge_request.description).to be_nil
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'merge request already has a description set' do
|
|
|
|
let(:description) { 'Merge request description' }
|
|
|
|
|
|
|
|
it 'keeps the description from the initial params' do
|
|
|
|
expect(merge_request.description).to eq(description)
|
|
|
|
end
|
|
|
|
end
|
2016-04-28 06:36:37 -04:00
|
|
|
|
|
|
|
context 'branch starts with GitLab issue IID followed by a hyphen' do
|
|
|
|
let(:source_branch) { "#{issue.iid}-fix-issue" }
|
|
|
|
|
|
|
|
it 'sets the title to: Resolves "$issue-title"' do
|
|
|
|
expect(merge_request.title).to eq("Resolve \"#{issue.title}\"")
|
|
|
|
end
|
|
|
|
|
2016-11-22 05:25:04 -05:00
|
|
|
context 'when issue is not accessible to user' do
|
|
|
|
before do
|
|
|
|
project.team.truncate
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'uses branch title as the merge request title' do
|
|
|
|
expect(merge_request.title).to eq("#{issue.iid} fix issue")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-04-28 06:36:37 -04:00
|
|
|
context 'issue does not exist' do
|
|
|
|
let(:source_branch) { "#{issue.iid.succ}-fix-issue" }
|
|
|
|
|
|
|
|
it 'uses the title of the branch as the merge request title' do
|
|
|
|
expect(merge_request.title).to eq("#{issue.iid.succ} fix issue")
|
|
|
|
end
|
|
|
|
end
|
2016-04-28 07:12:03 -04:00
|
|
|
|
|
|
|
context 'issue is confidential' do
|
|
|
|
let(:issue_confidential) { true }
|
|
|
|
|
|
|
|
it 'uses the title of the branch as the merge request title' do
|
|
|
|
expect(merge_request.title).to eq("#{issue.iid} fix issue")
|
|
|
|
end
|
|
|
|
end
|
2016-04-28 06:36:37 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
context 'branch starts with external issue IID followed by a hyphen' do
|
|
|
|
let(:source_branch) { '12345-fix-issue' }
|
|
|
|
|
2017-06-14 14:18:56 -04:00
|
|
|
before do
|
2017-07-10 03:38:42 -04:00
|
|
|
allow(project).to receive(:external_issue_tracker).and_return(true)
|
2017-06-14 14:18:56 -04:00
|
|
|
end
|
2016-04-28 06:36:37 -04:00
|
|
|
|
|
|
|
it 'sets the title to: Resolves External Issue $issue-iid' do
|
|
|
|
expect(merge_request.title).to eq('Resolve External Issue 12345')
|
|
|
|
end
|
|
|
|
end
|
2016-04-28 05:45:45 -04:00
|
|
|
end
|
2016-10-06 21:09:15 -04:00
|
|
|
|
|
|
|
context 'source branch does not exist' do
|
|
|
|
before do
|
|
|
|
allow(project).to receive(:commit).with(source_branch).and_return(nil)
|
|
|
|
allow(project).to receive(:commit).with(target_branch).and_return(commit_1)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'forbids the merge request from being created' do
|
|
|
|
expect(merge_request.can_be_created).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds an error message to the merge request' do
|
|
|
|
expect(merge_request.errors).to contain_exactly('Source branch "feature-branch" does not exist')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'target branch does not exist' do
|
|
|
|
before do
|
|
|
|
allow(project).to receive(:commit).with(source_branch).and_return(commit_1)
|
|
|
|
allow(project).to receive(:commit).with(target_branch).and_return(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'forbids the merge request from being created' do
|
|
|
|
expect(merge_request.can_be_created).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds an error message to the merge request' do
|
|
|
|
expect(merge_request.errors).to contain_exactly('Target branch "master" does not exist')
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'both source and target branches do not exist' do
|
|
|
|
before do
|
|
|
|
allow(project).to receive(:commit).and_return(nil)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'forbids the merge request from being created' do
|
|
|
|
expect(merge_request.can_be_created).to eq(false)
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'adds both error messages to the merge request' do
|
|
|
|
expect(merge_request.errors).to contain_exactly(
|
|
|
|
'Source branch "feature-branch" does not exist',
|
|
|
|
'Target branch "master" does not exist'
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2017-04-03 14:47:14 -04:00
|
|
|
|
2017-04-26 18:04:07 -04:00
|
|
|
context 'upstream project has disabled merge requests' do
|
2017-08-02 15:55:11 -04:00
|
|
|
let(:upstream_project) { create(:project, :merge_requests_disabled) }
|
|
|
|
let(:project) { create(:project, forked_from_project: upstream_project) }
|
2017-04-26 18:04:07 -04:00
|
|
|
let(:commits) { Commit.decorate([commit_1], project) }
|
|
|
|
|
|
|
|
it 'sets target project correctly' do
|
|
|
|
expect(merge_request.target_project).to eq(project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-04-03 14:47:14 -04:00
|
|
|
context 'target_project is set and accessible by current_user' do
|
|
|
|
let(:target_project) { create(:project, :public, :repository)}
|
|
|
|
let(:commits) { Commit.decorate([commit_1], project) }
|
|
|
|
|
|
|
|
it 'sets target project correctly' do
|
|
|
|
expect(merge_request.target_project).to eq(target_project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'target_project is set but not accessible by current_user' do
|
|
|
|
let(:target_project) { create(:project, :private, :repository)}
|
|
|
|
let(:commits) { Commit.decorate([commit_1], project) }
|
|
|
|
|
|
|
|
it 'sets target project correctly' do
|
|
|
|
expect(merge_request.target_project).to eq(project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'source_project is set and accessible by current_user' do
|
|
|
|
let(:source_project) { create(:project, :public, :repository)}
|
|
|
|
let(:commits) { Commit.decorate([commit_1], project) }
|
|
|
|
|
|
|
|
it 'sets target project correctly' do
|
|
|
|
expect(merge_request.source_project).to eq(source_project)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
context 'source_project is set but not accessible by current_user' do
|
|
|
|
let(:source_project) { create(:project, :private, :repository)}
|
|
|
|
let(:commits) { Commit.decorate([commit_1], project) }
|
|
|
|
|
|
|
|
it 'sets target project correctly' do
|
|
|
|
expect(merge_request.source_project).to eq(project)
|
|
|
|
end
|
|
|
|
end
|
2016-04-28 05:45:45 -04:00
|
|
|
end
|
|
|
|
end
|