Repository navigation
Expand file tree
/
Copy pathrelease.js
More file actions
324 lines (283 loc) · 9.2 KB
/
Copy pathrelease.js
File metadata and controls
324 lines (283 loc) · 9.2 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
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import { resolve } from 'node:path'
import { pathToFileURL } from 'node:url'
import {
dispatchWorkflow,
gitopsRepo,
readBlob,
readJson,
writeSummary
} from './github.js'
import { OutcomeError, pinPath } from './gitops.js'
const RELEASE_BRANCH = 'release-please--branches--main'
const RELEASE_TAG = /^(v\d+)\.\d+\.\d+$/
const COMMIT_SHA = /^[0-9a-f]{40}$/
const PASSED = new Set(['success', 'neutral', 'skipped'])
function releaseOf(env) {
const major = RELEASE_TAG.exec(env.AIP_TAG ?? '')?.[1]
if (!major || !COMMIT_SHA.test(env.AIP_SHA ?? '')) {
throw new OutcomeError('failed', 'a release needs a tag vX.Y.Z and a SHA')
}
return { tag: env.AIP_TAG, major, sha: env.AIP_SHA }
}
async function requiredChecks(github, { owner, repo }) {
const rules = await github.paginate(github.rest.repos.getBranchRules, {
owner,
repo,
branch: 'main',
per_page: 100
})
return rules
.filter((rule) => rule.type === 'required_status_checks')
.flatMap((rule) =>
rule.parameters.required_status_checks.map((check) => check.context)
)
}
// The release App opened the pull request and wrote every commit on it, so
// nobody else's change reaches main through the App's bypass. A ruleset lets
// only the App push to the release branch, which makes this unforgeable.
async function writtenByReleaseApp(github, { owner, repo }, pull, slug) {
const bot = `${slug}[bot]`
if (pull.user?.type !== 'Bot' || pull.user.login !== bot) {
return false
}
const commits = await github.paginate(github.rest.pulls.listCommits, {
owner,
repo,
pull_number: pull.number,
per_page: 100
})
return commits.every((commit) => commit.author?.login === bot)
}
/**
* merge-release: run when the pull request checks complete. The App merges
* the open release-please pull request, as the ruleset's bypass actor, once
* every check main requires has passed on the commit those checks ran on
* (direction 13 October 2026, 10B), and only when the release App wrote it.
* Nothing is merged when main requires no checks.
*
* Inputs: AIP_APP_SLUG, the release App.
*
* @param {object} input - Command context from main.js; `context.payload`
* is the workflow_run event.
* @returns {Promise<{outcome: string, waiting?: string[]}>} Result.
*/
async function mergeRelease({ github, context, core, env }) {
const { owner, repo } = context.repo
const run = context.payload.workflow_run
if (run?.conclusion !== 'success' || run.head_branch !== RELEASE_BRANCH) {
return { outcome: 'not-a-release' }
}
const { data: pulls } = await github.rest.pulls.list({
owner,
repo,
head: `${owner}:${run.head_branch}`,
state: 'open',
per_page: 1
})
const pull = pulls[0]
if (!pull || pull.head.sha !== run.head_sha) {
await writeSummary(core, 'not-merged', [
'The release pull request is closed or has moved on; the checks of its new head merge it.'
])
return { outcome: 'not-merged' }
}
if (
!(await writtenByReleaseApp(github, context.repo, pull, env.AIP_APP_SLUG))
) {
throw new OutcomeError(
'not-merged',
`${pull.html_url} was not written only by the release App`
)
}
const required = await requiredChecks(github, context.repo)
if (required.length === 0) {
throw new OutcomeError(
'not-merged',
'main requires no status checks, so a release is never merged unchecked'
)
}
const checkRuns = await github.paginate(github.rest.checks.listForRef, {
owner,
repo,
ref: run.head_sha,
per_page: 100
})
const waiting = required.filter(
(name) =>
!checkRuns.some(
(check) =>
check.name === name &&
check.status === 'completed' &&
PASSED.has(check.conclusion)
)
)
if (waiting.length > 0) {
await writeSummary(
core,
'waiting',
waiting.map((name) => `- ${name}`)
)
return { outcome: 'waiting', waiting }
}
await github.rest.pulls.merge({
owner,
repo,
pull_number: pull.number,
merge_method: 'squash',
sha: run.head_sha
})
await writeSummary(core, 'merged', [`Release pull request: ${pull.html_url}`])
return { outcome: 'merged', number: pull.number }
}
/**
* resolve-release: the release this run made. release-please reports a
* release only on the run that creates it, so a rerun after a later step
* failed finds the release whose tag points at this run's commit, and the
* idempotent steps after it (major tag, pins) run again. Exports AIP_TAG and
* AIP_SHA when there is a release.
*
* Inputs: AIP_CREATED, AIP_TAG and AIP_SHA from release-please;
* GITHUB_RUN_ATTEMPT.
*
* @param {object} input - Command context from main.js.
* @returns {Promise<{released: boolean, tag?: string, sha?: string}>}
*/
async function resolveRelease({ github, context, core, env }) {
let release = env.AIP_CREATED === 'true' ? releaseOf(env) : null
if (!release && env.GITHUB_RUN_ATTEMPT !== '1') {
const { data: releases } = await github.rest.repos.listReleases({
...context.repo,
per_page: 10
})
for (const candidate of releases) {
if (!RELEASE_TAG.test(candidate.tag_name)) {
continue
}
// release-please tags through the releases API: lightweight tags.
const { data: ref } = await github.rest.git.getRef({
...context.repo,
ref: `tags/${candidate.tag_name}`
})
if (ref.object.sha === context.sha) {
release = releaseOf({
AIP_TAG: candidate.tag_name,
AIP_SHA: context.sha
})
break
}
}
}
if (!release) {
return { released: false }
}
core.exportVariable('AIP_TAG', release.tag)
core.exportVariable('AIP_SHA', release.sha)
return { released: true, tag: release.tag, sha: release.sha }
}
/**
* move-major-tag: points the major tag (v1 for v1.4.2) at the release
* commit through the Git refs API, which needs only contents write.
*
* Inputs: AIP_TAG, AIP_SHA.
*
* @param {object} input - Command context from main.js.
* @returns {Promise<{major: string, sha: string}>} The tag moved.
*/
async function moveMajorTag({ github, context, core, env }) {
const { major, sha, tag } = releaseOf(env)
try {
await github.rest.git.updateRef({
...context.repo,
ref: `tags/${major}`,
sha,
force: true
})
} catch (error) {
if (error.status !== 422) {
throw error
}
await github.rest.git.createRef({
...context.repo,
ref: `refs/tags/${major}`,
sha
})
}
await writeSummary(core, `Moved ${major} to ${tag}`, [`Commit \`${sha}\`.`])
return { major, sha }
}
/**
* submit-auto-pins: after an ai-platform-infra release, submits a pin of
* the new release for every registry entry with autoPin, through the
* deployments repository's submit workflow (D9). The pin is serialised by
* the released checks' canonical-json.js, the bytes submit validates.
*
* Inputs: AIP_TAG, AIP_SHA, AIP_APP_SLUG (the pins are requested by
* app:{slug}), AIP_CANONICAL_MODULE (path of canonical-json.js in the
* release checkout).
*
* @param {object} input - Command context from main.js; `github` reads
* ai-platform-environments and ai-platform-deployments and dispatches in
* the latter.
* @returns {Promise<{submitted: string[]}>} The environments pinned.
*/
async function submitAutoPins({ github, context, core, env }) {
const { tag, sha } = releaseOf(env)
const { owner } = context.repo
const requestedBy = `app:${env.AIP_APP_SLUG}`
const { canonicalJson } = await import(
pathToFileURL(resolve(env.AIP_CANONICAL_MODULE)).href
)
const registry = await readJson(github, {
...gitopsRepo(owner, 'environments'),
path: 'registry.json',
ref: 'main'
})
const deployments = gitopsRepo(owner, 'deployments')
const submitted = []
const lines = []
for (const entry of registry.environments.filter((e) => e.autoPin)) {
const path = pinPath(entry.name)
const current = await readBlob(github, {
...deployments,
path,
ref: 'main'
})
const previous = current ? JSON.parse(current.text) : null
// One operation per environment.
const operationId = `rel-${entry.name}-${tag.replaceAll('.', '-')}-${context.runId}`
const pin = {
schemaVersion: 1,
environment: entry.name,
release: tag,
sha,
previousRelease: previous?.release ?? null,
requestedBy,
operationId,
requestedAt: new Date().toISOString()
}
const url = await dispatchWorkflow(github, {
...deployments,
workflow: 'submit.yml',
inputs: {
action: 'write',
environment: entry.name,
path,
document: canonicalJson(pin),
operation_id: operationId,
requested_by: requestedBy,
// The pin file as main has it now, which submit commits on or refuses
// as stale-revision (AC20); 0 for an environment's first pin.
expected_base: current?.sha ?? '0'
}
})
submitted.push(entry.name)
lines.push(`- \`${entry.name}\` (\`${operationId}\`): ${url ?? 'started'}`)
}
await writeSummary(
core,
`Pins of ${tag} submitted`,
lines.length > 0 ? lines : ['No environment has autoPin.']
)
return { submitted }
}
export { mergeRelease, moveMajorTag, resolveRelease, submitAutoPins }