Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ To use the "Merge Base Branch into PR" action in your GitHub workflows, follow t
The action requires the following input:

- `baseBranch` (required): The base branch to compare and merge with the pull request branch.
- `autoMerge` (optional): A flag to enable or disable the automatic merge of the base branch into the pull request branch. Default is `true`.
- `descriptionMerged` (optional): Customized the merge commit message.
- `descriptionReminder` (optional): Customized the reminder message posted on the PR comment if autoMerge is disabled.

### Example Workflow

Expand Down
39 changes: 34 additions & 5 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,21 @@ inputs:
required: true
description: 'The base branch to compare and merge.'

autoMerge:
required: false
description: 'Automatically merge the base branch into the pull request branch if necessary.'
default: 'true'

descriptionMerged:
required: false
description: 'Description of the merge commit.'
default: 'Merge ${{ github.event.pull_request.base.ref }} into ${{ github.event.pull_request.head.ref }}'

descriptionReminder:
required: false
description: 'Description of the PR comment if "autoMerge" is false.'
default: 'Please merge `${{ github.event.pull_request.base.ref }}` into `${{ github.event.pull_request.head.ref }}`.'

runs:
using: 'composite'
steps:
Expand All @@ -28,29 +43,43 @@ runs:
BASE=$(git merge-base origin/${{ github.event.pull_request.base.ref }} HEAD)
if [ $BASE = $(git rev-parse origin/${{ github.event.pull_request.base.ref }}) ]; then
echo "Branches are up to date, skipping merge."
echo "::set-output name=mergeRequired::false"
echo "mergeRequired=false" >> $GITHUB_OUTPUT
else
echo "Branches are not up to date, merge is required."
echo "::set-output name=mergeRequired::true"
echo "mergeRequired=true" >> $GITHUB_OUTPUT
fi

- name: Merge base branch into PR branch
shell: bash
if: steps.checkBranches.outputs.mergeRequired == 'true'
if: steps.checkBranches.outputs.mergeRequired == 'true' && inputs.autoMerge == 'true'
run: |
git merge --no-edit --no-commit origin/${{ github.event.pull_request.base.ref }}
if [ $? -ne 0 ]; then
echo "Merge conflict detected. Please resolve conflicts before merging."
exit 1
fi
git commit -m "Merge ${{ github.event.pull_request.base.ref }} into ${{ github.event.pull_request.head.ref }}"
git commit -m "${{ inputs.descriptionMerged }}"

- name: Push changes
shell: bash
if: steps.checkBranches.outputs.mergeRequired == 'true'
if: steps.checkBranches.outputs.mergeRequired == 'true' && inputs.autoMerge == 'true'
run: |
git push origin ${{ github.event.pull_request.head.ref }}

- name: Command on PR to remind the PR branch is not up to date
if: steps.checkBranches.outputs.mergeRequired == 'true' && inputs.autoMerge == 'false'
uses: thollander/actions-comment-pull-request@v3.0.1
with:
message: |
## This branch is out-of-date with the base branch
${{ inputs.descriptionReminder }}
comment-tag: pr-up-to-date

- name: Block the workflow if the PR branch is not up to date
shell: bash
if: steps.checkBranches.outputs.mergeRequired == 'true' && inputs.autoMerge == 'false'
run: exit 1

branding:
icon: 'git-pull-request'
color: 'blue'