-
Notifications
You must be signed in to change notification settings - Fork 39
144 lines (125 loc) · 5.01 KB
/
slash-command-merge.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
name: /merge handler
on:
repository_dispatch:
types: [merge-command]
permissions:
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:
- uses: actions/checkout@v4
with:
sparse-checkout: |
tools/ci_format_pr_description.js
- id: pr-data
name: Get PR data and trim PR body
uses: actions/github-script@v7
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
}
mergeQueueEntry {
state
}
closed
body
}
}
}`;
const { node: pr_info } = await github.graphql(query, { id: pull_id });
core.debug(`PR data: ${JSON.stringify(pr_info)}`);
if (pr_info.closed) {
core.notice('PR is already merged or closed.');
return;
}
if (pr_info.autoMergeRequest || pr_info.mergeQueueEntry) {
core.notice('PR is already queued for merging.');
return;
}
const formatPR = require('./tools/ci_format_pr_description.js');
let [ body, body_tail ] = pr_info.body.split(/^---\s*$/m, 2);
body = formatPR.formatPRdescription(body, 72);
body = body.trimEnd();
body_tail = body_tail?.trimEnd() || '';
// Only runs when the PR is still open and is not staged for merging.
core.setOutput('run', 'true');
core.setOutput('body', body);
core.setOutput('body-tail', body_tail);
if (pr_info.body !== body) {
// Only attempt PR description update if there are changes to be made
core.setOutput('update-body', 'true');
}
- if: steps.pr-data.outputs.run
id: find-comment
name: Find previously generated informational comment
uses: peter-evans/find-comment@v3
with:
issue-number: ${{ github.event.client_payload.pull_request.number }}
comment-author: github-actions[bot]
body-includes: <!-- /merge info comment -->
direction: last
# Only update the comment if there's a tail. This prevents the
# tail from being replaced due to multiple /merge calls.
- 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@v4
with:
comment-id: ${{ steps.find-comment.outputs.comment-id }}
issue-number: ${{ github.event.client_payload.pull_request.number }}
body: |
<!-- /merge info comment -->
<!-- Autogenerated by merge-command. DO NOT EDIT -->
Merge requested by: @${{ github.event.client_payload.github.payload.comment.user.login }}
Contents after the first section break of the PR 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
env:
PR_URL: ${{ github.event.client_payload.pull_request.html_url }}
NEW_PR_BODY: ${{ steps.pr-data.outputs.body }}
GH_TOKEN: ${{ github.token }}
run: gh pr edit "$PR_URL" --body "$NEW_PR_BODY"
# An app token is required for CI to trigger
- if: steps.pr-data.outputs.run
id: token
name: Create app token for merges
uses: actions/create-github-app-token@v1
with:
app-id: ${{ secrets.CHORE_APP_ID }}
private-key: ${{ secrets.CHORE_APP_KEY }}
# TODO: Switch to GitHub CLI once
# https://github.com/cli/cli/issues/7213 is solved.
- if: steps.pr-data.outputs.run
name: Queue for merging
uses: actions/github-script@v7
with:
script: |
const pull_id = context.payload.client_payload.pull_request.node_id;
const queue = `mutation ($input: EnablePullRequestAutoMergeInput!) {
enablePullRequestAutoMerge(input: $input) {
clientMutationId
}
}`;
await github.graphql(queue, {
input: {
pullRequestId: pull_id
}
});
github-token: ${{ steps.token.outputs.token }}