@andorra-labs/code 0.1.14 → 0.1.16
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/dist/src/acp-agent.d.ts +54 -0
- package/dist/src/acp-agent.js +991 -0
- package/dist/src/agent-definitions.js +29 -6
- package/dist/src/agent-execution.js +8 -2
- package/dist/src/agent-mcp-catalog.js +2 -0
- package/dist/src/agent-mcp.d.ts +3 -1
- package/dist/src/agent-mcp.js +97 -11
- package/dist/src/andorra/entry.js +21 -3
- package/dist/src/andorra/hosted.d.ts +3 -0
- package/dist/src/andorra/hosted.js +5 -1
- package/dist/src/andorra/notices.d.ts +10 -1
- package/dist/src/andorra/notices.js +25 -2
- package/dist/src/andorra/terminal.d.ts +2 -0
- package/dist/src/andorra/terminal.js +2 -2
- package/dist/src/app-server/protocol.d.ts +9 -2
- package/dist/src/app-server/protocol.js +1 -1
- package/dist/src/app-server/workspace.d.ts +1 -0
- package/dist/src/app-server/workspace.js +141 -10
- package/dist/src/app-server.d.ts +115 -0
- package/dist/src/app-server.js +980 -63
- package/dist/src/auth/AuthManager.js +4 -4
- package/dist/src/auth/deployment.js +2 -2
- package/dist/src/browser-bridge.d.ts +1 -0
- package/dist/src/browser-bridge.js +5 -2
- package/dist/src/browser-external-app.d.ts +27 -0
- package/dist/src/browser-external-app.js +60 -0
- package/dist/src/browser-panel-tool.d.ts +6 -0
- package/dist/src/browser-panel-tool.js +36 -0
- package/dist/src/browser-runner.js +1 -0
- package/dist/src/browser-server.d.ts +5 -0
- package/dist/src/browser-server.js +27 -3
- package/dist/src/browser-ui/style.css +3 -3
- package/dist/src/browser-ui/tokens.css +27 -16
- package/dist/src/browser-view.d.ts +16 -0
- package/dist/src/browser-view.js +60 -2
- package/dist/src/bug-report.d.ts +81 -0
- package/dist/src/bug-report.js +235 -0
- package/dist/src/claude-provider.d.ts +2 -1
- package/dist/src/claude-provider.js +13 -9
- package/dist/src/cli-sign-in.d.ts +3 -1
- package/dist/src/cli-sign-in.js +4 -1
- package/dist/src/cli.js +184 -40
- package/dist/src/codex-errors.js +4 -1
- package/dist/src/coding-tools.js +7 -7
- package/dist/src/command-screens.d.ts +9 -2
- package/dist/src/command-screens.js +166 -97
- package/dist/src/commands.d.ts +3 -2
- package/dist/src/commands.js +1 -0
- package/dist/src/compaction-phrases.d.ts +4 -0
- package/dist/src/compaction-phrases.js +20 -0
- package/dist/src/completion-gates.d.ts +22 -0
- package/dist/src/completion-gates.js +11 -0
- package/dist/src/config.d.ts +5 -0
- package/dist/src/config.js +48 -0
- package/dist/src/context-compaction.js +2 -1
- package/dist/src/decision/DecisionManager.js +3 -2
- package/dist/src/decision/requestPhrases.d.ts +4 -0
- package/dist/src/decision/requestPhrases.js +23 -0
- package/dist/src/github-inbox.d.ts +55 -0
- package/dist/src/github-inbox.js +153 -0
- package/dist/src/google-provider.d.ts +31 -0
- package/dist/src/google-provider.js +153 -0
- package/dist/src/graph-health.js +8 -4
- package/dist/src/graph-manager-chat.js +6 -1
- package/dist/src/graph-provider-portability.js +32 -10
- package/dist/src/graph-quota.d.ts +29 -2
- package/dist/src/graph-quota.js +68 -10
- package/dist/src/graph-runner.js +24 -7
- package/dist/src/graph-server.d.ts +1 -1
- package/dist/src/graph-server.js +9 -7
- package/dist/src/graph-sync.d.ts +1 -0
- package/dist/src/graph-sync.js +93 -34
- package/dist/src/graph-tool.d.ts +2 -0
- package/dist/src/graph-tool.js +2 -2
- package/dist/src/graph-ui/app.js +177 -41
- package/dist/src/graph-ui/controls.css +1 -0
- package/dist/src/graph-ui/edge-router.js +121 -53
- package/dist/src/graph-ui/embed.js +2 -2
- package/dist/src/graph-ui/icons.js +1 -0
- package/dist/src/graph-ui/index.html +1 -1
- package/dist/src/graph-ui/manager.css +1 -1
- package/dist/src/graph-ui/manager.js +2 -2
- package/dist/src/graph-ui/sidebar.js +30 -0
- package/dist/src/graph-ui/studio-canvas.js +26 -13
- package/dist/src/graph-ui/studio.css +15 -7
- package/dist/src/graph-ui/studio.js +11 -4
- package/dist/src/graph-ui/style.css +77 -45
- package/dist/src/graph-ui/theme.js +3 -36
- package/dist/src/graph-ui/tokens.css +27 -19
- package/dist/src/graph-visibility.d.ts +31 -0
- package/dist/src/graph-visibility.js +102 -0
- package/dist/src/graphs.js +4 -3
- package/dist/src/live-model.d.ts +24 -0
- package/dist/src/live-model.js +37 -0
- package/dist/src/managed-sign-in.d.ts +1 -0
- package/dist/src/managed-sign-in.js +13 -0
- package/dist/src/membership-errors.d.ts +2 -0
- package/dist/src/membership-errors.js +5 -1
- package/dist/src/membership.d.ts +23 -1
- package/dist/src/membership.js +48 -6
- package/dist/src/model-health-cache.js +1 -1
- package/dist/src/model-metadata.d.ts +1 -1
- package/dist/src/model-metadata.js +12 -3
- package/dist/src/paged-transcript.d.ts +2 -0
- package/dist/src/paged-transcript.js +16 -0
- package/dist/src/plan-pass.d.ts +59 -0
- package/dist/src/plan-pass.js +165 -0
- package/dist/src/provider-connection.js +17 -0
- package/dist/src/provider-engine.d.ts +5 -0
- package/dist/src/provider-engine.js +6 -3
- package/dist/src/provider-import.js +3 -1
- package/dist/src/provider-settings.js +14 -2
- package/dist/src/providers.d.ts +33 -2
- package/dist/src/providers.js +101 -28
- package/dist/src/pull-request-checks.d.ts +26 -0
- package/dist/src/pull-request-checks.js +56 -0
- package/dist/src/pull-request-conversation.d.ts +134 -0
- package/dist/src/pull-request-conversation.js +221 -0
- package/dist/src/pull-request-create.d.ts +175 -0
- package/dist/src/pull-request-create.js +392 -0
- package/dist/src/pull-request-details.d.ts +242 -0
- package/dist/src/pull-request-details.js +614 -0
- package/dist/src/pull-request-edit.d.ts +23 -0
- package/dist/src/pull-request-edit.js +91 -0
- package/dist/src/pull-request-logs.d.ts +76 -0
- package/dist/src/pull-request-logs.js +172 -0
- package/dist/src/pull-request-merge.d.ts +112 -0
- package/dist/src/pull-request-merge.js +258 -0
- package/dist/src/pull-request-review.d.ts +137 -0
- package/dist/src/pull-request-review.js +401 -0
- package/dist/src/pull-requests.d.ts +43 -2
- package/dist/src/pull-requests.js +112 -21
- package/dist/src/react-runtime.js +58 -9
- package/dist/src/read-document-tool.d.ts +2 -0
- package/dist/src/read-document-tool.js +6 -6
- package/dist/src/remote/engine-pool.js +42 -6
- package/dist/src/run-control.d.ts +10 -0
- package/dist/src/run-control.js +24 -0
- package/dist/src/run-progress.d.ts +10 -0
- package/dist/src/run-progress.js +122 -0
- package/dist/src/run-records.d.ts +2 -2
- package/dist/src/runtime-environment.d.ts +1 -0
- package/dist/src/runtime-environment.js +13 -0
- package/dist/src/runtime.d.ts +5 -1
- package/dist/src/runtime.js +14 -2
- package/dist/src/save-lock.js +14 -7
- package/dist/src/session-title.d.ts +3 -0
- package/dist/src/session-title.js +5 -2
- package/dist/src/sign-in-detection.d.ts +1 -1
- package/dist/src/sign-in-detection.js +10 -3
- package/dist/src/sign-in-offer.js +1 -1
- package/dist/src/skill-commands.js +1 -1
- package/dist/src/skills/bundled/bug-report/SKILL.md +80 -0
- package/dist/src/skills/bundled/graph-manager/SKILL.md +9 -1
- package/dist/src/skills/bundled/graph-manager/errors.md +1 -0
- package/dist/src/skills/bundled/provenance.json +7 -2
- package/dist/src/skills/runtime-skills.js +1 -0
- package/dist/src/subagent-run.js +1 -0
- package/dist/src/telemetry/capture-core.d.ts +17 -0
- package/dist/src/telemetry/capture-core.js +59 -1
- package/dist/src/telemetry/classify.d.ts +3 -1
- package/dist/src/telemetry/classify.js +37 -0
- package/dist/src/telemetry/emitter.d.ts +2 -0
- package/dist/src/telemetry/emitter.js +42 -5
- package/dist/src/telemetry/schema.d.ts +22 -0
- package/dist/src/telemetry/schema.js +68 -1
- package/dist/src/telemetry/surface.d.ts +1 -0
- package/dist/src/telemetry/uploader.d.ts +12 -1
- package/dist/src/telemetry/uploader.js +30 -2
- package/dist/src/terminal-links.d.ts +16 -0
- package/dist/src/terminal-links.js +113 -0
- package/dist/src/terminal-ui.d.ts +8 -1
- package/dist/src/terminal-ui.js +40 -14
- package/dist/src/tool-catalog.js +1 -1
- package/dist/src/trajectory.js +6 -1
- package/dist/src/transcript.d.ts +2 -0
- package/dist/src/transcript.js +10 -3
- package/dist/src/types.d.ts +9 -0
- package/dist/src/usage-analytics.js +18 -10
- package/package.json +2 -1
|
@@ -0,0 +1,392 @@
|
|
|
1
|
+
import { execFile } from 'node:child_process';
|
|
2
|
+
import { lstat, open, realpath } from 'node:fs/promises';
|
|
3
|
+
import { join, sep } from 'node:path';
|
|
4
|
+
import { PullRequestAccountChanged, PullRequestRefusal } from './pull-request-details.js';
|
|
5
|
+
import { ghAccounts, isGhAccessError, isGhListingUnsupported } from './pull-requests.js';
|
|
6
|
+
export function gitIn(folder) {
|
|
7
|
+
return (args, options) => new Promise(resolve => {
|
|
8
|
+
execFile('git', ['-C', folder, ...args], {
|
|
9
|
+
windowsHide: true, timeout: options?.timeoutMs ?? 20_000, maxBuffer: 4 * 1024 * 1024,
|
|
10
|
+
env: { ...process.env, GIT_TERMINAL_PROMPT: '0', GCM_INTERACTIVE: 'never', GIT_ASKPASS: '', SSH_ASKPASS: '', LC_ALL: 'C', ...options?.env },
|
|
11
|
+
}, (error, stdout, stderr) => {
|
|
12
|
+
const code = error === null ? 0 : typeof error.code === 'number' ? error.code : 1;
|
|
13
|
+
resolve({ code, stdout: String(stdout), stderr: String(stderr) || (error && typeof error.code !== 'number' ? error.message : '') });
|
|
14
|
+
});
|
|
15
|
+
});
|
|
16
|
+
}
|
|
17
|
+
export class PullRequestCreateBlocked extends Error {
|
|
18
|
+
block;
|
|
19
|
+
constructor(block) {
|
|
20
|
+
super(block.message);
|
|
21
|
+
this.block = block;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
const blocked = (kind, message, branch) => { throw new PullRequestCreateBlocked({ kind, message, ...(branch === undefined ? {} : { branch }) }); };
|
|
25
|
+
const text = (value) => typeof value === 'string' && value !== '' ? value : undefined;
|
|
26
|
+
const record = (value) => typeof value === 'object' && value !== null && !Array.isArray(value) ? value : {};
|
|
27
|
+
const list = (value) => Array.isArray(value) ? value : [];
|
|
28
|
+
/** The GitHub repository a remote URL names: https, ssh or scp-like, with or without `.git`. */
|
|
29
|
+
export function githubRepositoryOf(url) {
|
|
30
|
+
const match = /^(?:https?:\/\/(?:[^@/]+@)?github\.com\/|ssh:\/\/git@github\.com(?::\d+)?\/|git@github\.com:)([A-Za-z0-9](?:[A-Za-z0-9-]{0,38}))\/([A-Za-z0-9._-]{1,100}?)(?:\.git)?\/?$/.exec(url.trim());
|
|
31
|
+
return match ? { owner: match[1], repo: match[2] } : undefined;
|
|
32
|
+
}
|
|
33
|
+
const TEMPLATE_PATHS = [
|
|
34
|
+
'.github/pull_request_template.md', '.github/PULL_REQUEST_TEMPLATE.md',
|
|
35
|
+
'pull_request_template.md', 'PULL_REQUEST_TEMPLATE.md',
|
|
36
|
+
'docs/pull_request_template.md', 'docs/PULL_REQUEST_TEMPLATE.md',
|
|
37
|
+
];
|
|
38
|
+
const TEMPLATE_LIMIT = 64 * 1024;
|
|
39
|
+
/**
|
|
40
|
+
* The repository's PR template: a regular file inside the repository, never one a link points out of it
|
|
41
|
+
* to (a cloned repository's template could otherwise put a local file's text in the draft), nor a pipe.
|
|
42
|
+
*/
|
|
43
|
+
async function prTemplate(folder) {
|
|
44
|
+
const root = await realpath(folder).catch(() => undefined);
|
|
45
|
+
if (root === undefined)
|
|
46
|
+
return undefined;
|
|
47
|
+
for (const path of TEMPLATE_PATHS) {
|
|
48
|
+
const file = join(root, path);
|
|
49
|
+
const info = await lstat(file).catch(() => undefined);
|
|
50
|
+
if (info === undefined || !info.isFile() || info.size === 0)
|
|
51
|
+
continue;
|
|
52
|
+
const real = await realpath(file).catch(() => undefined);
|
|
53
|
+
if (real === undefined || (real !== root && !real.startsWith(`${root}${sep}`)))
|
|
54
|
+
continue;
|
|
55
|
+
const handle = await open(real, 'r').catch(() => undefined);
|
|
56
|
+
if (handle === undefined)
|
|
57
|
+
continue;
|
|
58
|
+
try {
|
|
59
|
+
const buffer = Buffer.alloc(Math.min(info.size, TEMPLATE_LIMIT));
|
|
60
|
+
const { bytesRead } = await handle.read(buffer, 0, buffer.length, 0);
|
|
61
|
+
const found = buffer.subarray(0, bytesRead).toString('utf8');
|
|
62
|
+
if (found.trim() !== '')
|
|
63
|
+
return { path, text: found };
|
|
64
|
+
}
|
|
65
|
+
finally {
|
|
66
|
+
await handle.close();
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
return undefined;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The title and body a new PR starts with. One commit: its subject, and its message as the body. More:
|
|
73
|
+
* the task's name, and the commits as a list. The repository's template, when it has one, is the body.
|
|
74
|
+
*/
|
|
75
|
+
export function draftText(commits, taskName, branch, template) {
|
|
76
|
+
const humanized = branch.replace(/^[^/]+\//, '').replace(/[-_]+/g, ' ').trim();
|
|
77
|
+
const fallback = humanized ? humanized[0].toUpperCase() + humanized.slice(1) : branch;
|
|
78
|
+
const title = commits.length === 1 ? commits[0].headline : (taskName?.trim() || fallback);
|
|
79
|
+
if (template !== undefined)
|
|
80
|
+
return { title, body: template };
|
|
81
|
+
if (commits.length === 1)
|
|
82
|
+
return { title, body: commits[0].body?.trim() ?? '' };
|
|
83
|
+
return { title, body: [...commits].reverse().map(commit => `- ${commit.headline}`).join('\n') };
|
|
84
|
+
}
|
|
85
|
+
const COMMITS_SHOWN = 50;
|
|
86
|
+
const PULLS_FROM_HEAD = 'pullRequests(headRefName: $head, states: OPEN, first: 10) { nodes { number url title headRepositoryOwner { login } } }';
|
|
87
|
+
const DRAFT_QUERY = `query($owner: String!, $name: String!, $head: String!, $headRef: String!) { viewer { login } repository(owner: $owner, name: $name) { viewerPermission defaultBranchRef { name } refs(refPrefix: "refs/heads/", first: 100) { nodes { name } } ref(qualifiedName: $headRef) { target { oid } } ${PULLS_FROM_HEAD} isFork parent { owner { login } name defaultBranchRef { name } refs(refPrefix: "refs/heads/", first: 100) { nodes { name } } ${PULLS_FROM_HEAD} } } }`;
|
|
88
|
+
/** The open PR among `nodes` whose head is `owner`'s branch. */
|
|
89
|
+
function openFrom(nodes, owner) {
|
|
90
|
+
const found = list(nodes).map(record).find(node => text(record(node.headRepositoryOwner).login)?.toLowerCase() === owner.toLowerCase());
|
|
91
|
+
const url = text(found?.url);
|
|
92
|
+
const number = typeof found?.number === 'number' ? found.number : undefined;
|
|
93
|
+
return url !== undefined && number !== undefined ? { url, number, title: text(found?.title) ?? '' } : undefined;
|
|
94
|
+
}
|
|
95
|
+
const branchNames = (refs) => list(record(refs).nodes).map(node => text(record(node).name)).filter((name) => name !== undefined);
|
|
96
|
+
async function repositoryState(gh, owner, repo, head) {
|
|
97
|
+
const raw = await gh(['api', 'graphql', '--hostname', 'github.com', '-f', `query=${DRAFT_QUERY}`, '-f', `owner=${owner}`, '-f', `name=${repo}`, '-f', `head=${head}`, '-f', `headRef=refs/heads/${head}`]);
|
|
98
|
+
const value = record(JSON.parse(raw));
|
|
99
|
+
const errors = list(value.errors).map(entry => text(record(entry).message)).filter((message) => message !== undefined);
|
|
100
|
+
if (errors.length > 0)
|
|
101
|
+
throw new Error(`GraphQL: ${errors.join('; ')}`);
|
|
102
|
+
const data = record(value.data);
|
|
103
|
+
const repository = record(data.repository);
|
|
104
|
+
const viewer = text(record(data.viewer).login);
|
|
105
|
+
if (viewer === undefined || data.repository === null || data.repository === undefined)
|
|
106
|
+
throw new Error(`GraphQL: Could not resolve to a Repository with the name '${owner}/${repo}'.`);
|
|
107
|
+
const permission = text(repository.viewerPermission);
|
|
108
|
+
const existing = openFrom(record(repository.pullRequests).nodes, owner);
|
|
109
|
+
const parent = record(repository.parent);
|
|
110
|
+
const parentOwner = text(record(parent.owner).login);
|
|
111
|
+
const parentName = text(parent.name);
|
|
112
|
+
const parentDefault = text(record(parent.defaultBranchRef).name);
|
|
113
|
+
const parentExisting = openFrom(record(parent.pullRequests).nodes, owner);
|
|
114
|
+
return {
|
|
115
|
+
viewer,
|
|
116
|
+
writable: permission === 'ADMIN' || permission === 'MAINTAIN' || permission === 'WRITE',
|
|
117
|
+
...(text(record(repository.defaultBranchRef).name) === undefined ? {} : { defaultBranch: text(record(repository.defaultBranchRef).name) }),
|
|
118
|
+
branches: branchNames(repository.refs),
|
|
119
|
+
...(text(record(record(repository.ref).target).oid) === undefined ? {} : { remoteSha: text(record(record(repository.ref).target).oid) }),
|
|
120
|
+
...(existing === undefined ? {} : { existing }),
|
|
121
|
+
...(repository.isFork === true && parentOwner !== undefined && parentName !== undefined ? {
|
|
122
|
+
parent: { owner: parentOwner, repo: parentName, defaultBranch: parentDefault ?? 'main', branches: branchNames(parent.refs), ...(parentExisting === undefined ? {} : { existing: parentExisting }) },
|
|
123
|
+
} : {}),
|
|
124
|
+
};
|
|
125
|
+
}
|
|
126
|
+
/** The first signed-in account that can push to the repository, read as that account. */
|
|
127
|
+
async function writableAccount(gh, owner, repo, head) {
|
|
128
|
+
let accounts;
|
|
129
|
+
try {
|
|
130
|
+
accounts = await ghAccounts(gh);
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
if (!isGhListingUnsupported(error))
|
|
134
|
+
throw error;
|
|
135
|
+
accounts = [];
|
|
136
|
+
}
|
|
137
|
+
// gh too old to list its accounts: read as its default account.
|
|
138
|
+
if (accounts.length === 0)
|
|
139
|
+
return { state: await repositoryState(gh, owner, repo, head) };
|
|
140
|
+
let readable;
|
|
141
|
+
let firstError;
|
|
142
|
+
for (const user of accounts) {
|
|
143
|
+
try {
|
|
144
|
+
const state = await repositoryState((args, options) => gh(args, { ...options, user }), owner, repo, head);
|
|
145
|
+
if (state.writable)
|
|
146
|
+
return { user, state };
|
|
147
|
+
readable ??= { user, state };
|
|
148
|
+
}
|
|
149
|
+
catch (error) {
|
|
150
|
+
if (!isGhAccessError(error))
|
|
151
|
+
throw error;
|
|
152
|
+
firstError ??= error;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (readable)
|
|
156
|
+
return readable;
|
|
157
|
+
throw firstError;
|
|
158
|
+
}
|
|
159
|
+
async function revParse(git, ref) {
|
|
160
|
+
const found = await git(['rev-parse', '--verify', '--quiet', `${ref}^{commit}`]);
|
|
161
|
+
return found.code === 0 ? found.stdout.trim() || undefined : undefined;
|
|
162
|
+
}
|
|
163
|
+
/** The branch's commits the base lacks, as far as the folder knows the base; `known` is false when it does not have it. */
|
|
164
|
+
async function localCommits(git, base, head) {
|
|
165
|
+
const from = await revParse(git, `refs/remotes/origin/${base}`) ?? await revParse(git, `refs/heads/${base}`);
|
|
166
|
+
if (from === undefined)
|
|
167
|
+
return { commits: [], more: false, known: false };
|
|
168
|
+
const log = await git(['log', `--max-count=${COMMITS_SHOWN + 1}`, '--format=%H%x1f%s%x1f%b%x1e', `${from}..refs/heads/${head}`]);
|
|
169
|
+
if (log.code !== 0)
|
|
170
|
+
return { commits: [], more: false, known: false };
|
|
171
|
+
const commits = log.stdout.split('\x1e').map(entry => entry.replace(/^\n/, '')).filter(Boolean).map(entry => {
|
|
172
|
+
const [oid = '', headline = '', body = ''] = entry.split('\x1f');
|
|
173
|
+
return { oid, headline, ...(body.trim() === '' ? {} : { body: body.trim() }) };
|
|
174
|
+
}).filter(commit => /^[0-9a-f]{40}$/.test(commit.oid));
|
|
175
|
+
return { commits: commits.slice(0, COMMITS_SHOWN), more: commits.length > COMMITS_SHOWN, known: true };
|
|
176
|
+
}
|
|
177
|
+
/**
|
|
178
|
+
* What a Create Pull Request from the task's folder would open. Refused with a `PullRequestCreateBlocked`
|
|
179
|
+
* when there is nothing to open: not a Git repository, a detached head, the default branch itself, no
|
|
180
|
+
* GitHub remote, no commits past the base, a branch GitHub has commits of that the folder lacks, or no
|
|
181
|
+
* signed-in account that can push.
|
|
182
|
+
*/
|
|
183
|
+
export async function readCreateDraft(gh, git, folder, options = {}) {
|
|
184
|
+
const top = await git(['rev-parse', '--show-toplevel']);
|
|
185
|
+
if (top.code !== 0)
|
|
186
|
+
blocked('no-repository', 'This task’s folder is not a Git repository.');
|
|
187
|
+
const branchRead = await git(['symbolic-ref', '--quiet', '--short', 'HEAD']);
|
|
188
|
+
const head = branchRead.stdout.trim();
|
|
189
|
+
if (branchRead.code !== 0 || head === '')
|
|
190
|
+
blocked('detached', 'This task’s folder is not on a branch (a detached HEAD). Switch to a branch to open a pull request.');
|
|
191
|
+
const headSha = await revParse(git, 'HEAD');
|
|
192
|
+
if (headSha === undefined)
|
|
193
|
+
blocked('no-commits', `${head} has no commits yet.`, head);
|
|
194
|
+
const remote = await git(['remote', 'get-url', 'origin']);
|
|
195
|
+
const repository = remote.code === 0 ? githubRepositoryOf(remote.stdout) : undefined;
|
|
196
|
+
if (repository === undefined)
|
|
197
|
+
blocked('no-github-remote', 'This task’s folder has no GitHub remote named origin.', head);
|
|
198
|
+
const { owner, repo } = repository;
|
|
199
|
+
const { user, state } = await writableAccount(gh, owner, repo, head);
|
|
200
|
+
if (!state.writable)
|
|
201
|
+
blocked('no-push-access', `None of the GitHub accounts signed in to gh can push to ${owner}/${repo}.`, head);
|
|
202
|
+
const defaultBranch = state.defaultBranch ?? 'main';
|
|
203
|
+
if (head === defaultBranch)
|
|
204
|
+
blocked('default-branch', `This task’s folder is on ${defaultBranch}, the default branch. Switch to a branch with the task’s commits to open a pull request.`, head);
|
|
205
|
+
const base = options.base !== undefined && options.base !== head ? options.base : defaultBranch;
|
|
206
|
+
const { commits, more, known } = await localCommits(git, base, head);
|
|
207
|
+
let push = 'none';
|
|
208
|
+
if (state.remoteSha === undefined)
|
|
209
|
+
push = 'new';
|
|
210
|
+
else if (state.remoteSha !== headSha) {
|
|
211
|
+
const ancestor = await git(['merge-base', '--is-ancestor', state.remoteSha, headSha]);
|
|
212
|
+
if (ancestor.code !== 0)
|
|
213
|
+
blocked('diverged', `${head} on GitHub has commits this folder does not. Pull them before opening a pull request.`, head);
|
|
214
|
+
push = 'ahead';
|
|
215
|
+
}
|
|
216
|
+
// Where the folder does not have the base, the commits are unknown and GitHub decides.
|
|
217
|
+
if (known && commits.length === 0 && state.existing === undefined)
|
|
218
|
+
blocked('no-commits', `${head} has no commits that ${base} lacks.`, head);
|
|
219
|
+
const template = await prTemplate(top.stdout.trim() || folder);
|
|
220
|
+
const drafted = draftText(commits, options.taskName, head, template?.text);
|
|
221
|
+
const { title } = drafted;
|
|
222
|
+
// A task started from an issue of this repository closes it when the PR merges, as GitHub's keyword does.
|
|
223
|
+
const issue = options.closesIssue?.(owner, repo);
|
|
224
|
+
// Qualified with the repository, so it still names origin's issue when the PR opens in a fork's parent (review P3).
|
|
225
|
+
const body = issue === undefined ? drafted.body : `${drafted.body.trimEnd()}${drafted.body.trim() === '' ? '' : '\n\n'}Closes ${owner}/${repo}#${issue}`;
|
|
226
|
+
const status = await git(['status', '--porcelain=v1', '-z', '--untracked-files=no']);
|
|
227
|
+
const uncommitted = status.code === 0 ? status.stdout.split('\0').filter(Boolean).length : 0;
|
|
228
|
+
const branches = [defaultBranch, ...state.branches.filter(name => name !== defaultBranch && name !== head)];
|
|
229
|
+
return {
|
|
230
|
+
viewer: user ?? state.viewer,
|
|
231
|
+
owner, repo, head, headSha: headSha, base, defaultBranch, branches, title, body,
|
|
232
|
+
...(template === undefined ? {} : { template: template.path }),
|
|
233
|
+
commits: commits.map(({ oid, headline }) => ({ oid, headline })),
|
|
234
|
+
...(known ? {} : { commitsUnknown: true }),
|
|
235
|
+
...(more ? { moreCommits: true } : {}),
|
|
236
|
+
push, uncommitted,
|
|
237
|
+
...(state.existing === undefined ? {} : { existing: state.existing }),
|
|
238
|
+
...(state.parent === undefined ? {} : { parent: { ...state.parent, branches: [state.parent.defaultBranch, ...state.parent.branches.filter(name => name !== state.parent.defaultBranch)] } }),
|
|
239
|
+
};
|
|
240
|
+
}
|
|
241
|
+
/**
|
|
242
|
+
* The confirmed account's own token, for git to push as it: gh's token for that login, or, where gh
|
|
243
|
+
* cannot name one (it signs in with GH_TOKEN, or is too old for --user), the token gh itself uses, whose
|
|
244
|
+
* account the caller already checked answers as `login` (`runAsAccount`).
|
|
245
|
+
*/
|
|
246
|
+
export async function accountToken(gh, login) {
|
|
247
|
+
try {
|
|
248
|
+
const token = (await gh(['auth', 'token', '--hostname', 'github.com', '--user', login])).trim();
|
|
249
|
+
if (token)
|
|
250
|
+
return token;
|
|
251
|
+
}
|
|
252
|
+
catch (error) {
|
|
253
|
+
if (!/no (?:oauth )?token (?:found )?for|unknown flag: --user/i.test(error instanceof Error ? error.message : String(error)))
|
|
254
|
+
throw error;
|
|
255
|
+
}
|
|
256
|
+
const fromEnvironment = process.env.GH_TOKEN || process.env.GITHUB_TOKEN;
|
|
257
|
+
if (fromEnvironment)
|
|
258
|
+
return fromEnvironment;
|
|
259
|
+
const token = (await gh(['auth', 'token', '--hostname', 'github.com'])).trim();
|
|
260
|
+
if (!token)
|
|
261
|
+
throw new PullRequestAccountChanged(`gh has no token for ${login}. Sign in with gh auth login and try again.`);
|
|
262
|
+
return token;
|
|
263
|
+
}
|
|
264
|
+
/**
|
|
265
|
+
* How git signs in as the confirmed account for one push or fetch: a credential helper that hands git
|
|
266
|
+
* the account's token, in place of every helper git would otherwise ask (its stored credentials, gh's
|
|
267
|
+
* own helper). The token travels in the environment of that one git command, never in an argument.
|
|
268
|
+
*/
|
|
269
|
+
export function tokenCredentials(token) {
|
|
270
|
+
return {
|
|
271
|
+
GIT_CONFIG_COUNT: '2',
|
|
272
|
+
GIT_CONFIG_KEY_0: 'credential.https://github.com.helper', GIT_CONFIG_VALUE_0: '',
|
|
273
|
+
GIT_CONFIG_KEY_1: 'credential.https://github.com.helper',
|
|
274
|
+
GIT_CONFIG_VALUE_1: '!f() { test "$1" = get || exit 0; echo username=x-access-token; echo "password=$ANDORRA_GIT_TOKEN"; }; f',
|
|
275
|
+
ANDORRA_GIT_TOKEN: token,
|
|
276
|
+
};
|
|
277
|
+
}
|
|
278
|
+
/** A repository's https address on GitHub, which a push as the confirmed account goes to. */
|
|
279
|
+
export const githubHttpsUrl = (owner, repo) => `https://github.com/${owner}/${repo}.git`;
|
|
280
|
+
/**
|
|
281
|
+
* Pushes `sha`, the commit the person confirmed, to `head` in `owner/repo`: the GitHub repository the
|
|
282
|
+
* confirm named (origin, checked), not wherever `remote.origin.pushurl` points, over https as the
|
|
283
|
+
* confirmed account (its token, never git's stored credentials or an ssh key). Never forced; never
|
|
284
|
+
* prompts. A branch without an upstream then tracks origin's.
|
|
285
|
+
*/
|
|
286
|
+
export async function pushBranch(git, repository, head, sha, token) {
|
|
287
|
+
const pushed = await git(['push', '--porcelain', githubHttpsUrl(repository.owner, repository.repo), `${sha}:refs/heads/${head}`], { timeoutMs: 120_000, env: tokenCredentials(token) });
|
|
288
|
+
if (pushed.code !== 0) {
|
|
289
|
+
const reason = pushed.stderr.split('\n').map(line => line.trim()).filter(line => line && !/^(To |Done$)/.test(line)).slice(0, 3).join(' ');
|
|
290
|
+
const scope = /workflow` scope|without `?workflow`? scope/i.test(reason) ? ' The account’s gh token lacks the workflow scope; run gh auth refresh -s workflow and try again.' : '';
|
|
291
|
+
throw new PullRequestPushFailed(`git could not push ${head}: ${reason || `exit ${pushed.code}`}${scope}`);
|
|
292
|
+
}
|
|
293
|
+
// As `git push -u origin` would; best effort, the push has happened.
|
|
294
|
+
if ((await git(['config', '--get', `branch.${head}.remote`])).code !== 0) {
|
|
295
|
+
await git(['config', `branch.${head}.remote`, 'origin']);
|
|
296
|
+
await git(['config', `branch.${head}.merge`, `refs/heads/${head}`]);
|
|
297
|
+
}
|
|
298
|
+
await git(['update-ref', `refs/remotes/origin/${head}`, sha]);
|
|
299
|
+
}
|
|
300
|
+
/** git refused the push; nothing was opened on GitHub. */
|
|
301
|
+
export class PullRequestPushFailed extends PullRequestRefusal {
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* Opens the pull request the person confirmed, through `gh` already pinned to their account. The folder
|
|
305
|
+
* must still be on `head` at `headSha` with origin at `owner/repo`; the branch is pushed first when GitHub
|
|
306
|
+
* lacks it or its newest commits (never forced); then the PR is opened. With `attach`, the open PR GitHub
|
|
307
|
+
* already has for the branch is returned instead, and nothing is written.
|
|
308
|
+
*/
|
|
309
|
+
export async function createFromFolder(gh, git, request) {
|
|
310
|
+
const branchRead = await git(['symbolic-ref', '--quiet', '--short', 'HEAD']);
|
|
311
|
+
const headSha = await revParse(git, 'HEAD');
|
|
312
|
+
if (branchRead.code !== 0 || branchRead.stdout.trim() !== request.head || headSha?.toLowerCase() !== request.headSha.toLowerCase()) {
|
|
313
|
+
throw new PullRequestRefusal(`This task’s folder changed since you looked: it is no longer on ${request.head} at the commit you saw. Look again before opening the pull request.`);
|
|
314
|
+
}
|
|
315
|
+
const remote = await git(['remote', 'get-url', 'origin']);
|
|
316
|
+
const repository = remote.code === 0 ? githubRepositoryOf(remote.stdout) : undefined;
|
|
317
|
+
if (repository === undefined || repository.owner.toLowerCase() !== request.owner.toLowerCase() || repository.repo.toLowerCase() !== request.repo.toLowerCase()) {
|
|
318
|
+
throw new PullRequestRefusal(`This task’s folder no longer pushes to ${request.owner}/${request.repo}. Look again before opening the pull request.`);
|
|
319
|
+
}
|
|
320
|
+
const into = request.into !== undefined && (request.into.owner.toLowerCase() !== request.owner.toLowerCase() || request.into.repo.toLowerCase() !== request.repo.toLowerCase()) ? request.into : undefined;
|
|
321
|
+
if (into === undefined && request.head === request.base)
|
|
322
|
+
throw new PullRequestRefusal(`${request.head} cannot be opened into itself. Pick another base.`);
|
|
323
|
+
const state = await repositoryState(gh, request.owner, request.repo, request.head);
|
|
324
|
+
if (state.viewer.toLowerCase() !== request.as.toLowerCase())
|
|
325
|
+
throw new PullRequestAccountChanged(`GitHub now answers as ${state.viewer}, not ${request.as}. Check the account and try again.`);
|
|
326
|
+
// The default branch is never pushed from here, whatever asks.
|
|
327
|
+
if (request.head === (state.defaultBranch ?? 'main'))
|
|
328
|
+
throw new PullRequestRefusal(`${request.head} is the default branch of ${request.owner}/${request.repo}; a pull request needs a branch of its own.`);
|
|
329
|
+
// A fork's PR goes only into the repository GitHub says it was forked from.
|
|
330
|
+
if (into !== undefined && (state.parent === undefined || state.parent.owner.toLowerCase() !== into.owner.toLowerCase() || state.parent.repo.toLowerCase() !== into.repo.toLowerCase())) {
|
|
331
|
+
throw new PullRequestRefusal(`${request.owner}/${request.repo} is not a fork of ${into.owner}/${into.repo}. Look again before opening the pull request.`);
|
|
332
|
+
}
|
|
333
|
+
const existing = into === undefined ? state.existing : state.parent?.existing;
|
|
334
|
+
if (existing !== undefined) {
|
|
335
|
+
if (request.attach === true)
|
|
336
|
+
return { url: existing.url, number: existing.number, pushed: false, attached: true };
|
|
337
|
+
throw new PullRequestRefusal(`#${existing.number} is already open for ${request.head}. Attach it instead.`);
|
|
338
|
+
}
|
|
339
|
+
if (request.attach === true)
|
|
340
|
+
throw new PullRequestRefusal(`No pull request is open for ${request.head} any more.`);
|
|
341
|
+
if (!state.writable)
|
|
342
|
+
throw new PullRequestRefusal(`${state.viewer} cannot push to ${request.owner}/${request.repo}.`);
|
|
343
|
+
let pushed = false;
|
|
344
|
+
if (state.remoteSha?.toLowerCase() !== headSha.toLowerCase()) {
|
|
345
|
+
if (state.remoteSha !== undefined && (await git(['merge-base', '--is-ancestor', state.remoteSha, headSha])).code !== 0) {
|
|
346
|
+
throw new PullRequestRefusal(`${request.head} on GitHub has commits this folder does not. Pull them before opening a pull request.`);
|
|
347
|
+
}
|
|
348
|
+
await pushBranch(git, { owner: request.owner, repo: request.repo }, request.head, request.headSha, await accountToken(gh, request.as));
|
|
349
|
+
pushed = true;
|
|
350
|
+
}
|
|
351
|
+
try {
|
|
352
|
+
const created = await createPullRequest(gh, into === undefined ? request : { ...request, owner: into.owner, repo: into.repo, head: `${request.owner}:${request.head}` });
|
|
353
|
+
return { ...created, pushed };
|
|
354
|
+
}
|
|
355
|
+
catch (error) {
|
|
356
|
+
// The push stands even though GitHub did not open the pull request; say so.
|
|
357
|
+
if (pushed)
|
|
358
|
+
throw Object.assign(error instanceof Error ? error : new Error(String(error)), { pushedBranch: request.head });
|
|
359
|
+
throw error;
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
/** Opens the pull request (`POST /repos/{owner}/{repo}/pulls`), with the text on standard input. */
|
|
363
|
+
export async function createPullRequest(gh, request) {
|
|
364
|
+
const raw = await gh(['api', '--hostname', 'github.com', '-X', 'POST', `repos/${request.owner}/${request.repo}/pulls`, '--input', '-'], {
|
|
365
|
+
input: JSON.stringify({ title: request.title, head: request.head, base: request.base, draft: request.draft, ...(request.body === undefined ? {} : { body: request.body }) }),
|
|
366
|
+
});
|
|
367
|
+
const value = record(JSON.parse(raw));
|
|
368
|
+
const url = text(value.html_url);
|
|
369
|
+
const number = typeof value.number === 'number' ? value.number : undefined;
|
|
370
|
+
if (url === undefined || number === undefined)
|
|
371
|
+
throw new Error('GitHub opened the pull request but did not say where.');
|
|
372
|
+
return { url, number };
|
|
373
|
+
}
|
|
374
|
+
// What the app-server accepts, checked before anything reaches git or gh.
|
|
375
|
+
export function branchName(value, name) {
|
|
376
|
+
if (typeof value !== 'string' || value === '' || value.length > 250 || value.startsWith('-') || value.startsWith('/') || value.endsWith('/') || /[\s~^:?*[\\\0]|\.\.|@\{|\/\/|\.lock$|^\.|\/\./.test(value)) {
|
|
377
|
+
throw new Error(`${name} is not a branch name`);
|
|
378
|
+
}
|
|
379
|
+
return value;
|
|
380
|
+
}
|
|
381
|
+
export function repositoryName(value, name) {
|
|
382
|
+
if (typeof value !== 'string' || !/^[A-Za-z0-9._-]{1,100}$/.test(value))
|
|
383
|
+
throw new Error(`${name} is not a repository name`);
|
|
384
|
+
return value;
|
|
385
|
+
}
|
|
386
|
+
export function pullRequestTitle(value) {
|
|
387
|
+
if (typeof value !== 'string' || value.trim() === '')
|
|
388
|
+
throw new Error('title is required');
|
|
389
|
+
if (value.length > 256)
|
|
390
|
+
throw new Error('title is longer than GitHub’s 256 characters');
|
|
391
|
+
return value;
|
|
392
|
+
}
|
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
import type { FileDiff } from './file-diff.js';
|
|
2
|
+
import { type PullRequestCheck } from './pull-request-checks.js';
|
|
3
|
+
export { checksRollup, isChecksRollup, parseCheck, sameChecksRollup, type PullRequestCheck, type PullRequestCheckState, type PullRequestChecksRollup } from './pull-request-checks.js';
|
|
4
|
+
import { type GhRunner, type PullRequestState, type SessionPullRequest } from './pull-requests.js';
|
|
5
|
+
import { type PullRequestReviewContext } from './pull-request-review.js';
|
|
6
|
+
import { type PullRequestMergeContext } from './pull-request-merge.js';
|
|
7
|
+
import { type CheckLogExcerpt } from './pull-request-logs.js';
|
|
8
|
+
export interface PullRequestCommit {
|
|
9
|
+
oid: string;
|
|
10
|
+
headline: string;
|
|
11
|
+
body?: string;
|
|
12
|
+
authors: string[];
|
|
13
|
+
committedAt?: string;
|
|
14
|
+
}
|
|
15
|
+
export type PullRequestReviewState = 'approved' | 'changes-requested' | 'commented' | 'dismissed' | 'pending';
|
|
16
|
+
export interface PullRequestReview {
|
|
17
|
+
author: string;
|
|
18
|
+
state: PullRequestReviewState;
|
|
19
|
+
submittedAt?: string;
|
|
20
|
+
body?: string;
|
|
21
|
+
}
|
|
22
|
+
export type PullRequestMergeable = 'mergeable' | 'conflicting' | 'unknown';
|
|
23
|
+
export type PullRequestMergeState = 'clean' | 'blocked' | 'behind' | 'dirty' | 'unstable' | 'draft' | 'has-hooks' | 'unknown';
|
|
24
|
+
export type PullRequestReviewDecision = 'approved' | 'changes-requested' | 'review-required';
|
|
25
|
+
export interface PullRequestDetails {
|
|
26
|
+
url: string;
|
|
27
|
+
number: number;
|
|
28
|
+
title: string;
|
|
29
|
+
state: PullRequestState;
|
|
30
|
+
headRefName: string;
|
|
31
|
+
headSha?: string;
|
|
32
|
+
/** The fork's owner when the head branch lives in another repository. */
|
|
33
|
+
headOwner?: string;
|
|
34
|
+
baseRefName: string;
|
|
35
|
+
author?: {
|
|
36
|
+
login: string;
|
|
37
|
+
bot?: true;
|
|
38
|
+
};
|
|
39
|
+
createdAt?: string;
|
|
40
|
+
updatedAt?: string;
|
|
41
|
+
mergedAt?: string;
|
|
42
|
+
closedAt?: string;
|
|
43
|
+
mergedBy?: string;
|
|
44
|
+
mergeable: PullRequestMergeable;
|
|
45
|
+
mergeState: PullRequestMergeState;
|
|
46
|
+
reviewDecision?: PullRequestReviewDecision;
|
|
47
|
+
body: string;
|
|
48
|
+
additions: number;
|
|
49
|
+
deletions: number;
|
|
50
|
+
changedFiles: number;
|
|
51
|
+
commits: PullRequestCommit[];
|
|
52
|
+
checks: PullRequestCheck[];
|
|
53
|
+
reviews: PullRequestReview[];
|
|
54
|
+
}
|
|
55
|
+
export type PullRequestFileStatus = 'added' | 'modified' | 'deleted' | 'renamed' | 'copied';
|
|
56
|
+
export interface PullRequestFile {
|
|
57
|
+
path: string;
|
|
58
|
+
previousPath?: string;
|
|
59
|
+
status: PullRequestFileStatus;
|
|
60
|
+
additions: number;
|
|
61
|
+
deletions: number;
|
|
62
|
+
/** The file's diff; null when GitHub sends none (a binary or very large file) or it is past the limits below. */
|
|
63
|
+
diff: Pick<FileDiff, 'hunks' | 'hiddenLines'> | null;
|
|
64
|
+
binary?: true;
|
|
65
|
+
tooLarge?: true;
|
|
66
|
+
}
|
|
67
|
+
export type PullRequestReadErrorKind = 'gh-missing' | 'signed-out' | 'no-access' | 'rate-limited' | 'failed';
|
|
68
|
+
export interface PullRequestReadError {
|
|
69
|
+
kind: PullRequestReadErrorKind;
|
|
70
|
+
message: string;
|
|
71
|
+
/** When a rate-limited read may be tried again. */
|
|
72
|
+
retryAt?: string;
|
|
73
|
+
}
|
|
74
|
+
export declare function pullRequestStateOf(value: {
|
|
75
|
+
state?: unknown;
|
|
76
|
+
isDraft?: unknown;
|
|
77
|
+
}): PullRequestState;
|
|
78
|
+
/** The answer of `gh pr view --json <DETAIL_FIELDS>`, checked field by field. */
|
|
79
|
+
export declare function parsePullRequestDetails(raw: unknown, fallbackUrl: string): PullRequestDetails;
|
|
80
|
+
export declare function readPullRequestDetails(gh: GhRunner, url: string): Promise<PullRequestDetails>;
|
|
81
|
+
/** Per-file and whole-PR caps on the lines sent to the desktop; past them a file is listed without its diff. */
|
|
82
|
+
export declare const FILE_DIFF_LINE_LIMIT = 3000;
|
|
83
|
+
export declare const PULL_REQUEST_DIFF_LINE_LIMIT = 30000;
|
|
84
|
+
/** GitHub's `patch` for one file (hunks without the ---/+++ header) as the desktop's diff hunks. */
|
|
85
|
+
export declare function parsePatch(patch: string, maxLines?: number): Pick<FileDiff, 'hunks' | 'hiddenLines'>;
|
|
86
|
+
/** The diff text sent for one PR, so the answer stays well inside a paired machine's 12 MiB relay limit. */
|
|
87
|
+
export declare const PULL_REQUEST_DIFF_CHAR_LIMIT = 4000000;
|
|
88
|
+
/**
|
|
89
|
+
* GitHub's files list as the tab shows it. A file with no `patch` is binary when its path says so, too
|
|
90
|
+
* large to diff when it has changed lines, and otherwise has no lines to show (an empty file, a pure
|
|
91
|
+
* rename, a mode change). Past the line or character budget a file is listed without its diff.
|
|
92
|
+
*/
|
|
93
|
+
export declare function parsePullRequestFiles(pages: readonly unknown[], budget?: number, chars?: number): PullRequestFile[];
|
|
94
|
+
export declare function readPullRequestFiles(gh: GhRunner, pullRequest: Pick<SessionPullRequest, 'owner' | 'repo' | 'number'>): Promise<{
|
|
95
|
+
files: PullRequestFile[];
|
|
96
|
+
truncated: boolean;
|
|
97
|
+
}>;
|
|
98
|
+
/** What went wrong reading a PR, in the tab's terms. The text of the original error is kept as the message. */
|
|
99
|
+
export declare function classifyGhError(error: unknown): PullRequestReadError;
|
|
100
|
+
export type PullRequestWriteErrorKind = PullRequestReadErrorKind | 'refused' | 'account-changed';
|
|
101
|
+
/**
|
|
102
|
+
* Why a write did not happen. `refused` is GitHub (or the engine) saying no, in its own words;
|
|
103
|
+
* `account-changed` means the account that would write is no longer the one the person confirmed.
|
|
104
|
+
* `sent` counts line comments that reached a pending review before the rest failed.
|
|
105
|
+
*/
|
|
106
|
+
export interface PullRequestWriteError {
|
|
107
|
+
kind: PullRequestWriteErrorKind;
|
|
108
|
+
message: string;
|
|
109
|
+
/** The comment of a "close with comment" was posted before the close failed. */
|
|
110
|
+
commentPosted?: true;
|
|
111
|
+
retryAt?: string;
|
|
112
|
+
sent?: number;
|
|
113
|
+
/** Which line comments (by their place in the request) joined a pending review before the rest failed, when they are not just the first `sent`. */
|
|
114
|
+
joined?: number[];
|
|
115
|
+
}
|
|
116
|
+
/** A write the engine itself turns down before asking GitHub, in words for the person. */
|
|
117
|
+
export declare class PullRequestRefusal extends Error {
|
|
118
|
+
}
|
|
119
|
+
/** The account that would write is not the one the person confirmed. */
|
|
120
|
+
export declare class PullRequestAccountChanged extends Error {
|
|
121
|
+
}
|
|
122
|
+
/**
|
|
123
|
+
* What went wrong with a write: the read's kinds, plus GitHub refusing it. gh prints a refused
|
|
124
|
+
* GraphQL mutation as "gh: <reason>" (older gh and the GUI stub as "GraphQL: <reason>") and a refused
|
|
125
|
+
* REST call as "gh: <reason> (HTTP 422)" or "HTTP 422: <reason>".
|
|
126
|
+
*/
|
|
127
|
+
export declare function classifyWriteError(error: unknown): PullRequestWriteError;
|
|
128
|
+
/**
|
|
129
|
+
* Runs a write as `login` and nobody else: with that account's own token (`gh auth token --user`), never
|
|
130
|
+
* whichever account gh has active. Where gh cannot give one (GH_TOKEN, an old gh), it first checks that
|
|
131
|
+
* gh's default account answers as that login, and refuses otherwise. A write is never retried as
|
|
132
|
+
* another account.
|
|
133
|
+
*/
|
|
134
|
+
export declare function runAsAccount<T>(gh: GhRunner, login: string, run: (gh: GhRunner) => Promise<T>): Promise<T>;
|
|
135
|
+
export interface PullRequestDetailsAnswer {
|
|
136
|
+
details: PullRequestDetails | null;
|
|
137
|
+
fetchedAt?: string;
|
|
138
|
+
error?: PullRequestReadError;
|
|
139
|
+
}
|
|
140
|
+
export interface PullRequestFilesAnswer {
|
|
141
|
+
files: PullRequestFile[] | null;
|
|
142
|
+
headSha?: string;
|
|
143
|
+
truncated?: true;
|
|
144
|
+
fetchedAt?: string;
|
|
145
|
+
error?: PullRequestReadError;
|
|
146
|
+
}
|
|
147
|
+
export interface PullRequestReviewAnswer {
|
|
148
|
+
review: PullRequestReviewContext | null;
|
|
149
|
+
fetchedAt?: string;
|
|
150
|
+
error?: PullRequestReadError;
|
|
151
|
+
}
|
|
152
|
+
export interface PullRequestMergeAnswer {
|
|
153
|
+
merge: PullRequestMergeContext | null;
|
|
154
|
+
fetchedAt?: string;
|
|
155
|
+
error?: PullRequestReadError;
|
|
156
|
+
}
|
|
157
|
+
export type PullRequestWriteAnswer<T> = {
|
|
158
|
+
ok: true;
|
|
159
|
+
value: T;
|
|
160
|
+
} | {
|
|
161
|
+
ok: false;
|
|
162
|
+
error: PullRequestWriteError;
|
|
163
|
+
};
|
|
164
|
+
/** A request inside this window after the last read is answered from the cache, so polls from several windows cost one read. */
|
|
165
|
+
export declare const DETAILS_FRESH_MS = 15000;
|
|
166
|
+
/** An explicit refresh still waits this long after the last read. */
|
|
167
|
+
export declare const REFRESH_FLOOR_MS = 2000;
|
|
168
|
+
export declare class PullRequestReader {
|
|
169
|
+
private readonly gh;
|
|
170
|
+
private readonly now;
|
|
171
|
+
private readonly onDetails?;
|
|
172
|
+
private readonly details;
|
|
173
|
+
private readonly files;
|
|
174
|
+
private readonly reviews;
|
|
175
|
+
private readonly merges;
|
|
176
|
+
private readonly teams;
|
|
177
|
+
/**
|
|
178
|
+
* `onDetails` hears each details read GitHub actually answered, never a cached or failed one, so what
|
|
179
|
+
* it records (the sidebar's checks rollup) is never older than what is already stored.
|
|
180
|
+
*/
|
|
181
|
+
constructor(gh: GhRunner, now?: () => number, onDetails?: ((sessionId: string, pullRequest: SessionPullRequest, details: PullRequestDetails) => void) | undefined);
|
|
182
|
+
readDetails(sessionId: string, pullRequest: SessionPullRequest, options?: {
|
|
183
|
+
refresh?: boolean;
|
|
184
|
+
}): Promise<PullRequestDetailsAnswer>;
|
|
185
|
+
readFiles(sessionId: string, pullRequest: SessionPullRequest, options?: {
|
|
186
|
+
refresh?: boolean;
|
|
187
|
+
headSha?: string;
|
|
188
|
+
}): Promise<PullRequestFilesAnswer>;
|
|
189
|
+
/**
|
|
190
|
+
* The PR's review state (threads, viewed files, reviewers) through the account that reads its details.
|
|
191
|
+
* `force` reads GitHub again at once, after a write, once any read already under way has finished.
|
|
192
|
+
*/
|
|
193
|
+
readReview(sessionId: string, pullRequest: SessionPullRequest, options?: {
|
|
194
|
+
refresh?: boolean;
|
|
195
|
+
force?: boolean;
|
|
196
|
+
}): Promise<PullRequestReviewAnswer>;
|
|
197
|
+
/**
|
|
198
|
+
* Runs a write as the account that read the review the person saw, and only when that account is
|
|
199
|
+
* still `as`, the login they confirmed: a write never goes out under another name. The write uses
|
|
200
|
+
* that account's own token (`gh auth token --user`), never whichever account gh has active; where
|
|
201
|
+
* gh cannot give one (GH_TOKEN, an old gh), it first checks that gh's default account answers as
|
|
202
|
+
* that login. A write is never retried as another account. Afterwards the next read of the review
|
|
203
|
+
* (and of the details, for writes that change them) goes to GitHub.
|
|
204
|
+
*/
|
|
205
|
+
write<T>(sessionId: string, pullRequest: SessionPullRequest, as: string, run: (gh: GhRunner, state: PullRequestReviewContext, details: PullRequestDetails | undefined) => Promise<T>, options?: {
|
|
206
|
+
details?: boolean;
|
|
207
|
+
}): Promise<PullRequestWriteAnswer<T>>;
|
|
208
|
+
/**
|
|
209
|
+
* What merging needs: the methods the repository allows, GitHub's own commit message, why it is
|
|
210
|
+
* blocked, and whether the branch can be updated. Read when the Merge panel opens, through the
|
|
211
|
+
* account that reads the PR's details, cached and backed off like them.
|
|
212
|
+
*/
|
|
213
|
+
readMerge(sessionId: string, pullRequest: SessionPullRequest, options?: {
|
|
214
|
+
refresh?: boolean;
|
|
215
|
+
force?: boolean;
|
|
216
|
+
}): Promise<PullRequestMergeAnswer>;
|
|
217
|
+
/**
|
|
218
|
+
* A merge-side write (merge, update the branch, ready for review): like `write`, but checked against
|
|
219
|
+
* the merge read the person saw, and the details, review and merge reads all go to GitHub next.
|
|
220
|
+
*/
|
|
221
|
+
writeMerge<T>(sessionId: string, pullRequest: SessionPullRequest, as: string, run: (gh: GhRunner, state: PullRequestMergeContext) => Promise<T>): Promise<PullRequestWriteAnswer<T>>;
|
|
222
|
+
/**
|
|
223
|
+
* The teams that can be asked to review, kept for 10 minutes per repository and account: teams change
|
|
224
|
+
* rarely, and the review is read every minute. A failed read leaves the picker without teams.
|
|
225
|
+
*/
|
|
226
|
+
private repositoryTeams;
|
|
227
|
+
/** The account a read of this PR last succeeded as, from whichever read knows it. */
|
|
228
|
+
private knownUser;
|
|
229
|
+
private readonly logs;
|
|
230
|
+
/**
|
|
231
|
+
* A failed job's log excerpt, through the account that reads the PR. A finished job's log does not
|
|
232
|
+
* change, so it is kept for ten minutes; a running one is asked again after 15 seconds.
|
|
233
|
+
*/
|
|
234
|
+
readCheckLog(sessionId: string, pullRequest: SessionPullRequest, jobId: string): Promise<{
|
|
235
|
+
log: CheckLogExcerpt | null;
|
|
236
|
+
error?: PullRequestReadError;
|
|
237
|
+
}>;
|
|
238
|
+
forget(sessionId: string): void;
|
|
239
|
+
private entry;
|
|
240
|
+
private load;
|
|
241
|
+
private stamps;
|
|
242
|
+
}
|