diff options
author | Sean McGivern <sean@gitlab.com> | 2016-04-28 11:36:37 +0100 |
---|---|---|
committer | Sean McGivern <sean@gitlab.com> | 2016-04-28 13:13:04 +0100 |
commit | 1f5a243a11e13ca7340a0442e4e57373415bdee8 (patch) | |
tree | 1265db20644be41df3dbf196126dad616d0195eb | |
parent | b0b8eb085e8b24f4d385395b3f0529ba825b1697 (diff) | |
download | gitlab-ce-1f5a243a11e13ca7340a0442e4e57373415bdee8.tar.gz |
Auto-set title for branches created from issues
If a branch starts with an issue's IID, followed by a hyphen, the
description will be updated to say that is closes the issue. This also
updates the title of the merge request to 'Resolves "$issue-title"', as
long as:
- There is more than one commit in the merge request (if there is only
one commit, the commit's title will be used as before)
- The issue's IID is valid for the project
-rw-r--r-- | app/services/merge_requests/build_service.rb | 36 | ||||
-rw-r--r-- | spec/services/merge_requests/build_service_spec.rb | 28 |
2 files changed, 57 insertions, 7 deletions
diff --git a/app/services/merge_requests/build_service.rb b/app/services/merge_requests/build_service.rb index fa34753c4fd..dd228a17bb4 100644 --- a/app/services/merge_requests/build_service.rb +++ b/app/services/merge_requests/build_service.rb @@ -38,21 +38,45 @@ module MergeRequests merge_request.can_be_created = false end + set_title_and_description(merge_request) + end + + private + + def set_title_and_description(merge_request) + # When your branch name starts with an iid followed by a dash this pattern will be + # interpreted as the user wants to close that issue on this project. + # + # For example: + # - Issue 112 exists, title: Emoji don't show up in commit title + # - Source branch is: 112-fix-mep-mep + # + # Will lead to: + # - Appending `Closes #112` to the description + # - Setting the title as 'Resolves "Emoji don't show up in commit title"' if there + # is more than one commit in the MR + # + if match = merge_request.source_branch.match(/\A(\d+)-/) + iid = match[1] + end + commits = merge_request.compare_commits if commits && commits.count == 1 commit = commits.first merge_request.title = commit.title merge_request.description ||= commit.description.try(:strip) + elsif iid && (issue = merge_request.target_project.get_issue(iid)) + case issue + when Issue + merge_request.title = "Resolves \"#{issue.title}\"" + when ExternalIssue + merge_request.title = "Resolves #{issue.title}" + end else merge_request.title = merge_request.source_branch.titleize.humanize end - # When your branch name starts with an iid followed by a dash this pattern will - # be interpreted as the use wants to close that issue on this project - # Pattern example: 112-fix-mep-mep - # Will lead to appending `Closes #112` to the description - if match = merge_request.source_branch.match(/\A(\d+)-/) - iid = match[1] + if iid closes_issue = "Closes ##{iid}" if merge_request.description.present? diff --git a/spec/services/merge_requests/build_service_spec.rb b/spec/services/merge_requests/build_service_spec.rb index 7ad13478dd4..87aebae3e9b 100644 --- a/spec/services/merge_requests/build_service_spec.rb +++ b/spec/services/merge_requests/build_service_spec.rb @@ -5,7 +5,7 @@ describe MergeRequests::BuildService, services: true do let(:project) { create(:project) } let(:user) { create(:user) } - let(:issue) { create(:issue, project: project) } + let(:issue) { create(:issue, project: project, title: 'A bug') } let(:description) { nil } let(:source_branch) { 'feature-branch' } let(:target_branch) { 'master' } @@ -141,6 +141,32 @@ describe MergeRequests::BuildService, services: true do expect(merge_request.description).to eq(description) end end + + 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("Resolves \"#{issue.title}\"") + 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 + expect(merge_request.title).to eq("#{issue.iid.succ} fix issue") + end + end + end + + context 'branch starts with external issue IID followed by a hyphen' do + let(:source_branch) { '12345-fix-issue' } + + before { allow(project).to receive(:default_issues_tracker?).and_return(false) } + + it 'sets the title to: Resolves External Issue $issue-iid' do + expect(merge_request.title).to eq('Resolves External Issue 12345') + end + end end end end |