Skip to content

Commit

Permalink
ci: implements merge tool (#2)
Browse files Browse the repository at this point in the history
  • Loading branch information
alaviss authored Apr 21, 2023
1 parent 52af352 commit aa1d68f
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 0 deletions.
108 changes: 108 additions & 0 deletions .github/workflows/merge-pr.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
name: /merge handler

on:
repository_dispatch:
types: [merge-command]

permissions:
contents: write
pull-requests: write

concurrency: merge-handler-${{ github.event.client_payload.pull_request.node_id || github.run_id }}

jobs:
merge:
if: github.event.client_payload.pull_request != null

name: Trim description and merge

runs-on: ubuntu-latest

steps:
- id: pr-data
name: Get PR data and trim PR body
uses: actions/github-script@v6
with:
script: |
const pull_id = context.payload.client_payload.pull_request.node_id;
core.debug(`Pull request ID: ${pull_id}.`);
const query = `query($id: ID!) {
node(id: $id) {
... on PullRequest {
autoMergeRequest {
mergeMethod
}
closed
body
}
}
}`;
const { node: pr_info } = await github.graphql(query, { id: pull_id });
core.debug(`PR data: ${pr_info}`);
if (node.autoMergeRequest || node.closed) {
core.notice("PR is already merged or is in queue.");
return;
}
const [ body, body_tail ] = pr_info.body.split(/^---\s*$/, 2)
body = body.trimEnd()
body_tail = body_tail?.trimEnd() || ""
core.setOutput('run', 'true')
core.setOutput('body', body)
core.setOutput('body-tail', body_tail)
core.setOutput('update-body', pr_info.body !== result.body)
- if: steps.pr-data.outputs.run
id: find-comment
uses: peter-evans/find-comment@v2
with:
issue-number: ${{ github.event.client_payload.pull_request.number }}
comment-author: github-actions[bot]
body-includes: <!-- /merge info comment -->
direction: last

- if: steps.pr-data.outputs.run && !(steps.find-comment.outputs.comment-id && steps.pr-data.outputs.body_tail == "")
name: Create or update informational comment
uses: peter-evans/create-or-update-comment@v3
with:
comment-id: ${{ steps.find-comment.outputs.comment-id }}
issue-number: ${{ github.event.client_payload.pull_request.number }}
body: |
<!-- /merge info comment -->
Merge requested by: @${{ github.event.client_payload.github.payload.comment.user.login }}
Contents after the first section break of this PR's description has been removed and preserved below:
---
<blockquote>
${{ steps.pr-data.outputs.body-tail }}
</blockquote>
edit-mode: replace

- if: steps.pr-data.outputs.run && steps.pr-data.outputs.update-body
name: Update PR description
uses: actions/github-script@v6
env:
NEW_PR_BODY: ${{ steps.pr-data.outputs.body }}
with:
script: |
const pull_id = context.payload.client_payload.pull_request.node_id;
const update = `mutation ($input: UpdatePullRequestInput!) {
updatePullRequest(input: $input) {
clientMutationId
}
}`;
await github.graphql(update, {
pullRequestId: pull_id,
body: process.env.NEW_PR_BODY
});
- name: Queue for merging
env:
URL: ${{ github.event.client_payload.pull_request.html_url }}
run: gh pr merge "$URL" --auto
23 changes: 23 additions & 0 deletions .github/workflows/slash-commands.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Dispatch slash commands

on:
issue_comment:
types:
- created

permissions:
pull-requests: write

jobs:
dispatch:
name: Dispatch command
runs-on: ubuntu-latest
environment: pr-merge
steps:
- uses: peter-evans/slash-command-dispatch@v3
with:
token: ${{ secrets.PAT }}
commands: |
merge
issue-type: pull-request
permission: write

0 comments on commit aa1d68f

Please sign in to comment.