@kana-ai/edgar-ext-github 0.0.0-stage → 0.1.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 +202 -0
- package/README.md +469 -2
- package/agents/dismiss/agent.toml +4 -0
- package/agents/dismiss/instructions.md +25 -0
- package/dist/src/api/app.d.ts +16 -0
- package/dist/src/api/app.js +32 -0
- package/dist/src/api/client.d.ts +26 -0
- package/dist/src/api/client.js +103 -0
- package/dist/src/api/failures.d.ts +8 -0
- package/dist/src/api/failures.js +31 -0
- package/dist/src/feed/alerts.d.ts +26 -0
- package/dist/src/feed/alerts.js +117 -0
- package/dist/src/feed/cards.d.ts +32 -0
- package/dist/src/feed/cards.js +164 -0
- package/dist/src/feed/service.d.ts +28 -0
- package/dist/src/feed/service.js +163 -0
- package/dist/src/index.d.ts +490 -0
- package/dist/src/index.js +62 -0
- package/dist/src/schemas/config.d.ts +57 -0
- package/dist/src/schemas/config.js +89 -0
- package/dist/src/schemas/github.d.ts +417 -0
- package/dist/src/schemas/github.js +216 -0
- package/dist/src/schemas/tools.d.ts +620 -0
- package/dist/src/schemas/tools.js +501 -0
- package/dist/src/tools/alerts.d.ts +45 -0
- package/dist/src/tools/alerts.js +100 -0
- package/dist/src/tools/branch.d.ts +9 -0
- package/dist/src/tools/branch.js +48 -0
- package/dist/src/tools/checks.d.ts +118 -0
- package/dist/src/tools/checks.js +223 -0
- package/dist/src/tools/discussion.d.ts +41 -0
- package/dist/src/tools/discussion.js +66 -0
- package/dist/src/tools/pull-request.d.ts +195 -0
- package/dist/src/tools/pull-request.js +537 -0
- package/dist/src/tools/repository.d.ts +53 -0
- package/dist/src/tools/repository.js +134 -0
- package/dist/src/tools/screenshot.d.ts +21 -0
- package/dist/src/tools/screenshot.js +99 -0
- package/dist/src/tracker/assignee.d.ts +15 -0
- package/dist/src/tracker/assignee.js +65 -0
- package/dist/src/tracker/cards.d.ts +41 -0
- package/dist/src/tracker/cards.js +361 -0
- package/dist/src/tracker/comments.d.ts +9 -0
- package/dist/src/tracker/comments.js +68 -0
- package/dist/src/tracker/description.d.ts +19 -0
- package/dist/src/tracker/description.js +55 -0
- package/dist/src/tracker/hand-back.d.ts +8 -0
- package/dist/src/tracker/hand-back.js +60 -0
- package/dist/src/tracker/port.d.ts +2 -0
- package/dist/src/tracker/port.js +42 -0
- package/dist/src/tracker/states.d.ts +77 -0
- package/dist/src/tracker/states.js +189 -0
- package/dist/src/tracker/tools.d.ts +143 -0
- package/dist/src/tracker/tools.js +131 -0
- package/dist/src/tracker/transitions.d.ts +12 -0
- package/dist/src/tracker/transitions.js +101 -0
- package/edgar-extension.json +507 -0
- package/package.json +43 -3
- package/schemas/assign.in.json +15 -0
- package/schemas/assign.out.json +14 -0
- package/schemas/attach_screenshot.in.json +44 -0
- package/schemas/attach_screenshot.out.json +31 -0
- package/schemas/batch_cards.in.json +27 -0
- package/schemas/batch_cards.out.json +37 -0
- package/schemas/branch_protection.in.json +15 -0
- package/schemas/branch_protection.out.json +67 -0
- package/schemas/ci_run.in.json +12 -0
- package/schemas/ci_run.out.json +109 -0
- package/schemas/close.in.json +14 -0
- package/schemas/close.out.json +10 -0
- package/schemas/close_pull_request.in.json +21 -0
- package/schemas/close_pull_request.out.json +14 -0
- package/schemas/comment.in.json +19 -0
- package/schemas/comment.out.json +10 -0
- package/schemas/config.json +202 -0
- package/schemas/count_sighting.in.json +17 -0
- package/schemas/count_sighting.out.json +10 -0
- package/schemas/create_card.in.json +108 -0
- package/schemas/create_card.out.json +13 -0
- package/schemas/dependabot_alerts.in.json +49 -0
- package/schemas/dependabot_alerts.out.json +75 -0
- package/schemas/dismiss_dependabot_alert.in.json +26 -0
- package/schemas/dismiss_dependabot_alert.out.json +27 -0
- package/schemas/find_assignee.in.json +15 -0
- package/schemas/find_assignee.out.json +10 -0
- package/schemas/find_cards.in.json +18 -0
- package/schemas/find_cards.out.json +33 -0
- package/schemas/find_dependabot_dismissal.in.json +12 -0
- package/schemas/find_dependabot_dismissal.out.json +27 -0
- package/schemas/find_description.in.json +15 -0
- package/schemas/find_description.out.json +10 -0
- package/schemas/find_pull_request.in.json +25 -0
- package/schemas/find_pull_request.out.json +14 -0
- package/schemas/hand_back.in.json +11 -0
- package/schemas/hand_back.out.json +18 -0
- package/schemas/linked_pull_requests.in.json +15 -0
- package/schemas/linked_pull_requests.out.json +44 -0
- package/schemas/list_pull_requests.in.json +26 -0
- package/schemas/list_pull_requests.out.json +42 -0
- package/schemas/merge_pull_request.in.json +22 -0
- package/schemas/merge_pull_request.out.json +16 -0
- package/schemas/move.in.json +15 -0
- package/schemas/move.out.json +10 -0
- package/schemas/note_pull_request.in.json +15 -0
- package/schemas/note_pull_request.out.json +10 -0
- package/schemas/open_pull_request.in.json +113 -0
- package/schemas/open_pull_request.out.json +15 -0
- package/schemas/pull_request_checks.in.json +11 -0
- package/schemas/pull_request_checks.out.json +72 -0
- package/schemas/pull_request_discussion.in.json +11 -0
- package/schemas/pull_request_discussion.out.json +107 -0
- package/schemas/pull_request_state.in.json +11 -0
- package/schemas/pull_request_state.out.json +52 -0
- package/schemas/push_branch.in.json +21 -0
- package/schemas/push_branch.out.json +13 -0
- package/schemas/release_follow_ups.in.json +15 -0
- package/schemas/release_follow_ups.out.json +13 -0
- package/schemas/reopen_dependabot_alert.in.json +33 -0
- package/schemas/reopen_dependabot_alert.out.json +22 -0
- package/schemas/restore_assignee.in.json +20 -0
- package/schemas/restore_assignee.out.json +10 -0
- package/schemas/restore_description.in.json +20 -0
- package/schemas/restore_description.out.json +10 -0
- package/schemas/update_description.in.json +15 -0
- package/schemas/update_description.out.json +14 -0
- package/schemas/update_pull_request.in.json +81 -0
- package/schemas/update_pull_request.out.json +10 -0
- package/schemas/wait_for_checks.in.json +11 -0
- package/schemas/wait_for_checks.out.json +72 -0
- package/schemas/wait_for_merge.in.json +11 -0
- package/schemas/wait_for_merge.out.json +17 -0
- package/schemas/wait_for_passed_checks.in.json +11 -0
- package/schemas/wait_for_passed_checks.out.json +72 -0
- package/schemas/wait_for_review.in.json +11 -0
- package/schemas/wait_for_review.out.json +52 -0
- package/workflows/resume.toml +17 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
import { defineTool, done, err, ok, permanentFailure, portFailure, } from "@kana-ai/edgar-sdk";
|
|
2
|
+
import { pushBranch as schema } from "#schemas/tools";
|
|
3
|
+
function pushable(branch) {
|
|
4
|
+
return /^edgar\/[\w.-]+(?:\/[\w.-]+)*$/.test(branch) && !branch.includes("..");
|
|
5
|
+
}
|
|
6
|
+
async function git(ctx, args) {
|
|
7
|
+
const ran = await ctx.sandbox.exec("git", args);
|
|
8
|
+
if (!ran.ok)
|
|
9
|
+
return err(portFailure(ran.error));
|
|
10
|
+
if (ran.value.exit_code !== 0) {
|
|
11
|
+
return err(permanentFailure("tool_error", `git ${args.join(" ")} failed`, {
|
|
12
|
+
cause: `exit ${String(ran.value.exit_code)}: ${ran.value.stderr.trim()}`,
|
|
13
|
+
}));
|
|
14
|
+
}
|
|
15
|
+
return ok(ran.value.stdout.trim());
|
|
16
|
+
}
|
|
17
|
+
/** Pushes through the sandbox's git, never forced. With `to`, the head of the sandbox goes to that `edgar/` branch of origin, the branch of an open pull request: what the checks ran on is what is pushed, whatever a role named its own branch. With `branch` alone, that `edgar/` branch goes to origin under its own name. */
|
|
18
|
+
export const pushBranch = defineTool({
|
|
19
|
+
description: "Push to origin through the sandbox, never forced: with `to`, the head of the sandbox to that edgar/ branch; with `branch` alone, that edgar/ branch under its own name",
|
|
20
|
+
input: schema.input,
|
|
21
|
+
output: schema.output,
|
|
22
|
+
capabilities: ["git:push", "sandbox:read"],
|
|
23
|
+
execute: async ({ branch, to }, ctx) => {
|
|
24
|
+
const target = to ?? branch;
|
|
25
|
+
if (target === undefined) {
|
|
26
|
+
return permanentFailure("invalid_input", "push_branch needs `branch` or `to`");
|
|
27
|
+
}
|
|
28
|
+
const refused = [branch ?? target, target].find((name) => !pushable(name));
|
|
29
|
+
if (refused !== undefined) {
|
|
30
|
+
return permanentFailure("refused", `push_branch pushes only edgar/ branches, not ${refused}`);
|
|
31
|
+
}
|
|
32
|
+
const commit = await git(ctx, [
|
|
33
|
+
"rev-parse",
|
|
34
|
+
"--verify",
|
|
35
|
+
to === undefined ? `refs/heads/${target}` : "HEAD",
|
|
36
|
+
]);
|
|
37
|
+
if (!commit.ok)
|
|
38
|
+
return commit.error;
|
|
39
|
+
const pushed = await git(ctx, [
|
|
40
|
+
"push",
|
|
41
|
+
"origin",
|
|
42
|
+
to === undefined ? target : `HEAD:refs/heads/${to}`,
|
|
43
|
+
]);
|
|
44
|
+
if (!pushed.ok)
|
|
45
|
+
return pushed.error;
|
|
46
|
+
return done({ branch: target, commit: commit.value });
|
|
47
|
+
},
|
|
48
|
+
});
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import type { z } from "zod";
|
|
2
|
+
import { checkRuns, combinedStatus } from "#schemas/github";
|
|
3
|
+
import { type checksState } from "#schemas/tools";
|
|
4
|
+
/** The wait a caller gives before it reads pending checks again. */
|
|
5
|
+
export declare const RECHECK_SECONDS = 60;
|
|
6
|
+
/** How long after a pull request's last update `wait_for_checks` waits for GitHub to register a first check. */
|
|
7
|
+
export declare const FIRST_CHECK_GRACE_SECONDS = 120;
|
|
8
|
+
/** One check of a commit as `pull_request_checks` judges it. */
|
|
9
|
+
export interface Check {
|
|
10
|
+
readonly name: string;
|
|
11
|
+
readonly url: string | null;
|
|
12
|
+
readonly state: z.infer<typeof checksState>;
|
|
13
|
+
}
|
|
14
|
+
/** Each check run and each commit status as one list: a run that has not completed, or a status that is `pending`, is pending. */
|
|
15
|
+
export declare function checksOf(runs: z.infer<typeof checkRuns>, status: z.infer<typeof combinedStatus>): readonly Check[];
|
|
16
|
+
/** `failed` when one check failed, else `pending` while one runs, else `passed`; no check at all is `passed`. */
|
|
17
|
+
export declare function stateOfChecks(checks: readonly Check[]): z.infer<typeof checksState>;
|
|
18
|
+
/** How much of a failed job's log one `ci_run` answer carries. */
|
|
19
|
+
export declare const LOG_LIMITS: {
|
|
20
|
+
readonly jobs: 3;
|
|
21
|
+
readonly lines: 120;
|
|
22
|
+
readonly chars: 16000;
|
|
23
|
+
readonly errors: 20;
|
|
24
|
+
};
|
|
25
|
+
/** A run, and the job to read, from `https://github.com/<owner>/<name>/actions/runs/<id>[/attempts/<n>][/job/<id>]`. */
|
|
26
|
+
export declare function runOf(url: string): {
|
|
27
|
+
repo: string;
|
|
28
|
+
run: string;
|
|
29
|
+
job?: string;
|
|
30
|
+
} | undefined;
|
|
31
|
+
/** The lines GitHub marked `##[error]`, and the lines up to and with the first of them (the end of the log when none), bounded by `LOG_LIMITS`; timestamps and colour codes are gone. */
|
|
32
|
+
export declare function excerptOf(log: string): {
|
|
33
|
+
errors: string[];
|
|
34
|
+
excerpt: string;
|
|
35
|
+
};
|
|
36
|
+
/** One sentence a later stage can post as it is: what passed, what failed with its link, or how many still run. */
|
|
37
|
+
export declare function summaryOf(checks: readonly Check[], commit: string): string;
|
|
38
|
+
/** The CI tools of the GitHub extension, by the name the manifest gives them. */
|
|
39
|
+
export declare const checkTools: {
|
|
40
|
+
/** shortcut: one page of 100 check runs and 100 statuses; upgrade to `Link` pagination when a commit has more. */
|
|
41
|
+
pull_request_checks: import("@kana-ai/edgar-sdk").Tool<{
|
|
42
|
+
url: string;
|
|
43
|
+
}, {
|
|
44
|
+
url: string;
|
|
45
|
+
commit: string;
|
|
46
|
+
state: "closed" | "failed" | "passed" | "pending";
|
|
47
|
+
merged: boolean;
|
|
48
|
+
checks: number;
|
|
49
|
+
failed: {
|
|
50
|
+
name: string;
|
|
51
|
+
url: string | null;
|
|
52
|
+
}[];
|
|
53
|
+
retry_after_seconds: number | null;
|
|
54
|
+
summary: string;
|
|
55
|
+
changed_files: number;
|
|
56
|
+
}, "GITHUB_TOKEN">;
|
|
57
|
+
/** The recheck of a `wait` stage: a recheck's `done` ends the wait, so pending, and no check yet on a pull request that was just updated, answer `skipped`. */
|
|
58
|
+
wait_for_checks: import("@kana-ai/edgar-sdk").Tool<{
|
|
59
|
+
url: string;
|
|
60
|
+
}, {
|
|
61
|
+
url: string;
|
|
62
|
+
commit: string;
|
|
63
|
+
state: "closed" | "failed" | "passed" | "pending";
|
|
64
|
+
merged: boolean;
|
|
65
|
+
checks: number;
|
|
66
|
+
failed: {
|
|
67
|
+
name: string;
|
|
68
|
+
url: string | null;
|
|
69
|
+
}[];
|
|
70
|
+
retry_after_seconds: number | null;
|
|
71
|
+
summary: string;
|
|
72
|
+
changed_files: number;
|
|
73
|
+
}, "GITHUB_TOKEN">;
|
|
74
|
+
/** The recheck of a wait that only passed checks end: a failed check keeps the wait open, so a later push or a rerun that passes ends it. */
|
|
75
|
+
wait_for_passed_checks: import("@kana-ai/edgar-sdk").Tool<{
|
|
76
|
+
url: string;
|
|
77
|
+
}, {
|
|
78
|
+
url: string;
|
|
79
|
+
commit: string;
|
|
80
|
+
state: "closed" | "failed" | "passed" | "pending";
|
|
81
|
+
merged: boolean;
|
|
82
|
+
checks: number;
|
|
83
|
+
failed: {
|
|
84
|
+
name: string;
|
|
85
|
+
url: string | null;
|
|
86
|
+
}[];
|
|
87
|
+
retry_after_seconds: number | null;
|
|
88
|
+
summary: string;
|
|
89
|
+
changed_files: number;
|
|
90
|
+
}, "GITHUB_TOKEN">;
|
|
91
|
+
/** shortcut: the latest attempt's jobs only, one page of 100; upgrade to `attempts/<n>/jobs` when a re-run that passed hides the failure a card is about. */
|
|
92
|
+
ci_run: import("@kana-ai/edgar-sdk").Tool<{
|
|
93
|
+
url: string;
|
|
94
|
+
}, {
|
|
95
|
+
url: string;
|
|
96
|
+
workflow: string | null;
|
|
97
|
+
status: string | null;
|
|
98
|
+
conclusion: string | null;
|
|
99
|
+
commit: string;
|
|
100
|
+
branch: string | null;
|
|
101
|
+
attempt: number;
|
|
102
|
+
jobs: {
|
|
103
|
+
id: number;
|
|
104
|
+
name: string;
|
|
105
|
+
status: string;
|
|
106
|
+
conclusion: string | null;
|
|
107
|
+
failed_steps: string[];
|
|
108
|
+
}[];
|
|
109
|
+
logs: {
|
|
110
|
+
job: number;
|
|
111
|
+
name: string;
|
|
112
|
+
gone: boolean;
|
|
113
|
+
errors: string[];
|
|
114
|
+
excerpt: string;
|
|
115
|
+
}[];
|
|
116
|
+
unread: number[];
|
|
117
|
+
}, "GITHUB_TOKEN">;
|
|
118
|
+
};
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { defineTool, done, permanentFailure, skipped, } from "@kana-ai/edgar-sdk";
|
|
2
|
+
import { LOG_HOSTS, apiAccess, github, githubText } from "#api/client";
|
|
3
|
+
import { checkRuns, combinedStatus, jobs, onePull, workflowRun } from "#schemas/github";
|
|
4
|
+
import { ciRun, pullRequestChecks } from "#schemas/tools";
|
|
5
|
+
import { pullOf } from "#tools/pull-request";
|
|
6
|
+
/** The wait a caller gives before it reads pending checks again. */
|
|
7
|
+
export const RECHECK_SECONDS = 60;
|
|
8
|
+
/** How long after a pull request's last update `wait_for_checks` waits for GitHub to register a first check. */
|
|
9
|
+
export const FIRST_CHECK_GRACE_SECONDS = 120;
|
|
10
|
+
const failedConclusions = new Set([
|
|
11
|
+
"failure",
|
|
12
|
+
"timed_out",
|
|
13
|
+
"cancelled",
|
|
14
|
+
"action_required",
|
|
15
|
+
"startup_failure",
|
|
16
|
+
"stale",
|
|
17
|
+
]);
|
|
18
|
+
/** Each check run and each commit status as one list: a run that has not completed, or a status that is `pending`, is pending. */
|
|
19
|
+
export function checksOf(runs, status) {
|
|
20
|
+
const fromRuns = runs.check_runs.map((run) => {
|
|
21
|
+
const url = run.details_url ?? run.html_url;
|
|
22
|
+
if (run.status !== "completed")
|
|
23
|
+
return { name: run.name, url, state: "pending" };
|
|
24
|
+
const failed = failedConclusions.has(run.conclusion ?? "");
|
|
25
|
+
return { name: run.name, url, state: failed ? "failed" : "passed" };
|
|
26
|
+
});
|
|
27
|
+
const fromStatuses = status.statuses.map((s) => ({
|
|
28
|
+
name: s.context,
|
|
29
|
+
url: s.target_url,
|
|
30
|
+
state: s.state === "pending" ? "pending" : s.state === "success" ? "passed" : "failed",
|
|
31
|
+
}));
|
|
32
|
+
return [...fromRuns, ...fromStatuses];
|
|
33
|
+
}
|
|
34
|
+
/** `failed` when one check failed, else `pending` while one runs, else `passed`; no check at all is `passed`. */
|
|
35
|
+
export function stateOfChecks(checks) {
|
|
36
|
+
if (checks.some((c) => c.state === "failed"))
|
|
37
|
+
return "failed";
|
|
38
|
+
return checks.some((c) => c.state === "pending") ? "pending" : "passed";
|
|
39
|
+
}
|
|
40
|
+
/** How much of a failed job's log one `ci_run` answer carries. */
|
|
41
|
+
export const LOG_LIMITS = { jobs: 3, lines: 120, chars: 16_000, errors: 20 };
|
|
42
|
+
const jobFailed = new Set(["failure", "timed_out"]);
|
|
43
|
+
/** A run, and the job to read, from `https://github.com/<owner>/<name>/actions/runs/<id>[/attempts/<n>][/job/<id>]`. */
|
|
44
|
+
export function runOf(url) {
|
|
45
|
+
const [, repo, run, job] = /^https:\/\/github\.com\/([\w.-]+\/[\w.-]+)\/actions\/runs\/(\d+)(?:\/attempts\/\d+)?(?:\/job\/(\d+))?\/?$/.exec(url) ?? [];
|
|
46
|
+
if (repo === undefined || run === undefined)
|
|
47
|
+
return undefined;
|
|
48
|
+
return job === undefined ? { repo, run } : { repo, run, job };
|
|
49
|
+
}
|
|
50
|
+
/** The lines GitHub marked `##[error]`, and the lines up to and with the first of them (the end of the log when none), bounded by `LOG_LIMITS`; timestamps and colour codes are gone. */
|
|
51
|
+
export function excerptOf(log) {
|
|
52
|
+
const lines = log
|
|
53
|
+
.replace(/^\uFEFF/, "")
|
|
54
|
+
.split("\n")
|
|
55
|
+
.map((line) => line.replace(/^\d{4}-\d\d-\d\dT[\d:.]+Z ?/, "").replace(/\u001b\[[0-9;?]*[A-Za-z]/g, ""));
|
|
56
|
+
const errors = lines
|
|
57
|
+
.filter((line) => line.includes("##[error]"))
|
|
58
|
+
.map((line) => line.replace(/^.*##\[error\]/, "").slice(0, 500))
|
|
59
|
+
.slice(0, LOG_LIMITS.errors);
|
|
60
|
+
const first = lines.findIndex((line) => line.includes("##[error]"));
|
|
61
|
+
const end = first === -1 ? lines.length : first + 1;
|
|
62
|
+
const excerpt = lines.slice(Math.max(0, end - LOG_LIMITS.lines), end).join("\n");
|
|
63
|
+
return { errors, excerpt: excerpt.slice(-LOG_LIMITS.chars) };
|
|
64
|
+
}
|
|
65
|
+
/** One sentence a later stage can post as it is: what passed, what failed with its link, or how many still run. */
|
|
66
|
+
export function summaryOf(checks, commit) {
|
|
67
|
+
const short = commit.slice(0, 7);
|
|
68
|
+
const failed = checks.filter((c) => c.state === "failed");
|
|
69
|
+
const running = checks.filter((c) => c.state === "pending").length;
|
|
70
|
+
const total = checks.length;
|
|
71
|
+
if (failed.length > 0) {
|
|
72
|
+
const named = failed.map((c) => (c.url === null ? c.name : `${c.name} (${c.url})`)).join(", ");
|
|
73
|
+
return `${String(failed.length)} of ${String(total)} checks failed: ${named}.`;
|
|
74
|
+
}
|
|
75
|
+
if (running > 0)
|
|
76
|
+
return running === 1 ? "1 check still runs." : `${String(running)} checks still run.`;
|
|
77
|
+
if (total === 0)
|
|
78
|
+
return `No checks ran on ${short}.`;
|
|
79
|
+
return total === 1
|
|
80
|
+
? `The 1 check passed on ${short}.`
|
|
81
|
+
: `All ${String(total)} checks passed on ${short}.`;
|
|
82
|
+
}
|
|
83
|
+
/** Reads the head commit of the pull request at `url` and the files it changes, then its check runs and statuses, and judges them. A closed or merged pull request is `closed` with no check read, so no wait goes on for it. With `now`, a pull request with no check that was updated less than `FIRST_CHECK_GRACE_SECONDS` before it is `skipped`. */
|
|
84
|
+
async function checksOfPull(ctx, url, now) {
|
|
85
|
+
const path = pullOf(url);
|
|
86
|
+
if (!path.ok)
|
|
87
|
+
return path.error;
|
|
88
|
+
const read = await github(ctx, "GET", path.value, onePull);
|
|
89
|
+
if (!read.ok)
|
|
90
|
+
return read.error;
|
|
91
|
+
const commit = read.value.head.sha;
|
|
92
|
+
const merged = read.value.merged_at !== null;
|
|
93
|
+
if (read.value.state === "closed") {
|
|
94
|
+
return done({
|
|
95
|
+
url: read.value.html_url,
|
|
96
|
+
commit,
|
|
97
|
+
state: "closed",
|
|
98
|
+
merged,
|
|
99
|
+
checks: 0,
|
|
100
|
+
failed: [],
|
|
101
|
+
retry_after_seconds: null,
|
|
102
|
+
summary: merged
|
|
103
|
+
? "The pull request was merged."
|
|
104
|
+
: "The pull request was closed with no merge.",
|
|
105
|
+
changed_files: read.value.changed_files,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
const repo = path.value.replace(/\/pulls\/\d+$/, "");
|
|
109
|
+
const runs = await github(ctx, "GET", `${repo}/commits/${commit}/check-runs?per_page=100`, checkRuns);
|
|
110
|
+
if (!runs.ok)
|
|
111
|
+
return runs.error;
|
|
112
|
+
const status = await github(ctx, "GET", `${repo}/commits/${commit}/status?per_page=100`, combinedStatus);
|
|
113
|
+
if (!status.ok)
|
|
114
|
+
return status.error;
|
|
115
|
+
const checks = checksOf(runs.value, status.value);
|
|
116
|
+
const sinceUpdate = (now ?? Infinity) - Date.parse(read.value.updated_at);
|
|
117
|
+
if (checks.length === 0 && sinceUpdate < FIRST_CHECK_GRACE_SECONDS * 1000)
|
|
118
|
+
return skipped();
|
|
119
|
+
const state = stateOfChecks(checks);
|
|
120
|
+
return done({
|
|
121
|
+
url: read.value.html_url,
|
|
122
|
+
commit,
|
|
123
|
+
state,
|
|
124
|
+
merged,
|
|
125
|
+
checks: checks.length,
|
|
126
|
+
failed: checks.filter((c) => c.state === "failed").map(({ name, url }) => ({ name, url })),
|
|
127
|
+
retry_after_seconds: state === "pending" ? RECHECK_SECONDS : null,
|
|
128
|
+
summary: summaryOf(checks, commit),
|
|
129
|
+
changed_files: read.value.changed_files,
|
|
130
|
+
});
|
|
131
|
+
}
|
|
132
|
+
/** The CI tools of the GitHub extension, by the name the manifest gives them. */
|
|
133
|
+
export const checkTools = {
|
|
134
|
+
/** shortcut: one page of 100 check runs and 100 statuses; upgrade to `Link` pagination when a commit has more. */
|
|
135
|
+
pull_request_checks: defineTool({
|
|
136
|
+
description: "Read the check runs and commit statuses of a pull request's head commit: pending, passed or failed, or closed when the pull request is closed or merged, with each failed check's name and details URL, and the files the pull request changes",
|
|
137
|
+
input: pullRequestChecks.input,
|
|
138
|
+
output: pullRequestChecks.output,
|
|
139
|
+
...apiAccess,
|
|
140
|
+
execute: async ({ url }, ctx) => await checksOfPull(ctx, url),
|
|
141
|
+
}),
|
|
142
|
+
/** The recheck of a `wait` stage: a recheck's `done` ends the wait, so pending, and no check yet on a pull request that was just updated, answer `skipped`. */
|
|
143
|
+
wait_for_checks: defineTool({
|
|
144
|
+
description: "As pull_request_checks, for a wait stage's recheck: skipped while a check runs or while a pull request updated in the last 2 minutes has no check yet, done once every check has ended or the pull request is closed or merged",
|
|
145
|
+
input: pullRequestChecks.input,
|
|
146
|
+
output: pullRequestChecks.output,
|
|
147
|
+
...apiAccess,
|
|
148
|
+
execute: async ({ url }, ctx) => {
|
|
149
|
+
const read = await checksOfPull(ctx, url, Date.now());
|
|
150
|
+
return read.outcome === "done" && read.output.state === "pending" ? skipped() : read;
|
|
151
|
+
},
|
|
152
|
+
}),
|
|
153
|
+
/** The recheck of a wait that only passed checks end: a failed check keeps the wait open, so a later push or a rerun that passes ends it. */
|
|
154
|
+
wait_for_passed_checks: defineTool({
|
|
155
|
+
description: "As wait_for_checks, for a wait that ends only when the checks pass: skipped also while a check has failed, done once every check has passed or the pull request is closed or merged",
|
|
156
|
+
input: pullRequestChecks.input,
|
|
157
|
+
output: pullRequestChecks.output,
|
|
158
|
+
...apiAccess,
|
|
159
|
+
execute: async ({ url }, ctx) => {
|
|
160
|
+
const read = await checksOfPull(ctx, url, Date.now());
|
|
161
|
+
const ended = read.outcome !== "done" || ["passed", "closed"].includes(read.output.state);
|
|
162
|
+
return ended ? read : skipped();
|
|
163
|
+
},
|
|
164
|
+
}),
|
|
165
|
+
/** shortcut: the latest attempt's jobs only, one page of 100; upgrade to `attempts/<n>/jobs` when a re-run that passed hides the failure a card is about. */
|
|
166
|
+
ci_run: defineTool({
|
|
167
|
+
description: "Read a GitHub Actions run: its workflow, commit and conclusion, each job with its failed steps, and the error lines and a bounded excerpt of each failed job's log",
|
|
168
|
+
input: ciRun.input,
|
|
169
|
+
output: ciRun.output,
|
|
170
|
+
secrets: apiAccess.secrets,
|
|
171
|
+
capabilities: [
|
|
172
|
+
...apiAccess.capabilities,
|
|
173
|
+
...LOG_HOSTS.map((host) => `network:${host}`),
|
|
174
|
+
],
|
|
175
|
+
execute: async ({ url }, ctx) => {
|
|
176
|
+
const asked = runOf(url);
|
|
177
|
+
if (asked === undefined) {
|
|
178
|
+
return permanentFailure("invalid_input", `${url} is not a GitHub Actions run URL`);
|
|
179
|
+
}
|
|
180
|
+
const base = `/repos/${asked.repo}/actions`;
|
|
181
|
+
const run = await github(ctx, "GET", `${base}/runs/${asked.run}`, workflowRun);
|
|
182
|
+
if (!run.ok)
|
|
183
|
+
return run.error;
|
|
184
|
+
const listed = await github(ctx, "GET", `${base}/runs/${asked.run}/jobs?per_page=100`, jobs);
|
|
185
|
+
if (!listed.ok)
|
|
186
|
+
return listed.error;
|
|
187
|
+
const all = listed.value.jobs;
|
|
188
|
+
const chosen = asked.job === undefined
|
|
189
|
+
? all.filter((j) => jobFailed.has(j.conclusion ?? ""))
|
|
190
|
+
: all.filter((j) => String(j.id) === asked.job);
|
|
191
|
+
if (asked.job !== undefined && chosen.length === 0) {
|
|
192
|
+
return permanentFailure("not_found", `job ${asked.job} is not in run ${asked.run}`);
|
|
193
|
+
}
|
|
194
|
+
const logs = [];
|
|
195
|
+
for (const job of chosen.slice(0, LOG_LIMITS.jobs)) {
|
|
196
|
+
const text = await githubText(ctx, `${base}/jobs/${String(job.id)}/logs`);
|
|
197
|
+
if (!text.ok && text.error.kind !== "not_found")
|
|
198
|
+
return text.error;
|
|
199
|
+
const read = text.ok ? excerptOf(text.value) : { errors: [], excerpt: "" };
|
|
200
|
+
logs.push({ job: job.id, name: job.name, gone: !text.ok, ...read });
|
|
201
|
+
}
|
|
202
|
+
const r = run.value;
|
|
203
|
+
return done({
|
|
204
|
+
url: r.html_url,
|
|
205
|
+
workflow: r.name,
|
|
206
|
+
status: r.status,
|
|
207
|
+
conclusion: r.conclusion,
|
|
208
|
+
commit: r.head_sha,
|
|
209
|
+
branch: r.head_branch,
|
|
210
|
+
attempt: r.run_attempt,
|
|
211
|
+
jobs: all.map((j) => ({
|
|
212
|
+
id: j.id,
|
|
213
|
+
name: j.name,
|
|
214
|
+
status: j.status,
|
|
215
|
+
conclusion: j.conclusion,
|
|
216
|
+
failed_steps: j.steps.filter((s) => jobFailed.has(s.conclusion ?? "")).map((s) => s.name),
|
|
217
|
+
})),
|
|
218
|
+
logs,
|
|
219
|
+
unread: chosen.slice(LOG_LIMITS.jobs).map((j) => j.id),
|
|
220
|
+
});
|
|
221
|
+
},
|
|
222
|
+
}),
|
|
223
|
+
};
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import { z } from "zod";
|
|
2
|
+
import { lineComment } from "#schemas/github";
|
|
3
|
+
import { pullRequestDiscussion } from "#schemas/tools";
|
|
4
|
+
type Output = z.infer<typeof pullRequestDiscussion.output>;
|
|
5
|
+
/** Line comments as threads: each reply goes under the comment it answers, in the order GitHub gives them. */
|
|
6
|
+
export declare function threadsOf(comments: readonly z.infer<typeof lineComment>[]): Output["threads"];
|
|
7
|
+
/** The discussion tool of the GitHub extension, by the name the manifest gives it. */
|
|
8
|
+
export declare const discussionTools: {
|
|
9
|
+
/** shortcut: one page of 100 of each list, said by `complete`; upgrade to `Link` pagination when a pull request holds more. */
|
|
10
|
+
pull_request_discussion: import("@kana-ai/edgar-sdk").Tool<{
|
|
11
|
+
url: string;
|
|
12
|
+
}, {
|
|
13
|
+
url: string;
|
|
14
|
+
reviews: {
|
|
15
|
+
by: string | null;
|
|
16
|
+
at: string | null;
|
|
17
|
+
body: string;
|
|
18
|
+
state: string;
|
|
19
|
+
}[];
|
|
20
|
+
threads: {
|
|
21
|
+
by: string | null;
|
|
22
|
+
at: string | null;
|
|
23
|
+
body: string;
|
|
24
|
+
path: string;
|
|
25
|
+
line: number | null;
|
|
26
|
+
hunk: string;
|
|
27
|
+
replies: {
|
|
28
|
+
by: string | null;
|
|
29
|
+
at: string | null;
|
|
30
|
+
body: string;
|
|
31
|
+
}[];
|
|
32
|
+
}[];
|
|
33
|
+
conversation: {
|
|
34
|
+
by: string | null;
|
|
35
|
+
at: string | null;
|
|
36
|
+
body: string;
|
|
37
|
+
}[];
|
|
38
|
+
complete: boolean;
|
|
39
|
+
}, "GITHUB_TOKEN">;
|
|
40
|
+
};
|
|
41
|
+
export {};
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { defineTool, done } from "@kana-ai/edgar-sdk";
|
|
2
|
+
import { z } from "zod";
|
|
3
|
+
import { apiAccess, github } from "#api/client";
|
|
4
|
+
import { issueComment, lineComment, said } from "#schemas/github";
|
|
5
|
+
import { pullRequestDiscussion } from "#schemas/tools";
|
|
6
|
+
import { pullOf } from "#tools/pull-request";
|
|
7
|
+
const PAGE = 100;
|
|
8
|
+
/** Line comments as threads: each reply goes under the comment it answers, in the order GitHub gives them. */
|
|
9
|
+
export function threadsOf(comments) {
|
|
10
|
+
const roots = new Map();
|
|
11
|
+
for (const c of comments) {
|
|
12
|
+
const said = { by: c.user?.login ?? null, at: c.created_at, body: c.body.trim() };
|
|
13
|
+
const root = c.in_reply_to_id === undefined ? undefined : roots.get(c.in_reply_to_id);
|
|
14
|
+
if (root !== undefined) {
|
|
15
|
+
root.replies.push(said);
|
|
16
|
+
roots.set(c.id, root);
|
|
17
|
+
continue;
|
|
18
|
+
}
|
|
19
|
+
const hunk = c.diff_hunk.split("\n").slice(-8).join("\n");
|
|
20
|
+
const line = c.line ?? c.original_line ?? null;
|
|
21
|
+
roots.set(c.id, { ...said, path: c.path, line, hunk, replies: [] });
|
|
22
|
+
}
|
|
23
|
+
return [...new Set(roots.values())];
|
|
24
|
+
}
|
|
25
|
+
/** The discussion tool of the GitHub extension, by the name the manifest gives it. */
|
|
26
|
+
export const discussionTools = {
|
|
27
|
+
/** shortcut: one page of 100 of each list, said by `complete`; upgrade to `Link` pagination when a pull request holds more. */
|
|
28
|
+
pull_request_discussion: defineTool({
|
|
29
|
+
description: "Read what people wrote on a pull request: each review verdict, the line comments as threads with the code they sit on, and the conversation",
|
|
30
|
+
input: pullRequestDiscussion.input,
|
|
31
|
+
output: pullRequestDiscussion.output,
|
|
32
|
+
...apiAccess,
|
|
33
|
+
execute: async ({ url }, ctx) => {
|
|
34
|
+
const path = pullOf(url);
|
|
35
|
+
if (!path.ok)
|
|
36
|
+
return path.error;
|
|
37
|
+
const issue = path.value.replace("/pulls/", "/issues/");
|
|
38
|
+
const reviews = await github(ctx, "GET", `${path.value}/reviews?per_page=${PAGE}`, z.array(said));
|
|
39
|
+
if (!reviews.ok)
|
|
40
|
+
return reviews.error;
|
|
41
|
+
const lines = await github(ctx, "GET", `${path.value}/comments?per_page=${PAGE}`, z.array(lineComment));
|
|
42
|
+
if (!lines.ok)
|
|
43
|
+
return lines.error;
|
|
44
|
+
const general = await github(ctx, "GET", `${issue}/comments?per_page=${PAGE}`, z.array(issueComment));
|
|
45
|
+
if (!general.ok)
|
|
46
|
+
return general.error;
|
|
47
|
+
const lists = [reviews.value, lines.value, general.value];
|
|
48
|
+
return done({
|
|
49
|
+
url,
|
|
50
|
+
reviews: reviews.value
|
|
51
|
+
.map((r) => ({
|
|
52
|
+
by: r.user?.login ?? null,
|
|
53
|
+
at: r.submitted_at ?? null,
|
|
54
|
+
body: (r.body ?? "").trim(),
|
|
55
|
+
state: r.state,
|
|
56
|
+
}))
|
|
57
|
+
.filter((r) => r.body !== "" || r.state !== "COMMENTED"),
|
|
58
|
+
threads: threadsOf(lines.value),
|
|
59
|
+
conversation: general.value
|
|
60
|
+
.map((c) => ({ by: c.user?.login ?? null, at: c.created_at, body: c.body.trim() }))
|
|
61
|
+
.filter((c) => c.body !== ""),
|
|
62
|
+
complete: lists.every((list) => list.length < PAGE),
|
|
63
|
+
});
|
|
64
|
+
},
|
|
65
|
+
}),
|
|
66
|
+
};
|