Add merge commit message template

Merged Piotr Stankowski requested to merge trakos/gitlab:merge-commit-message-template into master

What does this MR do?

Solves #20263 (closed) and #341394 (closed)

First attempt at adding merge commit templates used parser added in !50063 (merged). However, after discussions here in MR we've decided to use the same approach as for merge suggestion commit templates in !21411 (merged). My changes are similar to that MR.

This new field is editable in project settings, right after existing "Merge suggestions" field:

image

We are also removing "Include merge request description" checkbox from merge commit message editor:

image

It was adding complications to templating (we would have to have 2 separate templates or a variable that changes meaning depending on the checkbox value). Instead, we encourage users to use new template function.

As you can see from the screenshot, I've added link to documentation on this new feature in the same spot instead.

There is still a graphql API field defaultMergeCommitMessageWithDescription. I have marked it as deprecated. For projects with commit message template set, it will ignore the template.

Another difference from my first attempt is that only variables necessary to recreate default behaviour are available. I expect that more variables will be added in future MRs. There also should be a later MR that adds separate template to squash commits.

Following variables are supported in template:

  • source_branch
  • target_branch
  • title
  • description
  • issues - for example Closes #1 and #2 or empty string when no issues are mentioned with appropriate keywords in description
  • reference - for example gitlab-org/gitlab!63864

Template examples

Here's the default template:

Merge branch '%{source_branch}' into '%{target_branch}'

%{title}

%{issues}

See merge request %{reference}

It renders as:

Merge branch 'some-feature' into 'master'

MR title

Closes #1 and #2

See merge request group/project!1

Here's an example of alternative template:

%{title}

MergeRequest: %{reference}
SourceBranch: %{source_branch}
TargetBranch: %{target_branch}

This renders as:

This is mr title

MergeRequest: gitlab-org/gitlab!63864
SourceBranch: test-branch-name
TargetBranch: master

Removing whitespace characters before empty variables

Whenever a variable that is the only word in line is empty (it's possible only for description and issues variables) we are removing it along with all newline characters that precede it. This allows us to recreate behaviour of previous default merge commit message.

The default template is:

Merge branch '%{source_branch}' into '%{target_branch}'

%{title}

%{issues}

See merge request %{reference}

Without any issue closed in MR description it renders as:

Merge branch 'some-feature' into 'master'

MR title

See merge request group/project!1

Simple showcase video

2021-10-29_02-45__Display__OC_Studio_

Database migration

$ rake db:migrate
== 20211015021114 AddMergeCommitTemplateToProjectSettings: migrating ==========
-- add_column(:project_settings, :merge_commit_template, :text)
   -> 0.0008s
== 20211015021114 AddMergeCommitTemplateToProjectSettings: migrated (0.0008s) =

== 20211015024135 AddMergeCommitTemplateLimitToProjectSettings: migrating =====
-- transaction_open?()
   -> 0.0000s
-- current_schema()
   -> 0.0001s
-- transaction_open?()
   -> 0.0000s
-- execute("ALTER TABLE project_settings\nADD CONSTRAINT check_eaf7cfb6a7\nCHECK ( char_length(merge_commit_template) <= 500 )\nNOT VALID;\n")
   -> 0.0006s
-- current_schema()
   -> 0.0002s
-- execute("SET statement_timeout TO 0")
   -> 0.0003s
-- execute("ALTER TABLE project_settings VALIDATE CONSTRAINT check_eaf7cfb6a7;")
   -> 0.0010s
-- execute("RESET statement_timeout")
   -> 0.0003s
== 20211015024135 AddMergeCommitTemplateLimitToProjectSettings: migrated (0.0080s)

$ rake db:migrate:down VERSION=20211015024135
== 20211015024135 AddMergeCommitTemplateLimitToProjectSettings: reverting =====
-- transaction_open?()
   -> 0.0000s
-- transaction_open?()
   -> 0.0000s
-- execute("ALTER TABLE project_settings\nDROP CONSTRAINT IF EXISTS check_eaf7cfb6a7\n")
   -> 0.0006s
== 20211015024135 AddMergeCommitTemplateLimitToProjectSettings: reverted (0.0064s)

$ rake db:migrate:down VERSION=20211015021114
== 20211015021114 AddMergeCommitTemplateToProjectSettings: reverting ==========
-- remove_column(:project_settings, :merge_commit_template, :text)
   -> 0.0007s
== 20211015021114 AddMergeCommitTemplateToProjectSettings: reverted (0.0015s) =

Does this MR meet the acceptance criteria?

Conformity

Availability and Testing

Edited by Piotr Stankowski