Repository navigation
223 lines (204 loc) · 9.21 KB
/
Copy pathPRMessage.yml
File metadata and controls
223 lines (204 loc) · 9.21 KB
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
name: PR Comment
# Leaves a comment on a new pull-request, made of two collapsible sections:
#
# - "Information about what will happen to your PR", the fixed text of `.github/PRMessage.md`;
# - "Summary of your PR for reviewers", made by `lake exe pr_summary`, which substitutes the
# `<!-- PR_SUMMARY -->` marker of that file.
#
# Every push to the pull-request remakes the comment, so that the summary is of the commits which
# are now there. The comment left by the previous run is updated in place rather than replaced by
# a second one: it is found by the `<!-- physlib-pr-summary -->` marker which is the first line of
# `.github/PRMessage.md`.
#
# `pr_summary` is built from `master`, and only reads the diff of the branch of the pull-request,
# so no code of the pull-request is run.
#
# The tests of `pr_summary` are `#guard_msgs` commands in its source, so building it runs them.
# The `test` job below does that whenever the script changes.
on:
workflow_dispatch:
inputs:
pr:
description: The number of the pull-request to comment on
required: true
type: string
pull_request_target:
# `synchronize` is a push to the branch of the pull-request, which is what makes the summary
# of the previous run out of date.
types: [opened, synchronize]
push:
paths:
- scripts/alpha/github/pr_summary.lean
- .github/workflows/PRMessage.yml
pull_request:
paths:
- scripts/alpha/github/pr_summary.lean
- .github/workflows/PRMessage.yml
# A push which lands while the summary of the previous one is still being made cancels it, so
# that the comment ends up holding the summary of the last commit and not of an earlier one.
# The event is part of the group because a pull-request which changes the script triggers both
# `pull_request` and `pull_request_target`, and those two runs must not cancel each other.
concurrency:
group: >-
${{ github.workflow }}-${{ github.event_name }}-${{
github.event.pull_request.number || inputs.pr || github.ref }}
cancel-in-progress: true
# No permissions by default: each job asks for only what it needs. Only `comment` can write, and
# it runs no code from the pull-request.
permissions: {}
jobs:
test:
name: Test pr_summary
# The pull-request which changes the script, not the one to comment on.
if: github.event_name == 'push' || github.event_name == 'pull_request'
runs-on: ubuntu-latest
permissions:
contents: read
steps:
- name: Checkout
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
# The whole history, so that the smoke test below has something to diff.
fetch-depth: 0
- name: Configure Lean
uses: leanprover/lean-action@f061402b660e0c34644504b324e830f2991d4865 # v1.6.1
with:
auto-config: false
use-github-cache: false
use-mathlib-cache: false
# The `#guard_msgs` tests of the script run as it is compiled, so this fails if one of
# them fails.
- name: Build pr_summary and run its tests
run: lake build pr_summary
- name: Summarise the last commit
run: ./.lake/build/bin/pr_summary HEAD~1 HEAD
summarise:
name: Summarise the pull_request
if: github.event_name == 'pull_request_target' || github.event_name == 'workflow_dispatch'
runs-on: ubuntu-latest
# Read-only: this is the job which checks out the branch of the pull-request.
permissions:
contents: read
pull-requests: read
outputs:
number: ${{ steps.pr.outputs.number }}
comment: ${{ steps.assemble.outputs.comment }}
steps:
- name: Find the pull-request to comment on
id: pr
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
# The number comes from the event for a pull-request, and from the input for a
# manual run.
PR: ${{ github.event.pull_request.number || inputs.pr }}
run: |
gh pr view "${PR}" --repo "${GITHUB_REPOSITORY}" \
--json number,baseRefOid,headRefOid \
--jq '"number=\(.number)\nbase=\(.baseRefOid)\nhead=\(.headRefOid)"' \
| tee -a "${GITHUB_OUTPUT}"
- name: Checkout master to build pr_summary from
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
ref: master
path: tools
- name: Configure Lean
uses: leanprover/lean-action@f061402b660e0c34644504b324e830f2991d4865 # v1.6.1
with:
auto-config: false
use-github-cache: false
use-mathlib-cache: false
lake-package-directory: tools # Building here
- name: Build pr_summary from master
working-directory: tools
run: lake build pr_summary
- name: Checkout the branch of the pull-request
uses: actions/checkout@9c091bb21b7c1c1d1991bb908d89e4e9dddfe3e0 # v7.0.0
with:
ref: ${{ steps.pr.outputs.head }}
# The whole history, so that the base of the pull-request is there to diff against.
fetch-depth: 0
path: pr-branch
# Untrusted (potentially fork) checkout: don't persist the GITHUB_TOKEN into its
# .git/config.
persist-credentials: false
# pr_summary is built from the trusted base checkout and only reads the diff of this
# one, so checking out fork PR code under pull_request_target is safe.
allow-unsafe-pr-checkout: true
- name: Summarise the pull-request
working-directory: pr-branch
env:
BASE: ${{ steps.pr.outputs.base }}
HEAD: ${{ steps.pr.outputs.head }}
run: |
summary="${RUNNER_TEMP}/summary.md"
if ! "${GITHUB_WORKSPACE}/tools/.lake/build/bin/pr_summary" \
"${BASE}" "${HEAD}" > "${summary}"
then
# A comment with no summary is still worth posting.
printf '*The summary of this PR could not be made; see the logs of this workflow.*\n' \
> "${summary}"
fi
# The comment is remade on every push, so say which commit this summary is of.
printf '\n*Summary of commit `%s`, remade whenever a commit is pushed.*\n' \
"${HEAD:0:7}" >> "${summary}"
cat "${summary}"
- name: Assemble the comment
id: assemble
run: |
awk -v summary="${RUNNER_TEMP}/summary.md" '
$0 == "<!-- PR_SUMMARY -->" {
while ((getline line < summary) > 0) print line
next
}
{ print }' tools/.github/PRMessage.md > "${RUNNER_TEMP}/comment.md"
cat "${RUNNER_TEMP}/comment.md"
# The comment is handed to the `comment` job as a multi-line output. The delimiter is
# random, so that no line of the summary, which quotes the pull-request, can end it.
delimiter="EOF_$(openssl rand -hex 16)"
{
printf 'comment<<%s\n' "${delimiter}"
cat "${RUNNER_TEMP}/comment.md"
printf '\n%s\n' "${delimiter}"
} >> "${GITHUB_OUTPUT}"
comment:
name: Comment a pull_request
needs: summarise
runs-on: ubuntu-latest
# The only job which can write, and so the only one which checks nothing out and runs no
# code from the pull-request.
permissions:
pull-requests: write
steps:
- name: Comment a pull_request, or update the comment of the previous run
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PR: ${{ needs.summarise.outputs.number }}
# Passed through the environment, never into the script, so it is not run as shell.
COMMENT: ${{ needs.summarise.outputs.comment }}
# The first line of `.github/PRMessage.md`, and so of the comment this workflow leaves.
MARKER: <!-- physlib-pr-summary -->
run: |
printf '%s' "${COMMENT}" > "${RUNNER_TEMP}/comment.md"
# The comments are fetched in a step of their own, so that `set -e` stops here if the
# API call fails: an empty answer would otherwise read as "no comment yet" and leave a
# second one.
comments="${RUNNER_TEMP}/comments.json"
gh api --paginate "repos/${GITHUB_REPOSITORY}/issues/${PR}/comments" > "${comments}"
# Only a comment of the Actions bot is updated, so a comment which somebody else
# happened to open with the marker is left alone. The oldest match wins, so that a
# comment which was somehow left twice does not leave the two taking turns.
id=$(jq -r --arg marker "${MARKER}" \
'.[] | select(.user.login == "github-actions[bot]")
| select(.body | startswith($marker)) | .id' "${comments}" | head -1)
if [ -n "${id}" ]
then
printf 'Updating comment %s.\n' "${id}"
jq -Rs '{body: .}' "${RUNNER_TEMP}/comment.md" > "${RUNNER_TEMP}/comment.json"
gh api --method PATCH \
"repos/${GITHUB_REPOSITORY}/issues/comments/${id}" \
--input "${RUNNER_TEMP}/comment.json"
else
printf 'No comment of this workflow to update; leaving a new one.\n'
gh pr comment "${PR}" --repo "${GITHUB_REPOSITORY}" \
--body-file "${RUNNER_TEMP}/comment.md"
fi