@mnaimfaizy/agent-kit 1.0.0-beta.15 → 1.0.0-beta.17
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +2 -1
- package/package.json +1 -1
- package/src/cli.mjs +332 -5
- package/src/deep-scan/docker-driver.mjs +70 -3
- package/src/deep-scan/lifecycle.mjs +2 -2
- package/src/deep-scan/sbx-driver.mjs +34 -47
- package/src/doctor.mjs +53 -1
- package/src/exec.mjs +18 -5
- package/src/factory/budget/budget.mjs +260 -0
- package/src/factory/build/command.mjs +321 -0
- package/src/factory/build/factory.mjs +473 -0
- package/src/factory/build/findings.mjs +55 -0
- package/src/factory/build/host.mjs +230 -0
- package/src/factory/build/notes.mjs +191 -0
- package/src/factory/build/pull-request.mjs +74 -0
- package/src/factory/bundle.mjs +146 -0
- package/src/factory/checks/allowed-paths.mjs +50 -0
- package/src/factory/checks/plan-header.mjs +296 -0
- package/src/factory/checks/qa.mjs +85 -0
- package/src/factory/checks/review.mjs +73 -0
- package/src/factory/checks/security.mjs +50 -0
- package/src/factory/checks/stage-check.mjs +89 -0
- package/src/factory/claims/command.mjs +187 -0
- package/src/factory/claims/release.mjs +111 -0
- package/src/factory/decision/answers.mjs +103 -0
- package/src/factory/decision/command.mjs +64 -0
- package/src/factory/decision/plan.mjs +99 -0
- package/src/factory/decision/request.mjs +116 -0
- package/src/factory/doctor/files.mjs +349 -0
- package/src/factory/doctor/index.mjs +56 -0
- package/src/factory/doctor/local.mjs +513 -0
- package/src/factory/doctor/orchestrate.mjs +146 -0
- package/src/factory/engine/lease.mjs +146 -0
- package/src/factory/engine/local-engine.mjs +177 -0
- package/src/factory/engine/lockfile.mjs +64 -0
- package/src/factory/engine/workflow-engine.mjs +124 -0
- package/src/factory/gate/comment.mjs +88 -0
- package/src/factory/gate/config.mjs +57 -0
- package/src/factory/gate/gate-core.mjs +250 -0
- package/src/factory/gate/guarded.mjs +17 -0
- package/src/factory/gate/reads.mjs +105 -0
- package/src/factory/gate/task.mjs +193 -0
- package/src/factory/gate/trip-reads.mjs +82 -0
- package/src/factory/gate/trip.mjs +87 -0
- package/src/factory/init/blueprint.mjs +181 -0
- package/src/factory/init/index.mjs +338 -0
- package/src/factory/labels/evidence.mjs +162 -0
- package/src/factory/labels/moves.mjs +32 -0
- package/src/factory/labels/reconcile.mjs +267 -0
- package/src/factory/labels/revert.mjs +87 -0
- package/src/factory/labels/timeline.mjs +72 -0
- package/src/factory/labels/voided.mjs +59 -0
- package/src/factory/limits/harness.mjs +39 -0
- package/src/factory/limits/limits.mjs +131 -0
- package/src/factory/merge/command.mjs +172 -0
- package/src/factory/outbox/check.mjs +240 -0
- package/src/factory/outbox/rulings.mjs +195 -0
- package/src/factory/plan/command.mjs +66 -0
- package/src/factory/plan/comment.mjs +156 -0
- package/src/factory/plan/publish.mjs +149 -0
- package/src/factory/plan/risk.mjs +153 -0
- package/src/factory/qa/command.mjs +301 -0
- package/src/factory/qa/decide.mjs +86 -0
- package/src/factory/qa/report.mjs +50 -0
- package/src/factory/records/engine-record.mjs +133 -0
- package/src/factory/records/main.mjs +96 -0
- package/src/factory/records/record.mjs +226 -0
- package/src/factory/records/sink.mjs +57 -0
- package/src/factory/report/build.mjs +92 -0
- package/src/factory/report/command.mjs +80 -0
- package/src/factory/report/facts.mjs +226 -0
- package/src/factory/report/gate-section.mjs +122 -0
- package/src/factory/report/metrics.mjs +111 -0
- package/src/factory/report/reads.mjs +246 -0
- package/src/factory/report/render.mjs +121 -0
- package/src/factory/report/rollout.mjs +154 -0
- package/src/factory/review/brief.mjs +195 -0
- package/src/factory/review/command.mjs +182 -0
- package/src/factory/run/awake.mjs +70 -0
- package/src/factory/run/collect.mjs +77 -0
- package/src/factory/run/command.mjs +556 -0
- package/src/factory/run/gh.mjs +46 -0
- package/src/factory/run/launch.mjs +66 -0
- package/src/factory/run/pipelines.mjs +694 -0
- package/src/factory/run/raw.mjs +96 -0
- package/src/factory/run/restart.mjs +83 -0
- package/src/factory/run/s3.mjs +193 -0
- package/src/factory/run/secrets.mjs +142 -0
- package/src/factory/run/teardown.mjs +242 -0
- package/src/factory/run/token.mjs +70 -0
- package/src/factory/run/worktree.mjs +160 -0
- package/src/factory/security/audit.mjs +353 -0
- package/src/factory/security/command.mjs +157 -0
- package/src/factory/setup/app.mjs +122 -0
- package/src/factory/setup/command.mjs +472 -0
- package/src/factory/setup/labels.mjs +25 -0
- package/src/factory/setup/reads.mjs +159 -0
- package/src/factory/setup/writes.mjs +138 -0
- package/src/factory/snapshot/write.mjs +176 -0
- package/src/factory/stop/command.mjs +136 -0
- package/src/factory/sweep/actions-main.mjs +119 -0
- package/src/factory/sweep/command.mjs +65 -0
- package/src/factory/sweep/knobs.mjs +78 -0
- package/src/factory/sweep/marker-version.mjs +15 -0
- package/src/factory/sweep/markers.mjs +112 -0
- package/src/factory/sweep/overlap.mjs +24 -0
- package/src/factory/sweep/run.mjs +198 -0
- package/src/factory/sweep/shell.mjs +398 -0
- package/src/factory/sweep/split.mjs +104 -0
- package/src/factory/sweep/states.mjs +87 -0
- package/src/factory/sweep/sweep-core.mjs +2009 -0
- package/src/factory/tracker/github.mjs +713 -0
- package/src/factory/tracker/operations.mjs +153 -0
- package/src/factory/upgrade/index.mjs +83 -0
- package/src/init.mjs +62 -24
- package/src/kit-owned-paths.mjs +1 -1
- package/src/stage/build.mjs +100 -0
- package/src/stage/factory-live.mjs +288 -0
- package/src/stage.mjs +100 -4
- package/src/upgrade.mjs +39 -7
|
@@ -0,0 +1,321 @@
|
|
|
1
|
+
// The build Stage's trusted steps, as commands of the Kit-owned bundle
|
|
2
|
+
// (`node .github/agent-factory/sweep.mjs <command>`), run by
|
|
3
|
+
// agent-implement-reusable.yml when its `factory` input is on. Each is the
|
|
4
|
+
// argument parsing and the tracker around pure code, with the process boundary
|
|
5
|
+
// (`gh`, `git`, files) handed in, so a test drives them over fakes
|
|
6
|
+
// (scripts/factory-build-command.test.mjs).
|
|
7
|
+
//
|
|
8
|
+
// build-plan --issue <n> --plan-out <file> --paths-out <file>
|
|
9
|
+
// writes the latest plan comment and its Allowed paths for the
|
|
10
|
+
// builder's brief. No valid plan: exit 1.
|
|
11
|
+
// build-check --issue <n> --claim <nonce> --attempt <n> --allowed-paths <file>
|
|
12
|
+
// --bundle <file> --result <file> --base-sha <sha>
|
|
13
|
+
// runs in the publish job after the builder, before any token
|
|
14
|
+
// exists or anything is pushed. It reads the bundle as git
|
|
15
|
+
// objects (never a checkout), refuses a diff outside the plan's
|
|
16
|
+
// Allowed paths as one failed Attempt, and turns a wider-paths
|
|
17
|
+
// result into a Decision Request. It reads and writes no GitHub
|
|
18
|
+
// state: it prints `ending=ok|wider-paths|failed` and `note=`
|
|
19
|
+
// (the comment for the report job to post, base64) to
|
|
20
|
+
// GITHUB_OUTPUT. Exit 0 for ok and wider-paths, 1 for failed.
|
|
21
|
+
// build-pr --issue <n> --base-branch <b> --branch <name> --sha <sha> --result <file>
|
|
22
|
+
// runs in the publish job, after the push, with the App's token as
|
|
23
|
+
// GH_TOKEN: opens the pull request that closes the issue (the one
|
|
24
|
+
// call whose token starts CI). Prints `number=` and `url=`.
|
|
25
|
+
// Exit 2 is a usage error. Nothing the agent wrote reaches a log. Only build-pr
|
|
26
|
+
// is run with the App's token, and it writes no comment: the notes are posted
|
|
27
|
+
// by the report job, with the job's `GITHUB_TOKEN`.
|
|
28
|
+
|
|
29
|
+
import { earlierFindings } from "./findings.mjs";
|
|
30
|
+
import { createHash } from "node:crypto";
|
|
31
|
+
|
|
32
|
+
import { checkAllowedPaths } from "../checks/allowed-paths.mjs";
|
|
33
|
+
import { checkPlanHeader } from "../checks/plan-header.mjs";
|
|
34
|
+
import { WORKFLOW_IDENTITY } from "../engine/workflow-engine.mjs";
|
|
35
|
+
import { latestPlan } from "../plan/comment.mjs";
|
|
36
|
+
import { createGithubTracker } from "../tracker/github.mjs";
|
|
37
|
+
import { factoryPullRequestText } from "./pull-request.mjs";
|
|
38
|
+
import { parseWiderPathsRequest, renderBuildFailure, renderWiderPathsRequest } from "./notes.mjs";
|
|
39
|
+
|
|
40
|
+
const BUILD_REPO_SLUG = /^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/;
|
|
41
|
+
const BUILD_FULL_SHA = /^[0-9a-f]{40}$/;
|
|
42
|
+
/** The most commits the check reads; the publish step holds the bundle to fewer. */
|
|
43
|
+
const MAX_COMMITS = 200;
|
|
44
|
+
const MAX_LISTED_PATHS = 15;
|
|
45
|
+
|
|
46
|
+
const bareLogin = (login) =>
|
|
47
|
+
String(login ?? "")
|
|
48
|
+
.replace(/\[bot\]$/, "")
|
|
49
|
+
.toLowerCase();
|
|
50
|
+
|
|
51
|
+
/** Reads one `--name value` option. */
|
|
52
|
+
const optionReader = (args) => (name) => {
|
|
53
|
+
const at = args.indexOf(`--${name}`);
|
|
54
|
+
return at === -1 ? undefined : args[at + 1];
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
function stepContext({ gh, env, args, tracker, err, need }) {
|
|
58
|
+
const option = optionReader(args);
|
|
59
|
+
const repo = env.GITHUB_REPOSITORY ?? "";
|
|
60
|
+
if (!BUILD_REPO_SLUG.test(repo)) return { usage: "GITHUB_REPOSITORY is not set" };
|
|
61
|
+
const issue = Number(option("issue"));
|
|
62
|
+
if (!Number.isInteger(issue) || issue < 1) return { usage: "--issue is not an issue number" };
|
|
63
|
+
for (const name of need) {
|
|
64
|
+
if (!option(name)) return { usage: `--${name} is missing` };
|
|
65
|
+
}
|
|
66
|
+
const identity = env.AGENT_KIT_FACTORY_IDENTITY ?? WORKFLOW_IDENTITY;
|
|
67
|
+
return {
|
|
68
|
+
option,
|
|
69
|
+
issue,
|
|
70
|
+
tracker: tracker ?? createGithubTracker({ gh, repo }),
|
|
71
|
+
isEngine: (login) => bareLogin(login) === bareLogin(identity),
|
|
72
|
+
err,
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** The latest valid plan on the issue: its comment body and its checked header, or null. */
|
|
77
|
+
function readPlan(tracker, issue, isEngine) {
|
|
78
|
+
const comments = tracker.readComments(issue);
|
|
79
|
+
const plan = latestPlan(comments, isEngine);
|
|
80
|
+
if (!plan) return { comments, plan: null };
|
|
81
|
+
const checked = checkPlanHeader(plan.data, { trusted: true });
|
|
82
|
+
if (!checked.ok) return { comments, plan: null };
|
|
83
|
+
const body = comments.find((c) => String(c.id) === String(plan.commentId))?.body ?? "";
|
|
84
|
+
return { comments, plan: { n: plan.n, body, header: checked.header } };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function runBuildPlan({ gh, env, args, writeFile, out, err, tracker }) {
|
|
88
|
+
const c = stepContext({ gh, env, args, tracker, err, need: ["plan-out", "paths-out"] });
|
|
89
|
+
if (c.usage) {
|
|
90
|
+
err(`build-plan: ${c.usage}`);
|
|
91
|
+
return 2;
|
|
92
|
+
}
|
|
93
|
+
try {
|
|
94
|
+
const { plan, comments } = readPlan(c.tracker, c.issue, c.isEngine);
|
|
95
|
+
if (!plan) {
|
|
96
|
+
err("::error title=No verified plan::The issue has no valid plan comment of the engine's.");
|
|
97
|
+
return 1;
|
|
98
|
+
}
|
|
99
|
+
// The earlier Attempts' findings follow the plan, so the next builder reads them.
|
|
100
|
+
const findings = earlierFindings(comments, c.isEngine);
|
|
101
|
+
writeFile(c.option("plan-out"), `${plan.body}\n${findings ? `\n${findings}` : ""}`);
|
|
102
|
+
writeFile(c.option("paths-out"), `${plan.header.allowed_paths.join("\n")}\n`);
|
|
103
|
+
out(`Plan ${plan.n}: ${plan.header.allowed_paths.length} Allowed path(s).`);
|
|
104
|
+
return 0;
|
|
105
|
+
} catch (error) {
|
|
106
|
+
err(`::error title=Plan not read::${String(error.message).replace(/\s+/g, " ")}`);
|
|
107
|
+
return 1;
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/**
|
|
112
|
+
* The paths a bundle changes on top of `baseSha`, read as git objects alone:
|
|
113
|
+
* every commit's paths, renames off, so a rename lists both ends.
|
|
114
|
+
* @returns {{ paths: string[], commits: number }}
|
|
115
|
+
*/
|
|
116
|
+
export function bundleChangedPaths({ git, bundle, baseSha }) {
|
|
117
|
+
const run = (args) => {
|
|
118
|
+
const result = git(args);
|
|
119
|
+
if (result.status !== 0) throw new Error("git refused the bundle");
|
|
120
|
+
return result.stdout;
|
|
121
|
+
};
|
|
122
|
+
run(["cat-file", "-e", `${baseSha}^{commit}`]);
|
|
123
|
+
// fsck on the way in, with the tree-entry checks that default to warnings
|
|
124
|
+
// raised to errors, as the publish step's own fetch does.
|
|
125
|
+
run([
|
|
126
|
+
"-c",
|
|
127
|
+
"fetch.fsckObjects=true",
|
|
128
|
+
"-c",
|
|
129
|
+
"transfer.fsckObjects=true",
|
|
130
|
+
...["hasDotgit", "hasDot", "hasDotdot", "zeroPaddedFilemode"].flatMap((id) => [
|
|
131
|
+
"-c",
|
|
132
|
+
`fetch.fsck.${id}=error`,
|
|
133
|
+
]),
|
|
134
|
+
"fetch",
|
|
135
|
+
"--quiet",
|
|
136
|
+
"--no-tags",
|
|
137
|
+
"--no-recurse-submodules",
|
|
138
|
+
"--no-auto-maintenance",
|
|
139
|
+
"--no-write-fetch-head",
|
|
140
|
+
bundle,
|
|
141
|
+
"+HEAD:refs/agent-factory/build",
|
|
142
|
+
]);
|
|
143
|
+
const tip = run(["rev-parse", "--verify", "refs/agent-factory/build^{commit}"]).trim();
|
|
144
|
+
if (tip === baseSha) throw new Error("the bundle holds no new commit");
|
|
145
|
+
const commits = run(["rev-list", "--reverse", `${baseSha}..${tip}`])
|
|
146
|
+
.split("\n")
|
|
147
|
+
.filter(Boolean);
|
|
148
|
+
if (commits.length === 0 || commits.length > MAX_COMMITS) {
|
|
149
|
+
throw new Error(`the bundle holds ${commits.length} commits`);
|
|
150
|
+
}
|
|
151
|
+
const seen = new Set();
|
|
152
|
+
for (const sha of commits) {
|
|
153
|
+
const names = run([
|
|
154
|
+
"diff-tree",
|
|
155
|
+
"-r",
|
|
156
|
+
"-z",
|
|
157
|
+
"--no-renames",
|
|
158
|
+
"--no-commit-id",
|
|
159
|
+
"--root",
|
|
160
|
+
"--name-only",
|
|
161
|
+
sha,
|
|
162
|
+
]);
|
|
163
|
+
for (const path of names.split("\0")) if (path !== "") seen.add(path);
|
|
164
|
+
}
|
|
165
|
+
return { paths: [...seen], commits: commits.length };
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export function runBuildCheck({ git, env, args, readFile, appendOutput, out, err }) {
|
|
169
|
+
const option = optionReader(args);
|
|
170
|
+
const issue = option("issue") ?? "";
|
|
171
|
+
const claim = option("claim") ?? "";
|
|
172
|
+
const attempt = Number(option("attempt"));
|
|
173
|
+
const baseSha = option("base-sha") ?? "";
|
|
174
|
+
for (const name of ["allowed-paths", "bundle", "result"]) {
|
|
175
|
+
if (!option(name)) {
|
|
176
|
+
err(`build-check: --${name} is missing`);
|
|
177
|
+
return 2;
|
|
178
|
+
}
|
|
179
|
+
}
|
|
180
|
+
if (!/^[1-9][0-9]*$/.test(issue) || !/^[A-Za-z0-9._:+-]+$/.test(claim)) {
|
|
181
|
+
err("build-check: --issue or --claim is not valid");
|
|
182
|
+
return 2;
|
|
183
|
+
}
|
|
184
|
+
if (!Number.isInteger(attempt) || attempt < 1 || !BUILD_FULL_SHA.test(baseSha)) {
|
|
185
|
+
err("build-check: --attempt or --base-sha is not valid");
|
|
186
|
+
return 2;
|
|
187
|
+
}
|
|
188
|
+
// The ending, and the comment the report job posts for it (base64, one line).
|
|
189
|
+
const end = (ending, note) => {
|
|
190
|
+
appendOutput(`ending=${ending}`);
|
|
191
|
+
if (note) appendOutput(`note=${Buffer.from(note, "utf8").toString("base64")}`);
|
|
192
|
+
};
|
|
193
|
+
const fail = (reasons) => {
|
|
194
|
+
err(`::error title=Build refused::${reasons[0]}`);
|
|
195
|
+
end("failed", renderBuildFailure({ claim, attempt, reasons }));
|
|
196
|
+
return 1;
|
|
197
|
+
};
|
|
198
|
+
try {
|
|
199
|
+
const rawPaths = readFile(option("allowed-paths"));
|
|
200
|
+
const allowed = rawPaths
|
|
201
|
+
? Buffer.from(rawPaths)
|
|
202
|
+
.toString("utf8")
|
|
203
|
+
.split("\n")
|
|
204
|
+
.map((line) => line.trim())
|
|
205
|
+
.filter(Boolean)
|
|
206
|
+
: [];
|
|
207
|
+
if (allowed.length === 0) return fail(["the plan's Allowed paths are missing"]);
|
|
208
|
+
|
|
209
|
+
const rawResult = readFile(option("result"));
|
|
210
|
+
let result;
|
|
211
|
+
try {
|
|
212
|
+
result = rawResult ? JSON.parse(Buffer.from(rawResult).toString("utf8")) : null;
|
|
213
|
+
} catch {
|
|
214
|
+
result = null;
|
|
215
|
+
}
|
|
216
|
+
if (!result || typeof result.title !== "string" || typeof result.body !== "string") {
|
|
217
|
+
return fail(["the implementer's result is missing or not a title and a body"]);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const request = parseWiderPathsRequest(result);
|
|
221
|
+
if (request?.error) return fail([request.error]);
|
|
222
|
+
if (request) {
|
|
223
|
+
out(`The builder asked for ${request.paths.length} wider path(s): nothing is pushed.`);
|
|
224
|
+
end(
|
|
225
|
+
"wider-paths",
|
|
226
|
+
renderWiderPathsRequest({ claim, attempt, currentPaths: allowed, request }),
|
|
227
|
+
);
|
|
228
|
+
return 0;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
if (!readFile(option("bundle"))) return fail(["the implementer left no bundle"]);
|
|
232
|
+
let changed;
|
|
233
|
+
try {
|
|
234
|
+
changed = bundleChangedPaths({ git, bundle: option("bundle"), baseSha });
|
|
235
|
+
} catch (error) {
|
|
236
|
+
// git's own errors can quote the bundle; the kit's reasons do not.
|
|
237
|
+
const own = /^the bundle holds|no new commit/.test(error.message);
|
|
238
|
+
return fail([own ? error.message : "the bundle could not be read as git objects"]);
|
|
239
|
+
}
|
|
240
|
+
if (changed.paths.length === 0) {
|
|
241
|
+
return fail(["the build changed no file, so there is nothing to open a pull request for"]);
|
|
242
|
+
}
|
|
243
|
+
const verdict = checkAllowedPaths({ allowedPaths: allowed, changedPaths: changed.paths });
|
|
244
|
+
if (!verdict.ok) {
|
|
245
|
+
const shown = verdict.outside.slice(0, MAX_LISTED_PATHS);
|
|
246
|
+
const more = verdict.outside.length - shown.length;
|
|
247
|
+
return fail([
|
|
248
|
+
`the diff changes ${verdict.outside.length} path(s) outside the plan's Allowed paths:`,
|
|
249
|
+
...shown,
|
|
250
|
+
...(more > 0 ? [`and ${more} more`] : []),
|
|
251
|
+
]);
|
|
252
|
+
}
|
|
253
|
+
out(`The diff of ${changed.commits} commit(s) stays inside the plan's Allowed paths.`);
|
|
254
|
+
end("ok");
|
|
255
|
+
return 0;
|
|
256
|
+
} catch (error) {
|
|
257
|
+
err(`::error title=Build check failed::${String(error.message).replace(/\s+/g, " ")}`);
|
|
258
|
+
end(
|
|
259
|
+
"failed",
|
|
260
|
+
renderBuildFailure({ claim, attempt, reasons: ["the build could not be checked"] }),
|
|
261
|
+
);
|
|
262
|
+
return 1;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Opens the build's pull request, or finds the one already open for the branch.
|
|
268
|
+
* `gh` carries the App's token in its environment (the step's GH_TOKEN).
|
|
269
|
+
*/
|
|
270
|
+
export function runBuildPr({ gh, env, args, readFile, appendOutput, out, err }) {
|
|
271
|
+
const option = optionReader(args);
|
|
272
|
+
const repo = env.GITHUB_REPOSITORY ?? "";
|
|
273
|
+
const issue = option("issue") ?? "";
|
|
274
|
+
const branch = option("branch") ?? "";
|
|
275
|
+
const base = option("base-branch") ?? "";
|
|
276
|
+
const sha = option("sha") ?? "";
|
|
277
|
+
if (!BUILD_REPO_SLUG.test(repo) || !/^[1-9][0-9]*$/.test(issue) || !branch || !base) {
|
|
278
|
+
err("build-pr: needs GITHUB_REPOSITORY, --issue, --branch and --base-branch");
|
|
279
|
+
return 2;
|
|
280
|
+
}
|
|
281
|
+
if (!BUILD_FULL_SHA.test(sha)) {
|
|
282
|
+
err("build-pr: --sha is not a full sha");
|
|
283
|
+
return 2;
|
|
284
|
+
}
|
|
285
|
+
const json = (callArgs, input) => {
|
|
286
|
+
const result = gh(callArgs, input);
|
|
287
|
+
if (result.status !== 0) {
|
|
288
|
+
throw new Error(`gh ${callArgs.slice(0, 3).join(" ")} failed`);
|
|
289
|
+
}
|
|
290
|
+
return JSON.parse(result.stdout || "null");
|
|
291
|
+
};
|
|
292
|
+
try {
|
|
293
|
+
const raw = readFile(option("result") ?? "");
|
|
294
|
+
const result = raw ? JSON.parse(Buffer.from(raw).toString("utf8")) : null;
|
|
295
|
+
if (!result || typeof result.title !== "string" || typeof result.body !== "string") {
|
|
296
|
+
throw new Error("the implementer's result is not a title and a body");
|
|
297
|
+
}
|
|
298
|
+
const encoded = branch.split("/").map(encodeURIComponent).join("/");
|
|
299
|
+
const ref = json(["api", `repos/${repo}/git/ref/heads/${encoded}`]);
|
|
300
|
+
if (ref?.object?.sha !== sha) throw new Error("the branch moved after it was pushed");
|
|
301
|
+
const head = encodeURIComponent(`${repo.split("/")[0]}:${branch}`);
|
|
302
|
+
const [existing] = json(["api", `repos/${repo}/pulls?state=open&head=${head}`]) ?? [];
|
|
303
|
+
let pull = existing;
|
|
304
|
+
if (!pull) {
|
|
305
|
+
const text = factoryPullRequestText({ issue: Number(issue), result });
|
|
306
|
+
pull = json(
|
|
307
|
+
["api", `repos/${repo}/pulls`, "--input", "-"],
|
|
308
|
+
JSON.stringify({ ...text, head: branch, base, draft: false }),
|
|
309
|
+
);
|
|
310
|
+
}
|
|
311
|
+
out(`The pull request: ${pull.html_url}`);
|
|
312
|
+
appendOutput(`number=${pull.number}`);
|
|
313
|
+
appendOutput(`url=${pull.html_url}`);
|
|
314
|
+
return 0;
|
|
315
|
+
} catch (error) {
|
|
316
|
+
err(
|
|
317
|
+
`::error title=Pull request not opened::${String(error.message).replace(/\s+/g, " ")}. The branch is pushed; the Dispatcher counts this Attempt.`,
|
|
318
|
+
);
|
|
319
|
+
return 1;
|
|
320
|
+
}
|
|
321
|
+
}
|