@andorra-labs/code 0.1.15 → 0.1.17
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 +6 -0
- package/dist/src/acp-agent.js +15 -4
- package/dist/src/agent-definitions.js +29 -6
- package/dist/src/agent-execution.js +15 -10
- package/dist/src/agent-mcp-catalog.js +1 -0
- package/dist/src/agent-mcp.d.ts +4 -1
- package/dist/src/agent-mcp.js +46 -10
- package/dist/src/andorra/entry.d.ts +10 -0
- package/dist/src/andorra/entry.js +18 -4
- package/dist/src/andorra/hosted.d.ts +2 -1
- package/dist/src/andorra/hosted.js +2 -1
- package/dist/src/andorra/notices.d.ts +5 -0
- package/dist/src/andorra/notices.js +7 -0
- package/dist/src/andorra/state.d.ts +3 -2
- package/dist/src/andorra/state.js +4 -4
- package/dist/src/andorra/terminal.js +7 -2
- package/dist/src/anthropic-credentials.js +2 -0
- package/dist/src/app-server/error.d.ts +5 -0
- package/dist/src/app-server/error.js +7 -0
- package/dist/src/app-server/protocol.d.ts +22 -44
- package/dist/src/app-server/protocol.js +5 -1
- package/dist/src/app-server/workspace.d.ts +89 -0
- package/dist/src/app-server/workspace.js +457 -11
- package/dist/src/app-server.d.ts +465 -32
- package/dist/src/app-server.js +3308 -517
- package/dist/src/auth/deployment.js +2 -2
- package/dist/src/bridge/cloud.d.ts +27 -0
- package/dist/src/bridge/cloud.js +187 -0
- package/dist/src/bridge/command.d.ts +30 -0
- package/dist/src/bridge/command.js +347 -0
- package/dist/src/bridge/control.d.ts +68 -0
- package/dist/src/bridge/control.js +233 -0
- package/dist/src/bridge/engines.d.ts +90 -0
- package/dist/src/bridge/engines.js +601 -0
- package/dist/src/bridge/host-ops.d.ts +9 -0
- package/dist/src/bridge/host-ops.js +129 -0
- package/dist/src/bridge/host.d.ts +47 -0
- package/dist/src/bridge/host.js +540 -0
- package/dist/src/bridge/link.d.ts +38 -0
- package/dist/src/bridge/link.js +508 -0
- package/dist/src/bridge/login-link.d.ts +29 -0
- package/dist/src/bridge/login-link.js +54 -0
- package/dist/src/bridge/protocol.d.ts +136 -0
- package/dist/src/bridge/protocol.js +11 -0
- package/dist/src/bridge/store.d.ts +12 -0
- package/dist/src/bridge/store.js +61 -0
- package/dist/src/bridge/wording.d.ts +6 -0
- package/dist/src/bridge/wording.js +10 -0
- package/dist/src/browser-runner.js +1 -0
- package/dist/src/browser-server.js +7 -1
- package/dist/src/browser-ui/style.css +3 -3
- package/dist/src/browser-ui/tokens.css +28 -17
- package/dist/src/change-authors.d.ts +41 -0
- package/dist/src/change-authors.js +146 -0
- package/dist/src/claude-provider.js +3 -1
- package/dist/src/cli.js +202 -38
- package/dist/src/codex-errors.js +4 -1
- package/dist/src/coding-tools.js +15 -15
- package/dist/src/command-screens.d.ts +6 -0
- package/dist/src/command-screens.js +19 -14
- package/dist/src/commands.d.ts +14 -1
- package/dist/src/compaction-phrases.d.ts +4 -0
- package/dist/src/compaction-phrases.js +20 -0
- package/dist/src/completion.js +82 -5
- package/dist/src/config.d.ts +71 -3
- package/dist/src/config.js +202 -77
- package/dist/src/context-compaction.js +2 -1
- package/dist/src/deadline.js +13 -1
- package/dist/src/decision/DecisionManager.js +3 -2
- package/dist/src/decision/checks.js +1 -1
- package/dist/src/decision/requestPhrases.d.ts +4 -0
- package/dist/src/decision/requestPhrases.js +22 -0
- package/dist/src/default-graph.js +1 -1
- package/dist/src/file-diff.d.ts +4 -0
- package/dist/src/file-diff.js +2 -1
- package/dist/src/github-app/agent-writes.d.ts +102 -0
- package/dist/src/github-app/agent-writes.js +320 -0
- package/dist/src/github-app/client.d.ts +349 -0
- package/dist/src/github-app/client.js +810 -0
- package/dist/src/github-app/event-source.d.ts +208 -0
- package/dist/src/github-app/event-source.js +684 -0
- package/dist/src/github-app/metrics.d.ts +32 -0
- package/dist/src/github-app/metrics.js +49 -0
- package/dist/src/github-app/review-loop.d.ts +274 -0
- package/dist/src/github-app/review-loop.js +1177 -0
- package/dist/src/github-app/review-node.d.ts +81 -0
- package/dist/src/github-app/review-node.js +205 -0
- package/dist/src/github-app/review-post.d.ts +73 -0
- package/dist/src/github-app/review-post.js +139 -0
- package/dist/src/github-app/review-text.d.ts +107 -0
- package/dist/src/github-app/review-text.js +1133 -0
- package/dist/src/github-app/review-threads.d.ts +47 -0
- package/dist/src/github-app/review-threads.js +107 -0
- package/dist/src/github-app/reviewer.d.ts +121 -0
- package/dist/src/github-app/reviewer.js +598 -0
- package/dist/src/github-app/store.d.ts +59 -0
- package/dist/src/github-app/store.js +148 -0
- package/dist/src/github-app/triggers.d.ts +141 -0
- package/dist/src/github-app/triggers.js +441 -0
- package/dist/src/github-inbox.d.ts +55 -0
- package/dist/src/github-inbox.js +153 -0
- package/dist/src/github-names.d.ts +8 -0
- package/dist/src/github-names.js +8 -0
- package/dist/src/glob-search.d.ts +61 -0
- package/dist/src/glob-search.js +180 -0
- package/dist/src/graph-health.js +13 -4
- package/dist/src/graph-manager-chat.js +1 -1
- package/dist/src/graph-preview.d.ts +46 -0
- package/dist/src/graph-preview.js +55 -0
- package/dist/src/graph-provider-portability.js +34 -11
- package/dist/src/graph-quota.d.ts +29 -2
- package/dist/src/graph-quota.js +68 -10
- package/dist/src/graph-runner.js +88 -10
- package/dist/src/graph-server.d.ts +1 -1
- package/dist/src/graph-server.js +11 -9
- package/dist/src/graph-sync.js +3 -3
- package/dist/src/graph-ui/app.js +201 -40
- package/dist/src/graph-ui/controls.css +1 -0
- package/dist/src/graph-ui/embed.js +2 -2
- package/dist/src/graph-ui/icons.js +3 -1
- 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 +26 -5
- 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 +28 -20
- package/dist/src/graphs.d.ts +34 -1
- package/dist/src/graphs.js +162 -17
- package/dist/src/image-input.d.ts +16 -0
- package/dist/src/image-input.js +41 -0
- package/dist/src/membership.d.ts +30 -1
- package/dist/src/membership.js +48 -8
- package/dist/src/model-health-cache.js +1 -1
- package/dist/src/model-metadata.d.ts +7 -1
- package/dist/src/model-metadata.js +39 -17
- package/dist/src/model-relay/local.d.ts +116 -0
- package/dist/src/model-relay/local.js +468 -0
- package/dist/src/model-relay/remote.d.ts +129 -0
- package/dist/src/model-relay/remote.js +252 -0
- package/dist/src/model-relay/runs.d.ts +34 -0
- package/dist/src/model-relay/runs.js +146 -0
- package/dist/src/model-relay/wire.d.ts +5 -0
- package/dist/src/model-relay/wire.js +5 -0
- package/dist/src/plan-pass.d.ts +59 -0
- package/dist/src/plan-pass.js +165 -0
- package/dist/src/project-registry.d.ts +21 -0
- package/dist/src/project-registry.js +74 -0
- package/dist/src/provider-engine.js +3 -0
- package/dist/src/provider-import.d.ts +0 -12
- package/dist/src/provider-import.js +7 -195
- package/dist/src/providers.d.ts +40 -2
- package/dist/src/providers.js +93 -23
- 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 +87 -0
- package/dist/src/pull-request-logs.js +183 -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 +369 -8
- package/dist/src/pull-requests.js +524 -55
- package/dist/src/react-runtime.js +38 -5
- package/dist/src/remote/engine-pool.d.ts +88 -4
- package/dist/src/remote/engine-pool.js +379 -90
- package/dist/src/remote/roots.d.ts +19 -0
- package/dist/src/remote/roots.js +45 -7
- package/dist/src/remote/types.d.ts +0 -44
- package/dist/src/run-control.d.ts +17 -0
- package/dist/src/run-control.js +47 -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 +7 -2
- package/dist/src/run-records.js +71 -6
- package/dist/src/runtime.d.ts +9 -1
- package/dist/src/runtime.js +24 -4
- package/dist/src/session-changes.d.ts +35 -0
- package/dist/src/session-changes.js +194 -6
- package/dist/src/session-monitor.js +2 -1
- package/dist/src/session-reaper.d.ts +1 -1
- package/dist/src/session-reaper.js +6 -1
- package/dist/src/session-summary.d.ts +2 -1
- package/dist/src/session-summary.js +2 -1
- package/dist/src/sessions.d.ts +11 -1
- package/dist/src/sessions.js +17 -4
- package/dist/src/skills/bundled/diagram/SKILL.md +5 -3
- package/dist/src/skills/bundled/graph-manager/SKILL.md +7 -4
- package/dist/src/skills/bundled/provenance.json +2 -2
- package/dist/src/skills/runtime-skills.js +1 -1
- 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 +17 -0
- package/dist/src/telemetry/emitter.js +76 -5
- package/dist/src/telemetry/schema.d.ts +47 -2
- package/dist/src/telemetry/schema.js +120 -5
- package/dist/src/telemetry/surface.d.ts +3 -1
- package/dist/src/telemetry/surface.js +4 -1
- package/dist/src/telemetry/uploader.d.ts +6 -1
- package/dist/src/telemetry/uploader.js +22 -3
- package/dist/src/terminal-ui.d.ts +1 -0
- package/dist/src/terminal-ui.js +10 -3
- package/dist/src/test-edit-guard.d.ts +7 -0
- package/dist/src/test-edit-guard.js +49 -1
- package/dist/src/timers.d.ts +2 -0
- package/dist/src/tools.js +59 -9
- package/dist/src/transcript.js +8 -2
- package/dist/src/types.d.ts +7 -0
- package/dist/src/usage-analytics.js +18 -10
- package/package.json +6 -1
- package/dist/src/remote/cert.d.ts +0 -11
- package/dist/src/remote/cert.js +0 -87
- package/dist/src/remote/client.d.ts +0 -119
- package/dist/src/remote/client.js +0 -544
- package/dist/src/remote/command.d.ts +0 -4
- package/dist/src/remote/command.js +0 -194
- package/dist/src/remote/errors.d.ts +0 -11
- package/dist/src/remote/errors.js +0 -14
- package/dist/src/remote/network.d.ts +0 -22
- package/dist/src/remote/network.js +0 -102
- package/dist/src/remote/pairing.d.ts +0 -63
- package/dist/src/remote/pairing.js +0 -299
- package/dist/src/remote/pem.d.ts +0 -2
- package/dist/src/remote/pem.js +0 -7
- package/dist/src/remote/serve.d.ts +0 -68
- package/dist/src/remote/serve.js +0 -244
- package/dist/src/remote/server.d.ts +0 -21
- package/dist/src/remote/server.js +0 -406
|
@@ -0,0 +1,401 @@
|
|
|
1
|
+
import { abilitiesOf, CONVERSATION_FIELDS, parseConversation, parseReactions, REPOSITORY_LABEL_FIELDS, THREAD_COMMENT_FIELDS, } from './pull-request-conversation.js';
|
|
2
|
+
const THREAD_FIELDS = `id isResolved isOutdated path line originalLine startLine diffSide subjectType viewerCanResolve viewerCanUnresolve viewerCanReply resolvedBy { login } comments(first: 50) { totalCount nodes { id body createdAt url ${THREAD_COMMENT_FIELDS} } }`;
|
|
3
|
+
const REVIEWER_FIELDS = 'requestedReviewer { __typename ... on User { login } ... on Bot { login } ... on Mannequin { login } ... on Team { slug name } }';
|
|
4
|
+
export const REVIEW_CONTEXT_QUERY = `query($owner: String!, $name: String!, $number: Int!) { viewer { login } repository(owner: $owner, name: $name) { viewerPermission owner { __typename } pullRequest(number: $number) { id viewerDidAuthor reviews(states: PENDING, first: 1) { nodes { id comments { totalCount } } } reviewRequests(first: 50) { nodes { ${REVIEWER_FIELDS} } } suggestedReviewers { reviewer { login } } files(first: 100) { pageInfo { hasNextPage endCursor } nodes { path viewerViewedState } } reviewThreads(first: 100) { pageInfo { hasNextPage endCursor } nodes { ${THREAD_FIELDS} } } ${CONVERSATION_FIELDS} } assignableUsers(first: 100) { nodes { id login } } ${REPOSITORY_LABEL_FIELDS} } }`;
|
|
5
|
+
const THREAD_PAGE_QUERY = `query($owner: String!, $name: String!, $number: Int!, $after: String) { repository(owner: $owner, name: $name) { pullRequest(number: $number) { reviewThreads(first: 100, after: $after) { pageInfo { hasNextPage endCursor } nodes { ${THREAD_FIELDS} } } } } }`;
|
|
6
|
+
const FILE_PAGE_QUERY = 'query($owner: String!, $name: String!, $number: Int!, $after: String) { repository(owner: $owner, name: $name) { pullRequest(number: $number) { files(first: 100, after: $after) { pageInfo { hasNextPage endCursor } nodes { path viewerViewedState } } } } }';
|
|
7
|
+
/** At most this many pages of 100 threads and files; GitHub lists at most 3,000 files of a pull request. */
|
|
8
|
+
const MAX_THREAD_PAGES = 10;
|
|
9
|
+
const MAX_FILE_PAGES = 30;
|
|
10
|
+
const text = (value) => typeof value === 'string' && value !== '' ? value : undefined;
|
|
11
|
+
const record = (value) => typeof value === 'object' && value !== null && !Array.isArray(value) ? value : {};
|
|
12
|
+
const list = (value) => Array.isArray(value) ? value : [];
|
|
13
|
+
const positive = (value) => typeof value === 'number' && Number.isSafeInteger(value) && value > 0 ? value : undefined;
|
|
14
|
+
const loginOf = (value) => text(record(value).login)?.replace(/^app\//, '');
|
|
15
|
+
const PERMISSIONS = { ADMIN: 'admin', MAINTAIN: 'maintain', WRITE: 'write', TRIAGE: 'triage', READ: 'read' };
|
|
16
|
+
export function parseThread(value) {
|
|
17
|
+
const node = record(value);
|
|
18
|
+
const id = text(node.id);
|
|
19
|
+
const path = text(node.path);
|
|
20
|
+
if (id === undefined || path === undefined)
|
|
21
|
+
return undefined;
|
|
22
|
+
const comments = record(node.comments);
|
|
23
|
+
const read = list(comments.nodes).map(record).flatMap((comment) => {
|
|
24
|
+
const commentId = text(comment.id);
|
|
25
|
+
if (commentId === undefined)
|
|
26
|
+
return [];
|
|
27
|
+
const authorId = record(comment.author).databaseId;
|
|
28
|
+
return [{
|
|
29
|
+
id: commentId, author: loginOf(comment.author) ?? 'ghost', body: typeof comment.body === 'string' ? comment.body : '',
|
|
30
|
+
...defined({ createdAt: text(comment.createdAt), url: text(comment.url), authorId: positive(authorId) }),
|
|
31
|
+
...('viewerCanUpdate' in comment ? { ...abilitiesOf(comment), reactions: parseReactions(comment.reactionGroups) } : {}),
|
|
32
|
+
}];
|
|
33
|
+
});
|
|
34
|
+
const total = typeof comments.totalCount === 'number' && Number.isSafeInteger(comments.totalCount) ? comments.totalCount : read.length;
|
|
35
|
+
const line = positive(node.line);
|
|
36
|
+
const start = positive(node.startLine);
|
|
37
|
+
return {
|
|
38
|
+
id, path,
|
|
39
|
+
side: node.diffSide === 'LEFT' ? 'left' : 'right',
|
|
40
|
+
resolved: node.isResolved === true,
|
|
41
|
+
outdated: node.isOutdated === true,
|
|
42
|
+
canReply: node.viewerCanReply === true,
|
|
43
|
+
canResolve: node.viewerCanResolve === true,
|
|
44
|
+
canUnresolve: node.viewerCanUnresolve === true,
|
|
45
|
+
comments: read,
|
|
46
|
+
...defined({
|
|
47
|
+
line,
|
|
48
|
+
startLine: start !== undefined && line !== undefined && start < line ? start : undefined,
|
|
49
|
+
originalLine: positive(node.originalLine),
|
|
50
|
+
resolvedBy: loginOf(node.resolvedBy),
|
|
51
|
+
}),
|
|
52
|
+
...(node.subjectType === 'FILE' ? { onFile: true } : {}),
|
|
53
|
+
...(total > read.length ? { moreComments: total - read.length } : {}),
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
function viewedEntries(nodes, into) {
|
|
57
|
+
for (const node of list(nodes).map(record)) {
|
|
58
|
+
const path = text(node.path);
|
|
59
|
+
if (path === undefined)
|
|
60
|
+
continue;
|
|
61
|
+
if (node.viewerViewedState === 'VIEWED')
|
|
62
|
+
into[path] = 'viewed';
|
|
63
|
+
else if (node.viewerViewedState === 'DISMISSED')
|
|
64
|
+
into[path] = 'dismissed';
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
function reviewerOf(value) {
|
|
68
|
+
const reviewer = record(record(value).requestedReviewer);
|
|
69
|
+
if (reviewer.__typename === 'Team') {
|
|
70
|
+
const slug = text(reviewer.slug);
|
|
71
|
+
return slug === undefined ? undefined : { login: slug, team: true, ...defined({ name: text(reviewer.name) }) };
|
|
72
|
+
}
|
|
73
|
+
const login = loginOf(reviewer);
|
|
74
|
+
return login === undefined ? undefined : { login };
|
|
75
|
+
}
|
|
76
|
+
/** The first page of `REVIEW_CONTEXT_QUERY`, checked field by field. Later pages of threads and files are added by `readPullRequestReview`. */
|
|
77
|
+
export function parseReviewContext(raw) {
|
|
78
|
+
const data = record(record(raw).data);
|
|
79
|
+
const repository = record(data.repository);
|
|
80
|
+
const pull = record(repository.pullRequest);
|
|
81
|
+
const viewer = loginOf(data.viewer);
|
|
82
|
+
const pullRequestId = text(pull.id);
|
|
83
|
+
if (viewer === undefined || pullRequestId === undefined)
|
|
84
|
+
throw new Error('GraphQL: Could not resolve to a PullRequest (the review state came back empty)');
|
|
85
|
+
const viewed = {};
|
|
86
|
+
viewedEntries(record(pull.files).nodes, viewed);
|
|
87
|
+
const pending = record(list(record(pull.reviews).nodes)[0]);
|
|
88
|
+
const pendingId = text(pending.id);
|
|
89
|
+
const pendingCount = record(pending.comments).totalCount;
|
|
90
|
+
const unique = (logins) => [...new Set(logins.filter((login) => login !== undefined))];
|
|
91
|
+
return {
|
|
92
|
+
viewer,
|
|
93
|
+
pullRequestId,
|
|
94
|
+
viewerIsAuthor: pull.viewerDidAuthor === true,
|
|
95
|
+
permission: typeof repository.viewerPermission === 'string' ? PERMISSIONS[repository.viewerPermission] ?? 'none' : 'none',
|
|
96
|
+
threads: list(record(pull.reviewThreads).nodes).map(parseThread).filter((thread) => thread !== undefined),
|
|
97
|
+
viewed,
|
|
98
|
+
requestedReviewers: list(record(pull.reviewRequests).nodes).map(reviewerOf).filter((entry) => entry !== undefined),
|
|
99
|
+
suggestedReviewers: unique(list(pull.suggestedReviewers).map(entry => loginOf(record(entry).reviewer))),
|
|
100
|
+
assignableUsers: unique(list(record(repository.assignableUsers).nodes).map(loginOf)),
|
|
101
|
+
...(pendingId === undefined ? {} : { pendingReview: { id: pendingId, comments: typeof pendingCount === 'number' && pendingCount >= 0 ? pendingCount : 0 } }),
|
|
102
|
+
...(record(repository.owner).__typename === 'Organization' ? { organization: true } : {}),
|
|
103
|
+
...parseConversation(pull, repository),
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* The teams with access to the repository (`GET /repos/{owner}/{repo}/teams`), which GitHub lets the PR
|
|
108
|
+
* ask to review. Teams the viewer cannot see are left out by GitHub.
|
|
109
|
+
*/
|
|
110
|
+
export async function readRepositoryTeams(gh, pullRequest) {
|
|
111
|
+
const raw = await gh(['api', '--hostname', 'github.com', `repos/${pullRequest.owner}/${pullRequest.repo}/teams?per_page=100`]);
|
|
112
|
+
return list(JSON.parse(raw)).map(record).flatMap((team) => {
|
|
113
|
+
const slug = text(team.slug);
|
|
114
|
+
return slug === undefined ? [] : [{ login: slug, team: true, ...defined({ name: text(team.name) }) }];
|
|
115
|
+
});
|
|
116
|
+
}
|
|
117
|
+
const pageOf = (connection) => {
|
|
118
|
+
const info = record(record(connection).pageInfo);
|
|
119
|
+
return { hasNextPage: info.hasNextPage === true, ...defined({ endCursor: text(info.endCursor) }) };
|
|
120
|
+
};
|
|
121
|
+
/** One GraphQL read through `gh api graphql`; GitHub's errors are thrown in gh's own words ("GraphQL: …"). */
|
|
122
|
+
async function graphql(gh, query, variables) {
|
|
123
|
+
const raw = await gh(['api', 'graphql', '--hostname', 'github.com', '-f', `query=${query}`, ...variables], { maxBuffer: 32 * 1024 * 1024 });
|
|
124
|
+
const value = JSON.parse(raw);
|
|
125
|
+
graphqlErrors(value);
|
|
126
|
+
return value;
|
|
127
|
+
}
|
|
128
|
+
function graphqlErrors(value) {
|
|
129
|
+
const errors = list(value.errors).map(entry => text(record(entry).message)).filter((message) => message !== undefined);
|
|
130
|
+
if (errors.length > 0)
|
|
131
|
+
throw new Error(`GraphQL: ${errors.join('; ')}`);
|
|
132
|
+
}
|
|
133
|
+
export async function readPullRequestReview(gh, pullRequest) {
|
|
134
|
+
const variables = ['-f', `owner=${pullRequest.owner}`, '-f', `name=${pullRequest.repo}`, '-F', `number=${pullRequest.number}`];
|
|
135
|
+
const first = await graphql(gh, REVIEW_CONTEXT_QUERY, variables);
|
|
136
|
+
const state = parseReviewContext(first);
|
|
137
|
+
const pull = record(record(record(record(first).data).repository).pullRequest);
|
|
138
|
+
let truncated = false;
|
|
139
|
+
let threads = pageOf(pull.reviewThreads);
|
|
140
|
+
for (let page = 1; threads.hasNextPage && threads.endCursor !== undefined; page++) {
|
|
141
|
+
if (page >= MAX_THREAD_PAGES) {
|
|
142
|
+
truncated = true;
|
|
143
|
+
break;
|
|
144
|
+
}
|
|
145
|
+
const next = record(record(record(record(await graphql(gh, THREAD_PAGE_QUERY, [...variables, '-f', `after=${threads.endCursor}`])).data).repository).pullRequest).reviewThreads;
|
|
146
|
+
state.threads.push(...list(record(next).nodes).map(parseThread).filter((thread) => thread !== undefined));
|
|
147
|
+
threads = pageOf(next);
|
|
148
|
+
}
|
|
149
|
+
let files = pageOf(pull.files);
|
|
150
|
+
for (let page = 1; files.hasNextPage && files.endCursor !== undefined; page++) {
|
|
151
|
+
if (page >= MAX_FILE_PAGES) {
|
|
152
|
+
truncated = true;
|
|
153
|
+
break;
|
|
154
|
+
}
|
|
155
|
+
const next = record(record(record(record(await graphql(gh, FILE_PAGE_QUERY, [...variables, '-f', `after=${files.endCursor}`])).data).repository).pullRequest).files;
|
|
156
|
+
viewedEntries(record(next).nodes, state.viewed);
|
|
157
|
+
files = pageOf(next);
|
|
158
|
+
}
|
|
159
|
+
return truncated ? { ...state, truncated: true } : state;
|
|
160
|
+
}
|
|
161
|
+
/** One mutation, its variables in a JSON body on gh's standard input. GitHub's answer stays on a thrown error (`stdout`), for a batch that partly went through. */
|
|
162
|
+
async function mutate(gh, query, variables) {
|
|
163
|
+
const raw = await gh(['api', 'graphql', '--hostname', 'github.com', '--input', '-'], { input: JSON.stringify({ query, variables }) });
|
|
164
|
+
const value = JSON.parse(raw);
|
|
165
|
+
try {
|
|
166
|
+
graphqlErrors(value);
|
|
167
|
+
}
|
|
168
|
+
catch (error) {
|
|
169
|
+
throw Object.assign(error, { stdout: raw });
|
|
170
|
+
}
|
|
171
|
+
return record(value.data);
|
|
172
|
+
}
|
|
173
|
+
/** The `data` of GitHub's answer that a failed gh call printed, if it printed one. */
|
|
174
|
+
function answeredData(error) {
|
|
175
|
+
const stdout = error?.stdout;
|
|
176
|
+
if (typeof stdout !== 'string')
|
|
177
|
+
return {};
|
|
178
|
+
try {
|
|
179
|
+
return record(record(JSON.parse(stdout)).data);
|
|
180
|
+
}
|
|
181
|
+
catch {
|
|
182
|
+
return {};
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
/** Line comments join a pending review this many to a request, so a long review takes a few calls rather than one per comment. */
|
|
186
|
+
export const PENDING_COMMENT_BATCH = 10;
|
|
187
|
+
/** A REST call with a JSON body on standard input (`gh api -X <method> <endpoint> --input -`). */
|
|
188
|
+
async function rest(gh, method, endpoint, body) {
|
|
189
|
+
await gh(['api', '--hostname', 'github.com', '-X', method, endpoint, ...(body === undefined ? [] : ['--input', '-'])], body === undefined ? undefined : { input: JSON.stringify(body) });
|
|
190
|
+
}
|
|
191
|
+
export async function replyToThread(gh, threadId, body) {
|
|
192
|
+
await mutate(gh, 'mutation($thread: ID!, $body: String!) { addPullRequestReviewThreadReply(input: { pullRequestReviewThreadId: $thread, body: $body }) { comment { id } } }', { thread: threadId, body });
|
|
193
|
+
}
|
|
194
|
+
export async function setThreadResolved(gh, threadId, resolved) {
|
|
195
|
+
const field = resolved ? 'resolveReviewThread' : 'unresolveReviewThread';
|
|
196
|
+
await mutate(gh, `mutation($thread: ID!) { ${field}(input: { threadId: $thread }) { thread { id isResolved } } }`, { thread: threadId });
|
|
197
|
+
}
|
|
198
|
+
export async function setFileViewed(gh, pullRequestId, path, viewed) {
|
|
199
|
+
const field = viewed ? 'markFileAsViewed' : 'unmarkFileAsViewed';
|
|
200
|
+
await mutate(gh, `mutation($pr: ID!, $path: String!) { ${field}(input: { pullRequestId: $pr, path: $path }) { clientMutationId } }`, { pr: pullRequestId, path });
|
|
201
|
+
}
|
|
202
|
+
const EVENTS = { comment: 'COMMENT', approve: 'APPROVE', 'request-changes': 'REQUEST_CHANGES' };
|
|
203
|
+
const SIDES = { left: 'LEFT', right: 'RIGHT' };
|
|
204
|
+
const draftThread = (comment) => ({
|
|
205
|
+
path: comment.path, line: comment.line, side: SIDES[comment.side], body: comment.body,
|
|
206
|
+
...(comment.startLine === undefined ? {} : { startLine: comment.startLine, startSide: SIDES[comment.side] }),
|
|
207
|
+
});
|
|
208
|
+
/**
|
|
209
|
+
* Posts a review with its line comments. With no review pending on GitHub it is one call that creates
|
|
210
|
+
* and submits it at `commitOid` (the head the person read), so nothing is left half-posted. A review the
|
|
211
|
+
* person started on github.com is used instead: the comments join it and it is submitted.
|
|
212
|
+
*/
|
|
213
|
+
export async function submitReview(gh, state, review) {
|
|
214
|
+
const urlOf = (data, field) => defined({ url: text(record(record(data[field]).pullRequestReview).url) });
|
|
215
|
+
if (state.pendingReview === undefined) {
|
|
216
|
+
const data = await mutate(gh, 'mutation($pr: ID!, $event: PullRequestReviewEvent!, $body: String, $commit: GitObjectID, $threads: [DraftPullRequestReviewThread]) { addPullRequestReview(input: { pullRequestId: $pr, event: $event, body: $body, commitOID: $commit, threads: $threads }) { pullRequestReview { id url state } } }', { pr: state.pullRequestId, event: EVENTS[review.event], ...defined({ body: review.body, commit: review.commitOid }), threads: review.comments.map(draftThread) });
|
|
217
|
+
return urlOf(data, 'addPullRequestReview');
|
|
218
|
+
}
|
|
219
|
+
// The comments join the pending review in batches, one request each; GitHub runs each comment of a
|
|
220
|
+
// batch on its own. When one fails, the error says which joined (and how many in a row from the
|
|
221
|
+
// first, for an older desktop), so the tab drops those drafts and a retry does not post them twice.
|
|
222
|
+
const pending = state.pendingReview.id;
|
|
223
|
+
const joined = [];
|
|
224
|
+
try {
|
|
225
|
+
for (let start = 0; start < review.comments.length; start += PENDING_COMMENT_BATCH) {
|
|
226
|
+
const batch = review.comments.slice(start, start + PENDING_COMMENT_BATCH);
|
|
227
|
+
const declarations = batch.map((_, index) => `$t${index}: AddPullRequestReviewThreadInput!`).join(', ');
|
|
228
|
+
const fields = batch.map((_, index) => `t${index}: addPullRequestReviewThread(input: $t${index}) { thread { id } }`).join(' ');
|
|
229
|
+
const variables = Object.fromEntries(batch.map((comment, index) => [`t${index}`, { pullRequestReviewId: pending, ...draftThread(comment) }]));
|
|
230
|
+
try {
|
|
231
|
+
await mutate(gh, `mutation(${declarations}) { ${fields} }`, variables);
|
|
232
|
+
}
|
|
233
|
+
catch (error) {
|
|
234
|
+
const data = answeredData(error);
|
|
235
|
+
batch.forEach((_, index) => { if (text(record(record(data[`t${index}`]).thread).id) !== undefined)
|
|
236
|
+
joined.push(start + index); });
|
|
237
|
+
throw error;
|
|
238
|
+
}
|
|
239
|
+
batch.forEach((_, index) => joined.push(start + index));
|
|
240
|
+
}
|
|
241
|
+
const data = await mutate(gh, 'mutation($review: ID!, $event: PullRequestReviewEvent!, $body: String) { submitPullRequestReview(input: { pullRequestReviewId: $review, event: $event, body: $body }) { pullRequestReview { id url state } } }', { review: pending, event: EVENTS[review.event], ...defined({ body: review.body }) });
|
|
242
|
+
return urlOf(data, 'submitPullRequestReview');
|
|
243
|
+
}
|
|
244
|
+
catch (error) {
|
|
245
|
+
if (joined.length === 0)
|
|
246
|
+
throw error;
|
|
247
|
+
let prefix = 0;
|
|
248
|
+
while (joined.includes(prefix))
|
|
249
|
+
prefix++;
|
|
250
|
+
throw Object.assign(error instanceof Error ? error : new Error(String(error)), { sentComments: prefix, joinedComments: joined });
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
const RUN_URL = /^https:\/\/github\.com\/([^/]+)\/([^/]+)\/actions\/runs\/(\d+)(?:[/?#]|$)/i;
|
|
254
|
+
/** The GitHub Actions workflow runs behind the failed checks, in the PR's own repository; other checks cannot be re-run from here. */
|
|
255
|
+
export function failedRunIds(checks, pullRequest) {
|
|
256
|
+
const ids = new Set();
|
|
257
|
+
for (const check of checks) {
|
|
258
|
+
if (check.state !== 'failed' || check.url === undefined)
|
|
259
|
+
continue;
|
|
260
|
+
const match = RUN_URL.exec(check.url);
|
|
261
|
+
if (match && match[1].toLowerCase() === pullRequest.owner.toLowerCase() && match[2].toLowerCase() === pullRequest.repo.toLowerCase())
|
|
262
|
+
ids.add(match[3]);
|
|
263
|
+
}
|
|
264
|
+
return [...ids];
|
|
265
|
+
}
|
|
266
|
+
export async function rerunFailedJobs(gh, pullRequest, runId) {
|
|
267
|
+
await rest(gh, 'POST', `repos/${pullRequest.owner}/${pullRequest.repo}/actions/runs/${runId}/rerun-failed-jobs`);
|
|
268
|
+
}
|
|
269
|
+
/** Asks `add` (people) and `addTeams` (team slugs) to review and withdraws the requests of `remove` (users, or teams by slug). */
|
|
270
|
+
export async function changeReviewers(gh, pullRequest, change) {
|
|
271
|
+
const endpoint = `repos/${pullRequest.owner}/${pullRequest.repo}/pulls/${pullRequest.number}/requested_reviewers`;
|
|
272
|
+
const addTeams = change.addTeams ?? [];
|
|
273
|
+
if (change.add.length + addTeams.length > 0)
|
|
274
|
+
await rest(gh, 'POST', endpoint, { reviewers: change.add, ...(addTeams.length > 0 ? { team_reviewers: addTeams } : {}) });
|
|
275
|
+
const users = change.remove.filter(entry => entry.team !== true).map(entry => entry.login);
|
|
276
|
+
const teams = change.remove.filter(entry => entry.team === true).map(entry => entry.login);
|
|
277
|
+
if (users.length + teams.length > 0)
|
|
278
|
+
await rest(gh, 'DELETE', endpoint, { reviewers: users, team_reviewers: teams });
|
|
279
|
+
}
|
|
280
|
+
// What the app-server accepts for the writes, checked before anything reaches gh.
|
|
281
|
+
/** GitHub's limit on a comment's body. */
|
|
282
|
+
export const COMMENT_BODY_LIMIT = 65_536;
|
|
283
|
+
const MAX_DRAFT_COMMENTS = 100;
|
|
284
|
+
const MAX_REVIEWERS = 15;
|
|
285
|
+
// GitHub logins, including Enterprise Managed Users' "name_shortcode".
|
|
286
|
+
const LOGIN = /^[A-Za-z0-9](?:[A-Za-z0-9_-]{0,38})$/;
|
|
287
|
+
const SLUG = /^[A-Za-z0-9][A-Za-z0-9_.-]{0,99}$/;
|
|
288
|
+
const NODE_ID = /^[A-Za-z0-9_=-]{1,200}$/;
|
|
289
|
+
export function commentBody(value, name, required) {
|
|
290
|
+
if (value === undefined || value === null) {
|
|
291
|
+
if (required)
|
|
292
|
+
throw new Error(`${name} is required`);
|
|
293
|
+
return undefined;
|
|
294
|
+
}
|
|
295
|
+
if (typeof value !== 'string')
|
|
296
|
+
throw new Error(`${name} must be text`);
|
|
297
|
+
if (value.length > COMMENT_BODY_LIMIT)
|
|
298
|
+
throw new Error(`${name} is longer than GitHub's ${COMMENT_BODY_LIMIT} characters`);
|
|
299
|
+
if (value.trim() === '') {
|
|
300
|
+
if (required)
|
|
301
|
+
throw new Error(`${name} is empty`);
|
|
302
|
+
return undefined;
|
|
303
|
+
}
|
|
304
|
+
return value;
|
|
305
|
+
}
|
|
306
|
+
export function githubLogin(value, name) {
|
|
307
|
+
if (typeof value !== 'string' || !LOGIN.test(value))
|
|
308
|
+
throw new Error(`${name} is not a GitHub login`);
|
|
309
|
+
return value;
|
|
310
|
+
}
|
|
311
|
+
export function nodeId(value, name) {
|
|
312
|
+
if (typeof value !== 'string' || !NODE_ID.test(value))
|
|
313
|
+
throw new Error(`${name} is not a GitHub node id`);
|
|
314
|
+
return value;
|
|
315
|
+
}
|
|
316
|
+
export function reviewPath(value, name = 'path') {
|
|
317
|
+
if (typeof value !== 'string' || value === '' || value.length > 4096 || value.includes('\0') || value.startsWith('/'))
|
|
318
|
+
throw new Error(`${name} is not a file in this pull request`);
|
|
319
|
+
return value;
|
|
320
|
+
}
|
|
321
|
+
export function reviewEvent(value) {
|
|
322
|
+
if (value === 'comment' || value === 'approve' || value === 'request-changes')
|
|
323
|
+
return value;
|
|
324
|
+
throw new Error('event must be comment, approve or request-changes');
|
|
325
|
+
}
|
|
326
|
+
export function draftComments(value) {
|
|
327
|
+
if (value === undefined || value === null)
|
|
328
|
+
return [];
|
|
329
|
+
if (!Array.isArray(value))
|
|
330
|
+
throw new Error('comments must be a list');
|
|
331
|
+
if (value.length > MAX_DRAFT_COMMENTS)
|
|
332
|
+
throw new Error(`a review can carry at most ${MAX_DRAFT_COMMENTS} line comments here`);
|
|
333
|
+
return value.map((entry, index) => {
|
|
334
|
+
const comment = record(entry);
|
|
335
|
+
const line = positive(comment.line);
|
|
336
|
+
if (line === undefined)
|
|
337
|
+
throw new Error(`comments[${index}].line must be a line number`);
|
|
338
|
+
if (comment.side !== 'left' && comment.side !== 'right')
|
|
339
|
+
throw new Error(`comments[${index}].side must be left or right`);
|
|
340
|
+
const start = comment.startLine === undefined ? undefined : positive(comment.startLine);
|
|
341
|
+
if (comment.startLine !== undefined && (start === undefined || start >= line))
|
|
342
|
+
throw new Error(`comments[${index}].startLine must come before line`);
|
|
343
|
+
return { path: reviewPath(comment.path, `comments[${index}].path`), line, side: comment.side, body: commentBody(comment.body, `comments[${index}].body`, true), ...defined({ startLine: start }) };
|
|
344
|
+
});
|
|
345
|
+
}
|
|
346
|
+
export function reviewerLogins(value, name) {
|
|
347
|
+
if (value === undefined || value === null)
|
|
348
|
+
return [];
|
|
349
|
+
if (!Array.isArray(value))
|
|
350
|
+
throw new Error(`${name} must be a list`);
|
|
351
|
+
if (value.length > MAX_REVIEWERS)
|
|
352
|
+
throw new Error(`${name} can name at most ${MAX_REVIEWERS} people`);
|
|
353
|
+
return [...new Set(value.map((entry, index) => githubLogin(entry, `${name}[${index}]`)))];
|
|
354
|
+
}
|
|
355
|
+
export function reviewerEntries(value, name) {
|
|
356
|
+
if (value === undefined || value === null)
|
|
357
|
+
return [];
|
|
358
|
+
if (!Array.isArray(value))
|
|
359
|
+
throw new Error(`${name} must be a list`);
|
|
360
|
+
if (value.length > MAX_REVIEWERS)
|
|
361
|
+
throw new Error(`${name} can name at most ${MAX_REVIEWERS} reviewers`);
|
|
362
|
+
return value.map((entry, index) => {
|
|
363
|
+
const reviewer = record(entry);
|
|
364
|
+
if (reviewer.team === true) {
|
|
365
|
+
if (typeof reviewer.login !== 'string' || !SLUG.test(reviewer.login))
|
|
366
|
+
throw new Error(`${name}[${index}] is not a team slug`);
|
|
367
|
+
return { login: reviewer.login, team: true };
|
|
368
|
+
}
|
|
369
|
+
return { login: githubLogin(reviewer.login, `${name}[${index}].login`) };
|
|
370
|
+
});
|
|
371
|
+
}
|
|
372
|
+
export function teamSlugs(value, name) {
|
|
373
|
+
if (value === undefined || value === null)
|
|
374
|
+
return [];
|
|
375
|
+
if (!Array.isArray(value))
|
|
376
|
+
throw new Error(`${name} must be a list`);
|
|
377
|
+
if (value.length > MAX_REVIEWERS)
|
|
378
|
+
throw new Error(`${name} can name at most ${MAX_REVIEWERS} teams`);
|
|
379
|
+
return [...new Set(value.map((entry, index) => {
|
|
380
|
+
if (typeof entry !== 'string' || !SLUG.test(entry))
|
|
381
|
+
throw new Error(`${name}[${index}] is not a team slug`);
|
|
382
|
+
return entry;
|
|
383
|
+
}))];
|
|
384
|
+
}
|
|
385
|
+
export function runIds(value) {
|
|
386
|
+
if (!Array.isArray(value) || value.length === 0 || value.length > 20)
|
|
387
|
+
throw new Error('runIds must list 1 to 20 workflow runs');
|
|
388
|
+
return [...new Set(value.map((entry, index) => {
|
|
389
|
+
const id = typeof entry === 'number' && Number.isSafeInteger(entry) && entry > 0 ? String(entry) : entry;
|
|
390
|
+
if (typeof id !== 'string' || !/^\d{1,20}$/.test(id))
|
|
391
|
+
throw new Error(`runIds[${index}] is not a workflow run id`);
|
|
392
|
+
return id;
|
|
393
|
+
}))];
|
|
394
|
+
}
|
|
395
|
+
function defined(values) {
|
|
396
|
+
const kept = {};
|
|
397
|
+
for (const [key, value] of Object.entries(values))
|
|
398
|
+
if (value !== undefined)
|
|
399
|
+
kept[key] = value;
|
|
400
|
+
return kept;
|
|
401
|
+
}
|