Commit a4bb0ff8 authored by Sean McGivern's avatar Sean McGivern

Fix closing issues text added to MRs for external issue trackers

Before, this would:

1. Not use the correct reference for non-JIRA external trackers.
2. Append 'Closes ' if an external tracker was enabled, but no issue matched the
   branch name.
parent 5e829934
...@@ -39,7 +39,7 @@ class ExternalIssue ...@@ -39,7 +39,7 @@ class ExternalIssue
end end
def to_reference(_from = nil, full: nil) def to_reference(_from = nil, full: nil)
id reference_link_text
end end
def reference_link_text(from = nil) def reference_link_text(from = nil)
......
...@@ -134,7 +134,7 @@ module MergeRequests ...@@ -134,7 +134,7 @@ module MergeRequests
end end
def append_closes_description def append_closes_description
return unless issue return unless issue&.to_reference.present?
closes_issue = "Closes #{issue.to_reference}" closes_issue = "Closes #{issue.to_reference}"
...@@ -163,7 +163,7 @@ module MergeRequests ...@@ -163,7 +163,7 @@ module MergeRequests
return if merge_request.title.present? return if merge_request.title.present?
if issue_iid.present? if issue_iid.present?
merge_request.title = "Resolve #{issue_iid}" merge_request.title = "Resolve #{issue.to_reference}"
branch_title = source_branch.downcase.remove(issue_iid.downcase).titleize.humanize branch_title = source_branch.downcase.remove(issue_iid.downcase).titleize.humanize
merge_request.title += " \"#{branch_title}\"" if branch_title.present? merge_request.title += " \"#{branch_title}\"" if branch_title.present?
end end
......
require 'spec_helper' require 'spec_helper'
describe MergeRequests::BuildService do describe MergeRequests::BuildService do
using RSpec::Parameterized::TableSyntax
include RepoHelpers include RepoHelpers
let(:project) { create(:project, :repository) } let(:project) { create(:project, :repository) }
...@@ -111,6 +112,7 @@ describe MergeRequests::BuildService do ...@@ -111,6 +112,7 @@ describe MergeRequests::BuildService do
context 'one commit in the diff' do context 'one commit in the diff' do
let(:commits) { Commit.decorate([commit_1], project) } let(:commits) { Commit.decorate([commit_1], project) }
let(:commit_description) { commit_1.safe_message.split(/\n+/, 2).last }
before do before do
stub_compare stub_compare
...@@ -125,7 +127,7 @@ describe MergeRequests::BuildService do ...@@ -125,7 +127,7 @@ describe MergeRequests::BuildService do
end end
it 'uses the description of the commit as the description of the merge request' do 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) expect(merge_request.description).to eq(commit_description)
end end
context 'merge request already has a description set' do context 'merge request already has a description set' do
...@@ -148,68 +150,32 @@ describe MergeRequests::BuildService do ...@@ -148,68 +150,32 @@ describe MergeRequests::BuildService do
end end
end end
context 'branch starts with issue IID followed by a hyphen' do context 'when the source branch matches an issue' do
let(:source_branch) { "#{issue.iid}-fix-issue" } where(:issue_tracker, :source_branch, :closing_message) do
:jira | 'FOO-123-fix-issue' | 'Closes FOO-123'
it 'appends "Closes #$issue-iid" to the description' do :jira | 'fix-issue' | nil
expect(merge_request.description).to eq("#{commit_1.safe_message.split(/\n+/, 2).last}\n\nCloses ##{issue.iid}") :custom_issue_tracker | '123-fix-issue' | 'Closes #123'
:custom_issue_tracker | 'fix-issue' | nil
:internal | '123-fix-issue' | 'Closes #123'
:internal | 'fix-issue' | nil
end end
context 'merge request already has a description set' do with_them do
let(:description) { 'Merge request description' } before do
if issue_tracker == :internal
it 'appends "Closes #$issue-iid" to the description' do issue.update!(iid: 123)
expect(merge_request.description).to eq("#{description}\n\nCloses ##{issue.iid}") else
create(:"#{issue_tracker}_service", project: project)
end
end end
end
context 'commit has no description' do it 'appends the closing description' do
let(:commits) { Commit.decorate([commit_2], project) } expected_description = [commit_description, closing_message].compact.join("\n\n")
it 'sets the description to "Closes #$issue-iid"' do expect(merge_request.description).to eq(expected_description)
expect(merge_request.description).to eq("Closes ##{issue.iid}")
end end
end end
end end
context 'branch starts with numeric characters followed by a hyphen with no issue tracker' do
let(:source_branch) { '12345-fix-issue' }
before do
allow(project).to receive(:external_issue_tracker).and_return(false)
allow(project).to receive(:issues_enabled?).and_return(false)
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
commit_description = commit_1.safe_message.split(/\n+/, 2).last
expect(merge_request.description).to eq("#{commit_description}")
end
end
context 'branch starts with JIRA-formatted external issue IID followed by a hyphen' do
let(:source_branch) { 'EXMPL-12345-fix-issue' }
before do
allow(project).to receive(:external_issue_tracker).and_return(true)
allow(project).to receive(:issues_enabled?).and_return(false)
allow(project).to receive(:external_issue_reference_pattern).and_return(IssueTrackerService.reference_pattern)
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 and appends the closes text' do
commit_description = commit_1.safe_message.split(/\n+/, 2).last
expect(merge_request.description).to eq("#{commit_description}\n\nCloses EXMPL-12345")
end
end
end end
context 'more than one commit in the diff' do context 'more than one commit in the diff' do
...@@ -239,90 +205,62 @@ describe MergeRequests::BuildService do ...@@ -239,90 +205,62 @@ describe MergeRequests::BuildService do
end end
end end
context 'branch starts with GitLab issue IID followed by a hyphen' do context 'when the source branch matches an issue' do
let(:source_branch) { "#{issue.iid}-fix-issue" } where(:issue_tracker, :source_branch, :title, :closing_message) do
:jira | 'FOO-123-fix-issue' | 'Resolve FOO-123 "Fix issue"' | 'Closes FOO-123'
it 'sets the title to: Resolves "$issue-title"' do :jira | 'fix-issue' | 'Fix issue' | nil
expect(merge_request.title).to eq("Resolve \"#{issue.title}\"") :custom_issue_tracker | '123-fix-issue' | 'Resolve #123 "Fix issue"' | 'Closes #123'
:custom_issue_tracker | 'fix-issue' | 'Fix issue' | nil
:internal | '123-fix-issue' | 'Resolve "A bug"' | 'Closes #123'
:internal | 'fix-issue' | 'Fix issue' | nil
:internal | '124-fix-issue' | '124 fix issue' | nil
end end
context 'when issue is not accessible to user' do with_them do
before do before do
project.team.truncate if issue_tracker == :internal
end issue.update!(iid: 123)
else
it 'uses branch title as the merge request title' do create(:"#{issue_tracker}_service", project: project)
expect(merge_request.title).to eq("#{issue.iid} fix issue") end
end end
end
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 it 'sets the correct title' do
expect(merge_request.title).to eq("#{issue.iid.succ} fix issue") expect(merge_request.title).to eq(title)
end end
end
context 'issue is confidential' do
let(:issue_confidential) { true }
it 'uses the title of the branch as the merge request title' do it 'sets the closing description' do
expect(merge_request.title).to eq("#{issue.iid} fix issue") expect(merge_request.description).to eq(closing_message)
end end
end end
end end
context 'branch starts with numeric characters followed by a hyphen with no issue tracker' do context 'when the issue is not accessible to user' do
let(:source_branch) { '12345-fix-issue' } let(:source_branch) { "#{issue.iid}-fix-issue" }
before do before do
allow(project).to receive(:external_issue_tracker).and_return(false) project.team.truncate
allow(project).to receive(:issues_enabled?).and_return(false)
end end
it 'sets the title to the humanized branch title' do it 'uses branch title as the merge request title' do
expect(merge_request.title).to eq('12345 fix issue') expect(merge_request.title).to eq("#{issue.iid} fix issue")
end end
end
describe 'with JIRA enabled' do it 'does not set a description' do
before do expect(merge_request.description).to be_nil
allow(project).to receive(:external_issue_tracker).and_return(true)
allow(project).to receive(:issues_enabled?).and_return(false)
allow(project).to receive(:external_issue_reference_pattern).and_return(IssueTrackerService.reference_pattern)
end end
end
context 'branch does not start with JIRA-formatted external issue IID' do context 'when the issue is confidential' do
let(:source_branch) { 'test-branch' } let(:source_branch) { "#{issue.iid}-fix-issue" }
let(:issue_confidential) { true }
it 'sets the title to the humanized branch title' do it 'uses the title of the branch as the merge request title' do
expect(merge_request.title).to eq('Test branch') expect(merge_request.title).to eq("#{issue.iid} fix issue")
end
end end
context 'branch starts with JIRA-formatted external issue IID' do it 'does not set a description' do
let(:source_branch) { 'EXMPL-12345' } expect(merge_request.description).to be_nil
it 'sets the title to the humanized branch title' do
expect(merge_request.title).to eq('Resolve EXMPL-12345')
end
it 'appends the closes text' do
expect(merge_request.description).to eq('Closes EXMPL-12345')
end
context 'followed by hyphenated text' do
let(:source_branch) { 'EXMPL-12345-fix-issue' }
it 'sets the title to the humanized branch title' do
expect(merge_request.title).to eq('Resolve EXMPL-12345 "Fix issue"')
end
it 'appends the closes text' do
expect(merge_request.description).to eq('Closes EXMPL-12345')
end
end
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment