Commit 89b684bd authored by Toon Claes's avatar Toon Claes

Only set MR description from template when feature available

Only when the Issuable Default Templates feature is available, fill in
the description from settings when building a new MR.
parent 5f978ac2
module EE
module MergeRequests
module BuildService
def assign_title_and_description
super
assign_description_from_template
end
# Set MR description based on project template
def assign_description_from_template
return unless target_project.feature_available?(:issuable_default_templates) &&
target_project.merge_requests_template.present?
merge_request.description = target_project.merge_requests_template
append_closes_description
end
end
end
end
module MergeRequests
class BuildService < MergeRequests::BaseService
prepend EE::MergeRequests::BuildService
def execute
self.merge_request = MergeRequest.new(params)
merge_request.compare_commits = []
......@@ -105,33 +107,42 @@ module MergeRequests
# more than one commit in the MR
#
def assign_title_and_description
if match = source_branch.match(/\A(\d+)-/)
iid = match[1]
assign_title_and_description_from_single_commit
assign_title_from_issue
merge_request.title ||= source_branch.titleize.humanize
merge_request.title = wip_title if compare_commits.empty?
append_closes_description
end
def assign_title_and_description_from_single_commit
commits = compare_commits
if commits && commits.count == 1
return unless commits && commits.count == 1
commit = commits.first
merge_request.title = commit.title
merge_request.title ||= commit.title
merge_request.description ||= commit.description.try(:strip)
elsif iid && issue = target_project.get_issue(iid, current_user)
end
def assign_title_from_issue
return unless issue
case issue
when Issue
merge_request.title = "Resolve \"#{issue.title}\""
merge_request.title ||= "Resolve \"#{issue.title}\""
when ExternalIssue
merge_request.title = "Resolve #{issue.title}"
merge_request.title ||= "Resolve #{issue.title}"
end
else
merge_request.title = source_branch.titleize.humanize
end
# Set MR description based on project template
if merge_request.target_project.merge_requests_template.present?
merge_request.description = merge_request.target_project.merge_requests_template
end
def append_closes_description
return unless issue_iid
if iid
closes_issue = "Closes ##{iid}"
closes_issue = "Closes ##{issue_iid}"
if description.present?
merge_request.description += closes_issue.prepend("\n\n")
......@@ -140,7 +151,16 @@ module MergeRequests
end
end
merge_request.title = wip_title if commits.empty?
def issue_iid
return @issue_iid if defined?(@issue_iid)
@issue_iid = source_branch[/\A(\d+)-/, 1]
end
def issue
return @issue if defined?(@issue)
@issue = target_project.get_issue(issue_iid, current_user)
end
end
end
require 'spec_helper'
describe MergeRequests::BuildService, services: true do # rubocop:disable RSpec/FilePath
let(:source_project) { project }
let(:target_project) { project }
let(:user) { create(:user) }
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
MergeRequests::BuildService.new(project, user,
description: description,
source_branch: source_branch,
target_branch: target_branch,
source_project: source_project,
target_project: target_project)
end
before do
allow(service).to receive(:branches_valid?) { true }
end
context 'project default template configured' do
let(:template) { "I am the template, you fill me in" }
let(:project) { create(:empty_project, merge_requests_template: template) }
context 'issuable default templates feature not available' do
before do
stub_licensed_features(issuable_default_templates: false)
end
it 'does not set the MR description from template' do
expect(merge_request.description).not_to eq(template)
end
end
context 'issuable default templates feature available' do
before do
stub_licensed_features(issuable_default_templates: true)
end
it 'sets the MR description from template' do
expect(merge_request.description).to eq(template)
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