@ilya-lesikov/pi-pi 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/3p/pi-ask-user/index.ts +1835 -0
- package/3p/pi-ask-user/package.json +50 -0
- package/3p/pi-ask-user/single-select-layout.ts +203 -0
- package/3p/pi-lsp/extensions/lsp/client.ts +542 -0
- package/3p/pi-lsp/extensions/lsp/config.ts +135 -0
- package/3p/pi-lsp/extensions/lsp/effects/command.ts +42 -0
- package/3p/pi-lsp/extensions/lsp/effects/filesystem.ts +50 -0
- package/3p/pi-lsp/extensions/lsp/effects/runtime.ts +21 -0
- package/3p/pi-lsp/extensions/lsp/errors.ts +113 -0
- package/3p/pi-lsp/extensions/lsp/formatting.ts +315 -0
- package/3p/pi-lsp/extensions/lsp/index.ts +231 -0
- package/3p/pi-lsp/extensions/lsp/protocol.ts +209 -0
- package/3p/pi-lsp/extensions/lsp/retry.ts +39 -0
- package/3p/pi-lsp/extensions/lsp/tools/programs.ts +293 -0
- package/3p/pi-lsp/extensions/lsp/tools.ts +89 -0
- package/3p/pi-lsp/extensions/lsp/types.ts +243 -0
- package/3p/pi-lsp/package.json +54 -0
- package/3p/pi-plannotator/AGENTS.md +583 -0
- package/3p/pi-plannotator/apps/pi-extension/README.md +228 -0
- package/3p/pi-plannotator/apps/pi-extension/assistant-message.ts +128 -0
- package/3p/pi-plannotator/apps/pi-extension/config.test.ts +166 -0
- package/3p/pi-plannotator/apps/pi-extension/config.ts +318 -0
- package/3p/pi-plannotator/apps/pi-extension/current-pi-session.ts +147 -0
- package/3p/pi-plannotator/apps/pi-extension/index.ts +1279 -0
- package/3p/pi-plannotator/apps/pi-extension/package.json +56 -0
- package/3p/pi-plannotator/apps/pi-extension/plannotator-browser.test.ts +13 -0
- package/3p/pi-plannotator/apps/pi-extension/plannotator-browser.ts +616 -0
- package/3p/pi-plannotator/apps/pi-extension/plannotator-events.ts +342 -0
- package/3p/pi-plannotator/apps/pi-extension/plannotator.json +12 -0
- package/3p/pi-plannotator/apps/pi-extension/server/agent-jobs.ts +515 -0
- package/3p/pi-plannotator/apps/pi-extension/server/ai-runtime.ts +169 -0
- package/3p/pi-plannotator/apps/pi-extension/server/annotations.ts +85 -0
- package/3p/pi-plannotator/apps/pi-extension/server/external-annotations.ts +189 -0
- package/3p/pi-plannotator/apps/pi-extension/server/handlers.ts +210 -0
- package/3p/pi-plannotator/apps/pi-extension/server/helpers.ts +78 -0
- package/3p/pi-plannotator/apps/pi-extension/server/ide.ts +46 -0
- package/3p/pi-plannotator/apps/pi-extension/server/integrations.ts +195 -0
- package/3p/pi-plannotator/apps/pi-extension/server/network.test.ts +158 -0
- package/3p/pi-plannotator/apps/pi-extension/server/network.ts +268 -0
- package/3p/pi-plannotator/apps/pi-extension/server/pr.ts +126 -0
- package/3p/pi-plannotator/apps/pi-extension/server/project.ts +64 -0
- package/3p/pi-plannotator/apps/pi-extension/server/reference.ts +362 -0
- package/3p/pi-plannotator/apps/pi-extension/server/serverAnnotate.ts +199 -0
- package/3p/pi-plannotator/apps/pi-extension/server/serverPlan.ts +505 -0
- package/3p/pi-plannotator/apps/pi-extension/server/serverReview.ts +1229 -0
- package/3p/pi-plannotator/apps/pi-extension/server/vcs.ts +121 -0
- package/3p/pi-plannotator/apps/pi-extension/server.test.ts +966 -0
- package/3p/pi-plannotator/apps/pi-extension/server.ts +44 -0
- package/3p/pi-plannotator/apps/pi-extension/tool-scope.test.ts +88 -0
- package/3p/pi-plannotator/apps/pi-extension/tool-scope.ts +39 -0
- package/3p/pi-plannotator/apps/pi-extension/tsconfig.json +16 -0
- package/3p/pi-plannotator/apps/pi-extension/vendor.sh +48 -0
- package/3p/pi-plannotator/package.json +53 -0
- package/3p/pi-plannotator/packages/ai/ai.test.ts +1407 -0
- package/3p/pi-plannotator/packages/ai/base-session.ts +94 -0
- package/3p/pi-plannotator/packages/ai/context.ts +250 -0
- package/3p/pi-plannotator/packages/ai/endpoints.ts +326 -0
- package/3p/pi-plannotator/packages/ai/index.ts +105 -0
- package/3p/pi-plannotator/packages/ai/package.json +21 -0
- package/3p/pi-plannotator/packages/ai/provider.ts +103 -0
- package/3p/pi-plannotator/packages/ai/providers/claude-agent-sdk.ts +447 -0
- package/3p/pi-plannotator/packages/ai/providers/codex-sdk.ts +430 -0
- package/3p/pi-plannotator/packages/ai/providers/command-path.ts +115 -0
- package/3p/pi-plannotator/packages/ai/providers/opencode-sdk.ts +546 -0
- package/3p/pi-plannotator/packages/ai/providers/pi-events.ts +110 -0
- package/3p/pi-plannotator/packages/ai/providers/pi-sdk-node.ts +425 -0
- package/3p/pi-plannotator/packages/ai/providers/pi-sdk.ts +469 -0
- package/3p/pi-plannotator/packages/ai/session-manager.ts +195 -0
- package/3p/pi-plannotator/packages/ai/tsconfig.json +15 -0
- package/3p/pi-plannotator/packages/ai/types.ts +379 -0
- package/3p/pi-plannotator/packages/server/agent-jobs.ts +538 -0
- package/3p/pi-plannotator/packages/server/agent-review-message.test.ts +135 -0
- package/3p/pi-plannotator/packages/server/agent-review-message.ts +243 -0
- package/3p/pi-plannotator/packages/server/ai-runtime.ts +108 -0
- package/3p/pi-plannotator/packages/server/annotate.ts +401 -0
- package/3p/pi-plannotator/packages/server/browser.test.ts +87 -0
- package/3p/pi-plannotator/packages/server/browser.ts +245 -0
- package/3p/pi-plannotator/packages/server/claude-review.ts +352 -0
- package/3p/pi-plannotator/packages/server/code-nav.ts +73 -0
- package/3p/pi-plannotator/packages/server/codex-review-schema.json +41 -0
- package/3p/pi-plannotator/packages/server/codex-review.ts +330 -0
- package/3p/pi-plannotator/packages/server/config.ts +8 -0
- package/3p/pi-plannotator/packages/server/draft.ts +1 -0
- package/3p/pi-plannotator/packages/server/editor-annotations.ts +76 -0
- package/3p/pi-plannotator/packages/server/external-annotations.test.ts +18 -0
- package/3p/pi-plannotator/packages/server/external-annotations.ts +207 -0
- package/3p/pi-plannotator/packages/server/git.ts +141 -0
- package/3p/pi-plannotator/packages/server/goal-setup.test.ts +55 -0
- package/3p/pi-plannotator/packages/server/goal-setup.ts +248 -0
- package/3p/pi-plannotator/packages/server/ide.ts +43 -0
- package/3p/pi-plannotator/packages/server/image.test.ts +63 -0
- package/3p/pi-plannotator/packages/server/image.ts +66 -0
- package/3p/pi-plannotator/packages/server/index.ts +648 -0
- package/3p/pi-plannotator/packages/server/integrations.test.ts +172 -0
- package/3p/pi-plannotator/packages/server/integrations.ts +214 -0
- package/3p/pi-plannotator/packages/server/jj.test.ts +69 -0
- package/3p/pi-plannotator/packages/server/jj.ts +86 -0
- package/3p/pi-plannotator/packages/server/p4.ts +417 -0
- package/3p/pi-plannotator/packages/server/package.json +40 -0
- package/3p/pi-plannotator/packages/server/path-utils.ts +18 -0
- package/3p/pi-plannotator/packages/server/pr.ts +146 -0
- package/3p/pi-plannotator/packages/server/project.test.ts +115 -0
- package/3p/pi-plannotator/packages/server/project.ts +45 -0
- package/3p/pi-plannotator/packages/server/reference-handlers.ts +404 -0
- package/3p/pi-plannotator/packages/server/remote.test.ts +150 -0
- package/3p/pi-plannotator/packages/server/remote.ts +74 -0
- package/3p/pi-plannotator/packages/server/repo.ts +80 -0
- package/3p/pi-plannotator/packages/server/resolve-file.test.ts +310 -0
- package/3p/pi-plannotator/packages/server/review-workspace.test.ts +1032 -0
- package/3p/pi-plannotator/packages/server/review-workspace.ts +48 -0
- package/3p/pi-plannotator/packages/server/review.ts +1318 -0
- package/3p/pi-plannotator/packages/server/sessions.ts +111 -0
- package/3p/pi-plannotator/packages/server/share-url.ts +53 -0
- package/3p/pi-plannotator/packages/server/shared-handlers.test.ts +43 -0
- package/3p/pi-plannotator/packages/server/shared-handlers.ts +177 -0
- package/3p/pi-plannotator/packages/server/storage.test.ts +176 -0
- package/3p/pi-plannotator/packages/server/storage.ts +17 -0
- package/3p/pi-plannotator/packages/server/tour/tour-review.test.ts +146 -0
- package/3p/pi-plannotator/packages/server/tour/tour-review.ts +604 -0
- package/3p/pi-plannotator/packages/server/tsconfig.json +15 -0
- package/3p/pi-plannotator/packages/server/vcs.test.ts +48 -0
- package/3p/pi-plannotator/packages/server/vcs.ts +80 -0
- package/3p/pi-plannotator/packages/shared/agent-jobs.ts +132 -0
- package/3p/pi-plannotator/packages/shared/agents.ts +53 -0
- package/3p/pi-plannotator/packages/shared/annotate-args.test.ts +386 -0
- package/3p/pi-plannotator/packages/shared/annotate-args.ts +107 -0
- package/3p/pi-plannotator/packages/shared/at-reference.test.ts +99 -0
- package/3p/pi-plannotator/packages/shared/at-reference.ts +52 -0
- package/3p/pi-plannotator/packages/shared/checklist.ts +52 -0
- package/3p/pi-plannotator/packages/shared/code-file.test.ts +112 -0
- package/3p/pi-plannotator/packages/shared/code-file.ts +41 -0
- package/3p/pi-plannotator/packages/shared/code-nav.test.ts +515 -0
- package/3p/pi-plannotator/packages/shared/code-nav.ts +436 -0
- package/3p/pi-plannotator/packages/shared/compress.ts +51 -0
- package/3p/pi-plannotator/packages/shared/config.ts +262 -0
- package/3p/pi-plannotator/packages/shared/crypto.test.ts +172 -0
- package/3p/pi-plannotator/packages/shared/crypto.ts +97 -0
- package/3p/pi-plannotator/packages/shared/data-dir.ts +42 -0
- package/3p/pi-plannotator/packages/shared/diff-paths.test.ts +30 -0
- package/3p/pi-plannotator/packages/shared/diff-paths.ts +137 -0
- package/3p/pi-plannotator/packages/shared/draft.ts +64 -0
- package/3p/pi-plannotator/packages/shared/external-annotation.ts +397 -0
- package/3p/pi-plannotator/packages/shared/extract-code-paths.test.ts +59 -0
- package/3p/pi-plannotator/packages/shared/extract-code-paths.ts +66 -0
- package/3p/pi-plannotator/packages/shared/favicon.ts +5 -0
- package/3p/pi-plannotator/packages/shared/feedback-templates.test.ts +65 -0
- package/3p/pi-plannotator/packages/shared/feedback-templates.ts +29 -0
- package/3p/pi-plannotator/packages/shared/goal-setup.test.ts +231 -0
- package/3p/pi-plannotator/packages/shared/goal-setup.ts +336 -0
- package/3p/pi-plannotator/packages/shared/html-to-markdown.test.ts +62 -0
- package/3p/pi-plannotator/packages/shared/html-to-markdown.ts +32 -0
- package/3p/pi-plannotator/packages/shared/improvement-hooks.test.ts +135 -0
- package/3p/pi-plannotator/packages/shared/improvement-hooks.ts +115 -0
- package/3p/pi-plannotator/packages/shared/integrations-common.ts +243 -0
- package/3p/pi-plannotator/packages/shared/jj-core.test.ts +236 -0
- package/3p/pi-plannotator/packages/shared/jj-core.ts +433 -0
- package/3p/pi-plannotator/packages/shared/package.json +55 -0
- package/3p/pi-plannotator/packages/shared/pfm-reminder.test.ts +88 -0
- package/3p/pi-plannotator/packages/shared/pfm-reminder.ts +80 -0
- package/3p/pi-plannotator/packages/shared/pr-github.ts +661 -0
- package/3p/pi-plannotator/packages/shared/pr-gitlab.test.ts +202 -0
- package/3p/pi-plannotator/packages/shared/pr-gitlab.ts +620 -0
- package/3p/pi-plannotator/packages/shared/pr-provider.test.ts +266 -0
- package/3p/pi-plannotator/packages/shared/pr-provider.ts +123 -0
- package/3p/pi-plannotator/packages/shared/pr-stack.test.ts +104 -0
- package/3p/pi-plannotator/packages/shared/pr-stack.ts +194 -0
- package/3p/pi-plannotator/packages/shared/pr-types.ts +326 -0
- package/3p/pi-plannotator/packages/shared/project.ts +71 -0
- package/3p/pi-plannotator/packages/shared/prompts-integration.test.ts +421 -0
- package/3p/pi-plannotator/packages/shared/prompts.test.ts +504 -0
- package/3p/pi-plannotator/packages/shared/prompts.ts +247 -0
- package/3p/pi-plannotator/packages/shared/reference-common.ts +87 -0
- package/3p/pi-plannotator/packages/shared/repo.ts +71 -0
- package/3p/pi-plannotator/packages/shared/resolve-file.test.ts +113 -0
- package/3p/pi-plannotator/packages/shared/resolve-file.ts +509 -0
- package/3p/pi-plannotator/packages/shared/review-args.test.ts +64 -0
- package/3p/pi-plannotator/packages/shared/review-args.ts +85 -0
- package/3p/pi-plannotator/packages/shared/review-core.test.ts +286 -0
- package/3p/pi-plannotator/packages/shared/review-core.ts +895 -0
- package/3p/pi-plannotator/packages/shared/review-workspace-node.ts +230 -0
- package/3p/pi-plannotator/packages/shared/review-workspace.ts +436 -0
- package/3p/pi-plannotator/packages/shared/semantic-diff-types.ts +76 -0
- package/3p/pi-plannotator/packages/shared/semantic-diff.test.ts +322 -0
- package/3p/pi-plannotator/packages/shared/semantic-diff.ts +520 -0
- package/3p/pi-plannotator/packages/shared/storage.ts +378 -0
- package/3p/pi-plannotator/packages/shared/tour.ts +61 -0
- package/3p/pi-plannotator/packages/shared/tsconfig.json +15 -0
- package/3p/pi-plannotator/packages/shared/types.ts +29 -0
- package/3p/pi-plannotator/packages/shared/url-to-markdown.test.ts +177 -0
- package/3p/pi-plannotator/packages/shared/url-to-markdown.ts +351 -0
- package/3p/pi-plannotator/packages/shared/vcs-core.test.ts +332 -0
- package/3p/pi-plannotator/packages/shared/vcs-core.ts +482 -0
- package/3p/pi-plannotator/packages/shared/worktree-pool.test.ts +162 -0
- package/3p/pi-plannotator/packages/shared/worktree-pool.ts +103 -0
- package/3p/pi-plannotator/packages/shared/worktree.ts +119 -0
- package/3p/pi-subagents/package.json +50 -0
- package/3p/pi-subagents/src/agent-manager.ts +413 -0
- package/3p/pi-subagents/src/agent-runner.ts +502 -0
- package/3p/pi-subagents/src/agent-types.ts +248 -0
- package/3p/pi-subagents/src/context.ts +58 -0
- package/3p/pi-subagents/src/cross-extension-rpc.ts +135 -0
- package/3p/pi-subagents/src/custom-agents.ts +137 -0
- package/3p/pi-subagents/src/default-agents.ts +144 -0
- package/3p/pi-subagents/src/env.ts +33 -0
- package/3p/pi-subagents/src/group-join.ts +141 -0
- package/3p/pi-subagents/src/index.ts +1811 -0
- package/3p/pi-subagents/src/invocation-config.ts +40 -0
- package/3p/pi-subagents/src/memory.ts +165 -0
- package/3p/pi-subagents/src/model-resolver.ts +81 -0
- package/3p/pi-subagents/src/output-file.ts +77 -0
- package/3p/pi-subagents/src/prompts.ts +85 -0
- package/3p/pi-subagents/src/skill-loader.ts +79 -0
- package/3p/pi-subagents/src/types.ts +111 -0
- package/3p/pi-subagents/src/ui/agent-widget.ts +496 -0
- package/3p/pi-subagents/src/ui/conversation-viewer.ts +250 -0
- package/3p/pi-subagents/src/worktree.ts +162 -0
- package/3p/pi-tasks/package.json +52 -0
- package/3p/pi-tasks/src/auto-clear.ts +91 -0
- package/3p/pi-tasks/src/index.ts +1150 -0
- package/3p/pi-tasks/src/process-tracker.ts +140 -0
- package/3p/pi-tasks/src/reminder-cadence.ts +90 -0
- package/3p/pi-tasks/src/task-store.ts +324 -0
- package/3p/pi-tasks/src/tasks-config.ts +27 -0
- package/3p/pi-tasks/src/types.ts +40 -0
- package/3p/pi-tasks/src/ui/settings-menu.ts +152 -0
- package/3p/pi-tasks/src/ui/task-widget.ts +296 -0
- package/AGENTS.md +28 -0
- package/LICENSE +201 -0
- package/extensions/orchestrator/agents/brainstorm-reviewer.ts +77 -0
- package/extensions/orchestrator/agents/code-reviewer.ts +89 -0
- package/extensions/orchestrator/agents/explore.ts +33 -0
- package/extensions/orchestrator/agents/librarian.ts +43 -0
- package/extensions/orchestrator/agents/plan-reviewer.ts +75 -0
- package/extensions/orchestrator/agents/planner.ts +61 -0
- package/extensions/orchestrator/agents/registry.ts +156 -0
- package/extensions/orchestrator/agents/task.ts +50 -0
- package/extensions/orchestrator/agents/tool-routing.ts +84 -0
- package/extensions/orchestrator/ast-search.ts +85 -0
- package/extensions/orchestrator/cbm.ts +330 -0
- package/extensions/orchestrator/command-handlers.test.ts +163 -0
- package/extensions/orchestrator/command-handlers.ts +116 -0
- package/extensions/orchestrator/commands.test.ts +81 -0
- package/extensions/orchestrator/commands.ts +75 -0
- package/extensions/orchestrator/config.test.ts +221 -0
- package/extensions/orchestrator/config.ts +230 -0
- package/extensions/orchestrator/context.test.ts +293 -0
- package/extensions/orchestrator/context.ts +206 -0
- package/extensions/orchestrator/event-handlers.test.ts +190 -0
- package/extensions/orchestrator/event-handlers.ts +1416 -0
- package/extensions/orchestrator/exa.ts +104 -0
- package/extensions/orchestrator/flant-infra.ts +486 -0
- package/extensions/orchestrator/index.ts +80 -0
- package/extensions/orchestrator/integration.test.ts +1214 -0
- package/extensions/orchestrator/orchestrator.test.ts +252 -0
- package/extensions/orchestrator/orchestrator.ts +538 -0
- package/extensions/orchestrator/phases/brainstorm.test.ts +15 -0
- package/extensions/orchestrator/phases/brainstorm.ts +273 -0
- package/extensions/orchestrator/phases/implementation.ts +40 -0
- package/extensions/orchestrator/phases/machine.test.ts +293 -0
- package/extensions/orchestrator/phases/machine.ts +209 -0
- package/extensions/orchestrator/phases/planning.ts +255 -0
- package/extensions/orchestrator/phases/review.ts +193 -0
- package/extensions/orchestrator/plannotator.ts +56 -0
- package/extensions/orchestrator/pp-menu.ts +866 -0
- package/extensions/orchestrator/state.test.ts +343 -0
- package/extensions/orchestrator/state.ts +237 -0
- package/extensions/orchestrator/validate-artifacts.test.ts +88 -0
- package/extensions/orchestrator/validate-artifacts.ts +272 -0
- package/extensions/orchestrator/vendor.d.ts +26 -0
- package/package.json +73 -0
- package/scripts/postinstall.sh +18 -0
|
@@ -0,0 +1,661 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* GitHub-specific PR provider implementation.
|
|
3
|
+
*
|
|
4
|
+
* All functions use the `gh` CLI via the PRRuntime abstraction.
|
|
5
|
+
*/
|
|
6
|
+
|
|
7
|
+
import type { PRRuntime, PRMetadata, PRContext, PRReviewThread, PRThreadComment, PRReviewFileComment, CommandResult, PRStackTree, PRStackNode, PRListItem } from "./pr-types";
|
|
8
|
+
import { encodeApiFilePath } from "./pr-types";
|
|
9
|
+
|
|
10
|
+
// GitHub-specific PRRef shape (used internally)
|
|
11
|
+
interface GhPRRef {
|
|
12
|
+
platform: "github";
|
|
13
|
+
host: string;
|
|
14
|
+
owner: string;
|
|
15
|
+
repo: string;
|
|
16
|
+
number: number;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** Build the --repo flag value: HOST/OWNER/REPO for GHE, OWNER/REPO for github.com */
|
|
20
|
+
function repoFlag(ref: GhPRRef): string {
|
|
21
|
+
if (ref.host !== "github.com") {
|
|
22
|
+
return `${ref.host}/${ref.owner}/${ref.repo}`;
|
|
23
|
+
}
|
|
24
|
+
return `${ref.owner}/${ref.repo}`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** Append --hostname to args for gh api / gh auth on GHE */
|
|
28
|
+
function hostnameArgs(host: string, args: string[]): string[] {
|
|
29
|
+
if (host !== "github.com") {
|
|
30
|
+
return [...args, "--hostname", host];
|
|
31
|
+
}
|
|
32
|
+
return args;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
// --- Auth ---
|
|
36
|
+
|
|
37
|
+
export async function checkGhAuth(runtime: PRRuntime, host: string): Promise<void> {
|
|
38
|
+
const result = await runtime.runCommand("gh", hostnameArgs(host, ["auth", "status"]));
|
|
39
|
+
if (result.exitCode !== 0) {
|
|
40
|
+
const stderr = result.stderr.trim();
|
|
41
|
+
const hostHint = host !== "github.com" ? ` --hostname ${host}` : "";
|
|
42
|
+
throw new Error(
|
|
43
|
+
`GitHub CLI not authenticated. Run \`gh auth login${hostHint}\` first.\n${stderr}`,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
export async function getGhUser(runtime: PRRuntime, host: string): Promise<string | null> {
|
|
49
|
+
try {
|
|
50
|
+
const result = await runtime.runCommand("gh", hostnameArgs(host, ["api", "user", "--jq", ".login"]));
|
|
51
|
+
if (result.exitCode === 0 && result.stdout.trim()) {
|
|
52
|
+
return result.stdout.trim();
|
|
53
|
+
}
|
|
54
|
+
return null;
|
|
55
|
+
} catch {
|
|
56
|
+
return null;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// --- Fetch PR ---
|
|
61
|
+
|
|
62
|
+
export async function fetchGhPR(
|
|
63
|
+
runtime: PRRuntime,
|
|
64
|
+
ref: GhPRRef,
|
|
65
|
+
): Promise<{ metadata: PRMetadata; rawPatch: string }> {
|
|
66
|
+
const repo = repoFlag(ref);
|
|
67
|
+
|
|
68
|
+
// Fetch diff, metadata, and repository defaults in parallel.
|
|
69
|
+
const [diffResult, viewResult, repoResult] = await Promise.all([
|
|
70
|
+
runtime.runCommand("gh", [
|
|
71
|
+
"pr", "diff", String(ref.number),
|
|
72
|
+
"--repo", repo,
|
|
73
|
+
]),
|
|
74
|
+
runtime.runCommand("gh", [
|
|
75
|
+
"pr", "view", String(ref.number),
|
|
76
|
+
"--repo", repo,
|
|
77
|
+
"--json", "id,title,author,baseRefName,headRefName,baseRefOid,headRefOid,url",
|
|
78
|
+
]),
|
|
79
|
+
runtime.runCommand("gh", [
|
|
80
|
+
"repo", "view", repo,
|
|
81
|
+
"--json", "defaultBranchRef",
|
|
82
|
+
"--jq", ".defaultBranchRef.name",
|
|
83
|
+
]),
|
|
84
|
+
]);
|
|
85
|
+
|
|
86
|
+
if (diffResult.exitCode !== 0) {
|
|
87
|
+
throw new Error(
|
|
88
|
+
`Failed to fetch PR diff: ${diffResult.stderr.trim() || `exit code ${diffResult.exitCode}`}`,
|
|
89
|
+
);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (viewResult.exitCode !== 0) {
|
|
93
|
+
throw new Error(
|
|
94
|
+
`Failed to fetch PR metadata: ${viewResult.stderr.trim() || `exit code ${viewResult.exitCode}`}`,
|
|
95
|
+
);
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const raw = JSON.parse(viewResult.stdout) as {
|
|
99
|
+
id: string;
|
|
100
|
+
title: string;
|
|
101
|
+
author: { login: string };
|
|
102
|
+
baseRefName: string;
|
|
103
|
+
headRefName: string;
|
|
104
|
+
baseRefOid: string;
|
|
105
|
+
headRefOid: string;
|
|
106
|
+
url: string;
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
// Fetch the merge-base SHA — the common ancestor commit GitHub uses to compute the PR diff.
|
|
110
|
+
// baseSha (baseRefOid) is the tip of the base branch, which may have moved since the branch point.
|
|
111
|
+
// File contents must be fetched at the merge-base to match the diff hunks.
|
|
112
|
+
let mergeBaseSha: string | undefined;
|
|
113
|
+
try {
|
|
114
|
+
const compareResult = await runtime.runCommand("gh", hostnameArgs(ref.host, [
|
|
115
|
+
"api",
|
|
116
|
+
`repos/${ref.owner}/${ref.repo}/compare/${raw.baseRefOid}...${raw.headRefOid}`,
|
|
117
|
+
"--jq", ".merge_base_commit.sha",
|
|
118
|
+
]));
|
|
119
|
+
if (compareResult.exitCode === 0 && compareResult.stdout.trim()) {
|
|
120
|
+
mergeBaseSha = compareResult.stdout.trim();
|
|
121
|
+
}
|
|
122
|
+
} catch { /* fallback to baseSha if compare API fails */ }
|
|
123
|
+
|
|
124
|
+
const metadata: PRMetadata = {
|
|
125
|
+
platform: "github",
|
|
126
|
+
host: ref.host,
|
|
127
|
+
owner: ref.owner,
|
|
128
|
+
repo: ref.repo,
|
|
129
|
+
number: ref.number,
|
|
130
|
+
prNodeId: raw.id,
|
|
131
|
+
title: raw.title,
|
|
132
|
+
author: raw.author.login,
|
|
133
|
+
baseBranch: raw.baseRefName,
|
|
134
|
+
headBranch: raw.headRefName,
|
|
135
|
+
defaultBranch: repoResult.exitCode === 0 && repoResult.stdout.trim() && repoResult.stdout.trim() !== "null"
|
|
136
|
+
? repoResult.stdout.trim()
|
|
137
|
+
: undefined,
|
|
138
|
+
baseSha: raw.baseRefOid,
|
|
139
|
+
headSha: raw.headRefOid,
|
|
140
|
+
mergeBaseSha,
|
|
141
|
+
url: raw.url,
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
return { metadata, rawPatch: diffResult.stdout };
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
// --- PR Context ---
|
|
148
|
+
|
|
149
|
+
const GH_CONTEXT_FIELDS = [
|
|
150
|
+
"body", "state", "isDraft", "labels",
|
|
151
|
+
"comments", "reviews", "reviewDecision",
|
|
152
|
+
"mergeable", "mergeStateStatus",
|
|
153
|
+
"statusCheckRollup", "closingIssuesReferences",
|
|
154
|
+
].join(",");
|
|
155
|
+
|
|
156
|
+
function parseGhPRContext(raw: Record<string, unknown>): PRContext {
|
|
157
|
+
const arr = (v: unknown): unknown[] => (Array.isArray(v) ? v : []);
|
|
158
|
+
const str = (v: unknown): string => (typeof v === "string" ? v : "");
|
|
159
|
+
const login = (v: unknown): string =>
|
|
160
|
+
typeof v === "object" && v !== null && "login" in v
|
|
161
|
+
? String((v as { login: unknown }).login || "")
|
|
162
|
+
: "";
|
|
163
|
+
|
|
164
|
+
return {
|
|
165
|
+
body: str(raw.body),
|
|
166
|
+
state: str(raw.state),
|
|
167
|
+
isDraft: raw.isDraft === true,
|
|
168
|
+
labels: arr(raw.labels).map((l: any) => ({
|
|
169
|
+
name: str(l?.name),
|
|
170
|
+
color: str(l?.color),
|
|
171
|
+
})),
|
|
172
|
+
reviewDecision: str(raw.reviewDecision),
|
|
173
|
+
mergeable: str(raw.mergeable),
|
|
174
|
+
mergeStateStatus: str(raw.mergeStateStatus),
|
|
175
|
+
comments: arr(raw.comments).map((c: any) => ({
|
|
176
|
+
id: str(c?.id),
|
|
177
|
+
author: login(c?.author),
|
|
178
|
+
body: str(c?.body),
|
|
179
|
+
createdAt: str(c?.createdAt),
|
|
180
|
+
url: str(c?.url),
|
|
181
|
+
})),
|
|
182
|
+
reviews: arr(raw.reviews).map((r: any) => ({
|
|
183
|
+
id: str(r?.id),
|
|
184
|
+
author: login(r?.author),
|
|
185
|
+
state: str(r?.state),
|
|
186
|
+
body: str(r?.body),
|
|
187
|
+
submittedAt: str(r?.submittedAt),
|
|
188
|
+
...(r?.url ? { url: str(r.url) } : {}),
|
|
189
|
+
})),
|
|
190
|
+
reviewThreads: [], // populated via GraphQL after initial fetch
|
|
191
|
+
checks: arr(raw.statusCheckRollup).map((c: any) => ({
|
|
192
|
+
name: str(c?.name),
|
|
193
|
+
status: str(c?.status),
|
|
194
|
+
conclusion: typeof c?.conclusion === "string" ? c.conclusion : null,
|
|
195
|
+
workflowName: str(c?.workflowName),
|
|
196
|
+
detailsUrl: str(c?.detailsUrl),
|
|
197
|
+
})),
|
|
198
|
+
linkedIssues: arr(raw.closingIssuesReferences).map((i: any) => ({
|
|
199
|
+
number: typeof i?.number === "number" ? i.number : 0,
|
|
200
|
+
url: str(i?.url),
|
|
201
|
+
repo: i?.repository
|
|
202
|
+
? `${login(i.repository.owner)}/${str(i.repository.name)}`
|
|
203
|
+
: "",
|
|
204
|
+
})),
|
|
205
|
+
};
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
export async function fetchGhPRContext(
|
|
209
|
+
runtime: PRRuntime,
|
|
210
|
+
ref: GhPRRef,
|
|
211
|
+
): Promise<PRContext> {
|
|
212
|
+
const repo = repoFlag(ref);
|
|
213
|
+
|
|
214
|
+
const result = await runtime.runCommand("gh", [
|
|
215
|
+
"pr", "view", String(ref.number),
|
|
216
|
+
"--repo", repo,
|
|
217
|
+
"--json", GH_CONTEXT_FIELDS,
|
|
218
|
+
]);
|
|
219
|
+
|
|
220
|
+
if (result.exitCode !== 0) {
|
|
221
|
+
throw new Error(
|
|
222
|
+
`Failed to fetch PR context: ${result.stderr.trim() || `exit code ${result.exitCode}`}`,
|
|
223
|
+
);
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
const raw = JSON.parse(result.stdout) as Record<string, unknown>;
|
|
227
|
+
const context = parseGhPRContext(raw);
|
|
228
|
+
|
|
229
|
+
// Fetch inline review threads via GraphQL (parallel-safe, non-blocking failure)
|
|
230
|
+
try {
|
|
231
|
+
context.reviewThreads = await fetchGhReviewThreads(runtime, ref);
|
|
232
|
+
} catch {
|
|
233
|
+
// GraphQL may not be available or may fail — degrade gracefully
|
|
234
|
+
context.reviewThreads = [];
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
return context;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
// --- Review Threads (GraphQL) ---
|
|
241
|
+
|
|
242
|
+
const REVIEW_THREADS_QUERY = `
|
|
243
|
+
query($owner: String!, $repo: String!, $number: Int!) {
|
|
244
|
+
repository(owner: $owner, name: $repo) {
|
|
245
|
+
pullRequest(number: $number) {
|
|
246
|
+
reviewThreads(first: 100) {
|
|
247
|
+
nodes {
|
|
248
|
+
id
|
|
249
|
+
isResolved
|
|
250
|
+
isOutdated
|
|
251
|
+
line
|
|
252
|
+
startLine
|
|
253
|
+
path
|
|
254
|
+
diffSide
|
|
255
|
+
comments(first: 50) {
|
|
256
|
+
nodes {
|
|
257
|
+
id
|
|
258
|
+
body
|
|
259
|
+
author { login }
|
|
260
|
+
createdAt
|
|
261
|
+
url
|
|
262
|
+
diffHunk
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
}`;
|
|
270
|
+
|
|
271
|
+
async function fetchGhReviewThreads(
|
|
272
|
+
runtime: PRRuntime,
|
|
273
|
+
ref: GhPRRef,
|
|
274
|
+
): Promise<PRReviewThread[]> {
|
|
275
|
+
const result = await runtime.runCommand("gh", hostnameArgs(ref.host, [
|
|
276
|
+
"api", "graphql",
|
|
277
|
+
"-f", `query=${REVIEW_THREADS_QUERY}`,
|
|
278
|
+
"-f", `owner=${ref.owner}`,
|
|
279
|
+
"-f", `repo=${ref.repo}`,
|
|
280
|
+
"-F", `number=${ref.number}`,
|
|
281
|
+
]));
|
|
282
|
+
|
|
283
|
+
if (result.exitCode !== 0) return [];
|
|
284
|
+
|
|
285
|
+
const data = JSON.parse(result.stdout);
|
|
286
|
+
const threads = data?.data?.repository?.pullRequest?.reviewThreads?.nodes;
|
|
287
|
+
if (!Array.isArray(threads)) return [];
|
|
288
|
+
|
|
289
|
+
return threads.map((t: any): PRReviewThread => ({
|
|
290
|
+
id: String(t.id ?? ''),
|
|
291
|
+
isResolved: t.isResolved === true,
|
|
292
|
+
isOutdated: t.isOutdated === true,
|
|
293
|
+
path: String(t.path ?? ''),
|
|
294
|
+
line: typeof t.line === 'number' ? t.line : null,
|
|
295
|
+
startLine: typeof t.startLine === 'number' ? t.startLine : null,
|
|
296
|
+
diffSide: t.diffSide === 'LEFT' || t.diffSide === 'RIGHT' ? t.diffSide : null,
|
|
297
|
+
comments: Array.isArray(t.comments?.nodes)
|
|
298
|
+
? t.comments.nodes.map((c: any): PRThreadComment => ({
|
|
299
|
+
id: String(c.id ?? ''),
|
|
300
|
+
author: c.author?.login ? String(c.author.login) : '',
|
|
301
|
+
body: String(c.body ?? ''),
|
|
302
|
+
createdAt: String(c.createdAt ?? ''),
|
|
303
|
+
url: String(c.url ?? ''),
|
|
304
|
+
...(c.diffHunk ? { diffHunk: String(c.diffHunk) } : {}),
|
|
305
|
+
}))
|
|
306
|
+
: [],
|
|
307
|
+
}));
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
// --- File Content ---
|
|
311
|
+
|
|
312
|
+
export async function fetchGhPRFileContent(
|
|
313
|
+
runtime: PRRuntime,
|
|
314
|
+
ref: GhPRRef,
|
|
315
|
+
sha: string,
|
|
316
|
+
filePath: string,
|
|
317
|
+
): Promise<string | null> {
|
|
318
|
+
const result = await runtime.runCommand("gh", hostnameArgs(ref.host, [
|
|
319
|
+
"api",
|
|
320
|
+
`repos/${ref.owner}/${ref.repo}/contents/${encodeApiFilePath(filePath)}?ref=${sha}`,
|
|
321
|
+
"--jq", ".content",
|
|
322
|
+
]));
|
|
323
|
+
|
|
324
|
+
if (result.exitCode !== 0) return null;
|
|
325
|
+
|
|
326
|
+
const base64Content = result.stdout.trim();
|
|
327
|
+
if (!base64Content) return null;
|
|
328
|
+
|
|
329
|
+
// GitHub returns base64-encoded content with newlines
|
|
330
|
+
const cleaned = base64Content.replace(/\n/g, "");
|
|
331
|
+
try {
|
|
332
|
+
return Buffer.from(cleaned, "base64").toString("utf-8");
|
|
333
|
+
} catch {
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
// --- Viewed Files ---
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Fetch the per-file "viewed" state for a GitHub PR via GraphQL.
|
|
342
|
+
* Returns a map of { filePath: isViewed } where isViewed is true for
|
|
343
|
+
* VIEWED or DISMISSED states (i.e., the file was reviewed but may need
|
|
344
|
+
* re-review after new commits).
|
|
345
|
+
*/
|
|
346
|
+
export async function fetchGhPRViewedFiles(
|
|
347
|
+
runtime: PRRuntime,
|
|
348
|
+
ref: GhPRRef,
|
|
349
|
+
): Promise<Record<string, boolean>> {
|
|
350
|
+
const query = `
|
|
351
|
+
query($owner: String!, $repo: String!, $number: Int!, $cursor: String) {
|
|
352
|
+
repository(owner: $owner, name: $repo) {
|
|
353
|
+
pullRequest(number: $number) {
|
|
354
|
+
files(first: 100, after: $cursor) {
|
|
355
|
+
nodes {
|
|
356
|
+
path
|
|
357
|
+
viewerViewedState
|
|
358
|
+
}
|
|
359
|
+
pageInfo {
|
|
360
|
+
hasNextPage
|
|
361
|
+
endCursor
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
`;
|
|
368
|
+
|
|
369
|
+
const result: Record<string, boolean> = {};
|
|
370
|
+
let cursor: string | null = null;
|
|
371
|
+
|
|
372
|
+
// Paginate through all files (GitHub returns max 100 per page)
|
|
373
|
+
do {
|
|
374
|
+
const args = hostnameArgs(ref.host, [
|
|
375
|
+
"api", "graphql",
|
|
376
|
+
"-f", `query=${query}`,
|
|
377
|
+
"-F", `owner=${ref.owner}`,
|
|
378
|
+
"-F", `repo=${ref.repo}`,
|
|
379
|
+
"-F", `number=${ref.number}`,
|
|
380
|
+
]);
|
|
381
|
+
if (cursor) {
|
|
382
|
+
args.push("-F", `cursor=${cursor}`);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const res = await runtime.runCommand("gh", args);
|
|
386
|
+
if (res.exitCode !== 0) {
|
|
387
|
+
throw new Error(
|
|
388
|
+
`Failed to fetch PR viewed files: ${res.stderr.trim() || `exit code ${res.exitCode}`}`,
|
|
389
|
+
);
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
const data = JSON.parse(res.stdout) as {
|
|
393
|
+
data?: {
|
|
394
|
+
repository?: {
|
|
395
|
+
pullRequest?: {
|
|
396
|
+
files?: {
|
|
397
|
+
nodes: Array<{ path: string; viewerViewedState: string }>;
|
|
398
|
+
pageInfo: { hasNextPage: boolean; endCursor: string | null };
|
|
399
|
+
};
|
|
400
|
+
};
|
|
401
|
+
};
|
|
402
|
+
};
|
|
403
|
+
errors?: Array<{ message: string }>;
|
|
404
|
+
};
|
|
405
|
+
|
|
406
|
+
if (data.errors?.length) {
|
|
407
|
+
throw new Error(`GraphQL error: ${data.errors[0].message}`);
|
|
408
|
+
}
|
|
409
|
+
|
|
410
|
+
const files = data.data?.repository?.pullRequest?.files;
|
|
411
|
+
if (!files) break;
|
|
412
|
+
|
|
413
|
+
for (const node of files.nodes) {
|
|
414
|
+
// VIEWED = explicitly marked as viewed
|
|
415
|
+
// DISMISSED = was viewed but new commits arrived (still "was reviewed")
|
|
416
|
+
result[node.path] = node.viewerViewedState === "VIEWED" || node.viewerViewedState === "DISMISSED";
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
cursor = files.pageInfo.hasNextPage ? files.pageInfo.endCursor : null;
|
|
420
|
+
} while (cursor !== null);
|
|
421
|
+
|
|
422
|
+
return result;
|
|
423
|
+
}
|
|
424
|
+
|
|
425
|
+
/**
|
|
426
|
+
* Mark or unmark a set of files as viewed in a GitHub PR via GraphQL mutations.
|
|
427
|
+
* Uses Promise.allSettled so a single file failure doesn't block the rest.
|
|
428
|
+
* Throws only if ALL mutations fail.
|
|
429
|
+
*/
|
|
430
|
+
export async function markGhFilesViewed(
|
|
431
|
+
runtime: PRRuntime,
|
|
432
|
+
ref: GhPRRef,
|
|
433
|
+
prNodeId: string,
|
|
434
|
+
filePaths: string[],
|
|
435
|
+
viewed: boolean,
|
|
436
|
+
): Promise<void> {
|
|
437
|
+
if (filePaths.length === 0) return;
|
|
438
|
+
|
|
439
|
+
const mutationName = viewed ? "markFileAsViewed" : "unmarkFileAsViewed";
|
|
440
|
+
const mutation = `
|
|
441
|
+
mutation($id: ID!, $path: String!) {
|
|
442
|
+
${mutationName}(input: { pullRequestId: $id, path: $path }) {
|
|
443
|
+
clientMutationId
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
`;
|
|
447
|
+
|
|
448
|
+
const results = await Promise.allSettled(
|
|
449
|
+
filePaths.map((path) =>
|
|
450
|
+
runtime.runCommandWithInput
|
|
451
|
+
? runtime.runCommand("gh", hostnameArgs(ref.host, [
|
|
452
|
+
"api", "graphql",
|
|
453
|
+
"-f", `query=${mutation}`,
|
|
454
|
+
"-F", `id=${prNodeId}`,
|
|
455
|
+
"-F", `path=${path}`,
|
|
456
|
+
]))
|
|
457
|
+
: Promise.reject(new Error("Runtime does not support commands")),
|
|
458
|
+
),
|
|
459
|
+
);
|
|
460
|
+
|
|
461
|
+
const failures = results.filter((r): r is PromiseRejectedResult => r.status === "rejected");
|
|
462
|
+
if (failures.length === filePaths.length) {
|
|
463
|
+
throw new Error(
|
|
464
|
+
`Failed to ${mutationName} all files: ${failures[0].reason}`,
|
|
465
|
+
);
|
|
466
|
+
}
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
// --- Submit PR Review ---
|
|
470
|
+
|
|
471
|
+
export async function submitGhPRReview(
|
|
472
|
+
runtime: PRRuntime,
|
|
473
|
+
ref: GhPRRef,
|
|
474
|
+
headSha: string,
|
|
475
|
+
action: "approve" | "comment",
|
|
476
|
+
body: string,
|
|
477
|
+
fileComments: PRReviewFileComment[],
|
|
478
|
+
): Promise<void> {
|
|
479
|
+
const payload = JSON.stringify({
|
|
480
|
+
commit_id: headSha,
|
|
481
|
+
body,
|
|
482
|
+
event: action === "approve" ? "APPROVE" : "COMMENT",
|
|
483
|
+
comments: fileComments,
|
|
484
|
+
});
|
|
485
|
+
|
|
486
|
+
const endpoint = `repos/${ref.owner}/${ref.repo}/pulls/${ref.number}/reviews`;
|
|
487
|
+
|
|
488
|
+
let result: CommandResult;
|
|
489
|
+
|
|
490
|
+
if (runtime.runCommandWithInput) {
|
|
491
|
+
result = await runtime.runCommandWithInput(
|
|
492
|
+
"gh",
|
|
493
|
+
hostnameArgs(ref.host, ["api", endpoint, "--method", "POST", "--input", "-"]),
|
|
494
|
+
payload,
|
|
495
|
+
);
|
|
496
|
+
} else {
|
|
497
|
+
throw new Error("Runtime does not support stdin input; cannot submit PR review");
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
if (result.exitCode !== 0) {
|
|
501
|
+
const message = result.stderr.trim() || result.stdout.trim() || `exit code ${result.exitCode}`;
|
|
502
|
+
throw new Error(`Failed to submit PR review: ${message}`);
|
|
503
|
+
}
|
|
504
|
+
}
|
|
505
|
+
|
|
506
|
+
// --- Stack Tree (GraphQL) ---
|
|
507
|
+
|
|
508
|
+
type StackPRNode = { number: number; title: string; url: string; baseRefName: string; headRefName: string; state: string };
|
|
509
|
+
|
|
510
|
+
function stackPRQuery(kind: "head" | "base"): string {
|
|
511
|
+
const varName = kind === "head" ? "headRefName" : "baseRefName";
|
|
512
|
+
const first = kind === "head" ? 5 : 10;
|
|
513
|
+
return `
|
|
514
|
+
query($owner: String!, $repo: String!, $${varName}: String!) {
|
|
515
|
+
repository(owner: $owner, name: $repo) {
|
|
516
|
+
pullRequests(first: ${first}, ${varName}: $${varName}, states: [OPEN, MERGED]) {
|
|
517
|
+
nodes { number title url baseRefName headRefName state }
|
|
518
|
+
}
|
|
519
|
+
}
|
|
520
|
+
}`;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
async function queryPRsByRef(
|
|
524
|
+
runtime: PRRuntime,
|
|
525
|
+
ref: GhPRRef,
|
|
526
|
+
kind: "head" | "base",
|
|
527
|
+
refName: string,
|
|
528
|
+
): Promise<StackPRNode[]> {
|
|
529
|
+
const varName = kind === "head" ? "headRefName" : "baseRefName";
|
|
530
|
+
const result = await runtime.runCommand("gh", hostnameArgs(ref.host, [
|
|
531
|
+
"api", "graphql",
|
|
532
|
+
"-f", `query=${stackPRQuery(kind)}`,
|
|
533
|
+
"-f", `owner=${ref.owner}`,
|
|
534
|
+
"-f", `repo=${ref.repo}`,
|
|
535
|
+
"-f", `${varName}=${refName}`,
|
|
536
|
+
]));
|
|
537
|
+
if (result.exitCode !== 0) return [];
|
|
538
|
+
const data = JSON.parse(result.stdout);
|
|
539
|
+
const prs = data?.data?.repository?.pullRequests?.nodes;
|
|
540
|
+
return Array.isArray(prs) ? prs : [];
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
/**
|
|
544
|
+
* Walk up and down the PR stack from the current PR, resolving
|
|
545
|
+
* PR numbers/titles for every node in the chain.
|
|
546
|
+
*
|
|
547
|
+
* Up: walk from currentPR.baseBranch → defaultBranch (ancestors)
|
|
548
|
+
* Down: walk from currentPR.headBranch → leaf PRs (descendants)
|
|
549
|
+
*/
|
|
550
|
+
export async function fetchGhPRStack(
|
|
551
|
+
runtime: PRRuntime,
|
|
552
|
+
ref: GhPRRef,
|
|
553
|
+
metadata: PRMetadata,
|
|
554
|
+
): Promise<PRStackTree | null> {
|
|
555
|
+
if (metadata.platform !== "github") return null;
|
|
556
|
+
const defaultBranch = metadata.defaultBranch;
|
|
557
|
+
if (!defaultBranch) return null;
|
|
558
|
+
|
|
559
|
+
const currentNode: PRStackNode = {
|
|
560
|
+
branch: metadata.headBranch,
|
|
561
|
+
number: metadata.number,
|
|
562
|
+
title: metadata.title,
|
|
563
|
+
url: metadata.url,
|
|
564
|
+
isCurrent: true,
|
|
565
|
+
isDefaultBranch: false,
|
|
566
|
+
};
|
|
567
|
+
|
|
568
|
+
// Walk up: find the PR whose headRefName === baseBranch, repeat
|
|
569
|
+
const ancestors: PRStackNode[] = [];
|
|
570
|
+
let nextHead = metadata.baseBranch;
|
|
571
|
+
const maxDepth = 10;
|
|
572
|
+
|
|
573
|
+
for (let i = 0; i < maxDepth; i++) {
|
|
574
|
+
if (nextHead === defaultBranch) break;
|
|
575
|
+
|
|
576
|
+
const prs = await queryPRsByRef(runtime, ref, "head", nextHead);
|
|
577
|
+
if (prs.length === 0) {
|
|
578
|
+
ancestors.push({ branch: nextHead, isCurrent: false, isDefaultBranch: false });
|
|
579
|
+
break;
|
|
580
|
+
}
|
|
581
|
+
|
|
582
|
+
const pr = prs[0];
|
|
583
|
+
ancestors.push({
|
|
584
|
+
branch: pr.headRefName,
|
|
585
|
+
number: pr.number,
|
|
586
|
+
title: pr.title,
|
|
587
|
+
url: pr.url,
|
|
588
|
+
isCurrent: false,
|
|
589
|
+
isDefaultBranch: false,
|
|
590
|
+
state: (pr.state === 'MERGED' ? 'merged' : pr.state === 'CLOSED' ? 'closed' : 'open') as PRStackNode['state'],
|
|
591
|
+
});
|
|
592
|
+
nextHead = pr.baseRefName;
|
|
593
|
+
}
|
|
594
|
+
|
|
595
|
+
// Walk down: find PRs whose baseRefName === current headBranch, repeat
|
|
596
|
+
const descendants: PRStackNode[] = [];
|
|
597
|
+
let nextBase = metadata.headBranch;
|
|
598
|
+
|
|
599
|
+
for (let i = 0; i < maxDepth; i++) {
|
|
600
|
+
const prs = await queryPRsByRef(runtime, ref, "base", nextBase);
|
|
601
|
+
if (prs.length === 0) break;
|
|
602
|
+
|
|
603
|
+
const pr = prs[0];
|
|
604
|
+
descendants.push({
|
|
605
|
+
branch: pr.headRefName,
|
|
606
|
+
number: pr.number,
|
|
607
|
+
title: pr.title,
|
|
608
|
+
url: pr.url,
|
|
609
|
+
isCurrent: false,
|
|
610
|
+
isDefaultBranch: false,
|
|
611
|
+
state: (pr.state === 'MERGED' ? 'merged' : pr.state === 'CLOSED' ? 'closed' : 'open') as PRStackNode['state'],
|
|
612
|
+
});
|
|
613
|
+
nextBase = pr.headRefName;
|
|
614
|
+
}
|
|
615
|
+
|
|
616
|
+
// Build tree: defaultBranch → ancestors (reversed) → current → descendants
|
|
617
|
+
const nodes: PRStackNode[] = [
|
|
618
|
+
{ branch: defaultBranch, isCurrent: false, isDefaultBranch: true },
|
|
619
|
+
...ancestors.reverse(),
|
|
620
|
+
currentNode,
|
|
621
|
+
...descendants,
|
|
622
|
+
];
|
|
623
|
+
|
|
624
|
+
return { nodes };
|
|
625
|
+
}
|
|
626
|
+
|
|
627
|
+
// --- PR List ---
|
|
628
|
+
|
|
629
|
+
export async function fetchGhPRList(
|
|
630
|
+
runtime: PRRuntime,
|
|
631
|
+
ref: GhPRRef,
|
|
632
|
+
): Promise<PRListItem[]> {
|
|
633
|
+
const result = await runtime.runCommand("gh", [
|
|
634
|
+
"pr", "list",
|
|
635
|
+
"--repo", repoFlag(ref),
|
|
636
|
+
"--json", "number,title,author,url,baseRefName,state",
|
|
637
|
+
"--limit", "30",
|
|
638
|
+
"--state", "all",
|
|
639
|
+
]);
|
|
640
|
+
|
|
641
|
+
if (result.exitCode !== 0) return [];
|
|
642
|
+
|
|
643
|
+
const raw = JSON.parse(result.stdout) as Array<{
|
|
644
|
+
number: number;
|
|
645
|
+
title: string;
|
|
646
|
+
author: { login: string };
|
|
647
|
+
url: string;
|
|
648
|
+
baseRefName: string;
|
|
649
|
+
state: string;
|
|
650
|
+
}>;
|
|
651
|
+
|
|
652
|
+
return raw.map((pr) => ({
|
|
653
|
+
id: String(pr.number),
|
|
654
|
+
number: pr.number,
|
|
655
|
+
title: pr.title,
|
|
656
|
+
author: pr.author.login,
|
|
657
|
+
url: pr.url,
|
|
658
|
+
baseBranch: pr.baseRefName,
|
|
659
|
+
state: (pr.state === "OPEN" ? "open" : pr.state === "MERGED" ? "merged" : "closed") as PRListItem["state"],
|
|
660
|
+
}));
|
|
661
|
+
}
|