Repository navigation
241 lines (227 loc) · 11.8 KB
/
Copy pathkeep-pr-current.yml
File metadata and controls
241 lines (227 loc) · 11.8 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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
name: Keep Pull Request Current
# Replaces the Mergify `update` rule that used to merge `develop` into open
# pull requests (see `.github/mergify.yml` and #3574).
#
# Why this is a workflow and not a Mergify rule: Mergify reports a `failure`
# check whenever it cannot perform the update, with no option to change that.
# The schema accepts only `bot_account` for the action
# (https://docs.mergify.com/mergify-configuration-schema.json, `$defs.UpdateActionModel`,
# `additionalProperties: false`), so the red check could only be avoided by
# never attempting an update. That outcome is not gated — the attempt itself is
# what produced the failures. This workflow performs the same merge through
# GitHub's `update-branch` API and reports a conflict as a comment, so the
# check is green whether or not the merge was possible.
#
# The up-to-date requirement is unchanged: `develop-branch-ruleset` still sets
# `strict_required_status_checks_policy`, so a branch that is behind `develop`
# and cannot be updated is still blocked from merging. Nothing here relaxes the
# ruleset, and this workflow is deliberately not a required check.
#
# `synchronize` is deliberately absent from the `pull_request` types, not because
# it would loop -- an already-current result makes no further push, so the chain
# would terminate -- but because every update is itself a push and would double the
# runs for no gain. Branches fall behind because `develop` moves, which is why
# `push` is the trigger that matters. What `synchronize` would additionally cover
# is a pull request its author has just de-conflicted; that gap is accepted, see
# the schedule decision below.
#
# GitHub cannot run `pull_request` workflows at all on a pull request that
# conflicts (`refs/pull/<n>/merge` is 404 because the merge ref cannot be
# built), so a conflicting pull request is only ever reached from a push to
# `develop`. The `pull_request` trigger exists to cover the opened/reopened and
# ready-for-review cases.
#
# A periodic `schedule` was considered as a backstop and deliberately rejected.
# The gap it would close is real: a pull request can sit behind `develop` after
# its author pushes a conflict resolution, and that push is a `synchronize`
# event, which this workflow ignores. Measured against this repository on
# 2026-09-30, with 15 open pull requests and `develop` moving 218 commits in the
# preceding 7 days:
#
# * `*/15` is 96 runs a day, each enumerating 15 pull requests, so ~1,440
# `update-branch` calls a day. Almost all resolve to the 422
# "There are no new commits on the base branch" path, which is one wasted API
# call per pull request per run.
# * The cost that matters is not minutes. `develop-branch-ruleset` sets
# `dismiss_stale_reviews_on_push`, so every branch update moves the head and
# dismisses the approval. A schedule that updated pull requests merely
# because they were behind would churn heads on a fixed cadence and
# repeatedly void approvals that were given against an exact head -- turning
# the strict rule this workflow exists to satisfy into the thing breaking it.
# * `schedule` runs only from the default branch and GitHub disables scheduled
# workflows after 60 days of repository inactivity, so it would also be the
# least reliable of the available triggers.
#
# `workflow_dispatch` is the backstop instead: one click, run on demand, and it
# changes nothing that was not already stale.
#
# Why a GitHub App token and not GITHUB_TOKEN. A branch update is a push, and a
# push made with GITHUB_TOKEN does not leave the new head's checks in a usable
# state. Measured on a scratch pull request on 2026-09-30: after an update the
# new head had ten `pull_request` workflow runs, every one of them
# `action_required`, and therefore no check runs at all — no
# `Route PR template and apply labels`, no `Validate changelog on PR`, no
# `actionlint`, so the pull request sat BLOCKED with its required checks
# missing. GitHub documents exactly this, at
# https://docs.github.com/en/actions/concepts/security/github_token:
# "pull_request events with the opened, synchronize, or reopened activity types:
# when a workflow using GITHUB_TOKEN creates or updates a pull request, the
# resulting pull_request event creates workflow runs in an approval-required
# state." Reproduced twice, on two separate updates.
#
# The App is the same one `.github/workflows/documentation.yml` already uses for
# the same reason — its comment reads "App token (not GITHUB_TOKEN) so the
# regeneration PR triggers CI and can satisfy develop's required checks" — with
# the same two repository secrets. `updateBranch` needs `contents: write`;
# upserting a comment needs `Pull requests: write` or `Issues: write`
# (https://docs.github.com/en/rest/issues/comments), so no additional
# permission is requested. `permission-workflows` is deliberately absent, as in
# documentation.yml: this workflow does not change workflow files.
on:
push:
branches:
- develop
pull_request:
types:
- opened
- reopened
- ready_for_review
workflow_dispatch:
# The job's own GITHUB_TOKEN is read-only: it only needs to check the helper out.
# Every write goes through the App token below, so a compromised checkout cannot
# push with a workflow-scoped write token.
permissions:
contents: read
# One run per repository state. Queued behind any run already in flight so a
# burst of pushes to `develop` cannot have two runs merging the same branch.
concurrency:
group: keep-pr-current
cancel-in-progress: false
jobs:
keep-current:
name: Keep pull request current
runs-on: ubuntu-latest
timeout-minutes: 10
steps:
# Sparse checkout: the decision logic in scripts/ is all this needs, and
# no pull request code is ever checked out.
- name: Checkout repository
uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1
with:
persist-credentials: false
fetch-depth: 1
sparse-checkout: |
scripts/automation
- name: Set up Node.js
uses: actions/setup-node@949feb2413d6458794dcd2491c4babbbce0c15c1 # v7.1.0
with:
node-version-file: '.nvmrc'
# Skipped for pull requests from forks, where repository secrets are not
# exposed. Those are skipped by shouldAttemptUpdate before any write, so
# falling back to the read-only GITHUB_TOKEN below is safe.
- name: Generate GitHub App token
id: app-token
# continue-on-error: if the App secrets are absent or invalid, the token
# is empty and github-token falls back to GITHUB_TOKEN, which is
# read-only. The run then updates nothing and says so, rather than
# failing every pull request's check over a missing secret.
continue-on-error: true
if: github.event_name != 'pull_request' || github.event.pull_request.head.repo.full_name == github.repository
uses: actions/create-github-app-token@64ab729a6294000f5aa4a4bc0b73d1f32bc45510 # v3.3.0
with:
# Same secrets as documentation.yml. Not invented for this workflow.
client-id: ${{ secrets.BOT_PR_APP_CLIENT_ID }}
private-key: ${{ secrets.BOT_PR_APP_PRIVATE_KEY }}
# Least privilege: merge develop into the head branch, and upsert one
# comment. `permission-workflows` is deliberately absent — see the
# header comment.
permission-contents: write
permission-pull-requests: write
- name: Merge develop into open pull requests
uses: actions/github-script@3a2844b7e9c422d3c10d287c895573f7108da1b3 # v9.0.0
with:
# The App token, so the update's push triggers the pull request's
# checks. Falls back to GITHUB_TOKEN (read-only here) only for forks,
# which are never written to.
github-token: ${{ steps.app-token.outputs.token || github.token }}
script: |
// Relative specifier, matching the other workflows here
// (ai-feedback-validation.yml, orchestrate-phase-progression.yml).
const { processPullRequest } = require('./scripts/automation/keep-pr-current.cjs');
const { owner, repo } = context.repo;
// A pull_request event carries its own number; a push to develop has
// to enumerate what is open against it.
let numbers = [];
if (context.eventName === 'pull_request') {
numbers = [context.payload.pull_request.number];
} else {
// Guarded so a listing failure cannot fail the job. The
// per-pull-request work is untouched, because this is the one call
// that decides how many pull requests there are.
try {
const open = await github.paginate(github.rest.pulls.list, {
owner,
repo,
state: 'open',
base: 'develop',
per_page: 100,
});
numbers = open.map((item) => item.number);
} catch (error) {
const status = error?.status ?? 'unknown';
core.warning(
`Could not list open pull requests (${status}): ${error?.message}. ` +
'Nothing was updated; retry with workflow_dispatch.'
);
numbers = [];
}
}
core.info(`Considering ${numbers.length} pull request(s) against develop.`);
// Each pull request is handled independently by processPullRequest,
// which owns its own log lines and its own comment. Nothing is
// carried between iterations.
const summary = [];
const problems = [];
for (const number of numbers) {
// Belt and braces. processPullRequest handles every API rejection
// itself, but a throw here would stop every later pull request in
// the run, so the loop never depends on that remaining true.
let result;
try {
result = await processPullRequest({ github, owner, repo, number, logger: core });
} catch (error) {
const status = error?.status ?? 'unknown';
core.warning(`#${number}: unexpected failure (${status}): ${error?.message}`);
result = { outcome: 'error', status, message: error?.message || '' };
}
// A conflict whose comment could not be written is a problem worth
// counting, not a completed pull request.
const label = result.commentFailed
? `${result.outcome} (comment not written, HTTP ${result.commentStatus})`
: result.outcome;
summary.push(`#${number}: ${label}`);
if (result.commentFailed || ['error', 'retry', 'denied'].includes(result.outcome)) {
problems.push(`#${number}: ${label}`);
}
}
core.info(`Processed ${numbers.length} pull request(s).`);
if (problems.length) {
// Visible on the run page. The job still exits 0, because a
// repository or permission problem must not turn every pull
// request's check red, but it must not look like a clean pass
// either.
core.warning(
`${problems.length} of ${numbers.length} pull request(s) were not fully processed: ` +
`${problems.join('; ')}`
);
}
await core.summary
.addHeading('Keep pull request current')
.addTable([
[
{ data: 'Pull request', header: true },
{ data: 'Outcome', header: true },
],
...summary.map((line) => [line.split(':')[0], line.split(':').slice(1).join(':').trim()]),
])
.write();