Commit c389a705 authored by Max Coplan's avatar Max Coplan

Set default MR title/description to first multi-line commit

Closes https://gitlab.com/gitlab-org/gitlab/-/issues/300479

When creating a merge request, the behaviour of the prefilled title and
discription is different than the behaviour of squashed commit messages.

For squashed commit messages, the behaviour is described in the
[docs](https://docs.gitlab.com/ee/user/project/merge_requests/squash_and_merge.html#overview)
as

> Taken from the first multi-line commit message in the merge.

However, for merge requests, the current behavior is to use the branch
name.  This commit sets the prefilled MR title/description to the first
multi-line commit in the MR, if applicable.  Otherwise falling back to
the previous behaviour.
parent 463f8307
......@@ -1282,7 +1282,14 @@ class MergeRequest < ApplicationRecord
# Returns the oldest multi-line commit message, or the MR title if none found
def default_squash_commit_message
strong_memoize(:default_squash_commit_message) do
recent_commits.without_merge_commits.reverse_each.find(&:description?)&.safe_message || title
first_multiline_commit&.safe_message || title
end
end
# Returns the oldest multi-line commit
def first_multiline_commit
strong_memoize(:first_multiline_commit) do
recent_commits.without_merge_commits.reverse_each.find(&:description?)
end
end
......
......@@ -58,6 +58,7 @@ module MergeRequests
:compare_commits,
:wip_title,
:description,
:first_multiline_commit,
:errors,
to: :merge_request
......@@ -196,7 +197,8 @@ module MergeRequests
# 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
# - Issue 112 exists
# - title: Emoji don't show up in commit title
# - Source branch is: 112-fix-mep-mep
#
# Will lead to:
......@@ -205,7 +207,7 @@ module MergeRequests
# more than one commit in the MR
#
def assign_title_and_description
assign_title_and_description_from_single_commit
assign_title_and_description_from_commits
merge_request.title ||= title_from_issue if target_project.issues_enabled? || target_project.external_issue_tracker
merge_request.title ||= source_branch.titleize.humanize
merge_request.title = wip_title if compare_commits.empty?
......@@ -240,12 +242,16 @@ module MergeRequests
end
end
def assign_title_and_description_from_single_commit
def assign_title_and_description_from_commits
commits = compare_commits
return unless commits&.count == 1
if commits&.count == 1
commit = commits.first
else
commit = first_multiline_commit
return unless commit
end
commit = commits.first
merge_request.title ||= commit.title
merge_request.description ||= commit.description.try(:strip)
end
......
---
title: Prefill first multiline commit message for new MRs
merge_request: 52984
author: Max Coplan @vegerot
type: changed
......@@ -32,9 +32,12 @@ button and start a merge request from there.
On the **New Merge Request** page, start by filling in the title
and description for the merge request. If there are already
commits on the branch, the title is prefilled with the first
line of the first commit message, and the description is
prefilled with any additional lines in the commit message.
commits on the branch, the title is prefilled with either:
- The first line of the first multi-line commit message
and the description is prefilled with any additional lines
in that commit message.
- The branch name, if there are no multi-line commit messages.
The title is the only field that is mandatory in all cases.
From there, you can fill it with information (title, description,
......
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