Commit 39fa1f02 authored by Patrick Bajao's avatar Patrick Bajao

Merge branch 'merge_template_add_first_multiline_commit' into 'master'

Add first_commit and first_multiline_commit to commit template

See merge request gitlab-org/gitlab!75819
parents 58e133b3 be1b5c22
...@@ -96,7 +96,7 @@ module Types ...@@ -96,7 +96,7 @@ module Types
description: 'Rebase commit SHA of the merge request.' description: 'Rebase commit SHA of the merge request.'
field :rebase_in_progress, GraphQL::Types::Boolean, method: :rebase_in_progress?, null: false, calls_gitaly: true, field :rebase_in_progress, GraphQL::Types::Boolean, method: :rebase_in_progress?, null: false, calls_gitaly: true,
description: 'Indicates if there is a rebase currently in progress for the merge request.' description: 'Indicates if there is a rebase currently in progress for the merge request.'
field :default_merge_commit_message, GraphQL::Types::String, null: true, field :default_merge_commit_message, GraphQL::Types::String, null: true, calls_gitaly: true,
description: 'Default merge commit message of the merge request.' description: 'Default merge commit message of the merge request.'
field :default_merge_commit_message_with_description, GraphQL::Types::String, null: true, field :default_merge_commit_message_with_description, GraphQL::Types::String, null: true,
description: 'Default merge commit message of the merge request with description. Will have the same value as `defaultMergeCommitMessage` when project has `mergeCommitTemplate` set.', description: 'Default merge commit message of the merge request with description. Will have the same value as `defaultMergeCommitMessage` when project has `mergeCommitTemplate` set.',
......
...@@ -65,6 +65,9 @@ GitLab creates a squash commit message with this template: ...@@ -65,6 +65,9 @@ GitLab creates a squash commit message with this template:
## Supported variables in commit templates ## Supported variables in commit templates
> - [Introduced](https://gitlab.com/gitlab-org/gitlab/-/issues/20263) in GitLab 14.5.
> - [Added](https://gitlab.com/gitlab-org/gitlab/-/issues/346805) `first_commit` and `first_multiline_commit` variables in GitLab 14.6.
Commit message templates support these variables: Commit message templates support these variables:
| Variable | Description | Output example | | Variable | Description | Output example |
...@@ -73,8 +76,10 @@ Commit message templates support these variables: ...@@ -73,8 +76,10 @@ Commit message templates support these variables:
| `%{target_branch}` | The name of the branch that the changes are applied to. | `main` | | `%{target_branch}` | The name of the branch that the changes are applied to. | `main` |
| `%{title}` | Title of the merge request. | `Fix tests and translations` | | `%{title}` | Title of the merge request. | `Fix tests and translations` |
| `%{issues}` | String with phrase `Closes <issue numbers>`. Contains all issues mentioned in the merge request description that match [issue closing patterns](../issues/managing_issues.md#closing-issues-automatically). Empty if no issues are mentioned. | `Closes #465, #190 and #400` | | `%{issues}` | String with phrase `Closes <issue numbers>`. Contains all issues mentioned in the merge request description that match [issue closing patterns](../issues/managing_issues.md#closing-issues-automatically). Empty if no issues are mentioned. | `Closes #465, #190 and #400` |
| `%{description}` | Description of the merge request. | `Merge request description.<br>Can be multiline.` | | `%{description}` | Description of the merge request. | `Merge request description.`<br>`Can be multiline.` |
| `%{reference}` | Reference to the merge request. | `group-name/project-name!72359` | | `%{reference}` | Reference to the merge request. | `group-name/project-name!72359` |
| `%{first_commit}` | Full message of the first commit in merge request diff. | `Update README.md` |
| `%{first_multiline_commit}` | Full message of the first commit that's not a merge commit and has more than one line in message body. Merge Request title if all commits aren't multiline. | `Update README.md`<br><br>`Improved project description in readme file.` |
Empty variables that are the only word in a line are removed, along with all newline characters preceding it. Empty variables that are the only word in a line are removed, along with all newline characters preceding it.
......
...@@ -35,7 +35,9 @@ module Gitlab ...@@ -35,7 +35,9 @@ module Gitlab
"Closes #{closes_issues_references.to_sentence}" "Closes #{closes_issues_references.to_sentence}"
end, end,
'description' => ->(merge_request) { merge_request.description.presence || '' }, 'description' => ->(merge_request) { merge_request.description.presence || '' },
'reference' => ->(merge_request) { merge_request.to_reference(full: true) } 'reference' => ->(merge_request) { merge_request.to_reference(full: true) },
'first_commit' => -> (merge_request) { merge_request.first_commit&.safe_message&.strip.presence || '' },
'first_multiline_commit' => -> (merge_request) { merge_request.first_multiline_commit&.safe_message&.strip.presence || merge_request.title }
}.freeze }.freeze
PLACEHOLDERS_REGEX = Regexp.union(PLACEHOLDERS.keys.map do |key| PLACEHOLDERS_REGEX = Regexp.union(PLACEHOLDERS.keys.map do |key|
......
...@@ -16,6 +16,7 @@ RSpec.describe Gitlab::MergeRequests::CommitMessageGenerator do ...@@ -16,6 +16,7 @@ RSpec.describe Gitlab::MergeRequests::CommitMessageGenerator do
end end
let(:user) { project.creator } let(:user) { project.creator }
let(:source_branch) { 'feature' }
let(:merge_request_description) { "Merge Request Description\nNext line" } let(:merge_request_description) { "Merge Request Description\nNext line" }
let(:merge_request_title) { 'Bugfix' } let(:merge_request_title) { 'Bugfix' }
let(:merge_request) do let(:merge_request) do
...@@ -24,6 +25,8 @@ RSpec.describe Gitlab::MergeRequests::CommitMessageGenerator do ...@@ -24,6 +25,8 @@ RSpec.describe Gitlab::MergeRequests::CommitMessageGenerator do
:simple, :simple,
source_project: project, source_project: project,
target_project: project, target_project: project,
target_branch: 'master',
source_branch: source_branch,
author: user, author: user,
description: merge_request_description, description: merge_request_description,
title: merge_request_title title: merge_request_title
...@@ -226,6 +229,50 @@ RSpec.describe Gitlab::MergeRequests::CommitMessageGenerator do ...@@ -226,6 +229,50 @@ RSpec.describe Gitlab::MergeRequests::CommitMessageGenerator do
MSG MSG
end end
end end
context 'when project has merge commit template with first_commit' do
let(message_template_name) { <<~MSG.rstrip }
Message: %{first_commit}
MSG
it 'uses first commit' do
expect(result_message).to eq <<~MSG.rstrip
Message: Feature added
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
MSG
end
context 'when branch has no unmerged commits' do
let(:source_branch) { 'v1.1.0' }
it 'is an empty string' do
expect(result_message).to eq 'Message: '
end
end
end
context 'when project has merge commit template with first_multiline_commit' do
let(message_template_name) { <<~MSG.rstrip }
Message: %{first_multiline_commit}
MSG
it 'uses first multiline commit' do
expect(result_message).to eq <<~MSG.rstrip
Message: Feature added
Signed-off-by: Dmitriy Zaporozhets <dmitriy.zaporozhets@gmail.com>
MSG
end
context 'when branch has no multiline commits' do
let(:source_branch) { 'spooky-stuff' }
it 'is mr title' do
expect(result_message).to eq 'Message: Bugfix'
end
end
end
end end
end end
......
...@@ -1648,6 +1648,9 @@ RSpec.describe MergeRequest, factory_default: :keep do ...@@ -1648,6 +1648,9 @@ RSpec.describe MergeRequest, factory_default: :keep do
it 'uses template from target project' do it 'uses template from target project' do
request = build(:merge_request, title: 'Fix everything') request = build(:merge_request, title: 'Fix everything')
request.compare_commits = [
double(safe_message: 'Commit message', gitaly_commit?: true, merge_commit?: false, description?: false)
]
subject.target_project.merge_commit_template = '%{title}' subject.target_project.merge_commit_template = '%{title}'
expect(request.default_merge_commit_message) expect(request.default_merge_commit_message)
......
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