pr-shepherd 2.2.18 → 2.4.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/.claude-plugin/plugin.json +1 -1
- package/README.md +1 -0
- package/bin/api.d.mts +10 -0
- package/bin/api.mjs +21 -1
- package/bin/cli/iterate-formatter.mjs +3 -1
- package/bin/cli/triage-formatter.mjs +2 -0
- package/bin/cli/workflow-elsewhere-format.d.mts +3 -0
- package/bin/cli/workflow-elsewhere-format.mjs +112 -0
- package/bin/commands/iterate/base.mjs +1 -0
- package/bin/commands/iterate/helpers.mjs +1 -0
- package/bin/commands/iterate/index.mjs +11 -3
- package/bin/commands/iterate/queue-recovery-instructions.d.mts +1 -1
- package/bin/commands/iterate/queue-recovery-instructions.mjs +7 -4
- package/bin/commands/iterate/render.mjs +2 -1
- package/bin/commands/iterate/triage.mjs +24 -7
- package/bin/commands/iterate/workflow-elsewhere.d.mts +19 -0
- package/bin/commands/iterate/workflow-elsewhere.mjs +144 -0
- package/bin/commands/ready-delay.mjs +24 -1
- package/bin/comments/rest-reply.mjs +2 -1
- package/bin/comments/uncertain-replies.mjs +2 -1
- package/bin/comments/uncertain-reply-guards.mjs +2 -1
- package/bin/config/schema.d.mts +6 -0
- package/bin/config/schema.mjs +3 -0
- package/bin/execution-context.d.mts +7 -0
- package/bin/execution-context.mjs +13 -1
- package/bin/github/gql/default-branch-workflow-runs.gql +71 -0
- package/bin/github/graphql-http.mjs +2 -0
- package/bin/github/graphql-internal-retry.mjs +2 -1
- package/bin/github/queries.d.mts +2 -0
- package/bin/github/queries.mjs +2 -0
- package/bin/github/read-only-guard.d.mts +4 -0
- package/bin/github/read-only-guard.mjs +25 -0
- package/bin/github/rest-http.mjs +2 -0
- package/bin/github/rest-identities.mjs +16 -2
- package/bin/github/workflow-elsewhere-parse.d.mts +19 -0
- package/bin/github/workflow-elsewhere-parse.mjs +172 -0
- package/bin/github/workflow-elsewhere-queue.d.mts +28 -0
- package/bin/github/workflow-elsewhere-queue.mjs +137 -0
- package/bin/github/workflow-elsewhere-read.d.mts +26 -0
- package/bin/github/workflow-elsewhere-read.mjs +120 -0
- package/bin/github/workflow-elsewhere-rest-runs.d.mts +20 -0
- package/bin/github/workflow-elsewhere-rest-runs.mjs +54 -0
- package/bin/github/workflow-elsewhere-rest.d.mts +3 -0
- package/bin/github/workflow-elsewhere-rest.mjs +126 -0
- package/bin/log/log-file.mjs +1 -1
- package/bin/state/base.mjs +30 -12
- package/bin/state/check-blockers.mjs +2 -1
- package/bin/state/check-follow-up.mjs +2 -1
- package/bin/state/ci-retrigger.mjs +2 -1
- package/bin/state/conflicting-head-seen.mjs +2 -1
- package/bin/state/fix-attempts.mjs +2 -1
- package/bin/state/graphql-quota-claims.mjs +6 -1
- package/bin/state/graphql-quota-warnings.mjs +2 -1
- package/bin/state/guarded-fs.d.mts +16 -0
- package/bin/state/guarded-fs.mjs +49 -0
- package/bin/state/merge-request.mjs +2 -1
- package/bin/state/pr-fingerprint.mjs +2 -1
- package/bin/state/queue-removal-ack.mjs +2 -1
- package/bin/state/ready-receipts.mjs +2 -1
- package/bin/state/rest-cache.mjs +2 -1
- package/bin/state/rest-snapshot-report.mjs +2 -1
- package/bin/state/seen-comments.mjs +2 -1
- package/bin/state/signature-marker.mjs +2 -1
- package/bin/state/stack-merge-approval.mjs +2 -1
- package/bin/state/stall-repair.mjs +2 -1
- package/bin/state/stall-state-store.mjs +2 -1
- package/bin/types/iterate.d.mts +2 -0
- package/bin/types/report.d.mts +7 -0
- package/bin/types/workflow-elsewhere-raw.d.mts +108 -0
- package/bin/types/workflow-elsewhere-raw.mjs +1 -0
- package/bin/types/workflow-elsewhere.d.mts +55 -0
- package/bin/types/workflow-elsewhere.mjs +1 -0
- package/bin/types.d.mts +1 -0
- package/bin/types.mjs +1 -0
- package/package.json +1 -1
- package/plugins/pr-shepherd/.claude-plugin/plugin.json +1 -1
- package/plugins/pr-shepherd/.codex-plugin/plugin.json +1 -1
- package/plugins/pr-shepherd/skills/pr-shepherd/references/merge-queue-ejection.md +1 -1
- package/pr-shepherd.schema.json +13 -0
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
|
-
import { readFile
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { mkdir, writeFile } from "../state/guarded-fs.mjs";
|
|
3
4
|
import { dirname } from "node:path";
|
|
4
5
|
import { resolvePrStatePath } from "../state/base.mjs";
|
|
5
6
|
import { resolveRestThreadRoot } from "../github/rest-identities.mjs";
|
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
|
-
import {
|
|
2
|
+
import { readFile, readdir } from "node:fs/promises";
|
|
3
|
+
import { mkdir, unlink, writeFile } from "../state/guarded-fs.mjs";
|
|
3
4
|
import { join } from "node:path";
|
|
4
5
|
import { resolvePrStatePath } from "../state/base.mjs";
|
|
5
6
|
import { resolveRestThreadRoot } from "../github/rest-identities.mjs";
|
package/bin/config/schema.d.mts
CHANGED
|
@@ -77,6 +77,9 @@ export declare const configSchema: z.ZodObject<{
|
|
|
77
77
|
}>>;
|
|
78
78
|
"command-args": z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
79
79
|
}, z.core.$strict>>;
|
|
80
|
+
"merge-queue": z.ZodPrefault<z.ZodObject<{
|
|
81
|
+
"compare-window-hours": z.ZodDefault<z.ZodNumber>;
|
|
82
|
+
}, z.core.$strict>>;
|
|
80
83
|
actions: z.ZodPrefault<z.ZodObject<{
|
|
81
84
|
"auto-minimize-suppressed": z.ZodDefault<z.ZodBoolean>;
|
|
82
85
|
"work-while-queued": z.ZodDefault<z.ZodBoolean>;
|
|
@@ -164,6 +167,9 @@ export declare const configFileSchema: z.ZodObject<{
|
|
|
164
167
|
}>>;
|
|
165
168
|
"command-args": z.ZodDefault<z.ZodArray<z.ZodString>>;
|
|
166
169
|
}, z.core.$strict>>;
|
|
170
|
+
"merge-queue": z.ZodPrefault<z.ZodObject<{
|
|
171
|
+
"compare-window-hours": z.ZodDefault<z.ZodNumber>;
|
|
172
|
+
}, z.core.$strict>>;
|
|
167
173
|
actions: z.ZodPrefault<z.ZodObject<{
|
|
168
174
|
"auto-minimize-suppressed": z.ZodDefault<z.ZodBoolean>;
|
|
169
175
|
"work-while-queued": z.ZodDefault<z.ZodBoolean>;
|
package/bin/config/schema.mjs
CHANGED
|
@@ -159,6 +159,9 @@ export const configSchema = z.strictObject({
|
|
|
159
159
|
"command-args": mergeCommandArgs.default(() => []),
|
|
160
160
|
})
|
|
161
161
|
.prefault({}),
|
|
162
|
+
"merge-queue": z
|
|
163
|
+
.strictObject({ "compare-window-hours": nonNegative().max(720).default(6) })
|
|
164
|
+
.prefault({}),
|
|
162
165
|
actions: z
|
|
163
166
|
.strictObject({
|
|
164
167
|
"auto-minimize-suppressed": z.boolean().default(true),
|
|
@@ -3,6 +3,13 @@
|
|
|
3
3
|
* process state. This keeps independent API/MCP requests safe to run together.
|
|
4
4
|
*/
|
|
5
5
|
export declare function runWithExecutionCwd<T>(cwd: string | undefined, work: () => T): T;
|
|
6
|
+
/**
|
|
7
|
+
* Runs work in a read-only scope: state writes, deletes and directory creation become no-ops,
|
|
8
|
+
* and the HTTP layer refuses GitHub mutations. Composes with `runWithExecutionCwd` in either order.
|
|
9
|
+
*/
|
|
10
|
+
export declare function runReadOnly<T>(work: () => T): T;
|
|
11
|
+
/** True inside `runReadOnly`. */
|
|
12
|
+
export declare function isReadOnly(): boolean;
|
|
6
13
|
/** The caller-scoped working directory, when one was supplied. */
|
|
7
14
|
export declare function getExecutionCwd(): string | undefined;
|
|
8
15
|
/** The caller-scoped working directory, falling back to the CLI process cwd. */
|
|
@@ -7,7 +7,19 @@ const executionContext = new AsyncLocalStorage();
|
|
|
7
7
|
export function runWithExecutionCwd(cwd, work) {
|
|
8
8
|
if (cwd === undefined)
|
|
9
9
|
return work();
|
|
10
|
-
|
|
10
|
+
// Keep any enclosing scope's other fields (read-only) when only the cwd changes.
|
|
11
|
+
return executionContext.run({ ...executionContext.getStore(), cwd }, work);
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Runs work in a read-only scope: state writes, deletes and directory creation become no-ops,
|
|
15
|
+
* and the HTTP layer refuses GitHub mutations. Composes with `runWithExecutionCwd` in either order.
|
|
16
|
+
*/
|
|
17
|
+
export function runReadOnly(work) {
|
|
18
|
+
return executionContext.run({ ...executionContext.getStore(), readOnly: true }, work);
|
|
19
|
+
}
|
|
20
|
+
/** True inside `runReadOnly`. */
|
|
21
|
+
export function isReadOnly() {
|
|
22
|
+
return executionContext.getStore()?.readOnly === true;
|
|
11
23
|
}
|
|
12
24
|
/** The caller-scoped working directory, when one was supplied. */
|
|
13
25
|
export function getExecutionCwd() {
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
query DefaultBranchWorkflowRuns($owner: String!, $repo: String!) {
|
|
2
|
+
_shepherdRateLimit: rateLimit {
|
|
3
|
+
cost
|
|
4
|
+
limit
|
|
5
|
+
nodeCount
|
|
6
|
+
remaining
|
|
7
|
+
resetAt
|
|
8
|
+
used
|
|
9
|
+
}
|
|
10
|
+
repository(owner: $owner, name: $repo) {
|
|
11
|
+
defaultBranchRef {
|
|
12
|
+
name
|
|
13
|
+
target {
|
|
14
|
+
... on Commit {
|
|
15
|
+
history(first: 20) {
|
|
16
|
+
pageInfo {
|
|
17
|
+
hasNextPage
|
|
18
|
+
}
|
|
19
|
+
nodes {
|
|
20
|
+
oid
|
|
21
|
+
committedDate
|
|
22
|
+
checkSuites(first: 20) {
|
|
23
|
+
pageInfo {
|
|
24
|
+
hasNextPage
|
|
25
|
+
}
|
|
26
|
+
nodes {
|
|
27
|
+
status
|
|
28
|
+
conclusion
|
|
29
|
+
updatedAt
|
|
30
|
+
branch {
|
|
31
|
+
name
|
|
32
|
+
}
|
|
33
|
+
workflowRun {
|
|
34
|
+
databaseId
|
|
35
|
+
event
|
|
36
|
+
updatedAt
|
|
37
|
+
file {
|
|
38
|
+
path
|
|
39
|
+
}
|
|
40
|
+
workflow {
|
|
41
|
+
databaseId
|
|
42
|
+
name
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
checkRuns(first: 15, filterBy: { conclusions: [FAILURE, TIMED_OUT] }) {
|
|
46
|
+
nodes {
|
|
47
|
+
name
|
|
48
|
+
conclusion
|
|
49
|
+
annotations(first: 3) {
|
|
50
|
+
totalCount
|
|
51
|
+
nodes {
|
|
52
|
+
path
|
|
53
|
+
location {
|
|
54
|
+
start {
|
|
55
|
+
line
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
message
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
}
|
|
@@ -4,6 +4,7 @@ import { formatRequestEntry, formatResponseEntry } from "../log/session.mjs";
|
|
|
4
4
|
import { GitHubRequestError } from "./errors.mjs";
|
|
5
5
|
import { isGraphQlMutationDocument, withGraphQlInternalRetry } from "./graphql-internal-retry.mjs";
|
|
6
6
|
import { githubFetch } from "./github-fetch.mjs";
|
|
7
|
+
import { assertGraphQlReadable } from "./read-only-guard.mjs";
|
|
7
8
|
import { formatGraphQlErrors, parseGraphQlPayload } from "./graphql-response.mjs";
|
|
8
9
|
import { withoutToleratedErrors } from "./graphql-tolerated-errors.mjs";
|
|
9
10
|
import { CCR_GRAPHQL_REFUSAL_MESSAGE, isCcrGraphqlRefusal } from "./graphql-session-refusal.mjs";
|
|
@@ -20,6 +21,7 @@ function graphqlInner(query, vars, opts) {
|
|
|
20
21
|
return withAppQuotaFallback("graphql", () => graphqlAttempt(query, vars, opts));
|
|
21
22
|
}
|
|
22
23
|
async function graphqlAttempt(query, vars, opts) {
|
|
24
|
+
assertGraphQlReadable(query);
|
|
23
25
|
const url = `${BASE_URL}/graphql`;
|
|
24
26
|
const n = nextEntry();
|
|
25
27
|
appendEntry(formatRequestEntry({
|
|
@@ -5,7 +5,8 @@ const GRAPHQL_INTERNAL_RETRY_DELAYS = [500, 1500];
|
|
|
5
5
|
export function isGraphQlMutationDocument(document) {
|
|
6
6
|
let rest = document;
|
|
7
7
|
for (;;) {
|
|
8
|
-
|
|
8
|
+
// Commas are ignored tokens in GraphQL, like whitespace.
|
|
9
|
+
rest = rest.replace(/^[\s,]+/, "");
|
|
9
10
|
if (rest.startsWith("#")) {
|
|
10
11
|
const nl = rest.indexOf("\n");
|
|
11
12
|
rest = nl === -1 ? "" : rest.slice(nl + 1);
|
package/bin/github/queries.d.mts
CHANGED
|
@@ -80,3 +80,5 @@ export declare const GET_PR_BODY_QUERY: string;
|
|
|
80
80
|
export declare const UPDATE_PR_BODY_MUTATION: string;
|
|
81
81
|
/** CheckRun titles, summaries, and first annotations for routed stack-layer handoffs. */
|
|
82
82
|
export declare const STACK_HANDOFF_CHECK_RUNS_QUERY: string;
|
|
83
|
+
/** Recent default-branch commits and workflow check suites for merge-queue ejection comparison. */
|
|
84
|
+
export declare const DEFAULT_BRANCH_WORKFLOW_RUNS_QUERY: string;
|
package/bin/github/queries.mjs
CHANGED
|
@@ -86,3 +86,5 @@ export const GET_PR_BODY_QUERY = gql("get-pr-body.gql");
|
|
|
86
86
|
export const UPDATE_PR_BODY_MUTATION = gql("update-pr-body.gql");
|
|
87
87
|
/** CheckRun titles, summaries, and first annotations for routed stack-layer handoffs. */
|
|
88
88
|
export const STACK_HANDOFF_CHECK_RUNS_QUERY = gql("stack-handoff-check-runs.gql");
|
|
89
|
+
/** Recent default-branch commits and workflow check suites for merge-queue ejection comparison. */
|
|
90
|
+
export const DEFAULT_BRANCH_WORKFLOW_RUNS_QUERY = gql("default-branch-workflow-runs.gql");
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
/** Refuse a GraphQL document whose root operation is a mutation. */
|
|
2
|
+
export declare function assertGraphQlReadable(document: string): void;
|
|
3
|
+
/** Refuse any REST method other than GET or HEAD. */
|
|
4
|
+
export declare function assertRestReadable(method: string, path: string): void;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import { isReadOnly } from "../execution-context.mjs";
|
|
2
|
+
import { isGraphQlMutationDocument } from "./graphql-internal-retry.mjs";
|
|
3
|
+
/**
|
|
4
|
+
* Raised when a read-only scope (the library `status` method) reaches a GitHub mutation. A normal
|
|
5
|
+
* status tick never gets here: the call sites that mutate are skipped first, so this is only the
|
|
6
|
+
* backstop that keeps a missed site from changing GitHub.
|
|
7
|
+
*/
|
|
8
|
+
class ReadOnlyViolationError extends Error {
|
|
9
|
+
constructor(operation) {
|
|
10
|
+
super(`read-only status check refused a GitHub mutation: ${operation}`);
|
|
11
|
+
this.name = "ReadOnlyViolationError";
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
/** Refuse a GraphQL document whose root operation is a mutation. */
|
|
15
|
+
export function assertGraphQlReadable(document) {
|
|
16
|
+
if (isReadOnly() && isGraphQlMutationDocument(document)) {
|
|
17
|
+
throw new ReadOnlyViolationError("GraphQL mutation");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** Refuse any REST method other than GET or HEAD. */
|
|
21
|
+
export function assertRestReadable(method, path) {
|
|
22
|
+
if (isReadOnly() && method.toUpperCase() !== "GET" && method.toUpperCase() !== "HEAD") {
|
|
23
|
+
throw new ReadOnlyViolationError(`REST ${method.toUpperCase()} ${path}`);
|
|
24
|
+
}
|
|
25
|
+
}
|
package/bin/github/rest-http.mjs
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
/* eslint-disable max-lines */
|
|
2
|
+
import { assertRestReadable } from "./read-only-guard.mjs";
|
|
2
3
|
import { appendEntry, nextEntry } from "../log/log-file.mjs";
|
|
3
4
|
import { formatRequestEntry, formatResponseEntry } from "../log/session.mjs";
|
|
4
5
|
import { loadEtagEntry, storeEtagEntry } from "../state/rest-cache.mjs";
|
|
@@ -37,6 +38,7 @@ export function restWithRateLimit(method, path, body, opts) {
|
|
|
37
38
|
return withAppQuotaFallback("core", () => restAttempt(method, path, body, opts));
|
|
38
39
|
}
|
|
39
40
|
async function restAttempt(method, path, body, opts) {
|
|
41
|
+
assertRestReadable(method, path);
|
|
40
42
|
const url = githubApiUrl(path);
|
|
41
43
|
const n = nextEntry();
|
|
42
44
|
appendEntry(formatRequestEntry({ n, kind: "REST", method, url, body }));
|
|
@@ -1,7 +1,9 @@
|
|
|
1
1
|
import { createHash } from "node:crypto";
|
|
2
|
-
import {
|
|
2
|
+
import { readFile } from "node:fs/promises";
|
|
3
|
+
import { mkdir, writeFile } from "../state/guarded-fs.mjs";
|
|
3
4
|
import { join } from "node:path";
|
|
4
5
|
import { resolveStateBase } from "../state/base.mjs";
|
|
6
|
+
import { isReadOnly } from "../execution-context.mjs";
|
|
5
7
|
function identityPath(id) {
|
|
6
8
|
const hash = createHash("sha256").update(id).digest("hex");
|
|
7
9
|
return join(resolveStateBase(), "github-identities", `${hash}.json`);
|
|
@@ -15,7 +17,17 @@ function numeric(value) {
|
|
|
15
17
|
export function restThreadId(rootCommentId) {
|
|
16
18
|
return `rest-thread-${numeric(rootCommentId)}`;
|
|
17
19
|
}
|
|
20
|
+
/**
|
|
21
|
+
* Identities recorded inside a read-only scope, keyed by their would-be file path. The scope writes
|
|
22
|
+
* nothing to disk, but later reads in the same tick (check annotations, thread aliases) still need
|
|
23
|
+
* the identities that tick fetched.
|
|
24
|
+
*/
|
|
25
|
+
const readOnlyIdentities = new Map();
|
|
18
26
|
async function storeIdentity(id, entry) {
|
|
27
|
+
if (isReadOnly()) {
|
|
28
|
+
readOnlyIdentities.set(identityPath(id), JSON.stringify({ id, ...entry }));
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
19
31
|
await mkdir(join(resolveStateBase(), "github-identities"), { recursive: true });
|
|
20
32
|
await writeFile(identityPath(id), JSON.stringify({ id, ...entry }));
|
|
21
33
|
}
|
|
@@ -27,7 +39,9 @@ export async function recordRestIdentity(repo, pr, nodeId, numericId, kind) {
|
|
|
27
39
|
export async function resolveRestIdentity(id, kind) {
|
|
28
40
|
let entry;
|
|
29
41
|
try {
|
|
30
|
-
|
|
42
|
+
const path = identityPath(id);
|
|
43
|
+
const recorded = isReadOnly() ? readOnlyIdentities.get(path) : undefined;
|
|
44
|
+
entry = JSON.parse(recorded ?? (await readFile(path, "utf8")));
|
|
31
45
|
}
|
|
32
46
|
catch {
|
|
33
47
|
throw new Error(`No recorded REST identity for ${id}; refresh this PR with REST transport first`);
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { ElsewhereFailedJob, ElsewhereRun } from "../types/workflow-elsewhere.mts";
|
|
2
|
+
import type { ParsedDefaultBranchRuns, RawDefaultBranchRunsResponse, RawElsewhereSuite } from "../types/workflow-elsewhere-raw.mts";
|
|
3
|
+
export declare const MAX_ELSEWHERE_RUNS_PER_SOURCE = 3;
|
|
4
|
+
export declare const MAX_ELSEWHERE_FAILED_JOBS = 15;
|
|
5
|
+
export declare const MAX_ELSEWHERE_ANNOTATIONS = 3;
|
|
6
|
+
export declare const NON_DEFAULT_BRANCH_EVENTS: Set<string>;
|
|
7
|
+
/** Collapse newlines and whitespace and bound an annotation message for single-line display. */
|
|
8
|
+
export declare function normalizeAnnotationMessage(raw: string | null | undefined): string;
|
|
9
|
+
/** Remove null annotation entries in place and report whether any were present. */
|
|
10
|
+
export declare function stripNullAnnotations(suites: Array<RawElsewhereSuite | null> | undefined): boolean;
|
|
11
|
+
/** Extract failed/timed-out check runs and up to 3 annotations per job from one GraphQL suite. */
|
|
12
|
+
export declare function mapFailedJobs(suite: RawElsewhereSuite | undefined): ElsewhereFailedJob[];
|
|
13
|
+
/** Sort runs newest-first by completion timestamp, breaking ties by numeric run ID descending. */
|
|
14
|
+
export declare function sortNewestFirst<T extends {
|
|
15
|
+
completedAt: string;
|
|
16
|
+
runId: string;
|
|
17
|
+
}>(items: T[]): T[];
|
|
18
|
+
export declare function parseDefaultBranchRuns(data: RawDefaultBranchRunsResponse, workflowId: string, windowStartMs: number): ParsedDefaultBranchRuns;
|
|
19
|
+
export declare function stampSameJob(runs: readonly ElsewhereRun[], checkName: string): ElsewhereRun[];
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { normalizeWorkflowPath } from "../checks/workflow-path.mjs";
|
|
2
|
+
import { dropNullNodes } from "./null-nodes.mjs";
|
|
3
|
+
export const MAX_ELSEWHERE_RUNS_PER_SOURCE = 3;
|
|
4
|
+
export const MAX_ELSEWHERE_FAILED_JOBS = 15;
|
|
5
|
+
export const MAX_ELSEWHERE_ANNOTATIONS = 3;
|
|
6
|
+
const MAX_ANNOTATION_MESSAGE_CHARS = 240;
|
|
7
|
+
export const NON_DEFAULT_BRANCH_EVENTS = new Set([
|
|
8
|
+
"pull_request",
|
|
9
|
+
"pull_request_target",
|
|
10
|
+
"merge_group",
|
|
11
|
+
]);
|
|
12
|
+
/** Collapse newlines and whitespace and bound an annotation message for single-line display. */
|
|
13
|
+
export function normalizeAnnotationMessage(raw) {
|
|
14
|
+
const s = (raw ?? "").replace(/\s+/g, " ").trim();
|
|
15
|
+
return s.length <= MAX_ANNOTATION_MESSAGE_CHARS
|
|
16
|
+
? s
|
|
17
|
+
: `${s.slice(0, MAX_ANNOTATION_MESSAGE_CHARS - 1)}…`;
|
|
18
|
+
}
|
|
19
|
+
/** Remove null annotation entries in place and report whether any were present. */
|
|
20
|
+
export function stripNullAnnotations(suites) {
|
|
21
|
+
let foundNull = false;
|
|
22
|
+
for (const suite of suites ?? []) {
|
|
23
|
+
for (const checkRun of suite?.checkRuns?.nodes ?? []) {
|
|
24
|
+
const nodes = checkRun?.annotations?.nodes;
|
|
25
|
+
if (!Array.isArray(nodes))
|
|
26
|
+
continue;
|
|
27
|
+
const kept = nodes.filter((node) => node !== null);
|
|
28
|
+
if (kept.length !== nodes.length) {
|
|
29
|
+
foundNull = true;
|
|
30
|
+
checkRun.annotations.nodes = kept;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return foundNull;
|
|
35
|
+
}
|
|
36
|
+
/** Extract failed/timed-out check runs and up to 3 annotations per job from one GraphQL suite. */
|
|
37
|
+
export function mapFailedJobs(suite) {
|
|
38
|
+
const failed = (suite?.checkRuns?.nodes ?? []).filter((run) => {
|
|
39
|
+
const c = run?.conclusion?.toUpperCase() ?? "";
|
|
40
|
+
return c === "FAILURE" || c === "TIMED_OUT";
|
|
41
|
+
});
|
|
42
|
+
return failed.slice(0, MAX_ELSEWHERE_FAILED_JOBS).map((run) => {
|
|
43
|
+
const annotations = (run.annotations?.nodes ?? [])
|
|
44
|
+
.slice(0, MAX_ELSEWHERE_ANNOTATIONS)
|
|
45
|
+
.map((ann) => ({
|
|
46
|
+
path: (ann.path ?? "").trim(),
|
|
47
|
+
line: typeof ann.location?.start?.line === "number" && ann.location.start.line > 0
|
|
48
|
+
? ann.location.start.line
|
|
49
|
+
: null,
|
|
50
|
+
message: normalizeAnnotationMessage(ann.message),
|
|
51
|
+
}));
|
|
52
|
+
const total = run.annotations?.totalCount;
|
|
53
|
+
const more = typeof total === "number" && total > annotations.length
|
|
54
|
+
? total - annotations.length
|
|
55
|
+
: undefined;
|
|
56
|
+
return {
|
|
57
|
+
name: run.name,
|
|
58
|
+
conclusion: run.conclusion.toUpperCase(),
|
|
59
|
+
annotations,
|
|
60
|
+
...(more !== undefined && { moreAnnotations: more }),
|
|
61
|
+
};
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
/** Sort runs newest-first by completion timestamp, breaking ties by numeric run ID descending. */
|
|
65
|
+
export function sortNewestFirst(items) {
|
|
66
|
+
return [...items].sort((a, b) => Date.parse(b.completedAt) - Date.parse(a.completedAt) || Number(b.runId) - Number(a.runId));
|
|
67
|
+
}
|
|
68
|
+
function suiteUpdatedInWindow(suite, windowStartMs) {
|
|
69
|
+
const ts = suite?.workflowRun?.updatedAt ?? suite?.updatedAt;
|
|
70
|
+
const ms = ts ? Date.parse(ts) : Number.NaN;
|
|
71
|
+
return Number.isFinite(ms) && ms >= windowStartMs;
|
|
72
|
+
}
|
|
73
|
+
const emptyDefaultResult = (extra) => ({
|
|
74
|
+
runs: [],
|
|
75
|
+
incomplete: false,
|
|
76
|
+
commitsRead: 0,
|
|
77
|
+
matched: 0,
|
|
78
|
+
matchedComplete: false,
|
|
79
|
+
...extra,
|
|
80
|
+
});
|
|
81
|
+
export function parseDefaultBranchRuns(data, workflowId, windowStartMs) {
|
|
82
|
+
const unread = dropNullNodes(data);
|
|
83
|
+
const defaultRef = data.repository?.defaultBranchRef;
|
|
84
|
+
const history = defaultRef?.target?.history;
|
|
85
|
+
const commits = history?.nodes;
|
|
86
|
+
if (!defaultRef?.name || !Array.isArray(commits)) {
|
|
87
|
+
return emptyDefaultResult({ unavailable: "default branch history unavailable" });
|
|
88
|
+
}
|
|
89
|
+
const nullAnnotations = commits.some((c) => stripNullAnnotations(c?.checkSuites?.nodes));
|
|
90
|
+
const nullSuites = commits.some((c) => c?.checkSuites === null);
|
|
91
|
+
if (unread || nullAnnotations || nullSuites) {
|
|
92
|
+
return emptyDefaultResult({ incomplete: true, commitsRead: commits.length });
|
|
93
|
+
}
|
|
94
|
+
const targetId = Number(workflowId);
|
|
95
|
+
const byRunId = new Map();
|
|
96
|
+
let truncatedSuites = false;
|
|
97
|
+
let lastCommitInWindow = false;
|
|
98
|
+
let workflowName;
|
|
99
|
+
let workflowPath;
|
|
100
|
+
for (const commit of commits) {
|
|
101
|
+
let matchedOnCommit = false;
|
|
102
|
+
let hasInWindowSuite = false;
|
|
103
|
+
for (const suite of commit.checkSuites?.nodes ?? []) {
|
|
104
|
+
if (suiteUpdatedInWindow(suite, windowStartMs))
|
|
105
|
+
hasInWindowSuite = true;
|
|
106
|
+
const wfRun = suite.workflowRun;
|
|
107
|
+
if (!wfRun || wfRun.workflow?.databaseId !== targetId)
|
|
108
|
+
continue;
|
|
109
|
+
if (!workflowName && wfRun.workflow.name?.trim())
|
|
110
|
+
workflowName = wfRun.workflow.name.trim();
|
|
111
|
+
if (!workflowPath)
|
|
112
|
+
workflowPath = normalizeWorkflowPath(wfRun.file?.path);
|
|
113
|
+
if (wfRun.databaseId == null || !Number.isSafeInteger(wfRun.databaseId))
|
|
114
|
+
continue;
|
|
115
|
+
if (suite.branch?.name && suite.branch.name !== defaultRef.name)
|
|
116
|
+
continue;
|
|
117
|
+
if (wfRun.event && NON_DEFAULT_BRANCH_EVENTS.has(wfRun.event))
|
|
118
|
+
continue;
|
|
119
|
+
if (!suite.conclusion || (suite.status && suite.status !== "COMPLETED"))
|
|
120
|
+
continue;
|
|
121
|
+
const completedAt = wfRun.updatedAt ?? suite.updatedAt ?? commit.committedDate;
|
|
122
|
+
const completedMs = Date.parse(completedAt);
|
|
123
|
+
if (!Number.isFinite(completedMs) || completedMs < windowStartMs)
|
|
124
|
+
continue;
|
|
125
|
+
matchedOnCommit = true;
|
|
126
|
+
const runId = String(wfRun.databaseId);
|
|
127
|
+
if (byRunId.has(runId))
|
|
128
|
+
continue;
|
|
129
|
+
byRunId.set(runId, {
|
|
130
|
+
source: "default-branch",
|
|
131
|
+
branch: defaultRef.name,
|
|
132
|
+
headSha: commit.oid.slice(0, 7),
|
|
133
|
+
runId,
|
|
134
|
+
conclusion: suite.conclusion.toUpperCase(),
|
|
135
|
+
completedAt,
|
|
136
|
+
failedJobs: mapFailedJobs(suite),
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
const commitMs = Date.parse(commit.committedDate);
|
|
140
|
+
lastCommitInWindow =
|
|
141
|
+
hasInWindowSuite || !Number.isFinite(commitMs) || commitMs >= windowStartMs;
|
|
142
|
+
if (lastCommitInWindow && commit.checkSuites?.pageInfo?.hasNextPage && !matchedOnCommit) {
|
|
143
|
+
truncatedSuites = true;
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const truncated = truncatedSuites || (Boolean(history?.pageInfo?.hasNextPage) && lastCommitInWindow);
|
|
147
|
+
if (truncated && byRunId.size < MAX_ELSEWHERE_RUNS_PER_SOURCE) {
|
|
148
|
+
return emptyDefaultResult({
|
|
149
|
+
unavailable: "default branch workflow history truncated",
|
|
150
|
+
branch: defaultRef.name,
|
|
151
|
+
commitsRead: commits.length,
|
|
152
|
+
matched: byRunId.size,
|
|
153
|
+
});
|
|
154
|
+
}
|
|
155
|
+
return {
|
|
156
|
+
...emptyDefaultResult({
|
|
157
|
+
branch: defaultRef.name,
|
|
158
|
+
commitsRead: commits.length,
|
|
159
|
+
matched: byRunId.size,
|
|
160
|
+
matchedComplete: !truncated,
|
|
161
|
+
}),
|
|
162
|
+
runs: sortNewestFirst([...byRunId.values()]).slice(0, MAX_ELSEWHERE_RUNS_PER_SOURCE),
|
|
163
|
+
...(workflowName !== undefined && { workflowName }),
|
|
164
|
+
...(workflowPath !== undefined && { workflowPath }),
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
export function stampSameJob(runs, checkName) {
|
|
168
|
+
return runs.map((run) => ({
|
|
169
|
+
...run,
|
|
170
|
+
failedJobs: run.failedJobs.map((j) => j.name === checkName ? { ...j, sameJob: true } : { ...j }),
|
|
171
|
+
}));
|
|
172
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import type { ElsewhereRun } from "../types/workflow-elsewhere.mts";
|
|
2
|
+
import type { RawQueueCommitsResponse, RestRunCandidate, RestWorkflowRunItem } from "../types/workflow-elsewhere-raw.mts";
|
|
3
|
+
/** Filter and cap REST `merge_group` workflow runs to up to 3 completed entries from other PRs. */
|
|
4
|
+
export declare function selectOtherQueueRestRuns(runs: readonly RestWorkflowRunItem[], opts: {
|
|
5
|
+
prNumber: number;
|
|
6
|
+
currentRunId: string;
|
|
7
|
+
currentCommitOid: string;
|
|
8
|
+
windowStartMs: number;
|
|
9
|
+
}): {
|
|
10
|
+
candidates: RestRunCandidate[];
|
|
11
|
+
matched: number;
|
|
12
|
+
};
|
|
13
|
+
/** Filter and cap REST default-branch workflow runs to up to 3 completed entries in the window. */
|
|
14
|
+
export declare function selectDefaultBranchRestRuns(runs: readonly RestWorkflowRunItem[], branch: string, windowStartMs: number): {
|
|
15
|
+
candidates: RestRunCandidate[];
|
|
16
|
+
matched: number;
|
|
17
|
+
};
|
|
18
|
+
/** Build one aliased GraphQL query fetching failed check runs and annotations for queue commits. */
|
|
19
|
+
export declare function buildQueueCommitsQuery(owner: string, repo: string, oids: readonly string[]): {
|
|
20
|
+
query: string;
|
|
21
|
+
variables: Record<string, string>;
|
|
22
|
+
};
|
|
23
|
+
/** Combine REST queue-run candidates with GraphQL commit check suites; missing suites mark unavailable. */
|
|
24
|
+
export declare function mergeQueueCommitDetails(candidates: readonly RestRunCandidate[], oidToAlias: ReadonlyMap<string, string>, data: RawQueueCommitsResponse | undefined): {
|
|
25
|
+
runs: ElsewhereRun[];
|
|
26
|
+
incomplete: boolean;
|
|
27
|
+
unavailable?: string;
|
|
28
|
+
};
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { dropNullNodes } from "./null-nodes.mjs";
|
|
2
|
+
import { MAX_ELSEWHERE_RUNS_PER_SOURCE, NON_DEFAULT_BRANCH_EVENTS, mapFailedJobs, sortNewestFirst, stripNullAnnotations, } from "./workflow-elsewhere-parse.mjs";
|
|
3
|
+
const QUEUE_BRANCH_PR_RE = /^gh-readonly-queue\/.+\/pr-(\d+)-[0-9a-f]+$/i;
|
|
4
|
+
/** Parse the PR number from GitHub's merge-queue head branch `gh-readonly-queue/<base>/pr-<N>-<sha>`. */
|
|
5
|
+
function parseQueueHeadBranchPr(headBranch) {
|
|
6
|
+
if (!headBranch)
|
|
7
|
+
return undefined;
|
|
8
|
+
const match = QUEUE_BRANCH_PR_RE.exec(headBranch);
|
|
9
|
+
if (!match)
|
|
10
|
+
return undefined;
|
|
11
|
+
const num = Number.parseInt(match[1], 10);
|
|
12
|
+
return Number.isSafeInteger(num) && num > 0 ? num : undefined;
|
|
13
|
+
}
|
|
14
|
+
function validRunAttempt(attempt) {
|
|
15
|
+
return typeof attempt === "number" && Number.isSafeInteger(attempt) && attempt > 0
|
|
16
|
+
? attempt
|
|
17
|
+
: undefined;
|
|
18
|
+
}
|
|
19
|
+
/** Filter and cap REST `merge_group` workflow runs to up to 3 completed entries from other PRs. */
|
|
20
|
+
export function selectOtherQueueRestRuns(runs, opts) {
|
|
21
|
+
const byRunId = new Map();
|
|
22
|
+
for (const run of runs) {
|
|
23
|
+
if (run.status !== "completed" || !run.conclusion)
|
|
24
|
+
continue;
|
|
25
|
+
const runId = String(run.id);
|
|
26
|
+
if (runId === opts.currentRunId || run.head_sha === opts.currentCommitOid)
|
|
27
|
+
continue;
|
|
28
|
+
const pr = parseQueueHeadBranchPr(run.head_branch);
|
|
29
|
+
if (pr === opts.prNumber)
|
|
30
|
+
continue;
|
|
31
|
+
const completedAt = run.updated_at ?? run.created_at;
|
|
32
|
+
const completedMs = Date.parse(completedAt);
|
|
33
|
+
if (!Number.isFinite(completedMs) || completedMs < opts.windowStartMs)
|
|
34
|
+
continue;
|
|
35
|
+
if (byRunId.has(runId))
|
|
36
|
+
continue;
|
|
37
|
+
const runAttempt = validRunAttempt(run.run_attempt);
|
|
38
|
+
byRunId.set(runId, {
|
|
39
|
+
runId,
|
|
40
|
+
headSha: run.head_sha,
|
|
41
|
+
...(pr !== undefined && { pr }),
|
|
42
|
+
...(runAttempt !== undefined && { runAttempt }),
|
|
43
|
+
conclusion: run.conclusion.toUpperCase(),
|
|
44
|
+
completedAt,
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
const sorted = sortNewestFirst([...byRunId.values()]);
|
|
48
|
+
return {
|
|
49
|
+
candidates: sorted.slice(0, MAX_ELSEWHERE_RUNS_PER_SOURCE),
|
|
50
|
+
matched: sorted.length,
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
/** Filter and cap REST default-branch workflow runs to up to 3 completed entries in the window. */
|
|
54
|
+
export function selectDefaultBranchRestRuns(runs, branch, windowStartMs) {
|
|
55
|
+
const byRunId = new Map();
|
|
56
|
+
for (const run of runs) {
|
|
57
|
+
if (run.status !== "completed" || !run.conclusion)
|
|
58
|
+
continue;
|
|
59
|
+
if (run.event && NON_DEFAULT_BRANCH_EVENTS.has(run.event))
|
|
60
|
+
continue;
|
|
61
|
+
const completedAt = run.updated_at ?? run.created_at;
|
|
62
|
+
const completedMs = Date.parse(completedAt);
|
|
63
|
+
if (!Number.isFinite(completedMs) || completedMs < windowStartMs)
|
|
64
|
+
continue;
|
|
65
|
+
const runId = String(run.id);
|
|
66
|
+
if (byRunId.has(runId))
|
|
67
|
+
continue;
|
|
68
|
+
const runAttempt = validRunAttempt(run.run_attempt);
|
|
69
|
+
byRunId.set(runId, {
|
|
70
|
+
runId,
|
|
71
|
+
headSha: run.head_sha,
|
|
72
|
+
branch,
|
|
73
|
+
...(runAttempt !== undefined && { runAttempt }),
|
|
74
|
+
conclusion: run.conclusion.toUpperCase(),
|
|
75
|
+
completedAt,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
const sorted = sortNewestFirst([...byRunId.values()]);
|
|
79
|
+
return {
|
|
80
|
+
candidates: sorted.slice(0, MAX_ELSEWHERE_RUNS_PER_SOURCE),
|
|
81
|
+
matched: sorted.length,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
/** Build one aliased GraphQL query fetching failed check runs and annotations for queue commits. */
|
|
85
|
+
export function buildQueueCommitsQuery(owner, repo, oids) {
|
|
86
|
+
const decls = oids.map((_, i) => `, $oid${i}: String!`).join("");
|
|
87
|
+
const fields = oids
|
|
88
|
+
.map((_, i) => `c${i}: object(expression: $oid${i}) { ... on Commit { oid checkSuites(first: 20) { pageInfo { hasNextPage } nodes { workflowRun { databaseId } checkRuns(first: 15, filterBy: { conclusions: [FAILURE, TIMED_OUT] }) { nodes { name conclusion annotations(first: 3) { totalCount nodes { path location { start { line } } message } } } } } } } }`)
|
|
89
|
+
.join(" ");
|
|
90
|
+
const query = `query QueueCommitCheckSuites($owner: String!, $repo: String!${decls}) { _shepherdRateLimit: rateLimit { cost limit nodeCount remaining resetAt used } repository(owner: $owner, name: $repo) { ${fields} } }`;
|
|
91
|
+
const variables = { owner, repo };
|
|
92
|
+
oids.forEach((oid, i) => {
|
|
93
|
+
variables[`oid${i}`] = oid;
|
|
94
|
+
});
|
|
95
|
+
return { query, variables };
|
|
96
|
+
}
|
|
97
|
+
/** Combine REST queue-run candidates with GraphQL commit check suites; missing suites mark unavailable. */
|
|
98
|
+
export function mergeQueueCommitDetails(candidates, oidToAlias, data) {
|
|
99
|
+
if (data) {
|
|
100
|
+
const unread = dropNullNodes(data);
|
|
101
|
+
let nullAnnotations = false;
|
|
102
|
+
for (const commit of Object.values(data.repository ?? {})) {
|
|
103
|
+
if (stripNullAnnotations(commit?.checkSuites?.nodes))
|
|
104
|
+
nullAnnotations = true;
|
|
105
|
+
}
|
|
106
|
+
if (unread || nullAnnotations)
|
|
107
|
+
return { runs: [], incomplete: true };
|
|
108
|
+
}
|
|
109
|
+
const runs = [];
|
|
110
|
+
for (const candidate of candidates) {
|
|
111
|
+
let failedJobs = [];
|
|
112
|
+
if (candidate.conclusion !== "SUCCESS") {
|
|
113
|
+
const alias = oidToAlias.get(candidate.headSha);
|
|
114
|
+
const commit = alias ? data?.repository?.[alias] : undefined;
|
|
115
|
+
const suite = (commit?.checkSuites?.nodes ?? []).find((s) => s?.workflowRun?.databaseId != null &&
|
|
116
|
+
String(s.workflowRun.databaseId) === candidate.runId);
|
|
117
|
+
if (!suite) {
|
|
118
|
+
return {
|
|
119
|
+
runs: [],
|
|
120
|
+
incomplete: false,
|
|
121
|
+
unavailable: "merge-queue commit check suite unavailable",
|
|
122
|
+
};
|
|
123
|
+
}
|
|
124
|
+
failedJobs = mapFailedJobs(suite);
|
|
125
|
+
}
|
|
126
|
+
runs.push({
|
|
127
|
+
source: "merge-queue",
|
|
128
|
+
...(candidate.pr !== undefined && { pr: candidate.pr }),
|
|
129
|
+
headSha: candidate.headSha.slice(0, 7),
|
|
130
|
+
runId: candidate.runId,
|
|
131
|
+
conclusion: candidate.conclusion,
|
|
132
|
+
completedAt: candidate.completedAt,
|
|
133
|
+
failedJobs,
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
return { runs, incomplete: false };
|
|
137
|
+
}
|