-
-
Notifications
You must be signed in to change notification settings - Fork 12.5k
301 lines (269 loc) · 11.9 KB
/
create-replacement-pr.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
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
name: Create replacement pull request
run-name: "Replace PR #${{ inputs.pull_request }}"
defaults:
run:
shell: bash -xeuo pipefail {0}
concurrency:
group: ${{ github.workflow }}-${{ github.event.inputs.pull_request }}-${{ github.event.inputs.upload }}
cancel-in-progress: ${{ !fromJson(github.event.inputs.upload) }}
on:
workflow_dispatch:
inputs:
pull_request:
description: Pull request number
type: number
required: true
autosquash:
description: "Squash pull request commits according to Homebrew style? (default: true)"
type: boolean
required: false
default: true
upload:
description: >
Upload bottles built from original pull request? (default: false)
Warning: This destroys status check information when used with autosquash!
type: boolean
required: false
default: false
warn_on_upload_failure:
description: "Pass `--warn-on-upload-failure` to `brew pr-pull`? (default: false)"
type: boolean
required: false
default: false
message:
description: "Message to include when autosquashing revision bumps, deletions, and rebuilds (requires autosquash)"
required: false
env:
PR: ${{ inputs.pull_request }}
GNUPGHOME: /tmp/gnupghome
HOMEBREW_DEVELOPER: 1
HOMEBREW_NO_AUTO_UPDATE: 1
HOMEBREW_NO_INSTALL_FROM_API: 1
GH_REPO: ${{ github.repository }}
GH_NO_UPDATE_NOTIFIER: 1
GH_PROMPT_DISABLED: 1
RUN_URL: ${{ github.event.repository.html_url }}/actions/runs/${{ github.run_id }}
REPLACEMENT_BRANCH: PR/${{ inputs.pull_request }}
jobs:
create:
runs-on: ubuntu-latest
container:
image: ghcr.io/homebrew/ubuntu22.04:master
permissions:
contents: read
pull-requests: write # for `post-comment`, `gh api`, `gh pr edit`
repository-projects: write # for `gh pr edit`
attestations: write # for actions/attest-build-provenance
id-token: write # for actions/attest-build-provenance
steps:
- name: Post comment once started
uses: Homebrew/actions/post-comment@master
with:
token: ${{ secrets.GITHUB_TOKEN }}
issue: ${{ env.PR }}
body: ":shipit: @${{ github.actor }} has [requested creation of a replacement PR](${{ env.RUN_URL }})."
bot_body: ":robot: An automated task has [requested creation of a replacement PR](${{ env.RUN_URL }})."
bot: github-actions[bot]
- name: Set up Homebrew
id: set-up-homebrew
uses: Homebrew/actions/setup-homebrew@master
with:
core: true
cask: false
test-bot: false
- name: Get reviewers
id: reviewers
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
review_data="$(
gh api \
--header 'Accept: application/vnd.github+json' \
--header 'X-GitHub-Api-Version: 2022-11-28' \
"repos/$GITHUB_REPOSITORY/pulls/$PR/reviews"
)"
reviewers="$(jq --compact-output '[.[].user.login]' <<< "$review_data")"
approved="$(jq --raw-output 'any(.[].state; .== "APPROVED")' <<< "$review_data")"
# See https://github.com/octokit/octokit.net/issues/1763 for possible `mergeable_state` values.
mergeable="$(
gh api \
--header 'Accept: application/vnd.github+json' \
--header 'X-GitHub-Api-Version: 2022-11-28' \
"repos/$GITHUB_REPOSITORY/pulls/$PR" \
--jq '(.mergeable_state == "clean") and (.draft | not)'
)"
{
echo "reviewers=$reviewers"
echo "approved=$approved"
echo "mergeable=$mergeable"
} >> "$GITHUB_OUTPUT"
- name: Check approval if needed
if: >
inputs.upload &&
!(fromJson(steps.reviewers.outputs.approved) && fromJson(steps.reviewers.outputs.mergeable))
run: |
echo "::error ::Refusing to upload bottles because PR #$PR is not mergeable!"
exit 1
- name: Configure Git user
id: git-user-config
uses: Homebrew/actions/git-user-config@master
with:
username: BrewTestBot
- name: Checkout PR branch
if: ${{ !inputs.autosquash }}
working-directory: ${{steps.set-up-homebrew.outputs.repository-path}}
env:
GH_TOKEN: ${{secrets.GITHUB_TOKEN}}
run: gh pr checkout "$PR" --repo "$GITHUB_REPOSITORY"
- name: Checkout replacement PR branch
working-directory: ${{ steps.set-up-homebrew.outputs.repository-path }}
env:
START_POINT: ${{ inputs.autosquash && 'origin/master' || 'HEAD' }}
run: |
if git ls-remote --exit-code --heads origin "$REPLACEMENT_BRANCH"
then
echo "::error ::Branch $REPLACEMENT_BRANCH already exists!"
exit 1
fi
git checkout -b "$REPLACEMENT_BRANCH" "$START_POINT"
- name: Dismiss approvals
if: fromJson(steps.reviewers.outputs.approved)
uses: Homebrew/actions/dismiss-approvals@master
with:
token: ${{ secrets.GITHUB_TOKEN }}
pr: ${{ env.PR }}
message: Replacement PR dispatched
- name: Set up commit signing
uses: Homebrew/actions/setup-commit-signing@master
with:
signing_key: ${{ secrets.BREWTESTBOT_GPG_SIGNING_SUBKEY }}
- name: Pull PR
id: pr-pull
working-directory: ${{ steps.set-up-homebrew.outputs.repository-path }}
env:
BREWTESTBOT_NAME_EMAIL: "BrewTestBot <[email protected]>"
HOMEBREW_GPG_PASSPHRASE: ${{ inputs.autosquash && secrets.BREWTESTBOT_GPG_SIGNING_SUBKEY_PASSPHRASE }}
HOMEBREW_GITHUB_API_TOKEN: ${{ secrets.HOMEBREW_CORE_PUBLIC_REPO_EMAIL_TOKEN }}
MESSAGE: ${{ inputs.message }}
AUTOSQUASH_FLAG: ${{ inputs.autosquash && '--autosquash' || '' }}
CLEAN_FLAG: ${{ inputs.autosquash && '' || '--clean' }}
NO_CHERRY_PICK_FLAG: ${{ inputs.autosquash && '' || '--no-cherry-pick' }}
run: |
# Don't quote arguments that might be empty; this causes errors.
brew pr-pull \
--no-upload \
--debug \
--branch-okay \
--workflows=tests.yml \
--committer="$BREWTESTBOT_NAME_EMAIL" \
--root-url="https://ghcr.io/v2/homebrew/core" \
--retain-bottle-dir \
${AUTOSQUASH_FLAG:+"${AUTOSQUASH_FLAG}"} \
${CLEAN_FLAG:+"--clean"} \
${NO_CHERRY_PICK_FLAG:+"--no-cherry-pick"} \
${MESSAGE:+"--message=${MESSAGE}"} \
"$PR"
- name: Generate build provenance
uses: actions/attest-build-provenance@7668571508540a607bdfd90a87a560489fe372eb # v2.1.0
with:
subject-path: '${{steps.pr-pull.outputs.bottle_path}}/*.tar.gz'
if: inputs.upload
- name: Upload bottles to GitHub Packages
id: pr-upload
if: inputs.upload
working-directory: ${{steps.pr-pull.outputs.bottle_path}}
env:
BREWTESTBOT_NAME_EMAIL: "BrewTestBot <[email protected]>"
HOMEBREW_GPG_PASSPHRASE: ${{ secrets.BREWTESTBOT_GPG_SIGNING_SUBKEY_PASSPHRASE }}
HOMEBREW_GITHUB_PACKAGES_USER: brewtestbot
HOMEBREW_GITHUB_PACKAGES_TOKEN: ${{secrets.HOMEBREW_CORE_GITHUB_PACKAGES_TOKEN}}
WARN_ON_UPLOAD_FAILURE_FLAG: ${{inputs.warn_on_upload_failure && '--warn-on-upload-failure' || ''}}
run: |
# Don't quote arguments that might be empty; this causes errors when `brew`
# interprets them as empty arguments when we want `brew` to ignore them instead.
brew pr-upload \
--debug \
--committer="$BREWTESTBOT_NAME_EMAIL" \
--root-url="https://ghcr.io/v2/homebrew/core" \
${WARN_ON_UPLOAD_FAILURE_FLAG:+"${WARN_ON_UPLOAD_FAILURE_FLAG}"}
- name: Push commits
uses: Homebrew/actions/git-try-push@master
with:
token: ${{ secrets.HOMEBREW_GITHUB_PUBLIC_REPO_TOKEN }}
directory: ${{ steps.set-up-homebrew.outputs.repository-path }}
branch: ${{ env.REPLACEMENT_BRANCH }}
env:
GIT_COMMITTER_NAME: ${{ steps.git-user-config.outputs.name }}
GIT_COMMITTER_EMAIL: ${{ steps.git-user-config.outputs.email }}
HOMEBREW_GPG_PASSPHRASE: ${{ secrets.BREWTESTBOT_GPG_SIGNING_SUBKEY_PASSPHRASE }}
- name: Open replacement pull request
id: create-pr
working-directory: ${{ steps.set-up-homebrew.outputs.repository-path }}
env:
GH_TOKEN: ${{ secrets.HOMEBREW_GITHUB_PUBLIC_REPO_TOKEN }}
REVIEWERS: ${{ join(fromJson(steps.reviewers.outputs.reviewers)) }}
LABELS: ${{ inputs.upload && 'CI-published-bottle-commits' || '' }}
run: |
cat <<MESSAGE > body.txt
Created by [\`create-replacement-pr.yml\`]($RUN_URL)
-----
Closes #$PR
MESSAGE
original_title="$(gh pr view "$PR" --json title --jq '.title' --repo "$GITHUB_REPOSITORY")"
gh pr create \
--base "$GITHUB_REF" \
--title "$original_title (replacement for #$PR)" \
--body-file body.txt \
--head "$REPLACEMENT_BRANCH" \
--reviewer "$REVIEWERS" \
--label "$LABELS" \
--repo "$GITHUB_REPOSITORY"
pull_number="$(gh pr list --head "$REPLACEMENT_BRANCH" --limit 1 --json number --jq '.[].number' --repo "$GITHUB_REPOSITORY")"
echo "pull_number=$pull_number" >> "$GITHUB_OUTPUT"
- name: Approve replacement PR if applicable
if: >
fromJson(steps.reviewers.outputs.approved) &&
fromJson(steps.reviewers.outputs.mergeable)
working-directory: ${{ steps.set-up-homebrew.outputs.repository-path }}
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
REPLACEMENT_PR: ${{ steps.create-pr.outputs.pull_number }}
run: gh pr review --approve "$REPLACEMENT_PR" --repo "$GITHUB_REPOSITORY"
- name: Enable automerge for replacement PR if applicable
if: inputs.upload
working-directory: ${{ steps.set-up-homebrew.outputs.repository-path }}
env:
GH_TOKEN: ${{ secrets.HOMEBREW_GITHUB_PUBLIC_REPO_TOKEN }}
REPLACEMENT_PR: ${{ steps.create-pr.outputs.pull_number }}
run: |
gh pr merge "$REPLACEMENT_PR" \
--auto \
--merge \
--delete-branch \
--match-head-commit "$(git rev-parse HEAD)" \
--repo "$GITHUB_REPOSITORY"
- name: Label replaced PR
env:
GH_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: gh pr edit --add-label automerge-skip --add-label superseded "$PR" --repo "$GITHUB_REPOSITORY"
- name: Mark replaced PR as draft
env: # We need a PAT here. https://github.com/github/docs/issues/8925#issuecomment-970255180
GH_TOKEN: ${{ secrets.HOMEBREW_GITHUB_PUBLIC_REPO_TOKEN }}
run: gh pr ready --undo "$PR" --repo "$GITHUB_REPOSITORY"
- name: Post comment on success
uses: Homebrew/actions/post-comment@master
with:
token: ${{ secrets.GITHUB_TOKEN }}
issue: ${{ env.PR }}
body: ":white_check_mark: @${{ github.actor }} replacement PR created at #${{ steps.create-pr.outputs.pull_number }}."
bot_body: ":white_check_mark: Replacement PR created at #${{ steps.create-pr.outputs.pull_number }}."
bot: github-actions[bot]
- name: Post comment on failure
if: ${{ !success() }}
uses: Homebrew/actions/post-comment@master
with:
token: ${{ secrets.GITHUB_TOKEN }}
issue: ${{ env.PR }}
body: ":warning: @${{ github.actor }} replacement PR creation [failed](${{ env.RUN_URL }})."
bot_body: ":warning: Replacement PR creation [failed](${{ env.RUN_URL }})."
bot: github-actions[bot]