Repository navigation
77 lines (73 loc) · 2.86 KB
/
Copy pathauto-update-pr.yml
File metadata and controls
77 lines (73 loc) · 2.86 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
# SPDX-License-Identifier: Apache-2.0
# Managed by github.com/igorjs-iac/repo-config
name: Auto-Update PRs
on:
push:
branches: [main]
permissions:
contents: write
pull-requests: write
jobs:
update:
runs-on: ubuntu-latest
steps:
- uses: step-security/harden-runner@9af89fc71515a100421586dfdb3dc9c984fbf411 # v2.19.4
with:
egress-policy: block
allowed-endpoints: >
api.github.com:443
github.com:443
objects.githubusercontent.com:443
# Skip cleanly when the bot App isn't configured (no BOT_APP_CLIENT_ID),
# so pushes to main don't red-fail. Add the secret to enable auto-updating.
# Read via env, not inline interpolation, to avoid script injection.
- name: Check for bot App credentials
id: bot
env:
BOT_APP_CLIENT_ID: ${{ secrets.BOT_APP_CLIENT_ID }}
run: |
if [ -n "$BOT_APP_CLIENT_ID" ]; then
echo "configured=true" >> "$GITHUB_OUTPUT"
else
echo "configured=false" >> "$GITHUB_OUTPUT"
echo "::notice::BOT_APP_CLIENT_ID not set; skipping PR auto-update."
fi
# Mint a bot App token so branch updates are attributed to the App (which
# has write access), not github-actions[bot]. Updates pushed with the
# default GITHUB_TOKEN surface as "workflow awaiting approval" on the PRs.
- uses: actions/create-github-app-token@bcd2ba49218906704ab6c1aa796996da409d3eb1 # v3.2.0
if: steps.bot.outputs.configured == 'true'
id: app-token
with:
client-id: ${{ secrets.BOT_APP_CLIENT_ID }}
private-key: ${{ secrets.BOT_APP_PRIVATE_KEY }}
permission-contents: write
permission-pull-requests: write
- name: Update open PRs with latest main
if: steps.bot.outputs.configured == 'true'
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
github-token: ${{ steps.app-token.outputs.token }}
script: |
const pulls = await github.rest.pulls.list({
owner: context.repo.owner,
repo: context.repo.repo,
state: 'open',
base: 'main',
});
for (const pr of pulls.data) {
try {
await github.rest.pulls.updateBranch({
owner: context.repo.owner,
repo: context.repo.repo,
pull_number: pr.number,
});
core.info('Updated PR #' + pr.number + ': ' + pr.title);
} catch (err) {
if (err.status === 422) {
core.info('PR #' + pr.number + ' already up-to-date');
} else {
core.warning('Failed to update PR #' + pr.number + ': ' + err.message);
}
}
}