Repository navigation
Expand file tree
/
Copy pathbump.ts
More file actions
156 lines (150 loc) · 6.03 KB
/
Copy pathbump.ts
File metadata and controls
156 lines (150 loc) · 6.03 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
import { appendFileSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
import path from "node:path";
import { parseArgs } from "node:util";
import { findApp, listApps, selectApps } from "./lib/apps.ts";
import {
applyBump,
type Bump,
gateBump,
outstandingBumps,
planBumps,
readPin,
renderBumpBody,
shortSha,
} from "./lib/bump.ts";
import { createGhBumpHistory } from "./lib/bump-history.ts";
import { catalogRepo, info, runMain, warn } from "./lib/cli.ts";
import { parseJsonc } from "./lib/jsonc.ts";
import { appsDir } from "./lib/paths.ts";
import { createGhUpstream } from "./lib/upstream.ts";
const USAGE = `Usage:
pnpm -s bump plan [--only <slug,...>] --out <dir>
pnpm -s bump apply <slug> --ref <ref> --sha <sha>
plan Resolves each app's upstream with read-only gh api calls (newest stable
semver tag; for a pin on a tag with a prefix such as app-v0.42.1, the
newest stable tag with that prefix that contains the pinned commit; for
a branch pin without a newer tag that contains it, the pinned branch's
head, or the default branch's when upstream no longer has the pinned
branch) and keeps only moves forward.
Skips targets already proposed, and branch-tracked apps with a bump pull
request opened in the last week. Prints { bumps, failed, outstanding }
as JSON (each bump with the open pull requests it supersedes, and
autoMerge: true for an artifact tier entry that sets neither
bump.autoMerge: false nor source.version, unless the bump moves
source.ref to another branch) and writes the pull request
body for each bump to <dir>/<slug>.md. "outstanding" lists existing
bumps that need a person: a bump branch without a pull request, and an
open bump pull request whose head still lacks a required check after a
few hours.
apply Sets source.ref and source.sha in apps/<slug>/appflare.jsonc, keeping
its comments and layout.
`;
runMain(() => {
const { values, positionals } = parseArgs({
allowPositionals: true,
options: {
only: { type: "string" },
out: { type: "string" },
ref: { type: "string" },
sha: { type: "string" },
help: { type: "boolean", short: "h" },
},
});
const [command, slug] = positionals;
if (values.help || !command) {
process.stdout.write(USAGE);
return values.help ? 0 : 1;
}
if (command === "plan") {
if (!values.out) {
throw new Error("plan needs --out <dir>");
}
const upstream = createGhUpstream();
const history = createGhBumpHistory(catalogRepo());
const apps = selectApps(listApps(appsDir), values.only);
const plan = planBumps(apps, upstream, (slug) => history.lastBumpTag(slug));
const now = new Date();
mkdirSync(values.out, { recursive: true });
const bumps: (Bump & { supersedes: number[] })[] = [];
for (const bump of plan.bumps) {
const gate = gateBump(bump, history.existing(bump.slug), now);
if (gate.action === "skip") {
plan.skipped.push({ slug: bump.slug, reason: gate.reason });
continue;
}
let changes: { total: number; subjects: string[] } | null = null;
try {
changes = upstream.compare(bump.repo, bump.from.sha, bump.to.sha);
} catch (err) {
warn(`${bump.slug}: ${err instanceof Error ? err.message : String(err)}`);
}
const pin = readPin(findApp(appsDir, bump.slug));
writeFileSync(path.join(values.out, `${bump.slug}.md`), renderBumpBody(pin, bump, changes));
bumps.push({ ...bump, supersedes: gate.supersedes });
}
const superseded = new Set(bumps.flatMap((b) => b.supersedes));
const outstanding: { slug: string; error: string }[] = [];
for (const app of apps) {
try {
for (const error of outstandingBumps(
history.existing(app.slug),
(sha) => history.reportedChecks(sha),
now,
superseded,
)) {
outstanding.push({ slug: app.slug, error });
}
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
outstanding.push({ slug: app.slug, error: `checking its bump pull requests: ${message}` });
}
}
const summary = [
...bumps.map(
(b) =>
`- ${b.slug}: bump ${b.from.ref}@${shortSha(b.from.sha)} to ${b.to.ref}@${shortSha(b.to.sha)}${b.note ? ` (${b.note})` : ""}${b.refChange ? ` (${b.refChange}; not merging itself)` : ""}`,
),
...plan.skipped.map((s) => `- ${s.slug}: skipped, ${s.reason}`),
...plan.failed.map((f) => `- ${f.slug}: **failed**, ${f.error}`),
...outstanding.map((o) => `- ${o.slug}: **needs a maintainer**, ${o.error}`),
];
for (const line of summary) {
info(line.slice(2));
}
if (summary.length === 0) {
info("no apps");
}
if (process.env.GITHUB_STEP_SUMMARY) {
appendFileSync(
process.env.GITHUB_STEP_SUMMARY,
`### Bump check\n\n${summary.join("\n")}\n\n`,
);
}
for (const f of plan.failed) {
warn(`${f.slug}: could not read upstream: ${f.error}`);
}
for (const o of outstanding) {
warn(`${o.slug}: ${o.error}`);
}
process.stdout.write(`${JSON.stringify({ bumps, failed: plan.failed, outstanding })}\n`);
return 0;
}
if (command === "apply") {
if (!slug || !values.ref || !values.sha || !/^[0-9a-f]{40}$/.test(values.sha)) {
throw new Error("apply needs <slug>, --ref, and a 40-character --sha");
}
const app = findApp(appsDir, slug);
const updated = applyBump(readFileSync(app.manifestPath, "utf8"), {
ref: values.ref,
sha: values.sha,
});
const parsed = parseJsonc(updated) as { source?: { ref?: string; sha?: string } };
if (parsed.source?.ref !== values.ref || parsed.source?.sha !== values.sha) {
throw new Error(`${app.manifestPath}: the edit did not produce the expected source`);
}
writeFileSync(app.manifestPath, updated);
info(`${slug}: source is now ${values.ref}@${values.sha}`);
return 0;
}
throw new Error(`unknown command "${command}"\n\n${USAGE}`);
});