Automatically merging pull requests.

git:auto-merge

This task is generally used to automatically merge a pull request (PR) in the pull_request.mergeable pipeline after it has passed the pull_request pipeline check and code review. No manual merge button is required.

The triggering conditions and timing of the pull_request.mergeable event are the same as those for events.

# Applicable Events

  • pull_request.mergeable

# Parameters

# mergeType

The default merge strategy is set to "auto": if multiple people have made commits, it will use the "merge" strategy; otherwise, it will use the "squash" strategy.

# mergeCommitMessage

  • type: String
  • required: false

Merge commit message.

When the merge strategy is set to "rebase," the merge commit message is not applicable and does not need to be filled in.

When the merge strategy is set to "merge," the default merge commit message is "chore: merge node (merged by CNB)." It will automatically append the PR reference, the list of reviewers, and the list of contributors included in the PR. Here's an example:

chore: merge node(merged by CNB)

PR-URL: !916
Reviewed-By: tom
Reviewed-By: jerry
Co-authored-by: jack

When the merge strategy is set to "squash," the default value is the first commit message of the PR. It will automatically append the PR reference, the list of reviewers, and the list of contributors included in the PR. For example:

main:
 pull_request.mergeable:
   - stages:
     - name: automerge
       type: git:auto-merge
       options:
         mergeType: squash

This configuration will result in the following outcome:

In a specific PR (feat/model-a -> main) with two commit records,

Commit record 1, submitted on October 1, 2023.

feat(model-a): Add a new feature to module A

Due to certain reasons, add a specific feature

close #10

Commit record 2, fixing some issues pointed out during code review, submitted on October 2, 2023.

fix(model-a): Fix issues pointed out during code review

After the automatic merge, a commit node like this will be created on the main branch, which means that the subsequent commit records (i.e., commit record 2) will be overwritten.

feat(model-a): Add a new feature to module A

Due to certain reasons, add a specific feature

close #10

PR-URL: !3976
Reviewed-By: tom
Reviewed-By: jerry
Co-authored-by: jack

# mergeCommitFooter

  • type: String
  • required: false

To set merge points, use footnotes separated by \n.

This feature only applies to merge and squash operations.

When the merge strategy is set to rebase, this information is not applicable and does not need to be filled in.

When the merge strategy is set to merge or squash, the incoming information will be added as individual lines in the footnotes, followed by the PR reference, reviewers' list, and the list of contributors included in the PR. For example:

main:
 pull_request.mergeable:
   - stages:
     - name: automerge
       type: git:auto-merge
       options:
         mergeType: squash
         mergeCommitMessage: "add feature for some jobs"
         mergeCommitFooter: "--story=123\n--story=456"
add feature for some jobs

--story=123
--story=456
PR-URL: !916
Reviewed-By: tom
Reviewed-By: jerry
Co-authored-by: jack

# removeSourceBranch

  • type: Boolean
  • required: false
  • default: false

Whether to delete the source branch after merging.

The value is irrelevant when the source branch and the target branch are in different repositories.

# ignoreAssignee

  • type: Boolean
  • required: false
  • default: false

Whether to ignore the assignee.

The automatic merging logic will not be executed when the PR has a specified assignee. The original intention of the assignee is to designate someone to handle the task manually.

When set to true, the assignee can be ignored, and the merge can be forced.

Schema Definition

# AUTO_MERGE_TYPE

  • Enum<String>

merge | squash | rebase | auto

# Output Results

{
    reviewedBy, // string, the information of the reviewer to be appended after the commit message
    reviewers, // array<string>, list of reviewers
}

# Configuration Example

# Standalone Usage

main:
  pull_request.mergeable:
    - stages:
        - name: automerge
          type: git:auto-merge
          options:
            mergeType: merge

When a PR on the main branch triggers the pull_request.mergeable event, it will be automatically merged using the merge method.

# Usage with the push event on the target branch

main:
  push:
    - stages:
        - name: build
          script: npm run build
        - name: publish
          script: npm run publish
  pull_request.mergeable:
    - stages:
        - name: automerge
          type: git:auto-merge
          options:
            mergeType: merge

When a PR on the main branch triggers the pull_request.mergeable event, it will be automatically merged using the merge method. After the automatic merge, the push event will be triggered on the target branch (main), and the declared build and publish processes will continue to execute.

There is a question of who is responsible for the resulting push event process. The following assumptions are made:

  1. If the PR submitter is a member of the target repository, they are responsible for the subsequent push event process (i.e., the push build pipeline will be pushed to their repository).
  2. If the PR submitter is not a member of the target repository (e.g., in an open-source project, the PR is submitted from a forked repository), the last reviewer of the code review will be responsible for the subsequent push event process.

# Best Practices

# Use squash for automatic merging

Use squash merging, which creates only one commit node on the target branch for each PR operation, and deletes the source branch if authorized.

master:
  review:
    - stages:
        - name: automerge
          type: git:auto-merge
          options:
            mergeType: squash
            removeSourceBranch: true

# Use auto to automatically select the merge type

If multiple people submit changes, use merge; otherwise, use squash.

master:
  review:
    - stages:
        - name: automerge
          type: git:auto-merge
          options:
            mergeType: auto

# Establish a dedicated code review group for cross-review and automatic merging

  1. Automatically merge after code review approval.
  2. Record the reviewer information in the commit message.
main:
  pull_request.mergeable:
    - stages:
        - name: Auto-merge after code review approval
          type: git:auto-merge
          options:
            mergeType: squash
            mergeCommitMessage: $CNB_LATEST_COMMIT_MESSAGE
          exports:
            reviewedBy: REVIEWED_BY
        - name: notify
          image: tencentcom/wecom-message
          settings:
            robot: "155af237-6041-4125-9340-000000000000"
            msgType: markdown
            content: |
              > Auto-merge after code review approval <@${CNB_BUILD_USER}> 
              > 
              > ${CNB_PULL_REQUEST_TITLE}
              > [${CNB_EVENT_URL}](${CNB_EVENT_URL})
              > 
              > ${REVIEWED_BY}

  pull_request:
    - stages:
        # ...other tasks omitted
        - name: notify
          image: tencentcom/wecom-message
          options:
            robot: "155af237-6041-4125-9340-000000000000"
            msgType: markdown
            content: |
              > ${CURR_REVIEWER_FOR_AT}
              > 
              > ${CNB_PULL_REQUEST_TITLE}
              > [${CNB_EVENT_URL}](${CNB_EVENT_URL})
              > 
              > from ${CNB_BUILD_USER}