Created
October 19, 2021 23:19
-
-
Save samkit-jain/793fbcf4091f951a9122691b55a7f691 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Override Merge | |
on: | |
issue_comment: | |
types: [created] | |
jobs: | |
merge_comment: | |
# Run only if | |
# 1. The comment was made on a PR. | |
# 2. The comment was made by @samkit-jain. | |
# 3. The comment was exactly "/merge" sans the quotes. | |
# 4. The PR is open. | |
if: | | |
github.event.issue.pull_request && | |
github.event.comment.body == '/merge' && | |
!github.event.issue.closed_at && | |
github.event.issue.state == 'open' && | |
github.event.comment.user.login == 'samkit-jain' | |
runs-on: ubuntu-latest | |
name: Merge PR | |
steps: | |
- uses: actions/checkout@v2 | |
with: | |
fetch-depth: 0 | |
token: ${{ secrets.GH_TOKEN }} | |
# Get details of the PR. The target and base branch. And also whether the PR can be merged in or not. | |
- name: Get PR details | |
uses: octokit/request-action@v2.x | |
id: get-pr-details | |
with: | |
route: GET /repos/{repository}/pulls/{pull_number} | |
repository: ${{ github.repository }} | |
pull_number: ${{ github.event.issue.number }} | |
env: | |
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} | |
# Merge (rebase) the PR if it is allowed. | |
- name: Merge the PR | |
id: merge-status | |
shell: bash | |
env: | |
MERGEABLE_STATUS: ${{ fromJson(steps.get-pr-details.outputs.data).mergeable_state }} | |
BASE_BRANCH: ${{ fromJson(steps.get-pr-details.outputs.data).base.ref }} | |
HEAD_BRANCH: ${{ fromJson(steps.get-pr-details.outputs.data).head.ref }} | |
run: | | |
if [ "$MERGEABLE_STATUS" = "clean" ]; then | |
git config --global user.email "<>" | |
git config --global user.name "GitHub Actions" | |
git checkout $HEAD_BRANCH | |
git pull origin $HEAD_BRANCH | |
git checkout $BASE_BRANCH | |
git pull origin $BASE_BRANCH | |
git rebase $HEAD_BRANCH | |
git push origin $BASE_BRANCH | |
echo "::set-output name=message::'PR merged in succesfully.'" | |
else | |
echo "::set-output name=message::'PR cannot be merged in.'" | |
fi | |
# Post a success/failure comment to the PR. | |
- name: Add comment to PR | |
uses: octokit/request-action@v2.x | |
with: | |
route: POST /repos/{repository}/issues/{issue_number}/comments | |
repository: ${{ github.repository }} | |
issue_number: ${{ github.event.issue.number }} | |
body: ${{ steps.merge-status.outputs.message }} | |
env: | |
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} | |
# Post a failure message when any of the previous steps fail. | |
- name: Add failure comment to PR | |
if: ${{ failure() }} | |
uses: octokit/request-action@v2.x | |
with: | |
route: POST /repos/{repository}/issues/{issue_number}/comments | |
repository: ${{ github.repository }} | |
issue_number: ${{ github.event.issue.number }} | |
body: PR cannot be merged in. Check the Actions execution tab for details. | |
env: | |
GITHUB_TOKEN: ${{ secrets.GH_TOKEN }} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment