crysk 0.0.0 → 0.3.0
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/LICENSE +176 -0
- package/README.md +609 -3
- package/bin/pr-hero.js +166 -0
- package/config/models/anthropic.json +5 -0
- package/dist/SKILL-7e3725zn.md +496 -0
- package/dist/SKILL-7q35wcaz.md +66 -0
- package/dist/SKILL-9at22381.md +74 -0
- package/dist/SKILL-ex2hxxh3.md +134 -0
- package/dist/adjudicator-dtg2axnh.md +86 -0
- package/dist/admission-config.example-a9vqmmad.json +9 -0
- package/dist/ci-admission-camfbx2b.md +122 -0
- package/dist/cli.js +42292 -0
- package/dist/consolidate-ac0mnfyj.md +24 -0
- package/dist/deep-review-lifecycle-m698ybjp.md +362 -0
- package/dist/deep-review-logic-a6b1gczf.md +156 -0
- package/dist/deep-review-parity-0ec904db.md +120 -0
- package/dist/deep-review-reliability-qyakbjb9.md +125 -0
- package/dist/deep-review-resilience-3tz8y26e.md +118 -0
- package/dist/index.js +20190 -0
- package/dist/opencode-ci-7b0be64p.md +141 -0
- package/dist/review-refuter-j4e9gezy.md +71 -0
- package/dist/scout-yvp153q7.md +140 -0
- package/dist/summarizer-zca44nxa.md +18 -0
- package/dist/workflow-w1g9szna.yml +151 -0
- package/package.json +81 -4
- package/prompts/consolidate.md +24 -0
- package/prompts/default/PROVENANCE.md +18 -0
- package/prompts/default/deep-review-lifecycle.md +362 -0
- package/prompts/default/deep-review-logic.md +156 -0
- package/prompts/default/deep-review-parity.md +120 -0
- package/prompts/default/deep-review-reliability.md +125 -0
- package/prompts/default/deep-review-resilience.md +118 -0
- package/prompts/default/review-refuter.md +71 -0
- package/prompts/scout.md +140 -0
- package/prompts/summarizer.md +18 -0
- package/skills/crysk/SKILL.md +74 -0
- package/skills/crysk-ci-setup/SKILL.md +134 -0
- package/skills/crysk-ci-setup/assets/admission-config.example.json +9 -0
- package/skills/crysk-ci-setup/assets/workflow.yml +151 -0
- package/skills/crysk-ci-setup/references/ci-admission.md +122 -0
- package/skills/crysk-ci-setup/references/opencode-ci.md +141 -0
- package/skills/crysk-loop/SKILL.md +496 -0
- package/skills/crysk-loop/adjudicator.md +86 -0
- package/skills/crysk-pr/SKILL.md +66 -0
- package/src/agent-env.ts +1084 -0
- package/src/asset-manifest.ts +83 -0
- package/src/assets.d.ts +6 -0
- package/src/assets.ts +183 -0
- package/src/brand-paths.ts +155 -0
- package/src/brand.ts +255 -0
- package/src/ci/admission-ledger.ts +350 -0
- package/src/ci/classify-error.ts +73 -0
- package/src/ci/cli-record.ts +271 -0
- package/src/ci/force-comment.ts +39 -0
- package/src/ci/gates.ts +923 -0
- package/src/ci/outcome.ts +2181 -0
- package/src/ci/override.ts +187 -0
- package/src/ci/publish.ts +1639 -0
- package/src/ci/reporter.ts +626 -0
- package/src/ci/review-admission.ts +901 -0
- package/src/ci/review-levels.ts +310 -0
- package/src/ci/review-policy.ts +18 -0
- package/src/ci/review-risk.ts +184 -0
- package/src/ci/setup.ts +844 -0
- package/src/ci/trigger.ts +1353 -0
- package/src/cli.ts +348 -0
- package/src/commands/activity.ts +67 -0
- package/src/commands/ci-setup-questions.ts +397 -0
- package/src/commands/ci-setup.ts +381 -0
- package/src/commands/config.ts +99 -0
- package/src/commands/doctor.ts +108 -0
- package/src/commands/init.ts +139 -0
- package/src/commands/mcp.ts +10 -0
- package/src/commands/menu.ts +129 -0
- package/src/commands/post.ts +302 -0
- package/src/commands/skills.ts +83 -0
- package/src/commands/status.ts +173 -0
- package/src/commands/triage.ts +342 -0
- package/src/commands/uninstall.ts +37 -0
- package/src/commands/upgrade.ts +214 -0
- package/src/commands/usage.ts +98 -0
- package/src/compare/compare.ts +28 -0
- package/src/compare/floor-test.ts +471 -0
- package/src/compare/martian-adapter.ts +80 -0
- package/src/config/config.ts +267 -0
- package/src/corpus/corpus.ts +1094 -0
- package/src/corpus/preflight.ts +1789 -0
- package/src/diversity/accounting.ts +90 -0
- package/src/diversity/adjudication.ts +145 -0
- package/src/diversity/admission.ts +60 -0
- package/src/diversity/benchmark.ts +195 -0
- package/src/diversity/clustering.ts +285 -0
- package/src/diversity/errors.ts +7 -0
- package/src/diversity/identity.ts +285 -0
- package/src/diversity/index.ts +11 -0
- package/src/diversity/pipeline-integration.ts +448 -0
- package/src/diversity/projection.ts +57 -0
- package/src/diversity/self-contained.ts +68 -0
- package/src/diversity/target-validation.ts +179 -0
- package/src/doctor-migration.ts +209 -0
- package/src/doctor.ts +723 -0
- package/src/errors.ts +83 -0
- package/src/execution/admission.ts +75 -0
- package/src/execution/atomic-write.ts +35 -0
- package/src/execution/attempt-evidence.ts +415 -0
- package/src/execution/bucket-id.ts +136 -0
- package/src/execution/concurrency-limiter.ts +175 -0
- package/src/execution/contracts.ts +443 -0
- package/src/execution/failure-classification.ts +42 -0
- package/src/execution/failure-policy.ts +240 -0
- package/src/execution/harness.ts +2548 -0
- package/src/execution/settlement.ts +348 -0
- package/src/execution/spawned-process.ts +66 -0
- package/src/execution/spend-limiter.ts +424 -0
- package/src/execution/step-artifacts.ts +55 -0
- package/src/execution/usage-normalized.ts +469 -0
- package/src/git/git.ts +396 -0
- package/src/git/identity.ts +64 -0
- package/src/git/refs.ts +140 -0
- package/src/home-preflight.ts +443 -0
- package/src/home.ts +243 -0
- package/src/ignore-file.ts +336 -0
- package/src/ignore-read.ts +118 -0
- package/src/index.ts +34 -0
- package/src/legacy-launchd.ts +29 -0
- package/src/mcp/mcp.ts +150 -0
- package/src/mcp/preflight.ts +407 -0
- package/src/menu-context.ts +192 -0
- package/src/model/catalog.ts +251 -0
- package/src/model/exec-snapshots.ts +390 -0
- package/src/model/free-discovery.ts +302 -0
- package/src/model/provider-capabilities.ts +814 -0
- package/src/model/routing.ts +600 -0
- package/src/pr/admission.ts +847 -0
- package/src/pr/ci-admission-gate.ts +665 -0
- package/src/pr/ci-publish.ts +76 -0
- package/src/pr/discovery.ts +432 -0
- package/src/pr/inline.ts +507 -0
- package/src/pr/plan.ts +366 -0
- package/src/pr/posting.ts +115 -0
- package/src/pr/pr-status.ts +787 -0
- package/src/pr/pr.ts +3642 -0
- package/src/pr/preflight.ts +1031 -0
- package/src/pr/publish-outcome.ts +361 -0
- package/src/pr/range.ts +119 -0
- package/src/pr/reverts-preflight.ts +606 -0
- package/src/pr/reverts.ts +377 -0
- package/src/pr/review-pr.ts +927 -0
- package/src/pr/status.ts +154 -0
- package/src/pr/target.ts +388 -0
- package/src/pr/teardown.ts +437 -0
- package/src/pr/worktree-setup.ts +60 -0
- package/src/production-runtime.ts +1877 -0
- package/src/progress.ts +487 -0
- package/src/rereview/adjudicate.ts +89 -0
- package/src/rereview/classify.ts +362 -0
- package/src/rereview/identity.ts +211 -0
- package/src/rereview/plan.ts +308 -0
- package/src/rereview/prepare.ts +1025 -0
- package/src/rereview/state.ts +767 -0
- package/src/rereview/verify.ts +241 -0
- package/src/review/boundary.ts +105 -0
- package/src/review/consolidate.ts +246 -0
- package/src/review/dedupe.ts +368 -0
- package/src/review/drafts.ts +448 -0
- package/src/review/findings-conformance.ts +120 -0
- package/src/review/findings.ts +714 -0
- package/src/review/pipeline.ts +3977 -0
- package/src/review/preflight.ts +3575 -0
- package/src/review/prompt-set.ts +177 -0
- package/src/review/proof-refs.ts +187 -0
- package/src/review/report.ts +1821 -0
- package/src/review/review.ts +490 -0
- package/src/review/root-cause.ts +120 -0
- package/src/review/route-preflight.ts +289 -0
- package/src/review/run.ts +601 -0
- package/src/review/scout.ts +323 -0
- package/src/review/size-gate.ts +595 -0
- package/src/review/spec.ts +235 -0
- package/src/review/step-runner.ts +233 -0
- package/src/runner-authority.ts +596 -0
- package/src/security/binding-policy.ts +115 -0
- package/src/security/credential-broker.ts +773 -0
- package/src/security/evidence-redaction.ts +62 -0
- package/src/security/execution-authority.ts +27 -0
- package/src/security/redact.ts +71 -0
- package/src/security/workspace-read-broker.ts +466 -0
- package/src/server/client.ts +190 -0
- package/src/server/preflight.ts +217 -0
- package/src/server/server.ts +327 -0
- package/src/signal-handler.ts +50 -0
- package/src/store/activity.ts +413 -0
- package/src/store/backfill-preflight.ts +49 -0
- package/src/store/backfill.ts +141 -0
- package/src/store/gc-preflight.ts +206 -0
- package/src/store/gc.ts +424 -0
- package/src/store/metrics-preflight.ts +229 -0
- package/src/store/metrics.ts +236 -0
- package/src/store/preflight.ts +748 -0
- package/src/store/store.ts +693 -0
- package/src/system-tools.ts +355 -0
- package/src/transport-registry.ts +1124 -0
- package/src/transports/claude-code-cli.ts +1348 -0
- package/src/transports/opencode-admission.ts +515 -0
- package/src/transports/opencode-client.ts +3328 -0
- package/src/transports/opencode-evidence.ts +557 -0
- package/src/transports/opencode-mcp.ts +248 -0
- package/src/transports/opencode-sdk.ts +2294 -0
- package/src/transports/opencode-server.ts +535 -0
- package/src/triage/adjudication.ts +113 -0
- package/src/triage/reply.ts +125 -0
- package/src/triage/triage.ts +255 -0
- package/src/ui/activity.ts +173 -0
- package/src/ui/config-edit.ts +816 -0
- package/src/ui/config.ts +462 -0
- package/src/ui/menu.ts +1325 -0
- package/src/ui/plan.ts +1039 -0
- package/src/ui/primitives.ts +300 -0
- package/src/ui/progress.ts +298 -0
- package/src/ui/result.ts +552 -0
- package/src/ui/review-menu.ts +312 -0
- package/src/ui/select.ts +528 -0
- package/src/ui/tree.ts +178 -0
- package/src/uninstaller.ts +479 -0
- package/src/updater.ts +836 -0
- package/src/usage.ts +77 -0
- package/src/watch/preflight.ts +1389 -0
- package/src/watch/watch.ts +1235 -0
- package/src/wizard.ts +877 -0
|
@@ -0,0 +1,1639 @@
|
|
|
1
|
+
// The outcome publisher (odd/tasks/agent-first-ci.md, T2b / S3, T2c). Every
|
|
2
|
+
// outcome of a run becomes ONE annotation, ONE step summary and, when the PR is
|
|
3
|
+
// known, ONE sticky comment on that PR. The commit status is posted only when a
|
|
4
|
+
// valid head sha is known: a status needs a sha, a comment does not. The
|
|
5
|
+
// comment's marker says head=unknown when no head is known. One case decides on
|
|
6
|
+
// the head's own status: a CLI-started run whose cli.json is a `reviewing` progress
|
|
7
|
+
// record. That record says the CLI took the pending lock on its own head, so the
|
|
8
|
+
// status on that head is replaced when it is still pending or unreadable, and the
|
|
9
|
+
// comment alone is posted when it settled (see decideCliOutcome). The trigger writes
|
|
10
|
+
// the decision to trigger.json, the setup step may leave setup.json, and the CLI
|
|
11
|
+
// leaves cli.json: a progress record while it holds the lock, then its outcome. This
|
|
12
|
+
// step reads them and speaks.
|
|
13
|
+
//
|
|
14
|
+
// It runs `if: !cancelled()`: a failed install skips every later step, and this
|
|
15
|
+
// step is the only one that can still say so, while a cancelled job (a
|
|
16
|
+
// superseded run) must not report a failure it never had. It is gated on the trigger's
|
|
17
|
+
// `publish` output, not on `run`, because `run` is false for exactly the
|
|
18
|
+
// outcomes this step reports.
|
|
19
|
+
//
|
|
20
|
+
// WHY the CLI's record decides once the CLI started: `cli-started` is the marker
|
|
21
|
+
// the run step writes before the CLI runs, and cli.json says what the run did.
|
|
22
|
+
// A `reviewed` record cleans up: a completed review contradicts an older run's
|
|
23
|
+
// outcome comment, so pr-hero's own older outcome comments on the PR go. When that
|
|
24
|
+
// record also carries the status its CLI could not write, the status is posted first,
|
|
25
|
+
// once, on the record's head (see the cleanup branch of runPublish). An
|
|
26
|
+
// `error`, a `hunters-failed`, or a `reviewing` record is published here with its
|
|
27
|
+
// code, because the CLI posts no comment for those. A `skipped` or `yielded` run
|
|
28
|
+
// is left alone: the CLI posted its own notice. A record that is absent,
|
|
29
|
+
// unreadable or malformed reads as no record: the run took no lock the publisher
|
|
30
|
+
// can see, so it gets the comment alone and no status (see decideCliOutcome).
|
|
31
|
+
// Cleanup needs a PR, and it never posts a status on a run with no lock.
|
|
32
|
+
//
|
|
33
|
+
// WHY the lock's head comes from the record (F002 on #330): the CLI posts its pending
|
|
34
|
+
// hold on the head it resolved itself, and the reviewing record names that head. The
|
|
35
|
+
// trigger's head can differ after a push, so it is never read for a lock. For the
|
|
36
|
+
// other outcomes, cli.json names the head the CLI reviewed or resolved, when it knows
|
|
37
|
+
// one, and the trigger's head is the fallback, since a failed `gh pr view` at trigger
|
|
38
|
+
// time can leave it out.
|
|
39
|
+
//
|
|
40
|
+
// WHY ONLY BUILTINS (plus ./outcome and ./override): the step runs before anything is
|
|
41
|
+
// installed, so a package import has no node_modules to resolve against. ./outcome and
|
|
42
|
+
// ./override are siblings that are themselves builtins-only at runtime: ./outcome's
|
|
43
|
+
// repository imports are `import type`, erased at transpile, and ./override imports
|
|
44
|
+
// nothing. Relative imports need no node_modules.
|
|
45
|
+
//
|
|
46
|
+
// WHY repost, not edit: GitHub does not notify on an edit, and an edited comment
|
|
47
|
+
// does not move down the timeline. So the fresh comment is POSTED first, and the
|
|
48
|
+
// older outcome comments of the SAME author are deleted after it (see
|
|
49
|
+
// repostMarkedPrComment in src/pr/pr.ts, which made the same choice for the skip
|
|
50
|
+
// notice on #315). Posting first means a failed POST deletes nothing.
|
|
51
|
+
|
|
52
|
+
import { appendFileSync, existsSync, readFileSync, statSync } from "node:fs";
|
|
53
|
+
import path from "node:path";
|
|
54
|
+
import {
|
|
55
|
+
commandHint,
|
|
56
|
+
MARKER_OPEN,
|
|
57
|
+
matchMarkerPrefix,
|
|
58
|
+
STATUS_CONTEXT,
|
|
59
|
+
statusContextForms,
|
|
60
|
+
} from "../brand";
|
|
61
|
+
import type {
|
|
62
|
+
CliRecord,
|
|
63
|
+
ErrorParams,
|
|
64
|
+
RenderedError,
|
|
65
|
+
SetupRecord,
|
|
66
|
+
TriggerRecord,
|
|
67
|
+
} from "./outcome";
|
|
68
|
+
import {
|
|
69
|
+
actionsRunUrl,
|
|
70
|
+
CLI_RECORD_FILE,
|
|
71
|
+
OUTCOME_MARKER_PREFIX,
|
|
72
|
+
PENDING_AT_FILE,
|
|
73
|
+
parseCliRecord,
|
|
74
|
+
parseSetupRecord,
|
|
75
|
+
parseTriggerRecord,
|
|
76
|
+
renderCode,
|
|
77
|
+
renderError,
|
|
78
|
+
renderHunterCause,
|
|
79
|
+
STATUS_DESCRIPTION_MAX,
|
|
80
|
+
} from "./outcome";
|
|
81
|
+
import {
|
|
82
|
+
newestStatusInContext,
|
|
83
|
+
OVERRIDE_DESCRIPTION_PREFIX,
|
|
84
|
+
type OverrideStatusFact,
|
|
85
|
+
overrideStandsOverRun,
|
|
86
|
+
strandedStatusPair,
|
|
87
|
+
} from "./override";
|
|
88
|
+
import { OVERRIDE_COMMAND, REVIEW_COMMAND } from "./trigger";
|
|
89
|
+
|
|
90
|
+
// The outcome marker's spelling is outcome.ts's, shared with the status reader.
|
|
91
|
+
const MARKER_PREFIX = OUTCOME_MARKER_PREFIX.trimEnd();
|
|
92
|
+
const CLI_STARTED_FILE = "cli-started";
|
|
93
|
+
const RECORD_FILE = "trigger.json";
|
|
94
|
+
const SETUP_RECORD_FILE = "setup.json";
|
|
95
|
+
// The step a CLI outcome names. A CLI record carries no step, and every CLI
|
|
96
|
+
// outcome is about the review, so the publisher names it.
|
|
97
|
+
const REVIEW_STEP = "the review";
|
|
98
|
+
// The step a run names when it has no outcome: no cli.json, or a reviewing record
|
|
99
|
+
// that no outcome followed. The CLI exits without an outcome on a usage error, on a
|
|
100
|
+
// crash or kill, or when its record write failed, and the publisher cannot tell
|
|
101
|
+
// which, so the text claims only that it stopped before it reported an outcome.
|
|
102
|
+
const STOPPED_STEP = "the review before it reported an outcome";
|
|
103
|
+
// What a hunters-failed record says when its hunters gave no shared detail: the
|
|
104
|
+
// publisher's wording names what its own record means (sessionFailed: every hunter
|
|
105
|
+
// failed, src/review/findings.ts).
|
|
106
|
+
const HUNTERS_DETAIL = "each hunter's session ended without a result";
|
|
107
|
+
// The step an unverifiable lookup stopped at. The trigger writes the same text
|
|
108
|
+
// into its record; the publisher names it itself, so the message never depends
|
|
109
|
+
// on what a record carries.
|
|
110
|
+
const PERMISSION_STEP = "the commenter permission check";
|
|
111
|
+
// Short and fixed: one pause before the one retry of the status write.
|
|
112
|
+
const STATUS_RETRY_DELAY_MS = 2_000;
|
|
113
|
+
// The wait before the one retry of a failed head-status read (readHeadStatuses).
|
|
114
|
+
const STATUS_READ_RETRY_DELAY_MS = 2_000;
|
|
115
|
+
const GH_TIMEOUT_MS = 30_000;
|
|
116
|
+
|
|
117
|
+
export interface GhResult {
|
|
118
|
+
exitCode: number;
|
|
119
|
+
stdout: string;
|
|
120
|
+
stderr: string;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
interface StatusPost {
|
|
124
|
+
// `success` comes from an override (odd/tasks/agent-first-ci.md, S8) or from the
|
|
125
|
+
// final status a CLI could not write; `failure` only from the latter.
|
|
126
|
+
state: "error" | "success" | "failure";
|
|
127
|
+
context: typeof STATUS_CONTEXT;
|
|
128
|
+
description: string;
|
|
129
|
+
targetUrl: string;
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// The newest pr-hero status of the head a reviewing record names: a pending
|
|
133
|
+
// status is a CLI that never settled, a settled one is a finished CLI, and anything
|
|
134
|
+
// else (an unreadable answer, no pr-hero row, a state this table does not name) is
|
|
135
|
+
// unknown.
|
|
136
|
+
type LatestStatusState = "pending" | "settled" | "unknown";
|
|
137
|
+
|
|
138
|
+
export type Publication =
|
|
139
|
+
| { kind: "none" }
|
|
140
|
+
// Annotation and step summary only: no PR can be written to.
|
|
141
|
+
| { kind: "local"; rendered: RenderedError }
|
|
142
|
+
// A human's override (odd/tasks/agent-first-ci.md, S8): a success status on the
|
|
143
|
+
// head it names, then, only when that status was written, the cleanup of older
|
|
144
|
+
// outcome comments (the same cleanup a completed review runs, since both settle the
|
|
145
|
+
// head), then an audit comment. Nothing ran, so nothing is rendered as an outcome.
|
|
146
|
+
// The override's audit comments and the report summary are never cleaned up.
|
|
147
|
+
| {
|
|
148
|
+
kind: "override";
|
|
149
|
+
repository: string;
|
|
150
|
+
pr: number;
|
|
151
|
+
runId: string;
|
|
152
|
+
runAttempt: number;
|
|
153
|
+
runUrl: string;
|
|
154
|
+
actor: string;
|
|
155
|
+
// Undefined when the record names no head: the comment still posts, and says
|
|
156
|
+
// that no status could be set.
|
|
157
|
+
headSha: string | undefined;
|
|
158
|
+
}
|
|
159
|
+
// The CLI's review completed. Its record may carry the status the CLI could not
|
|
160
|
+
// write, which is posted first, on the record's head. Then only older runs' (or
|
|
161
|
+
// earlier attempts') outcome comments on this PR go, when the PR is known. runId
|
|
162
|
+
// and runAttempt are this run's place, the bound those comments are compared to.
|
|
163
|
+
| {
|
|
164
|
+
kind: "cleanup";
|
|
165
|
+
repository: string;
|
|
166
|
+
// Undefined when the PR is unknown: then only the unsettled status is written.
|
|
167
|
+
pr: number | undefined;
|
|
168
|
+
runId: string;
|
|
169
|
+
runAttempt: number;
|
|
170
|
+
// The unsettled status and the head it is for, when the record carries both.
|
|
171
|
+
// Undefined otherwise, which is every reviewed record without the field.
|
|
172
|
+
headSha?: string;
|
|
173
|
+
status?: StatusPost;
|
|
174
|
+
// The override cutoff the CLI computed for the run, when the record carries one. The repost
|
|
175
|
+
// is skipped when an override created at or after it stands on the head
|
|
176
|
+
// (overrideStandsBeforeRepost). Undefined means no cutoff to judge against, and the repost
|
|
177
|
+
// goes ahead as before.
|
|
178
|
+
overrideCutoffMs?: number;
|
|
179
|
+
}
|
|
180
|
+
| {
|
|
181
|
+
kind: "remote";
|
|
182
|
+
rendered: RenderedError;
|
|
183
|
+
repository: string;
|
|
184
|
+
pr: number;
|
|
185
|
+
// This run's id and attempt. The cleanup after the post deletes only
|
|
186
|
+
// comments of an older run, or of an earlier attempt of this run.
|
|
187
|
+
runId: string;
|
|
188
|
+
runAttempt: number;
|
|
189
|
+
// Undefined when no head is known (no valid head in the record, or a run that
|
|
190
|
+
// took no lock): the comment still posts.
|
|
191
|
+
headSha: string | undefined;
|
|
192
|
+
// Undefined exactly when headSha is: a commit status needs a sha.
|
|
193
|
+
status: StatusPost | undefined;
|
|
194
|
+
commentBody: string;
|
|
195
|
+
};
|
|
196
|
+
|
|
197
|
+
// The pure table. Which outcome this run has is the record's decision and class
|
|
198
|
+
// (renderError / renderCode say what it means); whether anything can be posted
|
|
199
|
+
// on a PR is the record's pr alone: the head only decides the status.
|
|
200
|
+
export function decidePublication(input: {
|
|
201
|
+
record: TriggerRecord | undefined;
|
|
202
|
+
cliStarted: boolean;
|
|
203
|
+
// The CLI's cli.json, read only for a review the CLI started. Undefined when
|
|
204
|
+
// the file is absent, unreadable or malformed: the same as no record.
|
|
205
|
+
cliRecord?: CliRecord;
|
|
206
|
+
setup?: SetupRecord;
|
|
207
|
+
// The status of the head a reviewing record names. runPublish reads it only for
|
|
208
|
+
// that case; undefined elsewhere, and treated as unknown in that one case.
|
|
209
|
+
latestStatusState?: LatestStatusState;
|
|
210
|
+
}): Publication {
|
|
211
|
+
const record = input.record;
|
|
212
|
+
if (record === undefined) return { kind: "none" };
|
|
213
|
+
if (record.decision === "ignore" || record.decision === "denied") {
|
|
214
|
+
// Denied and ignored commands publish nothing: the trigger answers them with a
|
|
215
|
+
// reaction on the comment instead (T2e), so the PR gets no text reply.
|
|
216
|
+
return { kind: "none" };
|
|
217
|
+
}
|
|
218
|
+
// WHY before the outcome fallthrough below: an override is not an error, and
|
|
219
|
+
// rendering it as one would publish a false PRH-INSTALL-FAILED.
|
|
220
|
+
if (record.decision === "override") return decideOverride(record);
|
|
221
|
+
if (record.decision === "review" && input.cliStarted) {
|
|
222
|
+
return decideCliOutcome(record, input.cliRecord, input.latestStatusState);
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
const params: ErrorParams = {
|
|
226
|
+
...record.params,
|
|
227
|
+
pr: record.pr,
|
|
228
|
+
runId: record.run_id,
|
|
229
|
+
runUrl: record.run_url,
|
|
230
|
+
};
|
|
231
|
+
return remoteOrLocal(
|
|
232
|
+
record,
|
|
233
|
+
renderRecordOutcome(record, params, input.setup),
|
|
234
|
+
record.head_sha,
|
|
235
|
+
);
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
// WHY the record's PR and actor are both required: parseTriggerRecord refuses an
|
|
239
|
+
// override without them, so this is the reader's own guarantee for the type.
|
|
240
|
+
function decideOverride(record: TriggerRecord): Publication {
|
|
241
|
+
if (record.pr === undefined || record.actor === undefined) {
|
|
242
|
+
return { kind: "none" };
|
|
243
|
+
}
|
|
244
|
+
return {
|
|
245
|
+
kind: "override",
|
|
246
|
+
repository: record.repository,
|
|
247
|
+
pr: record.pr,
|
|
248
|
+
runId: record.run_id,
|
|
249
|
+
runAttempt: record.run_attempt,
|
|
250
|
+
runUrl: record.run_url,
|
|
251
|
+
actor: record.actor,
|
|
252
|
+
headSha: record.head_sha,
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// WHY the CLI's record decides: the CLI posts its own status and comment for a
|
|
257
|
+
// completed review and for a skip, so those need nothing here. It posts neither
|
|
258
|
+
// for a crash, a class it could not map, or all hunters failing, so the publisher
|
|
259
|
+
// speaks for those with the code the record names. Cleanup needs a settled head:
|
|
260
|
+
// a completed review, or an override that set its status (publishOverride), makes
|
|
261
|
+
// a stale outcome comment contradict the PR, and nothing else removes that comment.
|
|
262
|
+
// A run with no record took no lock the publisher can see, so it gets the
|
|
263
|
+
// comment alone (see the no-record branch below). A reviewing record decides its status
|
|
264
|
+
// by the head it names (see that case), never by the trigger's head.
|
|
265
|
+
function decideCliOutcome(
|
|
266
|
+
record: TriggerRecord,
|
|
267
|
+
cliRecord: CliRecord | undefined,
|
|
268
|
+
latestStatusState: LatestStatusState | undefined,
|
|
269
|
+
): Publication {
|
|
270
|
+
const runParams: ErrorParams = {
|
|
271
|
+
pr: record.pr,
|
|
272
|
+
runId: record.run_id,
|
|
273
|
+
runUrl: record.run_url,
|
|
274
|
+
};
|
|
275
|
+
if (cliRecord === undefined) {
|
|
276
|
+
// WHY no status (F001 and F002 on #330): without a progress record the publisher
|
|
277
|
+
// cannot tell whose pending lock sits on a head, so it writes no status on any
|
|
278
|
+
// head. A skipped or yielded run must not turn red, and another process's live
|
|
279
|
+
// lock must not be overwritten. The comment alone says what happened. The one
|
|
280
|
+
// case this leaves is a CLI that held the lock but whose every record write
|
|
281
|
+
// failed: its pending then ends with the admission gate's 90-minute TTL. The
|
|
282
|
+
// marker says head=unknown, and nothing reads that field.
|
|
283
|
+
return remoteOrLocal(
|
|
284
|
+
record,
|
|
285
|
+
renderError(
|
|
286
|
+
{ kind: "unknown" },
|
|
287
|
+
{ ...runParams, step: STOPPED_STEP },
|
|
288
|
+
"action",
|
|
289
|
+
),
|
|
290
|
+
undefined,
|
|
291
|
+
);
|
|
292
|
+
}
|
|
293
|
+
switch (cliRecord.outcome) {
|
|
294
|
+
case "reviewing": {
|
|
295
|
+
// WHY the record's head (F002 on #330): the CLI posted its pending hold on the
|
|
296
|
+
// head this record names, so the lock to replace is there, whatever head the
|
|
297
|
+
// trigger saw. The CLI holds the lock and recorded no outcome (it was killed, or
|
|
298
|
+
// its terminal write failed), so the lock is this run's own and a visible red is
|
|
299
|
+
// recoverable. A settled status means the CLI finished, so it stands (F004). An
|
|
300
|
+
// unreadable status takes the error status too, the same reasoning.
|
|
301
|
+
const replacesLock = latestStatusState !== "settled";
|
|
302
|
+
return remoteOrLocal(
|
|
303
|
+
record,
|
|
304
|
+
renderError(
|
|
305
|
+
{ kind: "unknown" },
|
|
306
|
+
{ ...runParams, step: STOPPED_STEP },
|
|
307
|
+
"action",
|
|
308
|
+
),
|
|
309
|
+
replacesLock ? cliRecord.head_sha : undefined,
|
|
310
|
+
);
|
|
311
|
+
}
|
|
312
|
+
case "reviewed": {
|
|
313
|
+
// WHY the cleanup needs a PR and no head: the older comments are on the
|
|
314
|
+
// PR, and the completed review is the proof, whatever head the trigger saw.
|
|
315
|
+
// WHY the status needs neither: it sits on the record's head, so it is posted
|
|
316
|
+
// even when no PR is known, and then nothing is cleaned.
|
|
317
|
+
const unsettled = unsettledStatusOf(record, cliRecord);
|
|
318
|
+
if (record.pr === undefined && unsettled === undefined) {
|
|
319
|
+
return { kind: "none" };
|
|
320
|
+
}
|
|
321
|
+
return {
|
|
322
|
+
kind: "cleanup",
|
|
323
|
+
repository: record.repository,
|
|
324
|
+
pr: record.pr,
|
|
325
|
+
runId: record.run_id,
|
|
326
|
+
runAttempt: record.run_attempt,
|
|
327
|
+
...unsettled,
|
|
328
|
+
};
|
|
329
|
+
}
|
|
330
|
+
case "skipped":
|
|
331
|
+
case "yielded":
|
|
332
|
+
return { kind: "none" };
|
|
333
|
+
case "hunters-failed":
|
|
334
|
+
return remoteOrLocal(
|
|
335
|
+
record,
|
|
336
|
+
huntersFailedRendering(cliRecord, runParams),
|
|
337
|
+
cliRecord.head_sha ?? record.head_sha,
|
|
338
|
+
);
|
|
339
|
+
case "error":
|
|
340
|
+
// WHY the params merge: the review is the default step, the CLI's own
|
|
341
|
+
// params (a backend or a secret name, when it has one) replace that
|
|
342
|
+
// default, and this run's pr and run link replace both. The cls fallback
|
|
343
|
+
// only narrows the type: parseCliRecord always sets cls for an error.
|
|
344
|
+
return remoteOrLocal(
|
|
345
|
+
record,
|
|
346
|
+
renderError(
|
|
347
|
+
cliRecord.cls ?? { kind: "unknown" },
|
|
348
|
+
{ step: REVIEW_STEP, ...cliRecord.params, ...runParams },
|
|
349
|
+
"action",
|
|
350
|
+
),
|
|
351
|
+
cliRecord.head_sha ?? record.head_sha,
|
|
352
|
+
);
|
|
353
|
+
}
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// The status a reviewed record's CLI could not write, on the head that record names, in
|
|
357
|
+
// the shape postStatus takes. Undefined when the record carries no status, or names no
|
|
358
|
+
// head: a status needs a sha, and the publisher does not guess one from the trigger.
|
|
359
|
+
function unsettledStatusOf(
|
|
360
|
+
record: TriggerRecord,
|
|
361
|
+
cliRecord: CliRecord,
|
|
362
|
+
):
|
|
363
|
+
| { headSha: string; status: StatusPost; overrideCutoffMs?: number }
|
|
364
|
+
| undefined {
|
|
365
|
+
const unsettled = cliRecord.unsettled_status;
|
|
366
|
+
if (unsettled === undefined || cliRecord.head_sha === undefined) {
|
|
367
|
+
return undefined;
|
|
368
|
+
}
|
|
369
|
+
return {
|
|
370
|
+
headSha: cliRecord.head_sha,
|
|
371
|
+
overrideCutoffMs: cliRecord.override_cutoff_ms,
|
|
372
|
+
status: {
|
|
373
|
+
state: unsettled.state,
|
|
374
|
+
context: STATUS_CONTEXT,
|
|
375
|
+
description: unsettled.description,
|
|
376
|
+
targetUrl: record.run_url,
|
|
377
|
+
},
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
// The rendering of a hunters-failed run. WHY every hunter and not any: a run whose
|
|
382
|
+
// hunters failed for one known cause on the provider's or the configuration's side has
|
|
383
|
+
// nothing else to fix, so that cause's own code and owner are rendered. The provider
|
|
384
|
+
// and the model are named only when every entry agrees on them. A mix of causes, an
|
|
385
|
+
// entry with no cause, or a cause with no code of its own (runtime_unavailable) keeps
|
|
386
|
+
// the generic code, since no single cause explains the run.
|
|
387
|
+
function huntersFailedRendering(
|
|
388
|
+
cliRecord: CliRecord,
|
|
389
|
+
runParams: ErrorParams,
|
|
390
|
+
): RenderedError {
|
|
391
|
+
const failures = cliRecord.hunter_failures ?? [];
|
|
392
|
+
// The provider's own words, when every hunter reported the same ones. Hunters that
|
|
393
|
+
// disagree, or one that said nothing, leave the generic text: no single explanation
|
|
394
|
+
// covers the run.
|
|
395
|
+
const sharedDetail = sharedValue(failures.map((failure) => failure.detail));
|
|
396
|
+
const cause = sharedValue(failures.map((failure) => failure.cause));
|
|
397
|
+
if (cause !== undefined) {
|
|
398
|
+
const rendered = renderHunterCause(
|
|
399
|
+
cause,
|
|
400
|
+
{
|
|
401
|
+
...runParams,
|
|
402
|
+
step: REVIEW_STEP,
|
|
403
|
+
detail: HUNTERS_DETAIL,
|
|
404
|
+
provider: sharedValue(failures.map((failure) => failure.provider)),
|
|
405
|
+
model: sharedValue(failures.map((failure) => failure.model)),
|
|
406
|
+
},
|
|
407
|
+
"action",
|
|
408
|
+
{
|
|
409
|
+
detail: sharedDetail,
|
|
410
|
+
credential: sharedValue(failures.map((failure) => failure.credential)),
|
|
411
|
+
},
|
|
412
|
+
);
|
|
413
|
+
if (rendered !== undefined) return rendered;
|
|
414
|
+
}
|
|
415
|
+
return renderCode(
|
|
416
|
+
"PRH-HUNTERS-FAILED",
|
|
417
|
+
{ ...runParams, step: REVIEW_STEP, detail: sharedDetail ?? HUNTERS_DETAIL },
|
|
418
|
+
"action",
|
|
419
|
+
// Every hunter was refused for the size of the request: the fix is a smaller review.
|
|
420
|
+
cause === "context_window_exceeded" ? "context_window_exceeded" : undefined,
|
|
421
|
+
);
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// The one value every entry names, or undefined when they name none or differ.
|
|
425
|
+
function sharedValue<T>(values: readonly (T | undefined)[]): T | undefined {
|
|
426
|
+
const distinct = new Set(values);
|
|
427
|
+
if (distinct.size !== 1) return undefined;
|
|
428
|
+
const [only] = distinct;
|
|
429
|
+
return only;
|
|
430
|
+
}
|
|
431
|
+
|
|
432
|
+
// The one place an outcome becomes a plan: the annotation and summary always, and
|
|
433
|
+
// the PR's comment and status when the PR is known. The status needs a head; the
|
|
434
|
+
// comment does not, and its marker says head=unknown without one.
|
|
435
|
+
function remoteOrLocal(
|
|
436
|
+
record: TriggerRecord,
|
|
437
|
+
rendered: RenderedError,
|
|
438
|
+
headSha: string | undefined,
|
|
439
|
+
): Publication {
|
|
440
|
+
if (record.pr === undefined) return { kind: "local", rendered };
|
|
441
|
+
return {
|
|
442
|
+
kind: "remote",
|
|
443
|
+
rendered,
|
|
444
|
+
repository: record.repository,
|
|
445
|
+
pr: record.pr,
|
|
446
|
+
runId: record.run_id,
|
|
447
|
+
runAttempt: record.run_attempt,
|
|
448
|
+
headSha,
|
|
449
|
+
status:
|
|
450
|
+
headSha === undefined
|
|
451
|
+
? undefined
|
|
452
|
+
: {
|
|
453
|
+
state: "error",
|
|
454
|
+
context: STATUS_CONTEXT,
|
|
455
|
+
description: statusDescription(rendered),
|
|
456
|
+
targetUrl: record.run_url,
|
|
457
|
+
},
|
|
458
|
+
commentBody: [
|
|
459
|
+
`${MARKER_PREFIX} v=1 head=${headSha ?? "unknown"} run=${record.run_id} attempt=${record.run_attempt} -->`,
|
|
460
|
+
"",
|
|
461
|
+
...rendered.summary,
|
|
462
|
+
].join("\n"),
|
|
463
|
+
};
|
|
464
|
+
}
|
|
465
|
+
|
|
466
|
+
function renderRecordOutcome(
|
|
467
|
+
record: TriggerRecord,
|
|
468
|
+
params: ErrorParams,
|
|
469
|
+
setup: SetupRecord | undefined,
|
|
470
|
+
): RenderedError {
|
|
471
|
+
if (record.decision === "no-credentials") {
|
|
472
|
+
return renderError(
|
|
473
|
+
{ kind: "trigger", check: "no-credential" },
|
|
474
|
+
params,
|
|
475
|
+
"action",
|
|
476
|
+
);
|
|
477
|
+
}
|
|
478
|
+
if (record.decision === "unverifiable") {
|
|
479
|
+
// WHY the step name: a lookup with no captured status has no class to
|
|
480
|
+
// render, and "stopped at this step" needs a step to name.
|
|
481
|
+
return renderError(
|
|
482
|
+
record.cls ?? { kind: "unknown" },
|
|
483
|
+
{ ...params, step: PERMISSION_STEP },
|
|
484
|
+
"action",
|
|
485
|
+
);
|
|
486
|
+
}
|
|
487
|
+
// A review that never reached the CLI: a recorded class is the cause (the PR
|
|
488
|
+
// could not be resolved); a setup record names the malformed input the setup
|
|
489
|
+
// step found; otherwise the install step failed before the run.
|
|
490
|
+
if (record.cls !== undefined) {
|
|
491
|
+
return renderError(record.cls, params, "action");
|
|
492
|
+
}
|
|
493
|
+
if (setup !== undefined) {
|
|
494
|
+
return renderError(
|
|
495
|
+
setup.cls,
|
|
496
|
+
{ ...params, detail: setup.detail },
|
|
497
|
+
"action",
|
|
498
|
+
);
|
|
499
|
+
}
|
|
500
|
+
return renderError({ kind: "install", step: "" }, params, "action");
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
// One line, `<CODE>: <what>`, clipped to the status limit.
|
|
504
|
+
function statusDescription(rendered: RenderedError): string {
|
|
505
|
+
return clipStatusDescription(`${rendered.code}: ${rendered.what}`);
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
// The one clipping rule of a status description: one line, at most the status limit.
|
|
509
|
+
function clipStatusDescription(text: string): string {
|
|
510
|
+
const line = text.replace(/\s+/g, " ");
|
|
511
|
+
return line.length > STATUS_DESCRIPTION_MAX
|
|
512
|
+
? line.slice(0, STATUS_DESCRIPTION_MAX)
|
|
513
|
+
: line;
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
// The status a cancelled review settles its own pending on (SB3). A cancel kills the CLI
|
|
517
|
+
// before its signal settle lands, so the publisher writes this instead of leaving the head
|
|
518
|
+
// pending. It names the cancel and the way to review the head again.
|
|
519
|
+
export const CANCELLED_STATUS_DESCRIPTION =
|
|
520
|
+
"PRH-CANCELLED: not reviewed — /pr-hero review reviews this head";
|
|
521
|
+
|
|
522
|
+
// The run URL identifies the pending (SB3 follow-up). A pending the CLI posted in Actions carries
|
|
523
|
+
// its run's attempt URL (actionsRunUrl, src/ci/cli-record.ts), and the settle is this run's only
|
|
524
|
+
// when that URL is this run's. A pending that names another run's actions URL is never settled,
|
|
525
|
+
// whatever its created_at: that closes the residual where a cancel in the milliseconds before the
|
|
526
|
+
// pending-at record was written left the head pending, and the one where two pendings posted in
|
|
527
|
+
// the same second were not told apart.
|
|
528
|
+
//
|
|
529
|
+
// WHY the created_at rule is kept as the fallback (review #347 rounds 1-3): a pending with no run
|
|
530
|
+
// URL was posted by an older CLI. The CLI records the created_at GitHub returned for its own pending
|
|
531
|
+
// (PENDING_AT_FILE, written once the pending landed), and the settle overwrites the head's newest
|
|
532
|
+
// pending only when its created_at is exactly that time, so another run's pending (a Force Review
|
|
533
|
+
// beside a push run on the same head) is not touched. No record of the pending means this run's
|
|
534
|
+
// pending never landed, or the CLI died first, and there is nothing of this run's to settle.
|
|
535
|
+
// Any CLI record that names a head is this run's lock on it: the CLI rewrites its record
|
|
536
|
+
// (reviewing, then reviewed or hunters-failed) before its final status.
|
|
537
|
+
export function decideCancelSettle(input: {
|
|
538
|
+
record: CliRecord | undefined;
|
|
539
|
+
latest:
|
|
540
|
+
| { state: string; created_at: string; target_url?: string | null }
|
|
541
|
+
| undefined;
|
|
542
|
+
pendingAt: string | undefined;
|
|
543
|
+
// This run's own run URL (actionsRunUrl), or undefined outside Actions.
|
|
544
|
+
runUrl?: string;
|
|
545
|
+
}): { settle: true; headSha: string } | { settle: false; reason: string } {
|
|
546
|
+
const record = input.record;
|
|
547
|
+
if (record === undefined || record.head_sha === undefined) {
|
|
548
|
+
return { settle: false, reason: "no review lock in this run" };
|
|
549
|
+
}
|
|
550
|
+
const latest = input.latest;
|
|
551
|
+
if (latest?.state !== "pending") {
|
|
552
|
+
return {
|
|
553
|
+
settle: false,
|
|
554
|
+
reason: "the head's crysk status is not pending",
|
|
555
|
+
};
|
|
556
|
+
}
|
|
557
|
+
if (
|
|
558
|
+
typeof latest.target_url === "string" &&
|
|
559
|
+
ACTIONS_RUN_URL.test(latest.target_url)
|
|
560
|
+
) {
|
|
561
|
+
if (input.runUrl !== undefined && latest.target_url === input.runUrl) {
|
|
562
|
+
return { settle: true, headSha: record.head_sha };
|
|
563
|
+
}
|
|
564
|
+
return { settle: false, reason: "the pending status is another run's" };
|
|
565
|
+
}
|
|
566
|
+
const own = input.pendingAt;
|
|
567
|
+
if (
|
|
568
|
+
own === undefined ||
|
|
569
|
+
Date.parse(latest.created_at) !== Date.parse(own) ||
|
|
570
|
+
!Number.isFinite(Date.parse(own))
|
|
571
|
+
) {
|
|
572
|
+
return { settle: false, reason: "the pending status is not this run's" };
|
|
573
|
+
}
|
|
574
|
+
return { settle: true, headSha: record.head_sha };
|
|
575
|
+
}
|
|
576
|
+
|
|
577
|
+
// The actions URL of a run's attempt, as actionsRunUrl builds it.
|
|
578
|
+
const ACTIONS_RUN_URL = /\/actions\/runs\/[0-9]+/;
|
|
579
|
+
|
|
580
|
+
// ---------------------------------------------------------------------------
|
|
581
|
+
// The shell.
|
|
582
|
+
// ---------------------------------------------------------------------------
|
|
583
|
+
|
|
584
|
+
export interface PublishDeps {
|
|
585
|
+
env: Readonly<Record<string, string | undefined>>;
|
|
586
|
+
// Stdin is the comment body; the answer is gh's result.
|
|
587
|
+
gh: (args: readonly string[], stdin?: string) => GhResult;
|
|
588
|
+
sleep?: (ms: number) => void;
|
|
589
|
+
// The clock for an override's UTC time. Optional, so a test pins it and the real
|
|
590
|
+
// clock is the default.
|
|
591
|
+
now?: () => Date;
|
|
592
|
+
stdout: (text: string) => void;
|
|
593
|
+
appendSummary: (text: string) => void;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
function httpStatusOf(result: GhResult): number | undefined {
|
|
597
|
+
const match = /HTTP (\d{3})\b/.exec(result.stderr);
|
|
598
|
+
return match === null ? undefined : Number(match[1]);
|
|
599
|
+
}
|
|
600
|
+
|
|
601
|
+
function report(deps: PublishDeps, rendered: RenderedError): void {
|
|
602
|
+
deps.stdout(`${rendered.annotation}\n`);
|
|
603
|
+
deps.appendSummary(`${rendered.summary.join("\n")}\n`);
|
|
604
|
+
}
|
|
605
|
+
|
|
606
|
+
// Workflow-command text, escaped as the runner's toolkit escapes it, so a
|
|
607
|
+
// message can never start a second command.
|
|
608
|
+
function workflowCommand(
|
|
609
|
+
command: "error" | "warning" | "notice",
|
|
610
|
+
message: string,
|
|
611
|
+
): string {
|
|
612
|
+
const escaped = message
|
|
613
|
+
.replaceAll("%", "%25")
|
|
614
|
+
.replaceAll("\r", "%0D")
|
|
615
|
+
.replaceAll("\n", "%0A");
|
|
616
|
+
return `::${command} title=crysk::${escaped}\n`;
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
// The status write, on BOTH names of its context: the new one, then the legacy `pr-hero` one
|
|
620
|
+
// (statusContextForms). Users have `pr-hero` as a required check in branch protection, and a
|
|
621
|
+
// head that stops getting it blocks every PR, so the publisher keeps writing it for one version.
|
|
622
|
+
// A failed write never skips the other: the required check may be either name. The first
|
|
623
|
+
// failure is the one reported, so the renderer sees the same answer a single write gave.
|
|
624
|
+
function postStatus(
|
|
625
|
+
deps: PublishDeps,
|
|
626
|
+
repository: string,
|
|
627
|
+
headSha: string,
|
|
628
|
+
status: StatusPost,
|
|
629
|
+
attempts = 2,
|
|
630
|
+
): { ok: true } | { ok: false; httpStatus: number | undefined } {
|
|
631
|
+
let failure: { ok: false; httpStatus: number | undefined } | undefined;
|
|
632
|
+
for (const context of statusContextForms(status.context)) {
|
|
633
|
+
const posted = postStatusOn(
|
|
634
|
+
deps,
|
|
635
|
+
repository,
|
|
636
|
+
headSha,
|
|
637
|
+
status,
|
|
638
|
+
context,
|
|
639
|
+
attempts,
|
|
640
|
+
);
|
|
641
|
+
if (!posted.ok) failure ??= posted;
|
|
642
|
+
}
|
|
643
|
+
return failure ?? { ok: true };
|
|
644
|
+
}
|
|
645
|
+
|
|
646
|
+
// One write on one context, retried once unless `attempts` is 1. Returns the failure's HTTP
|
|
647
|
+
// status when gh printed one, so the renderer can pick the permission or the server
|
|
648
|
+
// wording.
|
|
649
|
+
function postStatusOn(
|
|
650
|
+
deps: PublishDeps,
|
|
651
|
+
repository: string,
|
|
652
|
+
headSha: string,
|
|
653
|
+
status: StatusPost,
|
|
654
|
+
context: string,
|
|
655
|
+
attempts: number,
|
|
656
|
+
): { ok: true } | { ok: false; httpStatus: number | undefined } {
|
|
657
|
+
const args = [
|
|
658
|
+
"api",
|
|
659
|
+
`repos/${repository}/statuses/${headSha}`,
|
|
660
|
+
"-f",
|
|
661
|
+
`state=${status.state}`,
|
|
662
|
+
"-f",
|
|
663
|
+
`context=${context}`,
|
|
664
|
+
"-f",
|
|
665
|
+
`description=${status.description}`,
|
|
666
|
+
"-f",
|
|
667
|
+
`target_url=${status.targetUrl}`,
|
|
668
|
+
];
|
|
669
|
+
let result = deps.gh(args);
|
|
670
|
+
if (result.exitCode !== 0 && attempts > 1) {
|
|
671
|
+
(deps.sleep ?? ((ms: number) => Bun.sleepSync(ms)))(STATUS_RETRY_DELAY_MS);
|
|
672
|
+
result = deps.gh(args);
|
|
673
|
+
}
|
|
674
|
+
if (result.exitCode === 0) return { ok: true };
|
|
675
|
+
return { ok: false, httpStatus: httpStatusOf(result) };
|
|
676
|
+
}
|
|
677
|
+
|
|
678
|
+
// The one rendering of a failed status write, for the outcome's status and for the
|
|
679
|
+
// unsettled one alike. The detail names the HTTP status when gh printed one.
|
|
680
|
+
function statusWriteFailure(
|
|
681
|
+
httpStatus: number | undefined,
|
|
682
|
+
runId: string,
|
|
683
|
+
runUrl: string,
|
|
684
|
+
): RenderedError {
|
|
685
|
+
const variant =
|
|
686
|
+
httpStatus === 403
|
|
687
|
+
? "permission"
|
|
688
|
+
: httpStatus !== undefined && httpStatus >= 500
|
|
689
|
+
? "server"
|
|
690
|
+
: undefined;
|
|
691
|
+
return renderCode(
|
|
692
|
+
"PRH-STATUS-WRITE-FAILED",
|
|
693
|
+
{
|
|
694
|
+
detail:
|
|
695
|
+
httpStatus === undefined
|
|
696
|
+
? "the request did not complete"
|
|
697
|
+
: `HTTP ${httpStatus}`,
|
|
698
|
+
runId,
|
|
699
|
+
runUrl,
|
|
700
|
+
},
|
|
701
|
+
"action",
|
|
702
|
+
variant,
|
|
703
|
+
);
|
|
704
|
+
}
|
|
705
|
+
|
|
706
|
+
// Posts the comment, then deletes the older outcome comments of the same author.
|
|
707
|
+
// Returns the failure of the POST only: the cleanup is best effort, because the
|
|
708
|
+
// fresh comment has already landed and one stale duplicate is the safe error.
|
|
709
|
+
// The comment's POST alone, with the id and author GitHub answers with. Either is
|
|
710
|
+
// undefined when the answer is unreadable, and then nothing is cleaned up.
|
|
711
|
+
function createComment(
|
|
712
|
+
deps: PublishDeps,
|
|
713
|
+
repo: string,
|
|
714
|
+
pr: number,
|
|
715
|
+
body: string,
|
|
716
|
+
):
|
|
717
|
+
| { ok: true; newId: number | undefined; author: string | undefined }
|
|
718
|
+
| { ok: false; detail: string } {
|
|
719
|
+
const created = deps.gh(
|
|
720
|
+
[
|
|
721
|
+
"api",
|
|
722
|
+
"--method",
|
|
723
|
+
"POST",
|
|
724
|
+
`repos/${repo}/issues/${pr}/comments`,
|
|
725
|
+
"-F",
|
|
726
|
+
"body=@-",
|
|
727
|
+
"--jq",
|
|
728
|
+
"[.id, .user.login] | @json",
|
|
729
|
+
],
|
|
730
|
+
body,
|
|
731
|
+
);
|
|
732
|
+
if (created.exitCode !== 0) {
|
|
733
|
+
const status = httpStatusOf(created);
|
|
734
|
+
return {
|
|
735
|
+
ok: false,
|
|
736
|
+
detail: status === undefined ? "no HTTP response" : `HTTP ${status}`,
|
|
737
|
+
};
|
|
738
|
+
}
|
|
739
|
+
const [newId, author] = parseCreated(created.stdout);
|
|
740
|
+
return { ok: true, newId, author };
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
// Posts the comment, then deletes the older outcome comments of the same author.
|
|
744
|
+
// Returns the failure of the POST only: the cleanup is best effort, because the
|
|
745
|
+
// fresh comment has already landed and one stale duplicate is the safe error.
|
|
746
|
+
function postComment(
|
|
747
|
+
deps: PublishDeps,
|
|
748
|
+
plan: Extract<Publication, { kind: "remote" }>,
|
|
749
|
+
): { ok: true } | { ok: false; detail: string } {
|
|
750
|
+
const repo = plan.repository;
|
|
751
|
+
const created = createComment(deps, repo, plan.pr, plan.commentBody);
|
|
752
|
+
if (!created.ok) return created;
|
|
753
|
+
const { newId, author } = created;
|
|
754
|
+
if (newId === undefined || author === undefined || author === "") {
|
|
755
|
+
// WHY nothing is deleted: without a known author, "the same author" is not
|
|
756
|
+
// a test this step can pass, so an old comment is never removed on a guess.
|
|
757
|
+
return { ok: true };
|
|
758
|
+
}
|
|
759
|
+
deleteOlderOutcomes(
|
|
760
|
+
deps,
|
|
761
|
+
repo,
|
|
762
|
+
plan.pr,
|
|
763
|
+
newId,
|
|
764
|
+
author,
|
|
765
|
+
currentRunOrder(plan.runId, plan.runAttempt),
|
|
766
|
+
);
|
|
767
|
+
return { ok: true };
|
|
768
|
+
}
|
|
769
|
+
|
|
770
|
+
function parseCreated(
|
|
771
|
+
stdout: string,
|
|
772
|
+
): [number | undefined, string | undefined] {
|
|
773
|
+
try {
|
|
774
|
+
const value: unknown = JSON.parse(stdout.trim());
|
|
775
|
+
if (
|
|
776
|
+
Array.isArray(value) &&
|
|
777
|
+
typeof value[0] === "number" &&
|
|
778
|
+
typeof value[1] === "string"
|
|
779
|
+
) {
|
|
780
|
+
return [value[0], value[1]];
|
|
781
|
+
}
|
|
782
|
+
} catch {
|
|
783
|
+
// Fall through: an unreadable answer is treated as an unknown author.
|
|
784
|
+
}
|
|
785
|
+
return [undefined, undefined];
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
// One comment, as the listing's --jq filter prints it.
|
|
789
|
+
interface ListedComment {
|
|
790
|
+
id: number;
|
|
791
|
+
login: string;
|
|
792
|
+
type: string;
|
|
793
|
+
firstLine: string;
|
|
794
|
+
}
|
|
795
|
+
|
|
796
|
+
// Every comment on the PR, in the order the API lists them. undefined when the
|
|
797
|
+
// listing call fails, so each caller words that failure for its own path.
|
|
798
|
+
function listComments(
|
|
799
|
+
deps: PublishDeps,
|
|
800
|
+
repo: string,
|
|
801
|
+
pr: number,
|
|
802
|
+
): ListedComment[] | undefined {
|
|
803
|
+
const listed = deps.gh([
|
|
804
|
+
"api",
|
|
805
|
+
"--paginate",
|
|
806
|
+
`repos/${repo}/issues/${pr}/comments`,
|
|
807
|
+
"--jq",
|
|
808
|
+
'.[] | [.id, .user.login, .user.type, (.body | split("\\n") | .[0])] | @json',
|
|
809
|
+
]);
|
|
810
|
+
if (listed.exitCode !== 0) return undefined;
|
|
811
|
+
const rows: ListedComment[] = [];
|
|
812
|
+
for (const line of listed.stdout.split("\n")) {
|
|
813
|
+
if (line.trim() === "") continue;
|
|
814
|
+
let row: unknown;
|
|
815
|
+
try {
|
|
816
|
+
row = JSON.parse(line);
|
|
817
|
+
} catch {
|
|
818
|
+
continue;
|
|
819
|
+
}
|
|
820
|
+
if (!Array.isArray(row)) continue;
|
|
821
|
+
const [id, login, type, firstLine] = row;
|
|
822
|
+
if (typeof id !== "number" || typeof firstLine !== "string") continue;
|
|
823
|
+
rows.push({
|
|
824
|
+
id,
|
|
825
|
+
login: typeof login === "string" ? login : "",
|
|
826
|
+
type: typeof type === "string" ? type : "",
|
|
827
|
+
firstLine,
|
|
828
|
+
});
|
|
829
|
+
}
|
|
830
|
+
return rows;
|
|
831
|
+
}
|
|
832
|
+
|
|
833
|
+
// WHY only the outcome marker counts: an override comment is the audit trail of a
|
|
834
|
+
// human's unlock (S8). Its marker starts `<!-- crysk-override` (or the legacy
|
|
835
|
+
// `<!-- pr-hero-override`), which does not
|
|
836
|
+
// start with MARKER_PREFIX, so no cleanup or older-run pass can remove it.
|
|
837
|
+
function isOutcomeComment(row: ListedComment): boolean {
|
|
838
|
+
return matchMarkerPrefix(row.firstLine, MARKER_PREFIX) !== undefined;
|
|
839
|
+
}
|
|
840
|
+
|
|
841
|
+
// Deletes each comment. A failed delete is a warning, and the rest still run.
|
|
842
|
+
function deleteComments(
|
|
843
|
+
deps: PublishDeps,
|
|
844
|
+
repo: string,
|
|
845
|
+
pr: number,
|
|
846
|
+
ids: readonly number[],
|
|
847
|
+
): void {
|
|
848
|
+
for (const id of ids) {
|
|
849
|
+
const removed = deps.gh([
|
|
850
|
+
"api",
|
|
851
|
+
"--method",
|
|
852
|
+
"DELETE",
|
|
853
|
+
`repos/${repo}/issues/comments/${id}`,
|
|
854
|
+
]);
|
|
855
|
+
if (removed.exitCode !== 0) {
|
|
856
|
+
deps.stdout(
|
|
857
|
+
workflowCommand(
|
|
858
|
+
"warning",
|
|
859
|
+
`Crysk could not delete an older outcome comment (${id}) on #${pr}.`,
|
|
860
|
+
),
|
|
861
|
+
);
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
}
|
|
865
|
+
|
|
866
|
+
// A positive integer, or undefined. Run ids and the ids in markers are read here.
|
|
867
|
+
function positiveInteger(text: string): number | undefined {
|
|
868
|
+
if (!/^\d+$/.test(text)) return undefined;
|
|
869
|
+
const value = Number(text);
|
|
870
|
+
return Number.isSafeInteger(value) && value > 0 ? value : undefined;
|
|
871
|
+
}
|
|
872
|
+
|
|
873
|
+
// A run's place in the order: its run id, then its attempt within that run.
|
|
874
|
+
interface RunOrder {
|
|
875
|
+
runId: number;
|
|
876
|
+
attempt: number;
|
|
877
|
+
}
|
|
878
|
+
|
|
879
|
+
// WHY the guard: run ids increase within a repository, so this run's place is
|
|
880
|
+
// its id. A run id that is not a safe positive integer has no place, and then no
|
|
881
|
+
// comment can be proven older than this run, so the caller keeps them all.
|
|
882
|
+
function currentRunOrder(runId: string, attempt: number): RunOrder | undefined {
|
|
883
|
+
const id = positiveInteger(runId);
|
|
884
|
+
return id === undefined ? undefined : { runId: id, attempt };
|
|
885
|
+
}
|
|
886
|
+
|
|
887
|
+
// The place a marker names. A marker with no positive run id has no place, so
|
|
888
|
+
// it is never deleted. A marker with no parseable attempt is attempt 1: every
|
|
889
|
+
// marker written before attempts were recorded is the first attempt of its run.
|
|
890
|
+
function markerRunOrder(firstLine: string): RunOrder | undefined {
|
|
891
|
+
const runId = positiveInteger(/\brun=(\S+)/.exec(firstLine)?.[1] ?? "");
|
|
892
|
+
if (runId === undefined) return undefined;
|
|
893
|
+
const attempt =
|
|
894
|
+
positiveInteger(/\battempt=(\S+)/.exec(firstLine)?.[1] ?? "") ?? 1;
|
|
895
|
+
return { runId, attempt };
|
|
896
|
+
}
|
|
897
|
+
|
|
898
|
+
// WHY the order: a comment from a run that is not older than this one was
|
|
899
|
+
// posted by a run still going, or by a newer one, and neither is this run's to
|
|
900
|
+
// remove. A re-run keeps the run id, so within one run the attempt decides:
|
|
901
|
+
// only an earlier attempt is older. Comment ids alone are not enough: a later
|
|
902
|
+
// run can post before this one, and its comment then has the lower id.
|
|
903
|
+
function isOlderRunComment(
|
|
904
|
+
row: ListedComment,
|
|
905
|
+
current: RunOrder | undefined,
|
|
906
|
+
): boolean {
|
|
907
|
+
if (current === undefined) return false;
|
|
908
|
+
const theirs = markerRunOrder(row.firstLine);
|
|
909
|
+
if (theirs === undefined) return false;
|
|
910
|
+
if (theirs.runId !== current.runId) return theirs.runId < current.runId;
|
|
911
|
+
return theirs.attempt < current.attempt;
|
|
912
|
+
}
|
|
913
|
+
|
|
914
|
+
function deleteOlderOutcomes(
|
|
915
|
+
deps: PublishDeps,
|
|
916
|
+
repo: string,
|
|
917
|
+
pr: number,
|
|
918
|
+
newId: number,
|
|
919
|
+
author: string,
|
|
920
|
+
current: RunOrder | undefined,
|
|
921
|
+
): void {
|
|
922
|
+
const rows = listComments(deps, repo, pr);
|
|
923
|
+
if (rows === undefined) {
|
|
924
|
+
deps.stdout(
|
|
925
|
+
workflowCommand(
|
|
926
|
+
"warning",
|
|
927
|
+
`Crysk could not list the older outcome comments on #${pr}; they were left in place.`,
|
|
928
|
+
),
|
|
929
|
+
);
|
|
930
|
+
return;
|
|
931
|
+
}
|
|
932
|
+
deleteComments(
|
|
933
|
+
deps,
|
|
934
|
+
repo,
|
|
935
|
+
pr,
|
|
936
|
+
rows
|
|
937
|
+
.filter(
|
|
938
|
+
(row) =>
|
|
939
|
+
row.id < newId &&
|
|
940
|
+
row.login === author &&
|
|
941
|
+
isOutcomeComment(row) &&
|
|
942
|
+
isOlderRunComment(row, current),
|
|
943
|
+
)
|
|
944
|
+
.map((row) => row.id),
|
|
945
|
+
);
|
|
946
|
+
}
|
|
947
|
+
|
|
948
|
+
// WHY cleanup runs once a head settles: a completed review (runPublish's cleanup
|
|
949
|
+
// branch), or an override whose success status was written (publishOverride). An
|
|
950
|
+
// older outcome comment says a run could not start the review. A later run has now
|
|
951
|
+
// settled the head, so that comment contradicts the PR, and nothing else removes it.
|
|
952
|
+
// Only older runs' and earlier attempts' comments go (see isOlderRunComment), and
|
|
953
|
+
// the override's audit comments and the report summary never match isOutcomeComment.
|
|
954
|
+
//
|
|
955
|
+
// WHY the identity chain: the older comments were written with the workflow's
|
|
956
|
+
// token, which cannot read /user. GraphQL `viewer` names the login that wrote
|
|
957
|
+
// them, but whether it answers for this token is not verified here, so the Bot
|
|
958
|
+
// type is the fallback: the workflow token posts as github-actions[bot], and a
|
|
959
|
+
// person is never a Bot. GraphQL spells that account `github-actions` while REST
|
|
960
|
+
// adds the `[bot]` suffix, so a REST login matches the viewer login or that
|
|
961
|
+
// login with the suffix. Any other Bot login is kept. A person's comment is
|
|
962
|
+
// never deleted on a guess: with no login, only a Bot is deleted.
|
|
963
|
+
function cleanupOlderOutcomes(
|
|
964
|
+
deps: PublishDeps,
|
|
965
|
+
plan: { repository: string; pr: number; runId: string; runAttempt: number },
|
|
966
|
+
): void {
|
|
967
|
+
const current = currentRunOrder(plan.runId, plan.runAttempt);
|
|
968
|
+
const viewer = deps.gh([
|
|
969
|
+
"api",
|
|
970
|
+
"graphql",
|
|
971
|
+
"-f",
|
|
972
|
+
"query=query { viewer { login } }",
|
|
973
|
+
"--jq",
|
|
974
|
+
".data.viewer.login",
|
|
975
|
+
]);
|
|
976
|
+
const login = viewer.exitCode === 0 ? viewer.stdout.trim() : "";
|
|
977
|
+
const rows = listComments(deps, plan.repository, plan.pr);
|
|
978
|
+
if (rows === undefined) {
|
|
979
|
+
deps.stdout(
|
|
980
|
+
workflowCommand(
|
|
981
|
+
"notice",
|
|
982
|
+
`Crysk could not clean up its older outcome comments on #${plan.pr}; they were left in place.`,
|
|
983
|
+
),
|
|
984
|
+
);
|
|
985
|
+
return;
|
|
986
|
+
}
|
|
987
|
+
const isMine = (row: ListedComment): boolean =>
|
|
988
|
+
login !== ""
|
|
989
|
+
? row.login === login || row.login === `${login}[bot]`
|
|
990
|
+
: row.type === "Bot";
|
|
991
|
+
deleteComments(
|
|
992
|
+
deps,
|
|
993
|
+
plan.repository,
|
|
994
|
+
plan.pr,
|
|
995
|
+
rows
|
|
996
|
+
.filter(
|
|
997
|
+
(row) =>
|
|
998
|
+
isOutcomeComment(row) &&
|
|
999
|
+
isMine(row) &&
|
|
1000
|
+
isOlderRunComment(row, current),
|
|
1001
|
+
)
|
|
1002
|
+
.map((row) => row.id),
|
|
1003
|
+
);
|
|
1004
|
+
}
|
|
1005
|
+
|
|
1006
|
+
function readIfExists(filePath: string): string | undefined {
|
|
1007
|
+
return existsSync(filePath) ? readFileSync(filePath, "utf8") : undefined;
|
|
1008
|
+
}
|
|
1009
|
+
|
|
1010
|
+
// The CLI's own record. Absent, unreadable or malformed all read as no record,
|
|
1011
|
+
// so the caller's one rule covers each of them. No gh call.
|
|
1012
|
+
function readCliRecord(dir: string): CliRecord | undefined {
|
|
1013
|
+
try {
|
|
1014
|
+
return parseCliRecord(
|
|
1015
|
+
readFileSync(path.join(dir, CLI_RECORD_FILE), "utf8"),
|
|
1016
|
+
);
|
|
1017
|
+
} catch {
|
|
1018
|
+
return undefined;
|
|
1019
|
+
}
|
|
1020
|
+
}
|
|
1021
|
+
|
|
1022
|
+
const SETTLED_STATES: ReadonlySet<string> = new Set([
|
|
1023
|
+
"success",
|
|
1024
|
+
"error",
|
|
1025
|
+
"failure",
|
|
1026
|
+
]);
|
|
1027
|
+
|
|
1028
|
+
// The result of reading the head's statuses. A failed read and an empty answer are different
|
|
1029
|
+
// things: an empty answer says the head has no statuses, a failed read says nothing about them.
|
|
1030
|
+
type HeadStatusRead = { ok: true; statuses: HeadStatusRow[] } | { ok: false };
|
|
1031
|
+
|
|
1032
|
+
// A status row as the publisher reads it: the override fact, and the target_url the cancel settle
|
|
1033
|
+
// matches a pending by (SB3 follow-up). Absent when the row has none.
|
|
1034
|
+
type HeadStatusRow = OverrideStatusFact & { targetUrl?: string };
|
|
1035
|
+
|
|
1036
|
+
// The head's statuses, as the combined-status endpoint returns them, read through this step's
|
|
1037
|
+
// own gh seam in one attempt. A failed gh call is { ok: false }. WHY per_page=100: the combined
|
|
1038
|
+
// endpoint pages at 30 contexts by default, and a busy head's pr-hero row can sit past the
|
|
1039
|
+
// first page. The description is read too, for the override rule.
|
|
1040
|
+
//
|
|
1041
|
+
// A malformed row is dropped and the rest are read, for both callers (the override check before a
|
|
1042
|
+
// repost, and the reviewing path). WHY: a corrupt row must never turn a settled head into an error
|
|
1043
|
+
// overwrite, and a valid override on the head must not be hidden by a garbage line next to it. Only
|
|
1044
|
+
// a failed gh call is an unknown read.
|
|
1045
|
+
//
|
|
1046
|
+
// WHY one retry (after 2 s): both callers fall back when this read fails (the repost goes ahead, or
|
|
1047
|
+
// the cancel settle leaves the head as it is). A blip on one read is common, and the retry narrows
|
|
1048
|
+
// that window; the fallback itself is unchanged, and so is its warning when the retry fails too.
|
|
1049
|
+
function readHeadStatuses(
|
|
1050
|
+
deps: PublishDeps,
|
|
1051
|
+
repo: string,
|
|
1052
|
+
sha: string,
|
|
1053
|
+
): HeadStatusRead {
|
|
1054
|
+
let read = readHeadStatusesOnce(deps, repo, sha);
|
|
1055
|
+
if (!read.ok) {
|
|
1056
|
+
(deps.sleep ?? ((ms: number) => Bun.sleepSync(ms)))(
|
|
1057
|
+
STATUS_READ_RETRY_DELAY_MS,
|
|
1058
|
+
);
|
|
1059
|
+
read = readHeadStatusesOnce(deps, repo, sha);
|
|
1060
|
+
}
|
|
1061
|
+
return read;
|
|
1062
|
+
}
|
|
1063
|
+
|
|
1064
|
+
function readHeadStatusesOnce(
|
|
1065
|
+
deps: PublishDeps,
|
|
1066
|
+
repo: string,
|
|
1067
|
+
sha: string,
|
|
1068
|
+
): HeadStatusRead {
|
|
1069
|
+
const read = deps.gh([
|
|
1070
|
+
"api",
|
|
1071
|
+
`repos/${repo}/commits/${sha}/status?per_page=100`,
|
|
1072
|
+
"--jq",
|
|
1073
|
+
".statuses[]? | [.context, .state, .created_at, .description, .target_url] | @json",
|
|
1074
|
+
]);
|
|
1075
|
+
if (read.exitCode !== 0) return { ok: false };
|
|
1076
|
+
const statuses: HeadStatusRow[] = [];
|
|
1077
|
+
for (const line of read.stdout.split("\n")) {
|
|
1078
|
+
if (line.trim() === "") continue;
|
|
1079
|
+
const row = parseStatusRow(line);
|
|
1080
|
+
if (row === undefined) continue;
|
|
1081
|
+
statuses.push(row);
|
|
1082
|
+
}
|
|
1083
|
+
return { ok: true, statuses };
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
// One --jq row as a status row, or undefined when the line does not parse to one.
|
|
1087
|
+
function parseStatusRow(line: string): HeadStatusRow | undefined {
|
|
1088
|
+
let value: unknown;
|
|
1089
|
+
try {
|
|
1090
|
+
value = JSON.parse(line);
|
|
1091
|
+
} catch {
|
|
1092
|
+
return undefined;
|
|
1093
|
+
}
|
|
1094
|
+
if (!Array.isArray(value)) return undefined;
|
|
1095
|
+
const [context, state, createdAt, description, targetUrl] = value;
|
|
1096
|
+
if (
|
|
1097
|
+
typeof context !== "string" ||
|
|
1098
|
+
typeof state !== "string" ||
|
|
1099
|
+
typeof createdAt !== "string"
|
|
1100
|
+
) {
|
|
1101
|
+
return undefined;
|
|
1102
|
+
}
|
|
1103
|
+
return {
|
|
1104
|
+
context,
|
|
1105
|
+
state,
|
|
1106
|
+
created_at: createdAt,
|
|
1107
|
+
description: typeof description === "string" ? description : undefined,
|
|
1108
|
+
...(typeof targetUrl === "string" ? { targetUrl } : {}),
|
|
1109
|
+
};
|
|
1110
|
+
}
|
|
1111
|
+
|
|
1112
|
+
// The head's newest pr-hero status, as the three states the table needs. The newest row is
|
|
1113
|
+
// picked by its time, not by its place in the answer. A failed read, no pr-hero row, or a
|
|
1114
|
+
// state the table does not name is unknown, which the table answers with the status.
|
|
1115
|
+
// Copies the verdict of a half-landed pair onto the name that missed it (strandedStatusPair).
|
|
1116
|
+
// The stuck name may be the repository's required check, and the readers below take the
|
|
1117
|
+
// newest row across both names, so nothing else would ever settle it. Only the stuck name
|
|
1118
|
+
// is written: the verdict, which may be a human's override, is never replaced. A failed
|
|
1119
|
+
// copy is a warning, and the run goes on as it would have. Returns the verdict whenever
|
|
1120
|
+
// the pair is stranded, copied or not: it, not the stuck pending row, is the gate's state,
|
|
1121
|
+
// and the readers' newest-row pick could still choose the stuck row on a same-second tie.
|
|
1122
|
+
function repairStrandedPair(
|
|
1123
|
+
deps: PublishDeps,
|
|
1124
|
+
repo: string,
|
|
1125
|
+
sha: string,
|
|
1126
|
+
statuses: readonly HeadStatusRow[],
|
|
1127
|
+
): HeadStatusRow | undefined {
|
|
1128
|
+
const pair = strandedStatusPair(statuses, STATUS_CONTEXT);
|
|
1129
|
+
if (pair === undefined) return undefined;
|
|
1130
|
+
const state = pair.verdict.state;
|
|
1131
|
+
const description = pair.verdict.description;
|
|
1132
|
+
// A verdict this step cannot reproduce faithfully is not copied: a blank description
|
|
1133
|
+
// would not even read as the override it may have been.
|
|
1134
|
+
if (
|
|
1135
|
+
(state !== "success" && state !== "failure" && state !== "error") ||
|
|
1136
|
+
typeof description !== "string" ||
|
|
1137
|
+
description === ""
|
|
1138
|
+
) {
|
|
1139
|
+
return pair.verdict;
|
|
1140
|
+
}
|
|
1141
|
+
const posted = postStatusOn(
|
|
1142
|
+
deps,
|
|
1143
|
+
repo,
|
|
1144
|
+
sha,
|
|
1145
|
+
{
|
|
1146
|
+
state,
|
|
1147
|
+
context: STATUS_CONTEXT,
|
|
1148
|
+
description,
|
|
1149
|
+
targetUrl: pair.verdict.targetUrl ?? actionsRunUrl(deps.env) ?? "",
|
|
1150
|
+
},
|
|
1151
|
+
pair.stranded.context,
|
|
1152
|
+
2,
|
|
1153
|
+
);
|
|
1154
|
+
deps.stdout(
|
|
1155
|
+
posted.ok
|
|
1156
|
+
? workflowCommand(
|
|
1157
|
+
"notice",
|
|
1158
|
+
`the ${pair.stranded.context} status missed the ${pair.verdict.context} verdict; copied it`,
|
|
1159
|
+
)
|
|
1160
|
+
: workflowCommand(
|
|
1161
|
+
"warning",
|
|
1162
|
+
`the ${pair.stranded.context} status is still pending behind the ${pair.verdict.context} verdict; copying it failed`,
|
|
1163
|
+
),
|
|
1164
|
+
);
|
|
1165
|
+
return pair.verdict;
|
|
1166
|
+
}
|
|
1167
|
+
|
|
1168
|
+
function readLatestCliStatusState(
|
|
1169
|
+
deps: PublishDeps,
|
|
1170
|
+
repo: string,
|
|
1171
|
+
sha: string,
|
|
1172
|
+
): LatestStatusState {
|
|
1173
|
+
const read = readHeadStatuses(deps, repo, sha);
|
|
1174
|
+
if (!read.ok) return "unknown";
|
|
1175
|
+
const latest =
|
|
1176
|
+
repairStrandedPair(deps, repo, sha, read.statuses) ??
|
|
1177
|
+
newestStatusInContext(read.statuses, STATUS_CONTEXT);
|
|
1178
|
+
if (latest === undefined) return "unknown";
|
|
1179
|
+
if (latest.state === "pending") return "pending";
|
|
1180
|
+
return SETTLED_STATES.has(latest.state) ? "settled" : "unknown";
|
|
1181
|
+
}
|
|
1182
|
+
|
|
1183
|
+
// Whether an override posted during the run stands over the repost of the CLI's unsettled
|
|
1184
|
+
// status (overrideStandsOverRun, src/ci/override.ts). WHY it is read here: the CLI's final
|
|
1185
|
+
// write failed, so its own status reads may have failed too, and the override may not have
|
|
1186
|
+
// been seen by the CLI. A record with no cutoff to judge against is no evidence of an override,
|
|
1187
|
+
// so the repost goes ahead as before. A failed or corrupt read is also no evidence, so the repost
|
|
1188
|
+
// goes ahead too, but it is logged as a warning: the override check could not read the head. A
|
|
1189
|
+
// malformed row is not a failed read: it is skipped, and the valid rows decide, so a fresh override
|
|
1190
|
+
// beside a garbage row stands.
|
|
1191
|
+
// WHY the warning: a repost over a head we could not read is the tradeoff, and it must be visible.
|
|
1192
|
+
// The status reposted is the review's own verdict, so a failed review's failure is reposted as a failure.
|
|
1193
|
+
//
|
|
1194
|
+
// WHY the cutoff is the CLI's and not re-derived here: the CLI computed it once for the run,
|
|
1195
|
+
// and it knows whether its own pending landed, which decides the clock-skew allowance (the
|
|
1196
|
+
// landed decision is carried by the cutoff, src/ci/override.ts overrideCutoffMs). The publisher
|
|
1197
|
+
// has neither the pending's outcome nor the allowance, so it compares with the cutoff as given.
|
|
1198
|
+
function overrideStandsBeforeRepost(
|
|
1199
|
+
deps: PublishDeps,
|
|
1200
|
+
repo: string,
|
|
1201
|
+
sha: string,
|
|
1202
|
+
overrideCutoffMs: number | undefined,
|
|
1203
|
+
): boolean {
|
|
1204
|
+
if (overrideCutoffMs === undefined) return false;
|
|
1205
|
+
const read = readHeadStatuses(deps, repo, sha);
|
|
1206
|
+
if (!read.ok) {
|
|
1207
|
+
deps.stdout(
|
|
1208
|
+
workflowCommand(
|
|
1209
|
+
"warning",
|
|
1210
|
+
`crysk: could not read the statuses of ${sha.slice(0, 7)}, so an override on it cannot be checked; the review's status is reposted as planned`,
|
|
1211
|
+
),
|
|
1212
|
+
);
|
|
1213
|
+
return false;
|
|
1214
|
+
}
|
|
1215
|
+
return overrideStandsOverRun(read.statuses, overrideCutoffMs);
|
|
1216
|
+
}
|
|
1217
|
+
|
|
1218
|
+
// The override's reporter: the shared rendering (statusWriteFailure), reported.
|
|
1219
|
+
function reportStatusWriteFailure(
|
|
1220
|
+
deps: PublishDeps,
|
|
1221
|
+
httpStatus: number | undefined,
|
|
1222
|
+
runId: string,
|
|
1223
|
+
runUrl: string,
|
|
1224
|
+
): void {
|
|
1225
|
+
report(deps, statusWriteFailure(httpStatus, runId, runUrl));
|
|
1226
|
+
}
|
|
1227
|
+
|
|
1228
|
+
// The one status an override writes. Its description starts with OVERRIDE_DESCRIPTION_PREFIX,
|
|
1229
|
+
// the text the override rule (src/ci/override.ts) recognises. Its description names the actor, so the
|
|
1230
|
+
// status alone says who unlocked the gate.
|
|
1231
|
+
function overrideDescription(actor: string): string {
|
|
1232
|
+
return `${OVERRIDE_DESCRIPTION_PREFIX}${actor} — no new review run`;
|
|
1233
|
+
}
|
|
1234
|
+
|
|
1235
|
+
// The audit comment. Its first line is the marker, which is not an outcome marker,
|
|
1236
|
+
// so no cleanup removes it. Its wording follows the status: a comment that claimed
|
|
1237
|
+
// an override the status never received would be a false record.
|
|
1238
|
+
function overrideCommentBody(
|
|
1239
|
+
plan: Extract<Publication, { kind: "override" }>,
|
|
1240
|
+
landed: readonly string[],
|
|
1241
|
+
at: Date,
|
|
1242
|
+
): string {
|
|
1243
|
+
const when = `${at.toISOString().slice(0, 16).replace("T", " ")} UTC`;
|
|
1244
|
+
const marker = `${MARKER_OPEN}override head=${plan.headSha ?? "unknown"} run=${plan.runId} attempt=${plan.runAttempt} -->`;
|
|
1245
|
+
if (landed.length > 0 && plan.headSha !== undefined) {
|
|
1246
|
+
// Name exactly the contexts that were written: a branch protection that requires the
|
|
1247
|
+
// other one is still blocked, and this comment is never cleaned up (#435 F001).
|
|
1248
|
+
const names = landed.map((context) => `\`${context}\``).join(" and ");
|
|
1249
|
+
const missed = statusContextForms(STATUS_CONTEXT).filter(
|
|
1250
|
+
(context) => !landed.includes(context),
|
|
1251
|
+
);
|
|
1252
|
+
const missedNote =
|
|
1253
|
+
missed.length === 0
|
|
1254
|
+
? ""
|
|
1255
|
+
: ` The ${missed.map((context) => `\`${context}\``).join(" and ")} status could not be written, so a rule that requires it is still blocked: comment ${commandHint(OVERRIDE_COMMAND)} again to retry.`;
|
|
1256
|
+
return [
|
|
1257
|
+
marker,
|
|
1258
|
+
"",
|
|
1259
|
+
(landed.includes(STATUS_CONTEXT)
|
|
1260
|
+
? `Crysk's gate was overridden by @${plan.actor} on head \`${plan.headSha}\` at ${when}. `
|
|
1261
|
+
: `@${plan.actor}'s override of Crysk's gate on head \`${plan.headSha}\` at ${when} landed only in part. `) +
|
|
1262
|
+
`The ${names} status${landed.length > 1 ? "es" : ""} on that head ${landed.length > 1 ? "are" : "is"} now success, with no review of this head.` +
|
|
1263
|
+
missedNote +
|
|
1264
|
+
` A new push or a later ${commandHint(REVIEW_COMMAND)} on this head replaces it.`,
|
|
1265
|
+
].join("\n");
|
|
1266
|
+
}
|
|
1267
|
+
const where =
|
|
1268
|
+
plan.headSha === undefined
|
|
1269
|
+
? ", because the head of this pull request is unknown"
|
|
1270
|
+
: ` on head \`${plan.headSha}\``;
|
|
1271
|
+
return [
|
|
1272
|
+
marker,
|
|
1273
|
+
"",
|
|
1274
|
+
`@${plan.actor} asked to override Crysk's gate at ${when}, but no commit status could be set${where}, so the gate is unchanged. Comment ${commandHint(OVERRIDE_COMMAND)} again to retry.`,
|
|
1275
|
+
].join("\n");
|
|
1276
|
+
}
|
|
1277
|
+
|
|
1278
|
+
// WHY the status goes first and the comment's wording follows it: the audit
|
|
1279
|
+
// comment must never claim an override that the status did not receive. The
|
|
1280
|
+
// cleanup of older outcome comments runs only after a written status (below), the
|
|
1281
|
+
// same settle a completed review does; a failed status leaves them in place.
|
|
1282
|
+
function publishOverride(
|
|
1283
|
+
deps: PublishDeps,
|
|
1284
|
+
plan: Extract<Publication, { kind: "override" }>,
|
|
1285
|
+
): number {
|
|
1286
|
+
const landed: string[] = [];
|
|
1287
|
+
let exitCode = 0;
|
|
1288
|
+
if (plan.headSha !== undefined) {
|
|
1289
|
+
// Each context is written on its own (postStatus would fold the pair into one answer):
|
|
1290
|
+
// when one lands and the other fails, the head IS overridden on the one that landed, and
|
|
1291
|
+
// the permanent audit comment must say so (#434 F001). The failed half is still reported.
|
|
1292
|
+
let failure: { ok: false; httpStatus: number | undefined } | undefined;
|
|
1293
|
+
for (const context of statusContextForms(STATUS_CONTEXT)) {
|
|
1294
|
+
const posted = postStatusOn(
|
|
1295
|
+
deps,
|
|
1296
|
+
plan.repository,
|
|
1297
|
+
plan.headSha,
|
|
1298
|
+
{
|
|
1299
|
+
state: "success",
|
|
1300
|
+
context: STATUS_CONTEXT,
|
|
1301
|
+
description: overrideDescription(plan.actor),
|
|
1302
|
+
targetUrl: plan.runUrl,
|
|
1303
|
+
},
|
|
1304
|
+
context,
|
|
1305
|
+
2,
|
|
1306
|
+
);
|
|
1307
|
+
if (posted.ok) landed.push(context);
|
|
1308
|
+
else failure ??= posted;
|
|
1309
|
+
}
|
|
1310
|
+
if (landed.includes(STATUS_CONTEXT)) {
|
|
1311
|
+
// WHY here, right after the success and before the audit comment: the crysk gate is
|
|
1312
|
+
// green now, so an earlier run's outcome comment contradicts it. When only the legacy
|
|
1313
|
+
// pr-hero status landed, crysk is still red and that comment still explains why. The audit comment does not
|
|
1314
|
+
// gate this cleanup, and its warnings never change the exit code (as for a review).
|
|
1315
|
+
cleanupOlderOutcomes(deps, plan);
|
|
1316
|
+
}
|
|
1317
|
+
if (failure !== undefined) {
|
|
1318
|
+
reportStatusWriteFailure(
|
|
1319
|
+
deps,
|
|
1320
|
+
failure.httpStatus,
|
|
1321
|
+
plan.runId,
|
|
1322
|
+
plan.runUrl,
|
|
1323
|
+
);
|
|
1324
|
+
exitCode = 1;
|
|
1325
|
+
}
|
|
1326
|
+
}
|
|
1327
|
+
deps.stdout(
|
|
1328
|
+
workflowCommand(
|
|
1329
|
+
"notice",
|
|
1330
|
+
landed.includes(STATUS_CONTEXT)
|
|
1331
|
+
? `crysk gate overridden by @${plan.actor} on head ${plan.headSha}.`
|
|
1332
|
+
: landed.length > 0
|
|
1333
|
+
? `crysk override by @${plan.actor} set only the ${landed.join(", ")} status on head ${plan.headSha}.`
|
|
1334
|
+
: `crysk override by @${plan.actor} set no commit status.`,
|
|
1335
|
+
),
|
|
1336
|
+
);
|
|
1337
|
+
const at = (deps.now ?? (() => new Date()))();
|
|
1338
|
+
const comment = createComment(
|
|
1339
|
+
deps,
|
|
1340
|
+
plan.repository,
|
|
1341
|
+
plan.pr,
|
|
1342
|
+
overrideCommentBody(plan, landed, at),
|
|
1343
|
+
);
|
|
1344
|
+
if (!comment.ok) {
|
|
1345
|
+
deps.stdout(
|
|
1346
|
+
workflowCommand(
|
|
1347
|
+
"error",
|
|
1348
|
+
`Crysk could not post its override comment on #${plan.pr} (${comment.detail}).`,
|
|
1349
|
+
),
|
|
1350
|
+
);
|
|
1351
|
+
return 1;
|
|
1352
|
+
}
|
|
1353
|
+
return exitCode;
|
|
1354
|
+
}
|
|
1355
|
+
|
|
1356
|
+
export function runPublish(deps: PublishDeps): number {
|
|
1357
|
+
const dir = deps.env.CRYSK_OUTCOME_DIR ?? "";
|
|
1358
|
+
if (dir === "") return 0;
|
|
1359
|
+
const recordText = readIfExists(path.join(dir, RECORD_FILE));
|
|
1360
|
+
if (recordText === undefined) return 0;
|
|
1361
|
+
const cliStarted = existsSync(path.join(dir, CLI_STARTED_FILE));
|
|
1362
|
+
|
|
1363
|
+
const record = parseTriggerRecord(recordText);
|
|
1364
|
+
// Read with the record, so both files are judged in one place. The decision
|
|
1365
|
+
// uses it only for an install failure.
|
|
1366
|
+
const setupText = readIfExists(path.join(dir, SETUP_RECORD_FILE));
|
|
1367
|
+
const setup =
|
|
1368
|
+
setupText === undefined ? undefined : parseSetupRecord(setupText);
|
|
1369
|
+
if (record === undefined) {
|
|
1370
|
+
// A record that is present but unreadable is reported, never skipped: a
|
|
1371
|
+
// silent skip here is exactly the outcome this step exists to prevent.
|
|
1372
|
+
if (cliStarted) return 0;
|
|
1373
|
+
report(
|
|
1374
|
+
deps,
|
|
1375
|
+
renderCode("PRH-UNCLASSIFIED", { step: "the outcome record" }, "action"),
|
|
1376
|
+
);
|
|
1377
|
+
return 1;
|
|
1378
|
+
}
|
|
1379
|
+
// WHY: the record must describe this run. A mismatch means the directory is
|
|
1380
|
+
// not this job's, and nothing is posted on a PR on a guess. The attempt is
|
|
1381
|
+
// part of it: gh run rerun keeps the run id, so a record of an earlier attempt
|
|
1382
|
+
// can still be on disk. An absent or invalid attempt is attempt 1, the same
|
|
1383
|
+
// default the trigger writes.
|
|
1384
|
+
const runRepository = deps.env.GITHUB_REPOSITORY ?? "";
|
|
1385
|
+
const runId = deps.env.GITHUB_RUN_ID ?? "";
|
|
1386
|
+
const runAttempt = positiveInteger(deps.env.GITHUB_RUN_ATTEMPT ?? "") ?? 1;
|
|
1387
|
+
if (
|
|
1388
|
+
(runRepository !== "" && record.repository !== runRepository) ||
|
|
1389
|
+
(runId !== "" && record.run_id !== runId) ||
|
|
1390
|
+
record.run_attempt !== runAttempt
|
|
1391
|
+
) {
|
|
1392
|
+
deps.stdout(
|
|
1393
|
+
workflowCommand(
|
|
1394
|
+
"error",
|
|
1395
|
+
"Crysk's outcome record belongs to another run or attempt, so nothing was posted.",
|
|
1396
|
+
),
|
|
1397
|
+
);
|
|
1398
|
+
return 1;
|
|
1399
|
+
}
|
|
1400
|
+
|
|
1401
|
+
if (setupText !== undefined && setup === undefined) {
|
|
1402
|
+
deps.stdout(
|
|
1403
|
+
workflowCommand(
|
|
1404
|
+
"warning",
|
|
1405
|
+
"Crysk could not read its setup outcome record, so this failure is reported as an install failure.",
|
|
1406
|
+
),
|
|
1407
|
+
);
|
|
1408
|
+
}
|
|
1409
|
+
// WHY read only for a review the CLI started: no other outcome depends on the
|
|
1410
|
+
// CLI's record.
|
|
1411
|
+
const cliRecord =
|
|
1412
|
+
record.decision === "review" && cliStarted ? readCliRecord(dir) : undefined;
|
|
1413
|
+
// WHY the read is here, and only here (F001 and F002 on #330): the status decides
|
|
1414
|
+
// only for a reviewing record, the one case where this run holds the lock, and it is
|
|
1415
|
+
// read on the head that record names, never the trigger's. Every other outcome decides
|
|
1416
|
+
// without it, so it makes no call.
|
|
1417
|
+
const latestStatusState =
|
|
1418
|
+
cliRecord?.outcome === "reviewing" &&
|
|
1419
|
+
cliRecord.head_sha !== undefined &&
|
|
1420
|
+
record.pr !== undefined
|
|
1421
|
+
? readLatestCliStatusState(deps, record.repository, cliRecord.head_sha)
|
|
1422
|
+
: undefined;
|
|
1423
|
+
const plan = decidePublication({
|
|
1424
|
+
record,
|
|
1425
|
+
cliStarted,
|
|
1426
|
+
cliRecord,
|
|
1427
|
+
setup,
|
|
1428
|
+
latestStatusState,
|
|
1429
|
+
});
|
|
1430
|
+
if (plan.kind === "none") return 0;
|
|
1431
|
+
if (plan.kind === "cleanup") {
|
|
1432
|
+
// WHY the status goes first, and once: the CLI's final write failed after its review
|
|
1433
|
+
// was delivered, so the head's required check is still pending. This step is the
|
|
1434
|
+
// publisher's one more chance to settle it. One attempt, because the CLI already
|
|
1435
|
+
// tried twice. A failure is reported on one annotation line and the step exits 1,
|
|
1436
|
+
// but the cleanup still runs, since the review did complete.
|
|
1437
|
+
let exitCode = 0;
|
|
1438
|
+
if (plan.headSha !== undefined && plan.status !== undefined) {
|
|
1439
|
+
// WHY the override check comes before the repost (Fix 4): an override posted during this
|
|
1440
|
+
// run stands over the review's verdict, the same rule the CLI's final write applies
|
|
1441
|
+
// (src/ci/override.ts), against the cutoff the CLI recorded. Then the repost is skipped,
|
|
1442
|
+
// and the notice says why.
|
|
1443
|
+
if (
|
|
1444
|
+
overrideStandsBeforeRepost(
|
|
1445
|
+
deps,
|
|
1446
|
+
plan.repository,
|
|
1447
|
+
plan.headSha,
|
|
1448
|
+
plan.overrideCutoffMs,
|
|
1449
|
+
)
|
|
1450
|
+
) {
|
|
1451
|
+
deps.stdout(
|
|
1452
|
+
workflowCommand(
|
|
1453
|
+
"notice",
|
|
1454
|
+
"crysk: an override on this head stands, so the review's status is not reposted over it.",
|
|
1455
|
+
),
|
|
1456
|
+
);
|
|
1457
|
+
} else {
|
|
1458
|
+
const status = postStatus(
|
|
1459
|
+
deps,
|
|
1460
|
+
plan.repository,
|
|
1461
|
+
plan.headSha,
|
|
1462
|
+
plan.status,
|
|
1463
|
+
1,
|
|
1464
|
+
);
|
|
1465
|
+
if (!status.ok) {
|
|
1466
|
+
report(
|
|
1467
|
+
deps,
|
|
1468
|
+
statusWriteFailure(
|
|
1469
|
+
status.httpStatus,
|
|
1470
|
+
plan.runId,
|
|
1471
|
+
plan.status.targetUrl,
|
|
1472
|
+
),
|
|
1473
|
+
);
|
|
1474
|
+
exitCode = 1;
|
|
1475
|
+
}
|
|
1476
|
+
}
|
|
1477
|
+
}
|
|
1478
|
+
if (plan.pr !== undefined) {
|
|
1479
|
+
cleanupOlderOutcomes(deps, { ...plan, pr: plan.pr });
|
|
1480
|
+
}
|
|
1481
|
+
return exitCode;
|
|
1482
|
+
}
|
|
1483
|
+
if (plan.kind === "override") return publishOverride(deps, plan);
|
|
1484
|
+
report(deps, plan.rendered);
|
|
1485
|
+
if (plan.kind === "local") return 0;
|
|
1486
|
+
|
|
1487
|
+
let exitCode = 0;
|
|
1488
|
+
// WHY the status is optional: it needs a sha, and the comment below does not.
|
|
1489
|
+
if (plan.status !== undefined && plan.headSha !== undefined) {
|
|
1490
|
+
const status = postStatus(deps, plan.repository, plan.headSha, plan.status);
|
|
1491
|
+
if (!status.ok) {
|
|
1492
|
+
report(
|
|
1493
|
+
deps,
|
|
1494
|
+
statusWriteFailure(status.httpStatus, record.run_id, record.run_url),
|
|
1495
|
+
);
|
|
1496
|
+
exitCode = 1;
|
|
1497
|
+
}
|
|
1498
|
+
}
|
|
1499
|
+
|
|
1500
|
+
const comment = postComment(deps, plan);
|
|
1501
|
+
if (!comment.ok) {
|
|
1502
|
+
deps.stdout(
|
|
1503
|
+
workflowCommand(
|
|
1504
|
+
"error",
|
|
1505
|
+
`Crysk could not post its outcome comment on #${plan.pr} (${comment.detail}).`,
|
|
1506
|
+
),
|
|
1507
|
+
);
|
|
1508
|
+
return 1;
|
|
1509
|
+
}
|
|
1510
|
+
return exitCode;
|
|
1511
|
+
}
|
|
1512
|
+
|
|
1513
|
+
// The settle of a cancelled review (SB3). It runs in the cancelled job only, after publish,
|
|
1514
|
+
// and it never fails the job: a cancel is not a failure, and a status it cannot write is a
|
|
1515
|
+
// warning. Its only write is this run's own pending on the head the CLI locked.
|
|
1516
|
+
export function runCancelSettle(deps: PublishDeps): number {
|
|
1517
|
+
const dir = deps.env.CRYSK_OUTCOME_DIR ?? "";
|
|
1518
|
+
if (dir === "") return 0;
|
|
1519
|
+
const record = readCliRecord(dir);
|
|
1520
|
+
// Guarded like readCliRecord: this step must exit 0 whatever the file system does.
|
|
1521
|
+
let pendingAt: string | undefined;
|
|
1522
|
+
try {
|
|
1523
|
+
pendingAt = readIfExists(path.join(dir, PENDING_AT_FILE))?.trim();
|
|
1524
|
+
} catch {
|
|
1525
|
+
pendingAt = undefined;
|
|
1526
|
+
}
|
|
1527
|
+
const repository = deps.env.GITHUB_REPOSITORY ?? "";
|
|
1528
|
+
const head = record?.head_sha;
|
|
1529
|
+
let latest:
|
|
1530
|
+
| { state: string; created_at: string; target_url?: string | null }
|
|
1531
|
+
| undefined;
|
|
1532
|
+
if (head !== undefined) {
|
|
1533
|
+
const read = readHeadStatuses(deps, repository, head);
|
|
1534
|
+
if (!read.ok) {
|
|
1535
|
+
deps.stdout(
|
|
1536
|
+
workflowCommand(
|
|
1537
|
+
"warning",
|
|
1538
|
+
"could not read the head's status after the cancel; the crysk status may stay pending",
|
|
1539
|
+
),
|
|
1540
|
+
);
|
|
1541
|
+
return 0;
|
|
1542
|
+
}
|
|
1543
|
+
const newest =
|
|
1544
|
+
repairStrandedPair(deps, repository, head, read.statuses) ??
|
|
1545
|
+
newestStatusInContext(read.statuses, STATUS_CONTEXT);
|
|
1546
|
+
// newestStatusInContext returns the row it was given, so the row's target is found by identity.
|
|
1547
|
+
latest =
|
|
1548
|
+
newest === undefined
|
|
1549
|
+
? undefined
|
|
1550
|
+
: {
|
|
1551
|
+
state: newest.state,
|
|
1552
|
+
created_at: newest.created_at,
|
|
1553
|
+
target_url: read.statuses.find((row) => row === newest)?.targetUrl,
|
|
1554
|
+
};
|
|
1555
|
+
}
|
|
1556
|
+
// This run's own URL, from the same env the CLI read when it posted its pending.
|
|
1557
|
+
const ownRunUrl = actionsRunUrl(deps.env);
|
|
1558
|
+
const decision = decideCancelSettle({
|
|
1559
|
+
record,
|
|
1560
|
+
latest,
|
|
1561
|
+
pendingAt: pendingAt === "" ? undefined : pendingAt,
|
|
1562
|
+
runUrl: ownRunUrl,
|
|
1563
|
+
});
|
|
1564
|
+
if (!decision.settle) {
|
|
1565
|
+
deps.stdout(
|
|
1566
|
+
workflowCommand(
|
|
1567
|
+
"notice",
|
|
1568
|
+
`cancel: nothing to settle (${decision.reason})`,
|
|
1569
|
+
),
|
|
1570
|
+
);
|
|
1571
|
+
return 0;
|
|
1572
|
+
}
|
|
1573
|
+
const serverUrl = deps.env.GITHUB_SERVER_URL || "https://github.com";
|
|
1574
|
+
const runUrl =
|
|
1575
|
+
ownRunUrl ??
|
|
1576
|
+
`${serverUrl}/${repository}/actions/runs/${deps.env.GITHUB_RUN_ID ?? ""}`;
|
|
1577
|
+
const status = postStatus(deps, repository, decision.headSha, {
|
|
1578
|
+
state: "error",
|
|
1579
|
+
context: STATUS_CONTEXT,
|
|
1580
|
+
description: clipStatusDescription(CANCELLED_STATUS_DESCRIPTION),
|
|
1581
|
+
targetUrl: runUrl,
|
|
1582
|
+
});
|
|
1583
|
+
if (status.ok) {
|
|
1584
|
+
deps.stdout(
|
|
1585
|
+
workflowCommand(
|
|
1586
|
+
"notice",
|
|
1587
|
+
"the review was cancelled; the crysk status is now PRH-CANCELLED",
|
|
1588
|
+
),
|
|
1589
|
+
);
|
|
1590
|
+
} else {
|
|
1591
|
+
deps.stdout(
|
|
1592
|
+
workflowCommand(
|
|
1593
|
+
"warning",
|
|
1594
|
+
"could not settle the cancelled review's crysk status; it may stay pending",
|
|
1595
|
+
),
|
|
1596
|
+
);
|
|
1597
|
+
}
|
|
1598
|
+
return 0;
|
|
1599
|
+
}
|
|
1600
|
+
|
|
1601
|
+
if (import.meta.main) {
|
|
1602
|
+
const summaryPath = process.env.GITHUB_STEP_SUMMARY ?? "";
|
|
1603
|
+
const run =
|
|
1604
|
+
process.env.CRYSK_PUBLISH_MODE === "cancelled"
|
|
1605
|
+
? runCancelSettle
|
|
1606
|
+
: runPublish;
|
|
1607
|
+
process.exit(
|
|
1608
|
+
run({
|
|
1609
|
+
env: process.env,
|
|
1610
|
+
gh: (args, stdin) => {
|
|
1611
|
+
// WHY the catch: Bun throws, rather than returning, when gh is not on
|
|
1612
|
+
// PATH. A throw would end the step non-zero, and cleanup must exit 0, so
|
|
1613
|
+
// a call that cannot run is a failed call like any other.
|
|
1614
|
+
try {
|
|
1615
|
+
const result = Bun.spawnSync(["gh", ...args], {
|
|
1616
|
+
stdin: stdin === undefined ? "ignore" : Buffer.from(stdin),
|
|
1617
|
+
stdout: "pipe",
|
|
1618
|
+
stderr: "pipe",
|
|
1619
|
+
// A hung API call must not hold the job: this step runs last.
|
|
1620
|
+
timeout: GH_TIMEOUT_MS,
|
|
1621
|
+
});
|
|
1622
|
+
return {
|
|
1623
|
+
exitCode: result.exitCode ?? -1,
|
|
1624
|
+
stdout: result.stdout.toString(),
|
|
1625
|
+
stderr: result.stderr.toString(),
|
|
1626
|
+
};
|
|
1627
|
+
} catch (error) {
|
|
1628
|
+
return { exitCode: -1, stdout: "", stderr: String(error) };
|
|
1629
|
+
}
|
|
1630
|
+
},
|
|
1631
|
+
stdout: (text) => {
|
|
1632
|
+
process.stdout.write(text);
|
|
1633
|
+
},
|
|
1634
|
+
appendSummary: (text) => {
|
|
1635
|
+
if (summaryPath !== "") appendFileSync(summaryPath, text);
|
|
1636
|
+
},
|
|
1637
|
+
}),
|
|
1638
|
+
);
|
|
1639
|
+
}
|