Skip to content

Instantly share code, notes, and snippets.

@haliphax
Created April 14, 2023 05:59
Show Gist options
  • Save haliphax/0675faa8ffa3b3e32e47d285d9186125 to your computer and use it in GitHub Desktop.
Save haliphax/0675faa8ffa3b3e32e47d285d9186125 to your computer and use it in GitHub Desktop.
Merge pull request with "merge" comment
# Merges a pull request (if able) when a comment is added by a collaborator,
# contributor, or owner with the word "merge" as its only content
name: Merge on comment
on:
issue_comment:
types: [created]
jobs:
merge-on-comment:
name: Merge on comment
if: |
(
github.event.issue.pull_request
&& github.event.comment.body == 'merge'
&& contains(
'COLLABORATOR CONTRIBUTOR OWNER',
github.event.comment.author_association
)
)
runs-on: ubuntu-latest
steps:
- name: Response
uses: thollander/actions-comment-pull-request@v2
with:
message: Attempting to merge your pull request. Check the **Actions** tab to investigate failures.
comment_tag: merge-on-comment
reactions: rocket
pr_number: ${{ github.event.issue.number }}
- name: Merge pull request
uses: juliangruber/merge-pull-request-action@v1
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
number: ${{ github.event.issue.number }}
method: squash
- name: Failure
if: failure()
uses: thollander/actions-comment-pull-request@v2
with:
message: Pull request failed to merge.
comment_tag: merge-on-comment
reactions: "-1"
pr_number: ${{ github.event.issue.number }}
- name: Success
if: success()
uses: thollander/actions-comment-pull-request@v2
with:
message: Pull request merged successfully.
comment_tag: merge-on-comment
reactions: "+1"
pr_number: ${{ github.event.issue.number }}
@haliphax
Copy link
Author

haliphax commented Apr 14, 2023

Here's a GitHub Workflow I pieced together this evening in order to merge a pull request with a comment as the trigger. Merge rules are still respected (i.e. required checks, approvals, etc. will still prevent merging).

Can be used as a way to prevent the editing of the pull request title before merging takes place (e.g. if squash merge is being used) or to provide outside contributors a way to merge their work without write access to the repository. (Note that the author_association check would need to be altered in that case.)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment