@aviaratech/ai-delivery 0.3.24 → 0.3.26
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/CONTRIBUTING.md +157 -16
- package/README.md +124 -9
- package/dist/agent.d.ts +2 -0
- package/dist/agent.js +1 -0
- package/dist/agent.js.map +1 -1
- package/dist/agentReadiness.test.d.ts +1 -0
- package/dist/agentReadiness.test.js +77 -0
- package/dist/agentReadiness.test.js.map +1 -0
- package/dist/cli.js +136 -36
- package/dist/cli.js.map +1 -1
- package/dist/config/deliveryConfig.js +5 -1
- package/dist/config/deliveryConfig.js.map +1 -1
- package/dist/contributorChecks.test.d.ts +1 -0
- package/dist/contributorChecks.test.js +1010 -0
- package/dist/contributorChecks.test.js.map +1 -0
- package/dist/currentConsumer.test.d.ts +1 -0
- package/dist/currentConsumer.test.js +832 -0
- package/dist/currentConsumer.test.js.map +1 -0
- package/dist/deliveryHealth.test.d.ts +1 -0
- package/dist/deliveryHealth.test.js +167 -0
- package/dist/deliveryHealth.test.js.map +1 -0
- package/dist/dependencyCompatibility.test.d.ts +1 -0
- package/dist/dependencyCompatibility.test.js +175 -0
- package/dist/dependencyCompatibility.test.js.map +1 -0
- package/dist/directoryIndependent.test.js +91 -1
- package/dist/directoryIndependent.test.js.map +1 -1
- package/dist/dispatch.d.ts +1 -0
- package/dist/dispatch.js +53 -4
- package/dist/dispatch.js.map +1 -1
- package/dist/fixtures/inertEntrypointChild.d.ts +1 -0
- package/dist/fixtures/inertEntrypointChild.js +62 -0
- package/dist/fixtures/inertEntrypointChild.js.map +1 -0
- package/dist/fixtures/inertEntrypoints.d.ts +49 -0
- package/dist/fixtures/inertEntrypoints.js +276 -0
- package/dist/fixtures/inertEntrypoints.js.map +1 -0
- package/dist/fixtures/inertTarget.d.ts +643 -0
- package/dist/fixtures/inertTarget.js +713 -0
- package/dist/fixtures/inertTarget.js.map +1 -0
- package/dist/fixtures/issue63.d.ts +68 -0
- package/dist/fixtures/issue63.js +272 -0
- package/dist/fixtures/issue63.js.map +1 -0
- package/dist/github/client.d.ts +2 -0
- package/dist/github/client.js +94 -12
- package/dist/github/client.js.map +1 -1
- package/dist/github/issueHistory.d.ts +40 -0
- package/dist/github/issueHistory.js +144 -0
- package/dist/github/issueHistory.js.map +1 -0
- package/dist/github/machineAuth.d.ts +9 -0
- package/dist/github/machineAuth.js +135 -0
- package/dist/github/machineAuth.js.map +1 -0
- package/dist/github/machineAuth.test.d.ts +1 -0
- package/dist/github/machineAuth.test.js +409 -0
- package/dist/github/machineAuth.test.js.map +1 -0
- package/dist/github/projectDelivery.d.ts +2 -0
- package/dist/github/projectDelivery.js +6 -0
- package/dist/github/projectDelivery.js.map +1 -1
- package/dist/github/relationships.d.ts +9 -2
- package/dist/github/relationships.js +6 -2
- package/dist/github/relationships.js.map +1 -1
- package/dist/investigationCompletion.test.d.ts +1 -0
- package/dist/investigationCompletion.test.js +728 -0
- package/dist/investigationCompletion.test.js.map +1 -0
- package/dist/issue.d.ts +29 -6
- package/dist/issue.js +153 -22
- package/dist/issue.js.map +1 -1
- package/dist/issue63History.test.d.ts +1 -0
- package/dist/issue63History.test.js +45 -0
- package/dist/issue63History.test.js.map +1 -0
- package/dist/issue63Routes.test.d.ts +1 -0
- package/dist/issue63Routes.test.js +153 -0
- package/dist/issue63Routes.test.js.map +1 -0
- package/dist/issueBlockerGuard.test.d.ts +1 -0
- package/dist/issueBlockerGuard.test.js +162 -0
- package/dist/issueBlockerGuard.test.js.map +1 -0
- package/dist/issueContract.d.ts +230 -0
- package/dist/issueContract.js +341 -0
- package/dist/issueContract.js.map +1 -0
- package/dist/issueContract.test.d.ts +27 -0
- package/dist/issueContract.test.js +314 -0
- package/dist/issueContract.test.js.map +1 -0
- package/dist/issueIntake.test.d.ts +1 -0
- package/dist/issueIntake.test.js +649 -0
- package/dist/issueIntake.test.js.map +1 -0
- package/dist/issueJournal.d.ts +1 -1
- package/dist/issueJournal.test.js +88 -6
- package/dist/issueJournal.test.js.map +1 -1
- package/dist/issueTracking.test.js +16 -8
- package/dist/issueTracking.test.js.map +1 -1
- package/dist/mcp/tools.d.ts +142 -12
- package/dist/mcp/tools.js +104 -34
- package/dist/mcp/tools.js.map +1 -1
- package/dist/pluginAuthProbe.d.ts +6 -0
- package/dist/pluginAuthProbe.js +53 -0
- package/dist/pluginAuthProbe.js.map +1 -0
- package/dist/pluginAuthProbe.test.d.ts +1 -0
- package/dist/pluginAuthProbe.test.js +66 -0
- package/dist/pluginAuthProbe.test.js.map +1 -0
- package/dist/pluginCredentials.test.d.ts +1 -0
- package/dist/pluginCredentials.test.js +277 -0
- package/dist/pluginCredentials.test.js.map +1 -0
- package/dist/pluginDoctor.test.d.ts +1 -0
- package/dist/pluginDoctor.test.js +627 -0
- package/dist/pluginDoctor.test.js.map +1 -0
- package/dist/pluginInstaller.d.ts +36 -0
- package/dist/pluginInstaller.js +238 -11
- package/dist/pluginInstaller.js.map +1 -1
- package/dist/pluginInstaller.test.js +242 -2
- package/dist/pluginInstaller.test.js.map +1 -1
- package/dist/pluginPackaging.test.js +123 -9
- package/dist/pluginPackaging.test.js.map +1 -1
- package/dist/pr.d.ts +90 -13
- package/dist/pr.js +875 -66
- package/dist/pr.js.map +1 -1
- package/dist/pr.test.js +166 -7
- package/dist/pr.test.js.map +1 -1
- package/dist/prAssociation.test.d.ts +1 -0
- package/dist/prAssociation.test.js +1336 -0
- package/dist/prAssociation.test.js.map +1 -0
- package/dist/prDispatch.test.d.ts +1 -0
- package/dist/prDispatch.test.js +96 -0
- package/dist/prDispatch.test.js.map +1 -0
- package/dist/releaseReconciliation.test.js +6 -2
- package/dist/releaseReconciliation.test.js.map +1 -1
- package/dist/releaseWorkflow.test.d.ts +1 -0
- package/dist/releaseWorkflow.test.js +447 -0
- package/dist/releaseWorkflow.test.js.map +1 -0
- package/dist/services/agentReadinessService.d.ts +9 -8
- package/dist/services/agentReadinessService.js +32 -223
- package/dist/services/agentReadinessService.js.map +1 -1
- package/dist/services/blockerGuardService.d.ts +17 -0
- package/dist/services/blockerGuardService.js +194 -0
- package/dist/services/blockerGuardService.js.map +1 -0
- package/dist/services/deliveryAdmission.js +22 -19
- package/dist/services/deliveryAdmission.js.map +1 -1
- package/dist/services/deliveryHealthService.d.ts +84 -0
- package/dist/services/deliveryHealthService.js +206 -0
- package/dist/services/deliveryHealthService.js.map +1 -0
- package/dist/services/worktreeRegistry.d.ts +84 -0
- package/dist/services/worktreeRegistry.js +265 -1
- package/dist/services/worktreeRegistry.js.map +1 -1
- package/dist/setup.test.js +97 -3
- package/dist/setup.test.js.map +1 -1
- package/dist/stagedChecks.test.d.ts +1 -0
- package/dist/stagedChecks.test.js +228 -0
- package/dist/stagedChecks.test.js.map +1 -0
- package/dist/start.test.js +30 -3
- package/dist/start.test.js.map +1 -1
- package/dist/stdoutRegression.test.js +36 -6
- package/dist/stdoutRegression.test.js.map +1 -1
- package/dist/targetPolicyEntrypoints.test.d.ts +1 -0
- package/dist/targetPolicyEntrypoints.test.js +275 -0
- package/dist/targetPolicyEntrypoints.test.js.map +1 -0
- package/dist/targetPolicyLocal.test.d.ts +1 -0
- package/dist/targetPolicyLocal.test.js +707 -0
- package/dist/targetPolicyLocal.test.js.map +1 -0
- package/dist/targetPolicyRoutes.test.d.ts +1 -0
- package/dist/targetPolicyRoutes.test.js +298 -0
- package/dist/targetPolicyRoutes.test.js.map +1 -0
- package/dist/verification.js +48 -12
- package/dist/verification.js.map +1 -1
- package/dist/verificationGate.test.d.ts +1 -0
- package/dist/verificationGate.test.js +106 -0
- package/dist/verificationGate.test.js.map +1 -0
- package/dist/worktree.d.ts +6 -1
- package/dist/worktree.js +90 -3
- package/dist/worktree.js.map +1 -1
- package/dist/worktreeTransition.test.js +1 -1
- package/dist/worktreeTransition.test.js.map +1 -1
- package/package.json +9 -6
- package/plugins/ai-delivery/.claude-plugin/plugin.json +2 -2
- package/plugins/ai-delivery/README.md +160 -1
- package/plugins/ai-delivery/plugin.json +1 -1
- package/plugins/ai-delivery/runtime/dist/THIRD-PARTY-NOTICES.md +1 -1
- package/plugins/ai-delivery/runtime/dist/cli.js +83 -49
- package/plugins/ai-delivery/runtime/package.json +1 -1
- package/plugins/ai-delivery/skills/intake-create/SKILL.md +45 -30
- package/plugins/ai-delivery/skills/pr-handoff/SKILL.md +20 -0
package/dist/pr.js
CHANGED
|
@@ -1,15 +1,329 @@
|
|
|
1
1
|
import { DeliveryError } from './errors.js';
|
|
2
2
|
import { createDeliveryGitHubClients } from './github/client.js';
|
|
3
3
|
import { getNativeBlockerRelationships } from './github/relationships.js';
|
|
4
|
-
import { issueBranch, startIssueBranch } from './issue.js';
|
|
4
|
+
import { commentIssue, issueBranch, linkedIssueBranches, readIssueClosure, startIssueBranch, } from './issue.js';
|
|
5
|
+
import { existsSync, lstatSync, realpathSync } from 'node:fs';
|
|
6
|
+
import { isAbsolute, join, relative, resolve, sep } from 'node:path';
|
|
7
|
+
import { assertPrivateFile, digestBytes, digestValue, stableJson } from './delivery/common.js';
|
|
8
|
+
import { gitCommonDir, primaryGitRoot } from './git.js';
|
|
9
|
+
import { ensurePrivateDirectoryDurably } from './utils/atomicJson.js';
|
|
10
|
+
import { resolveRepoFromRemote } from './github/repo.js';
|
|
11
|
+
import { authenticatedWorktreeActors, assertNativeWorktreeAcceptance } from './worktreeTransition.js';
|
|
12
|
+
import { withWorktreeTransitionWriterAbsent } from './verification.js';
|
|
13
|
+
import { assertInvestigationSource, investigationSourceIdentity, removeInvestigationSource } from './worktree.js';
|
|
14
|
+
import { InvestigationPreparationSchema, completeInvestigationCustody, readInvestigationAcknowledgement, readInvestigationPreparation, registerInvestigationCustody, } from './services/worktreeRegistry.js';
|
|
5
15
|
import { parseGitHubReviewArtifact, parseReviewArtifact, readGitHubPrScopeHash, readRequiredReviewState, submitReview, } from './review.js';
|
|
6
16
|
const Sha = /^[a-f0-9]{40}$/u;
|
|
7
17
|
const Branch = /^(?!.*(?:\.\.|@\{|\/\/))[A-Za-z0-9._/-]+$/u;
|
|
18
|
+
// Conservative refusals also cover Markdown decoration/escaped references.
|
|
19
|
+
// This is a closure safety check, never authority for an issue association.
|
|
20
|
+
function refuseClosingDirective(text) {
|
|
21
|
+
if (typeof text !== 'string')
|
|
22
|
+
throw new DeliveryError('Incomplete non-closing message readback.');
|
|
23
|
+
const normalized = text.replace(/[*_~`\\<>[\]]/gu, '');
|
|
24
|
+
if (/\b(?:close[sd]?|fix(?:es|ed)?|resolve[sd]?)\b(?:\s*:\s*|\s+)[^\n]*?(?:#[0-9]+|[A-Za-z0-9.-]+\/[A-Za-z0-9.-]+#[0-9]+|https?:\/\/(?:www\.)?github\.com\/[A-Za-z0-9.-]+\/[A-Za-z0-9.-]+\/(?:issues|pull)\/[0-9]+)/iu.test(normalized))
|
|
25
|
+
throw new DeliveryError('Non-closing delivery refuses closing directives in body, title or commit messages.');
|
|
26
|
+
}
|
|
27
|
+
function referenceLine(context, issueNumber) {
|
|
28
|
+
return `References ${context.config.repository}#${issueNumber}`;
|
|
29
|
+
}
|
|
30
|
+
function nonClosingBody(context, issueNumber, requested) {
|
|
31
|
+
refuseClosingDirective(requested);
|
|
32
|
+
const line = referenceLine(context, issueNumber);
|
|
33
|
+
const body = requested.startsWith(`${line}\n`) || requested === line ? requested : `${line}\n\n${requested}`;
|
|
34
|
+
verifyReferenceBody(context, issueNumber, body);
|
|
35
|
+
return body;
|
|
36
|
+
}
|
|
37
|
+
function verifyReferenceBody(context, issueNumber, body) {
|
|
38
|
+
refuseClosingDirective(body);
|
|
39
|
+
const lines = body.split(/\r?\n/u);
|
|
40
|
+
if (lines[0] !== referenceLine(context, issueNumber) ||
|
|
41
|
+
lines.filter((line) => /^References\s/iu.test(line)).length !== 1)
|
|
42
|
+
throw new DeliveryError('Non-closing PR body must retain one leading canonical References line.');
|
|
43
|
+
}
|
|
44
|
+
function pageCursor(connection, seen) {
|
|
45
|
+
const info = connection.pageInfo;
|
|
46
|
+
if (!info ||
|
|
47
|
+
typeof info.hasNextPage !== 'boolean' ||
|
|
48
|
+
!(info.endCursor === null || typeof info.endCursor === 'string'))
|
|
49
|
+
throw new DeliveryError('Incomplete non-closing native pagination.');
|
|
50
|
+
if (!info.hasNextPage)
|
|
51
|
+
return null;
|
|
52
|
+
if (!info.endCursor || seen.has(info.endCursor))
|
|
53
|
+
throw new DeliveryError('Non-closing native pagination did not advance.');
|
|
54
|
+
seen.add(info.endCursor);
|
|
55
|
+
return info.endCursor;
|
|
56
|
+
}
|
|
57
|
+
async function openReferenceIssue(context, issueNumber) {
|
|
58
|
+
const issue = (await context.clients.rest.issues.get({ ...context.repo, issue_number: issueNumber })).data;
|
|
59
|
+
if (issue.number !== issueNumber ||
|
|
60
|
+
'pull_request' in issue ||
|
|
61
|
+
issue.state !== 'open' ||
|
|
62
|
+
typeof issue.node_id !== 'string' ||
|
|
63
|
+
!issue.node_id ||
|
|
64
|
+
typeof issue.title !== 'string' ||
|
|
65
|
+
typeof issue.body !== 'string' ||
|
|
66
|
+
issue.html_url?.toLowerCase() !==
|
|
67
|
+
`https://github.com/${context.config.repository}/issues/${issueNumber}`.toLowerCase())
|
|
68
|
+
throw new DeliveryError('Non-closing delivery requires the exact canonical OPEN issue and complete title/body.');
|
|
69
|
+
return issue;
|
|
70
|
+
}
|
|
71
|
+
function sameReferencePr(before, after, expectedMerge = false) {
|
|
72
|
+
if (expectedMerge
|
|
73
|
+
? before.state !== 'open' ||
|
|
74
|
+
before.draft ||
|
|
75
|
+
before.merged ||
|
|
76
|
+
after.state !== 'closed' ||
|
|
77
|
+
after.draft ||
|
|
78
|
+
!after.merged
|
|
79
|
+
: before.state !== after.state || before.draft !== after.draft || before.merged !== after.merged)
|
|
80
|
+
throw new DeliveryError('Non-closing PR state/draft/merged readback drifted; reconcile read-only.');
|
|
81
|
+
if (before.node_id !== after.node_id ||
|
|
82
|
+
before.head.sha !== after.head.sha ||
|
|
83
|
+
before.head.ref !== after.head.ref ||
|
|
84
|
+
before.base.sha !== after.base.sha ||
|
|
85
|
+
before.base.ref !== after.base.ref ||
|
|
86
|
+
before.body !== after.body ||
|
|
87
|
+
before.title !== after.title ||
|
|
88
|
+
before.user.login.toLowerCase() !== after.user.login.toLowerCase())
|
|
89
|
+
throw new DeliveryError('Non-closing PR identity, reference, message or head/base drifted; reconcile read-only.');
|
|
90
|
+
}
|
|
91
|
+
function sameReferenceIssue(before, after) {
|
|
92
|
+
if (before.node_id !== after.node_id || before.title !== after.title || before.body !== after.body)
|
|
93
|
+
throw new DeliveryError('Non-closing issue identity/title/criteria drifted; reconcile read-only.');
|
|
94
|
+
}
|
|
95
|
+
function nonClosingMode(value) {
|
|
96
|
+
if (value !== undefined && typeof value !== 'boolean')
|
|
97
|
+
throw new DeliveryError('nonClosing must be a boolean.');
|
|
98
|
+
return value === true;
|
|
99
|
+
}
|
|
100
|
+
async function nonClosingRoute(context, base) {
|
|
101
|
+
const authorContext = context.clients.role === 'author'
|
|
102
|
+
? context
|
|
103
|
+
: {
|
|
104
|
+
...context,
|
|
105
|
+
clients: await createDeliveryGitHubClients({
|
|
106
|
+
config: context.config,
|
|
107
|
+
identity: context.config.roles.author.identity,
|
|
108
|
+
role: 'author',
|
|
109
|
+
}),
|
|
110
|
+
reviewerClients: context.clients,
|
|
111
|
+
};
|
|
112
|
+
const route = await preflightReviewRoute(authorContext, base);
|
|
113
|
+
if (route.rules.visibility !== 'complete' || route.approvalEligibility === 'insufficient-permission')
|
|
114
|
+
throw new DeliveryError('Non-closing delivery requires complete native rule visibility and an independent reviewer route.');
|
|
115
|
+
return route;
|
|
116
|
+
}
|
|
117
|
+
async function verifyNonClosingCommits(context, pr) {
|
|
118
|
+
let cursor = null;
|
|
119
|
+
const cursors = new Set();
|
|
120
|
+
const commits = new Map();
|
|
121
|
+
let total;
|
|
122
|
+
for (let page = 0; page < 20; page++) {
|
|
123
|
+
const data = await context.clients.graphql('query DeliveryNonClosingCommits($owner:String!,$repo:String!,$number:Int!,$cursor:String){repository(owner:$owner,name:$repo){nameWithOwner pullRequest(number:$number){id number headRefOid baseRefOid baseRefName commits(first:100,after:$cursor){totalCount nodes{commit{oid message parents(first:100){totalCount nodes{oid} pageInfo{hasNextPage endCursor}}}} pageInfo{hasNextPage endCursor}}}}}', { ...context.repo, number: pr.number, cursor });
|
|
124
|
+
const live = data.repository?.pullRequest;
|
|
125
|
+
const connection = live?.commits;
|
|
126
|
+
if (data.repository?.nameWithOwner?.toLowerCase() !== context.config.repository.toLowerCase() ||
|
|
127
|
+
live?.id !== pr.node_id ||
|
|
128
|
+
live.number !== pr.number ||
|
|
129
|
+
live.headRefOid !== pr.head.sha ||
|
|
130
|
+
live.baseRefOid !== pr.base.sha ||
|
|
131
|
+
live.baseRefName !== pr.base.ref ||
|
|
132
|
+
!connection ||
|
|
133
|
+
!Array.isArray(connection.nodes) ||
|
|
134
|
+
connection.nodes.length > 100 ||
|
|
135
|
+
!Number.isSafeInteger(connection.totalCount) ||
|
|
136
|
+
connection.totalCount < 1 ||
|
|
137
|
+
connection.totalCount > 2000 ||
|
|
138
|
+
(total !== undefined && total !== connection.totalCount))
|
|
139
|
+
throw new DeliveryError('Incomplete or moved introduced commit graph at the non-closing head/base.');
|
|
140
|
+
total = connection.totalCount;
|
|
141
|
+
for (const node of connection.nodes) {
|
|
142
|
+
const commit = node?.commit;
|
|
143
|
+
if (!commit ||
|
|
144
|
+
!validSha(commit.oid) ||
|
|
145
|
+
commits.has(commit.oid) ||
|
|
146
|
+
!commit.parents ||
|
|
147
|
+
!Array.isArray(commit.parents.nodes) ||
|
|
148
|
+
commit.parents.nodes.length > 100 ||
|
|
149
|
+
!Number.isSafeInteger(commit.parents.totalCount) ||
|
|
150
|
+
commit.parents.totalCount < 1 ||
|
|
151
|
+
commit.parents.totalCount !== commit.parents.nodes.length ||
|
|
152
|
+
commit.parents.pageInfo?.hasNextPage !== false ||
|
|
153
|
+
commit.parents.nodes.some((parent) => !validSha(parent?.oid)))
|
|
154
|
+
throw new DeliveryError('Incomplete, duplicate or truncated introduced commit identity/parents.');
|
|
155
|
+
refuseClosingDirective(commit.message);
|
|
156
|
+
commits.set(commit.oid, commit.message);
|
|
157
|
+
}
|
|
158
|
+
cursor = pageCursor(connection, cursors);
|
|
159
|
+
if (cursor === null) {
|
|
160
|
+
if (commits.size !== total || !commits.has(pr.head.sha))
|
|
161
|
+
throw new DeliveryError('Introduced commit count/head is incomplete.');
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
throw new DeliveryError('Non-closing commit graph exceeds the bounded readback window.');
|
|
166
|
+
}
|
|
167
|
+
async function verifyUnpublishedCommits(context, baseSha, headSha) {
|
|
168
|
+
const commits = new Set();
|
|
169
|
+
let total;
|
|
170
|
+
for (let page = 1; page <= 20; page++) {
|
|
171
|
+
const comparison = (await context.clients.rest.repos.compareCommitsWithBasehead({
|
|
172
|
+
...context.repo,
|
|
173
|
+
basehead: `${baseSha}...${headSha}`,
|
|
174
|
+
per_page: 100,
|
|
175
|
+
page,
|
|
176
|
+
})).data;
|
|
177
|
+
if (comparison.base_commit?.sha !== baseSha ||
|
|
178
|
+
!validSha(comparison.merge_base_commit?.sha) ||
|
|
179
|
+
!['ahead', 'diverged'].includes(comparison.status) ||
|
|
180
|
+
!Number.isSafeInteger(comparison.total_commits) ||
|
|
181
|
+
comparison.total_commits < 1 ||
|
|
182
|
+
comparison.total_commits > 2000 ||
|
|
183
|
+
(total !== undefined && total !== comparison.total_commits) ||
|
|
184
|
+
!Array.isArray(comparison.commits) ||
|
|
185
|
+
comparison.commits.length > 100)
|
|
186
|
+
throw new DeliveryError('Incomplete introduced commit range before non-closing publication.');
|
|
187
|
+
total = comparison.total_commits;
|
|
188
|
+
for (const commit of comparison.commits) {
|
|
189
|
+
if (!validSha(commit.sha) ||
|
|
190
|
+
commits.has(commit.sha) ||
|
|
191
|
+
!Array.isArray(commit.parents) ||
|
|
192
|
+
commit.parents.length < 1 ||
|
|
193
|
+
commit.parents.length > 100 ||
|
|
194
|
+
commit.parents.some((parent) => !validSha(parent.sha)))
|
|
195
|
+
throw new DeliveryError('Incomplete or duplicate introduced commit identity.');
|
|
196
|
+
refuseClosingDirective(commit.commit?.message);
|
|
197
|
+
commits.add(commit.sha);
|
|
198
|
+
}
|
|
199
|
+
if (commits.size === total && commits.has(headSha))
|
|
200
|
+
return;
|
|
201
|
+
if (comparison.commits.length < 100)
|
|
202
|
+
throw new DeliveryError('Truncated introduced commit range before publication.');
|
|
203
|
+
}
|
|
204
|
+
throw new DeliveryError('Introduced commit range exceeds the bounded readback window.');
|
|
205
|
+
}
|
|
206
|
+
async function verifyNonClosingReference(context, issueNumber, pr) {
|
|
207
|
+
if (typeof pr.node_id !== 'string' ||
|
|
208
|
+
!pr.node_id ||
|
|
209
|
+
pr.html_url?.toLowerCase() !== `https://github.com/${context.config.repository}/pull/${pr.number}`.toLowerCase())
|
|
210
|
+
throw new DeliveryError('Incomplete non-closing PR node identity.');
|
|
211
|
+
verifyReferenceBody(context, issueNumber, pr.body);
|
|
212
|
+
refuseClosingDirective(pr.title);
|
|
213
|
+
const issue = await openReferenceIssue(context, issueNumber);
|
|
214
|
+
const repository = (await context.clients.rest.repos.get({ ...context.repo })).data;
|
|
215
|
+
if (repository.full_name?.toLowerCase() !== context.config.repository.toLowerCase() ||
|
|
216
|
+
pr.base.ref !== repository.default_branch ||
|
|
217
|
+
typeof repository.node_id !== 'string' ||
|
|
218
|
+
!repository.node_id)
|
|
219
|
+
throw new DeliveryError('Non-closing PR must target the canonical default repository/base.');
|
|
220
|
+
const route = await nonClosingRoute(context, pr.base.ref);
|
|
221
|
+
if (pr.user.login.toLowerCase() !== route.author.actorLogin.toLowerCase())
|
|
222
|
+
throw new DeliveryError('Non-closing PR author differs from the configured author.');
|
|
223
|
+
let cursor = null;
|
|
224
|
+
const cursors = new Set();
|
|
225
|
+
const events = new Map();
|
|
226
|
+
let matched = false;
|
|
227
|
+
for (let page = 0; page < 20; page++) {
|
|
228
|
+
const data = await context.clients.graphql('query DeliveryNonClosingReferences($owner:String!,$repo:String!,$number:Int!,$cursor:String){repository(owner:$owner,name:$repo){id nameWithOwner issue(number:$number){id number state timelineItems(first:100,after:$cursor,itemTypes:[CROSS_REFERENCED_EVENT]){nodes{... on CrossReferencedEvent{id actor{login} isCrossRepository willCloseTarget source{__typename ... on Issue{id number repository{id nameWithOwner}} ... on PullRequest{id number repository{id nameWithOwner}}} target{__typename ... on Issue{id number repository{id nameWithOwner}} ... on PullRequest{id number repository{id nameWithOwner}}}}} pageInfo{hasNextPage endCursor}}}}}', { ...context.repo, number: issueNumber, cursor });
|
|
229
|
+
const target = data.repository?.issue;
|
|
230
|
+
const connection = target?.timelineItems;
|
|
231
|
+
if (data.repository?.id !== repository.node_id ||
|
|
232
|
+
data.repository.nameWithOwner?.toLowerCase() !== context.config.repository.toLowerCase() ||
|
|
233
|
+
target?.id !== issue.node_id ||
|
|
234
|
+
target.number !== issueNumber ||
|
|
235
|
+
target.state !== 'OPEN' ||
|
|
236
|
+
!connection ||
|
|
237
|
+
!Array.isArray(connection.nodes) ||
|
|
238
|
+
connection.nodes.length > 100)
|
|
239
|
+
throw new DeliveryError('Incomplete or conflicting native non-closing issue identity.');
|
|
240
|
+
for (const event of connection.nodes) {
|
|
241
|
+
const source = event?.source;
|
|
242
|
+
const referenced = event?.target;
|
|
243
|
+
if (!event ||
|
|
244
|
+
!nativeId(event.id) ||
|
|
245
|
+
!source ||
|
|
246
|
+
!referenced ||
|
|
247
|
+
!['Issue', 'PullRequest'].includes(source.__typename) ||
|
|
248
|
+
!nativeId(source.id) ||
|
|
249
|
+
!Number.isSafeInteger(source.number) ||
|
|
250
|
+
source.number <= 0 ||
|
|
251
|
+
!nativeId(source.repository?.id) ||
|
|
252
|
+
typeof source.repository.nameWithOwner !== 'string' ||
|
|
253
|
+
referenced.__typename !== 'Issue' ||
|
|
254
|
+
referenced.id !== issue.node_id ||
|
|
255
|
+
referenced.number !== issueNumber ||
|
|
256
|
+
referenced.repository?.id !== repository.node_id ||
|
|
257
|
+
referenced.repository.nameWithOwner?.toLowerCase() !== context.config.repository.toLowerCase() ||
|
|
258
|
+
typeof event.isCrossRepository !== 'boolean' ||
|
|
259
|
+
typeof event.willCloseTarget !== 'boolean' ||
|
|
260
|
+
!nativeId(event.actor?.login))
|
|
261
|
+
throw new DeliveryError('Incomplete native cross-reference event identity/actor.');
|
|
262
|
+
const signature = JSON.stringify([
|
|
263
|
+
source.__typename,
|
|
264
|
+
source.id,
|
|
265
|
+
source.number,
|
|
266
|
+
source.repository.id,
|
|
267
|
+
source.repository.nameWithOwner.toLowerCase(),
|
|
268
|
+
referenced.id,
|
|
269
|
+
referenced.number,
|
|
270
|
+
referenced.repository.id,
|
|
271
|
+
referenced.repository.nameWithOwner.toLowerCase(),
|
|
272
|
+
event.actor.login.toLowerCase(),
|
|
273
|
+
event.isCrossRepository,
|
|
274
|
+
event.willCloseTarget,
|
|
275
|
+
]);
|
|
276
|
+
if (events.has(event.id) && events.get(event.id) !== signature)
|
|
277
|
+
throw new DeliveryError('Conflicting duplicate native reference event.');
|
|
278
|
+
events.set(event.id, signature);
|
|
279
|
+
if (source.id === pr.node_id ||
|
|
280
|
+
(source.__typename === 'PullRequest' &&
|
|
281
|
+
source.number === pr.number &&
|
|
282
|
+
source.repository.nameWithOwner.toLowerCase() === context.config.repository.toLowerCase())) {
|
|
283
|
+
if (source.__typename !== 'PullRequest' ||
|
|
284
|
+
source.id !== pr.node_id ||
|
|
285
|
+
source.number !== pr.number ||
|
|
286
|
+
source.repository.id !== repository.node_id ||
|
|
287
|
+
source.repository.nameWithOwner.toLowerCase() !== context.config.repository.toLowerCase() ||
|
|
288
|
+
event.isCrossRepository ||
|
|
289
|
+
event.willCloseTarget !== false ||
|
|
290
|
+
event.actor.login.toLowerCase() !== route.author.actorLogin.toLowerCase())
|
|
291
|
+
throw new DeliveryError('Conflicting, closing or unauthorized native PR reference.');
|
|
292
|
+
matched = true;
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
cursor = pageCursor(connection, cursors);
|
|
296
|
+
if (cursor === null)
|
|
297
|
+
break;
|
|
298
|
+
if (page === 19)
|
|
299
|
+
throw new DeliveryError('Native non-closing references exceed the bounded readback window.');
|
|
300
|
+
}
|
|
301
|
+
if (!matched)
|
|
302
|
+
throw new DeliveryError('Missing authorized native non-closing PR-to-issue reference.');
|
|
303
|
+
await verifyPrIssueAssociation(context, pr.number, issueNumber, true, pr.node_id);
|
|
304
|
+
await verifyNonClosingCommits(context, pr);
|
|
305
|
+
if (!pr.merged) {
|
|
306
|
+
const ref = (await context.clients.rest.git.getRef({ ...context.repo, ref: `heads/${pr.base.ref}` })).data;
|
|
307
|
+
if (ref.ref !== `refs/heads/${pr.base.ref}` || ref.object.type !== 'commit' || ref.object.sha !== pr.base.sha)
|
|
308
|
+
throw new DeliveryError('Non-closing default base has moved or is incomplete.');
|
|
309
|
+
}
|
|
310
|
+
const after = await openReferenceIssue(context, issueNumber);
|
|
311
|
+
sameReferenceIssue(issue, after);
|
|
312
|
+
sameReferencePr(pr, await getPr(context, pr.number));
|
|
313
|
+
return issue;
|
|
314
|
+
}
|
|
315
|
+
function nativeId(value) {
|
|
316
|
+
return typeof value === 'string' && value.trim().length > 0;
|
|
317
|
+
}
|
|
8
318
|
function validSha(value) {
|
|
9
319
|
return typeof value === 'string' && Sha.test(value);
|
|
10
320
|
}
|
|
11
321
|
function validBranch(value) {
|
|
12
|
-
if (
|
|
322
|
+
if (typeof value !== 'string' ||
|
|
323
|
+
!Branch.test(value) ||
|
|
324
|
+
value.startsWith('/') ||
|
|
325
|
+
value.endsWith('/') ||
|
|
326
|
+
value.endsWith('.lock'))
|
|
13
327
|
throw new DeliveryError('Invalid GitHub branch name.');
|
|
14
328
|
}
|
|
15
329
|
async function getPr(context, prNumber) {
|
|
@@ -17,40 +331,73 @@ async function getPr(context, prNumber) {
|
|
|
17
331
|
throw new DeliveryError('Configured and selected GitHub repository identities disagree.');
|
|
18
332
|
const pr = (await context.clients.rest.pulls.get({ ...context.repo, pull_number: prNumber })).data;
|
|
19
333
|
if (pr.number !== prNumber ||
|
|
334
|
+
typeof pr.base.repo?.full_name !== 'string' ||
|
|
335
|
+
typeof pr.head.repo?.full_name !== 'string' ||
|
|
20
336
|
pr.base.repo?.full_name.toLowerCase() !== context.config.repository.toLowerCase() ||
|
|
21
337
|
pr.head.repo?.full_name.toLowerCase() !== context.config.repository.toLowerCase() ||
|
|
22
|
-
!validSha(pr.head.sha)
|
|
338
|
+
!validSha(pr.head.sha) ||
|
|
339
|
+
!validSha(pr.base.sha) ||
|
|
340
|
+
(pr.state !== 'open' && pr.state !== 'closed') ||
|
|
341
|
+
typeof pr.merged !== 'boolean' ||
|
|
342
|
+
typeof pr.draft !== 'boolean' ||
|
|
343
|
+
(pr.merged && pr.state !== 'closed'))
|
|
23
344
|
throw new DeliveryError('GitHub PR identity or exact head readback is invalid.');
|
|
345
|
+
validBranch(pr.head.ref);
|
|
346
|
+
validBranch(pr.base.ref);
|
|
24
347
|
return pr;
|
|
25
348
|
}
|
|
26
|
-
async function
|
|
349
|
+
async function verifyPrIssueAssociation(context, prNumber, issueNumber, nonClosing = false, expectedPrId, onlyIntended = false, expectedIssueId, expectedRepositoryId) {
|
|
27
350
|
let cursor = null;
|
|
28
351
|
const cursors = new Set();
|
|
29
352
|
let matches = 0;
|
|
30
353
|
for (let page = 0; page < 20; page++) {
|
|
31
|
-
const result = await context.clients.graphql('query DeliveryClosingIssues($owner:String!,$repo:String!,$number:Int!,$cursor:String){repository(owner:$owner,name:$repo){pullRequest(number:$number){closingIssuesReferences(first:100,after:$cursor){nodes{number repository{nameWithOwner}} pageInfo{hasNextPage endCursor}}}}}', { ...context.repo, number: prNumber, cursor });
|
|
354
|
+
const result = await context.clients.graphql('query DeliveryClosingIssues($owner:String!,$repo:String!,$number:Int!,$cursor:String){repository(owner:$owner,name:$repo){nameWithOwner pullRequest(number:$number){id number closingIssuesReferences(first:100,after:$cursor){nodes{__typename id number repository{id nameWithOwner}} pageInfo{hasNextPage endCursor}}}}}', { ...context.repo, number: prNumber, cursor });
|
|
32
355
|
const connection = result.repository?.pullRequest?.closingIssuesReferences;
|
|
33
|
-
if (
|
|
34
|
-
|
|
356
|
+
if (typeof result.repository?.nameWithOwner !== 'string' ||
|
|
357
|
+
result.repository.nameWithOwner.toLowerCase() !== context.config.repository.toLowerCase() ||
|
|
358
|
+
result.repository?.pullRequest?.number !== prNumber ||
|
|
359
|
+
(expectedPrId !== undefined && result.repository?.pullRequest?.id !== expectedPrId) ||
|
|
360
|
+
!connection ||
|
|
361
|
+
!Array.isArray(connection.nodes) ||
|
|
362
|
+
connection.nodes.length > 100 ||
|
|
363
|
+
typeof connection.pageInfo?.hasNextPage !== 'boolean' ||
|
|
364
|
+
!(connection.pageInfo.endCursor === null || typeof connection.pageInfo.endCursor === 'string'))
|
|
365
|
+
throw new DeliveryError('Incomplete or conflicting native GitHub PR issue association readback.');
|
|
35
366
|
for (const issue of connection.nodes) {
|
|
36
|
-
if (
|
|
37
|
-
|
|
367
|
+
if (onlyIntended &&
|
|
368
|
+
(!issue ||
|
|
369
|
+
issue.__typename !== 'Issue' ||
|
|
370
|
+
!nativeId(expectedIssueId) ||
|
|
371
|
+
issue.id !== expectedIssueId ||
|
|
372
|
+
!nativeId(expectedRepositoryId) ||
|
|
373
|
+
issue.repository?.id !== expectedRepositoryId))
|
|
374
|
+
throw new DeliveryError('Incomplete or conflicting typed closing issue/repository identity before body transition.');
|
|
375
|
+
if (nonClosing)
|
|
376
|
+
throw new DeliveryError('Non-closing PR has an unintended native closing association.');
|
|
377
|
+
if (!issue ||
|
|
378
|
+
!Number.isSafeInteger(issue.number) ||
|
|
379
|
+
issue.number <= 0 ||
|
|
380
|
+
typeof issue.repository?.nameWithOwner !== 'string' ||
|
|
381
|
+
!/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/u.test(issue.repository.nameWithOwner))
|
|
382
|
+
throw new DeliveryError('Incomplete native PR closing-issue identity.');
|
|
38
383
|
if (issue.number === issueNumber &&
|
|
39
384
|
issue.repository.nameWithOwner.toLowerCase() === context.config.repository.toLowerCase())
|
|
40
385
|
matches++;
|
|
386
|
+
else if (onlyIntended)
|
|
387
|
+
throw new DeliveryError('Existing PR has conflicting closing associations; body transition refused.');
|
|
41
388
|
}
|
|
42
389
|
if (!connection.pageInfo.hasNextPage) {
|
|
43
|
-
if (matches !== 1)
|
|
44
|
-
throw new DeliveryError('
|
|
390
|
+
if (!nonClosing && matches !== 1)
|
|
391
|
+
throw new DeliveryError('Native GitHub PR closing-issue association does not identify the intended issue.');
|
|
45
392
|
return;
|
|
46
393
|
}
|
|
47
394
|
const next = connection.pageInfo.endCursor;
|
|
48
395
|
if (!next || cursors.has(next))
|
|
49
|
-
throw new DeliveryError('
|
|
396
|
+
throw new DeliveryError('Native PR issue pagination did not advance.');
|
|
50
397
|
cursors.add(next);
|
|
51
398
|
cursor = next;
|
|
52
399
|
}
|
|
53
|
-
throw new DeliveryError('
|
|
400
|
+
throw new DeliveryError('Native PR issue association exceeds the bounded readback window.');
|
|
54
401
|
}
|
|
55
402
|
async function matchingPrs(context, branch, base) {
|
|
56
403
|
const matches = [];
|
|
@@ -63,34 +410,55 @@ async function matchingPrs(context, branch, base) {
|
|
|
63
410
|
page,
|
|
64
411
|
per_page: 100,
|
|
65
412
|
})).data;
|
|
413
|
+
if (!Array.isArray(batch) || batch.some((pr) => !pr || !Number.isSafeInteger(pr.number) || pr.number <= 0))
|
|
414
|
+
throw new DeliveryError('Incomplete GitHub PR match readback.');
|
|
66
415
|
matches.push(...batch);
|
|
67
416
|
if (batch.length < 100)
|
|
68
417
|
return matches;
|
|
69
418
|
}
|
|
70
419
|
throw new DeliveryError('GitHub PR match history exceeds the bounded readback window.');
|
|
71
420
|
}
|
|
72
|
-
async function verifyIssuePr(context, issueNumber, prNumber) {
|
|
421
|
+
async function verifyIssuePr(context, issueNumber, prNumber, nonClosing = false) {
|
|
73
422
|
const pr = await getPr(context, prNumber);
|
|
74
|
-
|
|
423
|
+
if (nonClosing)
|
|
424
|
+
await verifyNonClosingReference(context, issueNumber, pr);
|
|
425
|
+
else
|
|
426
|
+
await verifyPrIssueAssociation(context, prNumber, issueNumber);
|
|
75
427
|
if (!pr.merged) {
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
428
|
+
// GitHub replaces the issue's branch connection with its PR on publication.
|
|
429
|
+
// Existing connections remain consistency evidence, never a fallback authority.
|
|
430
|
+
const branches = await linkedIssueBranches(context, issueNumber);
|
|
431
|
+
if (branches.length > 0) {
|
|
432
|
+
const matches = branches.filter((branch) => branch.name === pr.head.ref);
|
|
433
|
+
if (matches.length !== 1 || matches[0].sha !== pr.head.sha)
|
|
434
|
+
throw new DeliveryError('Native linked issue branches conflict with the exact PR head/ref.');
|
|
435
|
+
}
|
|
436
|
+
const ref = (await context.clients.rest.git.getRef({ ...context.repo, ref: `heads/${pr.head.ref}` })).data;
|
|
437
|
+
if (ref.ref !== `refs/heads/${pr.head.ref}` || ref.object.type !== 'commit' || ref.object.sha !== pr.head.sha)
|
|
438
|
+
throw new DeliveryError('Current remote PR head/ref readback is incomplete or has moved.');
|
|
79
439
|
}
|
|
80
440
|
return pr;
|
|
81
441
|
}
|
|
82
442
|
export async function prInfo(context, input) {
|
|
443
|
+
const nonClosing = nonClosingMode(input.nonClosing);
|
|
444
|
+
if (nonClosing && (input.issueNumber === undefined || input.prNumber === undefined))
|
|
445
|
+
throw new DeliveryError('Non-closing inspection requires explicit canonical issue and PR numbers.');
|
|
83
446
|
let number = input.prNumber;
|
|
84
447
|
if (number === undefined) {
|
|
85
448
|
if (input.issueNumber === undefined)
|
|
86
449
|
throw new DeliveryError('prNumber or issueNumber is required.');
|
|
87
|
-
const
|
|
450
|
+
const branches = await linkedIssueBranches(context, input.issueNumber);
|
|
451
|
+
if (branches.length !== 1)
|
|
452
|
+
throw new DeliveryError('Issue-only PR inspection requires one GitHub-linked issue branch. After publication, supply the exact issue and PR numbers (CLI --issue and --pr) to verify the native PR closing-issue association.');
|
|
453
|
+
const branch = branches[0].name;
|
|
88
454
|
const matches = await matchingPrs(context, branch);
|
|
89
455
|
if (matches.length !== 1)
|
|
90
456
|
throw new DeliveryError('Select an explicit PR number when the remote issue branch has zero or multiple PRs.');
|
|
91
457
|
number = matches[0].number;
|
|
92
458
|
}
|
|
93
|
-
const pr =
|
|
459
|
+
const pr = input.issueNumber === undefined
|
|
460
|
+
? await getPr(context, number)
|
|
461
|
+
: await verifyIssuePr(context, input.issueNumber, number, nonClosing);
|
|
94
462
|
return {
|
|
95
463
|
prNumber: pr.number,
|
|
96
464
|
url: pr.html_url,
|
|
@@ -272,81 +640,262 @@ export async function preflightReviewRoute(context, base, selectedReviewer, purp
|
|
|
272
640
|
};
|
|
273
641
|
}
|
|
274
642
|
export async function publishPr(context, input) {
|
|
275
|
-
const
|
|
276
|
-
if (input.
|
|
277
|
-
throw new DeliveryError('
|
|
278
|
-
|
|
279
|
-
|
|
643
|
+
const nonClosing = nonClosingMode(input.nonClosing);
|
|
644
|
+
if (input.prNumber !== undefined && (!Number.isSafeInteger(input.prNumber) || input.prNumber <= 0))
|
|
645
|
+
throw new DeliveryError('Existing PR selector must be a positive safe integer.');
|
|
646
|
+
const issue = nonClosing
|
|
647
|
+
? await openReferenceIssue(context, input.issueNumber)
|
|
648
|
+
: (await context.clients.rest.issues.get({ ...context.repo, issue_number: input.issueNumber })).data;
|
|
280
649
|
if ('pull_request' in issue || issue.number !== input.issueNumber || issue.state !== 'open')
|
|
281
650
|
throw new DeliveryError('PR creation requires the requested open GitHub issue.');
|
|
282
|
-
const
|
|
283
|
-
|
|
284
|
-
|
|
651
|
+
const repository = (await context.clients.rest.repos.get({ ...context.repo })).data;
|
|
652
|
+
if (repository.full_name?.toLowerCase() !== context.config.repository.toLowerCase() ||
|
|
653
|
+
context.config.repository.toLowerCase() !== `${context.repo.owner}/${context.repo.repo}`.toLowerCase())
|
|
654
|
+
throw new DeliveryError('Configured and selected GitHub repository identities disagree.');
|
|
655
|
+
if (nonClosing && !nativeId(repository.node_id))
|
|
656
|
+
throw new DeliveryError('Incomplete canonical non-closing repository node identity.');
|
|
657
|
+
const base = input.baseBranch ?? repository.default_branch;
|
|
658
|
+
if (base !== repository.default_branch)
|
|
285
659
|
throw new DeliveryError('PR delivery requires the repository default branch for durable issue association.');
|
|
286
660
|
validBranch(base);
|
|
661
|
+
if (input.headBranch !== undefined)
|
|
662
|
+
validBranch(input.headBranch);
|
|
663
|
+
const explicit = input.prNumber === undefined ? undefined : await getPr(context, input.prNumber);
|
|
664
|
+
const explicitPromotion = input.draft === false && input.headBranch !== undefined;
|
|
665
|
+
const selectedMatches = explicit
|
|
666
|
+
? await matchingPrs(context, explicit.head.ref, base)
|
|
667
|
+
: explicitPromotion
|
|
668
|
+
? await matchingPrs(context, input.headBranch, base)
|
|
669
|
+
: undefined;
|
|
670
|
+
const branch = explicit
|
|
671
|
+
? explicit.head.ref
|
|
672
|
+
: explicitPromotion && selectedMatches.length > 0
|
|
673
|
+
? input.headBranch
|
|
674
|
+
: await issueBranch(context, input.issueNumber, input.headBranch);
|
|
675
|
+
if (input.headBranch !== undefined && branch !== input.headBranch)
|
|
676
|
+
throw new DeliveryError('Explicit PR and head selectors disagree.');
|
|
677
|
+
validBranch(branch);
|
|
287
678
|
const ref = (await context.clients.rest.git.getRef({ ...context.repo, ref: `heads/${branch}` })).data;
|
|
288
|
-
if (!validSha(ref.object.sha))
|
|
289
|
-
throw new DeliveryError('Remote issue branch head is invalid.');
|
|
290
|
-
const
|
|
679
|
+
if (ref.ref !== `refs/heads/${branch}` || ref.object.type !== 'commit' || !validSha(ref.object.sha))
|
|
680
|
+
throw new DeliveryError('Remote issue branch head/ref readback is invalid.');
|
|
681
|
+
const matches = selectedMatches ?? (await matchingPrs(context, branch, base));
|
|
682
|
+
if (matches.length > 1 || (explicit && (matches.length !== 1 || matches[0].number !== explicit.number)))
|
|
683
|
+
throw new DeliveryError('Remote issue branch has conflicting PR matches.');
|
|
684
|
+
let pr = explicit ?? (matches[0] === undefined ? undefined : await getPr(context, matches[0].number));
|
|
685
|
+
const requestedBody = input.body ??
|
|
686
|
+
(nonClosing ? (pr?.body ?? referenceLine(context, input.issueNumber)) : `Closes #${input.issueNumber}`);
|
|
291
687
|
if (!requestedBody.trim())
|
|
292
688
|
throw new DeliveryError('PR body must contain text.');
|
|
293
|
-
const body =
|
|
294
|
-
? requestedBody
|
|
295
|
-
:
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
689
|
+
const body = nonClosing
|
|
690
|
+
? nonClosingBody(context, input.issueNumber, requestedBody)
|
|
691
|
+
: new RegExp(`(?:close[sd]?|fix(?:es|ed)?|resolve[sd]?)\\s+#${input.issueNumber}(?![0-9])`, 'iu').test(requestedBody)
|
|
692
|
+
? requestedBody
|
|
693
|
+
: `${requestedBody}\n\nCloses #${input.issueNumber}`;
|
|
694
|
+
const transition = nonClosing && pr !== undefined && pr.body !== body;
|
|
695
|
+
if (transition && (input.prNumber === undefined || input.body === undefined))
|
|
696
|
+
throw new DeliveryError('Non-closing body transition requires an explicit existing PR number and body.');
|
|
300
697
|
if (pr &&
|
|
301
698
|
(pr.head.sha !== ref.object.sha ||
|
|
302
699
|
pr.head.ref !== branch ||
|
|
303
700
|
pr.base.ref !== base ||
|
|
304
701
|
pr.state !== 'open' ||
|
|
305
|
-
|
|
702
|
+
pr.merged ||
|
|
703
|
+
(!transition && input.body !== undefined && pr.body !== body) ||
|
|
306
704
|
(input.title !== undefined && pr.title !== input.title)))
|
|
307
705
|
throw new DeliveryError('Existing remote PR conflicts with the requested branch, intent or state.');
|
|
308
|
-
const reviewRoute = await preflightReviewRoute(context, base);
|
|
309
|
-
if (pr && pr.user
|
|
706
|
+
const reviewRoute = nonClosing ? await nonClosingRoute(context, base) : await preflightReviewRoute(context, base);
|
|
707
|
+
if (pr && pr.user.login.toLowerCase() !== reviewRoute.author.actorLogin.toLowerCase())
|
|
310
708
|
throw new DeliveryError('Existing PR author differs from the configured authenticated author.');
|
|
709
|
+
if (pr && (typeof pr.node_id !== 'string' || !pr.node_id))
|
|
710
|
+
throw new DeliveryError('Existing PR publication identity is incomplete.');
|
|
711
|
+
if (nonClosing)
|
|
712
|
+
refuseClosingDirective(input.title ?? pr?.title ?? issue.title);
|
|
713
|
+
const creationTitle = input.title ?? issue.title;
|
|
714
|
+
let publicationBaseSha;
|
|
715
|
+
const existing = pr;
|
|
716
|
+
let pendingTransition = transition;
|
|
717
|
+
let expectedBody = pr?.body;
|
|
718
|
+
const transitionDraft = pr?.draft;
|
|
719
|
+
let promotionApplied = false;
|
|
720
|
+
const preserveIssue = async () => {
|
|
721
|
+
if (nonClosing)
|
|
722
|
+
sameReferenceIssue(issue, await openReferenceIssue(context, input.issueNumber));
|
|
723
|
+
};
|
|
724
|
+
const readExisting = async () => {
|
|
725
|
+
const current = await verifyIssuePr(context, input.issueNumber, existing.number, nonClosing && !pendingTransition);
|
|
726
|
+
if (current.state !== 'open' ||
|
|
727
|
+
current.merged ||
|
|
728
|
+
current.node_id !== existing.node_id ||
|
|
729
|
+
current.head.sha !== ref.object.sha ||
|
|
730
|
+
current.head.ref !== branch ||
|
|
731
|
+
current.base.ref !== base ||
|
|
732
|
+
current.base.sha !== existing.base.sha ||
|
|
733
|
+
current.user.login.toLowerCase() !== reviewRoute.author.actorLogin.toLowerCase() ||
|
|
734
|
+
(transition && !promotionApplied && current.draft !== transitionDraft) ||
|
|
735
|
+
current.body !== expectedBody ||
|
|
736
|
+
current.title !== existing.title)
|
|
737
|
+
throw new DeliveryError('Existing PR publication head, identity, intent or author readback drifted.');
|
|
738
|
+
if (pendingTransition) {
|
|
739
|
+
await verifyPrIssueAssociation(context, current.number, input.issueNumber, false, current.node_id, true, issue.node_id, repository.node_id);
|
|
740
|
+
refuseClosingDirective(current.title);
|
|
741
|
+
await verifyNonClosingCommits(context, current);
|
|
742
|
+
const baseRef = (await context.clients.rest.git.getRef({ ...context.repo, ref: `heads/${base}` })).data;
|
|
743
|
+
if (baseRef.ref !== `refs/heads/${base}` ||
|
|
744
|
+
baseRef.object.type !== 'commit' ||
|
|
745
|
+
baseRef.object.sha !== current.base.sha)
|
|
746
|
+
throw new DeliveryError('Non-closing transition default base drifted.');
|
|
747
|
+
sameReferencePr(current, await getPr(context, current.number));
|
|
748
|
+
}
|
|
749
|
+
await preserveIssue();
|
|
750
|
+
return current;
|
|
751
|
+
};
|
|
752
|
+
const readNew = async () => {
|
|
753
|
+
const baseRef = (await context.clients.rest.git.getRef({ ...context.repo, ref: `heads/${base}` })).data;
|
|
754
|
+
if (baseRef.ref !== `refs/heads/${base}` || baseRef.object.type !== 'commit' || !validSha(baseRef.object.sha))
|
|
755
|
+
throw new DeliveryError('Incomplete publication default base.');
|
|
756
|
+
if (publicationBaseSha !== undefined && publicationBaseSha !== baseRef.object.sha)
|
|
757
|
+
throw new DeliveryError('Publication default base drifted before creation.');
|
|
758
|
+
publicationBaseSha = baseRef.object.sha;
|
|
759
|
+
if (nonClosing)
|
|
760
|
+
await verifyUnpublishedCommits(context, baseRef.object.sha, ref.object.sha);
|
|
761
|
+
const afterHead = (await context.clients.rest.git.getRef({ ...context.repo, ref: `heads/${branch}` })).data;
|
|
762
|
+
const afterBase = (await context.clients.rest.git.getRef({ ...context.repo, ref: `heads/${base}` })).data;
|
|
763
|
+
if (afterHead.ref !== ref.ref ||
|
|
764
|
+
afterHead.object.type !== 'commit' ||
|
|
765
|
+
afterHead.object.sha !== ref.object.sha ||
|
|
766
|
+
afterBase.ref !== baseRef.ref ||
|
|
767
|
+
afterBase.object.type !== 'commit' ||
|
|
768
|
+
afterBase.object.sha !== baseRef.object.sha)
|
|
769
|
+
throw new DeliveryError('Non-closing publication head/base drifted.');
|
|
770
|
+
await preserveIssue();
|
|
771
|
+
};
|
|
311
772
|
if (pr)
|
|
312
|
-
await
|
|
773
|
+
pr = await readExisting();
|
|
774
|
+
else
|
|
775
|
+
await readNew();
|
|
313
776
|
if (input.dryRun)
|
|
314
|
-
return {
|
|
777
|
+
return {
|
|
778
|
+
dryRun: true,
|
|
779
|
+
...(pr === undefined ? {} : { prNumber: pr.number, draft: pr.draft }),
|
|
780
|
+
...(nonClosing
|
|
781
|
+
? {
|
|
782
|
+
nonClosing: true,
|
|
783
|
+
nonClosingVerified: pr !== undefined && !pendingTransition,
|
|
784
|
+
body,
|
|
785
|
+
bodyTransition: pendingTransition,
|
|
786
|
+
}
|
|
787
|
+
: {}),
|
|
788
|
+
headSha: ref.object.sha,
|
|
789
|
+
headBranch: branch,
|
|
790
|
+
baseBranch: base,
|
|
791
|
+
reviewRoute,
|
|
792
|
+
};
|
|
793
|
+
let created = false;
|
|
315
794
|
if (pr) {
|
|
795
|
+
if (pendingTransition) {
|
|
796
|
+
pr = await readExisting();
|
|
797
|
+
await context.clients.rest.pulls.update({ ...context.repo, pull_number: pr.number, body });
|
|
798
|
+
pendingTransition = false;
|
|
799
|
+
expectedBody = body;
|
|
800
|
+
pr = await readExisting();
|
|
801
|
+
}
|
|
316
802
|
if (input.draft === false && pr.draft) {
|
|
317
|
-
await
|
|
318
|
-
|
|
803
|
+
pr = await readExisting();
|
|
804
|
+
if (pr.draft) {
|
|
805
|
+
const promoted = await context.clients.graphql('mutation DeliveryReadyPr($id:ID!){markPullRequestReadyForReview(input:{pullRequestId:$id}){pullRequest{id isDraft}}}', { id: pr.node_id });
|
|
806
|
+
if (promoted?.markPullRequestReadyForReview?.pullRequest?.id !== pr.node_id ||
|
|
807
|
+
promoted.markPullRequestReadyForReview.pullRequest.isDraft !== false)
|
|
808
|
+
throw new DeliveryError('PR promotion mutation readback is incomplete or conflicting; reconcile this PR.');
|
|
809
|
+
promotionApplied = true;
|
|
810
|
+
}
|
|
811
|
+
pr = await readExisting();
|
|
319
812
|
if (pr.draft)
|
|
320
813
|
throw new DeliveryError('PR promotion readback is still draft.');
|
|
321
814
|
}
|
|
322
815
|
}
|
|
323
816
|
else {
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
817
|
+
await readNew();
|
|
818
|
+
let response;
|
|
819
|
+
try {
|
|
820
|
+
response = await context.clients.rest.pulls.create({
|
|
821
|
+
...context.repo,
|
|
822
|
+
head: branch,
|
|
823
|
+
base,
|
|
824
|
+
title: creationTitle,
|
|
825
|
+
body,
|
|
826
|
+
draft: input.draft !== false,
|
|
827
|
+
});
|
|
828
|
+
}
|
|
829
|
+
catch (error) {
|
|
830
|
+
throw new DeliveryError(`PR creation acknowledgement is uncertain; reconcile the remote head read-only before any new mutation. ${error instanceof Error ? error.message : 'Unknown failure'}`);
|
|
831
|
+
}
|
|
832
|
+
if (!Number.isSafeInteger(response.data.number) || response.data.number <= 0)
|
|
833
|
+
throw new DeliveryError('PR creation acknowledgement lacks a valid PR identity; reconcile the remote head read-only, do not create again.');
|
|
834
|
+
created = true;
|
|
835
|
+
try {
|
|
836
|
+
pr = await getPr(context, response.data.number);
|
|
837
|
+
}
|
|
838
|
+
catch (error) {
|
|
839
|
+
throw new DeliveryError(`GitHub returned created PR #${response.data.number}; identity readback is unresolved. Reconcile read-only; do not create again. ${error instanceof Error ? error.message : 'Unknown failure'}`);
|
|
840
|
+
}
|
|
841
|
+
}
|
|
842
|
+
try {
|
|
843
|
+
if (created &&
|
|
844
|
+
(pr.body !== body ||
|
|
845
|
+
pr.title !== creationTitle ||
|
|
846
|
+
pr.draft !== (input.draft !== false) ||
|
|
847
|
+
pr.state !== 'open' ||
|
|
848
|
+
pr.merged ||
|
|
849
|
+
pr.base.sha !== publicationBaseSha))
|
|
850
|
+
throw new DeliveryError('Created PR body, title, draft/open state or default base differs from publication intent.');
|
|
851
|
+
if (pr.head.sha !== ref.object.sha || pr.head.ref !== branch || pr.base.ref !== base)
|
|
852
|
+
throw new DeliveryError('GitHub PR creation readback drifted.');
|
|
853
|
+
if (pr.user.login.toLowerCase() !== reviewRoute.author.actorLogin.toLowerCase())
|
|
854
|
+
throw new DeliveryError('Published PR author differs from the configured authenticated author.');
|
|
855
|
+
if (nonClosing) {
|
|
856
|
+
const verified = await verifyIssuePr(context, input.issueNumber, pr.number, true);
|
|
857
|
+
sameReferencePr(pr, verified);
|
|
858
|
+
pr = verified;
|
|
859
|
+
await preserveIssue();
|
|
860
|
+
}
|
|
861
|
+
else
|
|
862
|
+
await verifyPrIssueAssociation(context, pr.number, input.issueNumber);
|
|
863
|
+
if (created) {
|
|
864
|
+
const final = await getPr(context, pr.number);
|
|
865
|
+
sameReferencePr(pr, final);
|
|
866
|
+
pr = final;
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
catch (error) {
|
|
870
|
+
if (!created)
|
|
871
|
+
throw error;
|
|
872
|
+
throw new DeliveryError(`GitHub created PR #${pr.number} (${pr.html_url}) at ${pr.head.sha}; post-publication association/identity verification is unresolved. Reconcile this PR read-only; do not create again. ${error instanceof Error ? error.message : 'Unknown failure'}`);
|
|
333
873
|
}
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
874
|
+
return {
|
|
875
|
+
prNumber: pr.number,
|
|
876
|
+
url: pr.html_url,
|
|
877
|
+
headSha: pr.head.sha,
|
|
878
|
+
draft: pr.draft,
|
|
879
|
+
reviewRoute,
|
|
880
|
+
...(nonClosing ? { nonClosing: true } : {}),
|
|
881
|
+
};
|
|
340
882
|
}
|
|
341
883
|
export async function submitFormalReview(context, input) {
|
|
342
|
-
|
|
343
|
-
|
|
884
|
+
const nonClosing = nonClosingMode(input.nonClosing);
|
|
885
|
+
const pr = await verifyIssuePr(context, input.issueNumber, input.prNumber, nonClosing);
|
|
886
|
+
const issue = nonClosing ? await openReferenceIssue(context, input.issueNumber) : undefined;
|
|
887
|
+
const result = await submitReview({
|
|
344
888
|
context,
|
|
345
889
|
artifact: parseReviewArtifact(input.artifact),
|
|
346
890
|
issueNumber: input.issueNumber,
|
|
347
891
|
prNumber: input.prNumber,
|
|
348
892
|
...(input.dryRun === undefined ? {} : { dryRun: input.dryRun }),
|
|
349
893
|
});
|
|
894
|
+
if (nonClosing) {
|
|
895
|
+
sameReferencePr(pr, await verifyIssuePr(context, input.issueNumber, input.prNumber, true));
|
|
896
|
+
sameReferenceIssue(issue, await openReferenceIssue(context, input.issueNumber));
|
|
897
|
+
}
|
|
898
|
+
return result;
|
|
350
899
|
}
|
|
351
900
|
async function allReviews(context, prNumber) {
|
|
352
901
|
const reviews = [];
|
|
@@ -359,6 +908,9 @@ async function allReviews(context, prNumber) {
|
|
|
359
908
|
throw new DeliveryError('GitHub review history exceeds the bounded readback window.');
|
|
360
909
|
}
|
|
361
910
|
export async function mergePr(context, input) {
|
|
911
|
+
const nonClosing = nonClosingMode(input.nonClosing);
|
|
912
|
+
if (nonClosing && input.strategy !== undefined && input.strategy !== 'squash')
|
|
913
|
+
throw new DeliveryError('Non-closing merge requires squash with explicit safe messages.');
|
|
362
914
|
if (context.clients.role !== 'author' ||
|
|
363
915
|
context.clients.authSource !== (context.config.roles.author.authSource ?? 'app') ||
|
|
364
916
|
!context.clients.authenticatedAuthor)
|
|
@@ -366,7 +918,19 @@ export async function mergePr(context, input) {
|
|
|
366
918
|
const author = await context.clients.authenticatedAuthor();
|
|
367
919
|
if (!author.actorLogin || !author.credentialIdentity)
|
|
368
920
|
throw new DeliveryError('Author identity readback is incomplete.');
|
|
369
|
-
let pr = await verifyIssuePr(context, input.issueNumber, input.prNumber);
|
|
921
|
+
let pr = await verifyIssuePr(context, input.issueNumber, input.prNumber, nonClosing);
|
|
922
|
+
const initialPr = pr;
|
|
923
|
+
const issue = nonClosing ? await openReferenceIssue(context, input.issueNumber) : undefined;
|
|
924
|
+
const squash = nonClosing
|
|
925
|
+
? {
|
|
926
|
+
commit_title: pr.title,
|
|
927
|
+
commit_message: `${referenceLine(context, input.issueNumber)}\n\nDelivered by PR #${pr.number}.`,
|
|
928
|
+
}
|
|
929
|
+
: undefined;
|
|
930
|
+
if (squash) {
|
|
931
|
+
refuseClosingDirective(squash.commit_title);
|
|
932
|
+
refuseClosingDirective(squash.commit_message);
|
|
933
|
+
}
|
|
370
934
|
if (input.reviewedHeadSha !== undefined && input.reviewedHeadSha !== pr.head.sha)
|
|
371
935
|
throw new DeliveryError('Requested reviewed head differs from the GitHub PR.');
|
|
372
936
|
if (pr.merged && pr.state === 'closed' && validSha(pr.merge_commit_sha))
|
|
@@ -433,7 +997,13 @@ export async function mergePr(context, input) {
|
|
|
433
997
|
});
|
|
434
998
|
if (blockers.blockers.some((value) => value.state === 'OPEN'))
|
|
435
999
|
throw new DeliveryError('GitHub issue has unresolved native blockers.');
|
|
436
|
-
pr = await verifyIssuePr(context, input.issueNumber, input.prNumber);
|
|
1000
|
+
pr = await verifyIssuePr(context, input.issueNumber, input.prNumber, nonClosing);
|
|
1001
|
+
if (nonClosing) {
|
|
1002
|
+
sameReferencePr(initialPr, pr);
|
|
1003
|
+
sameReferenceIssue(issue, await openReferenceIssue(context, input.issueNumber));
|
|
1004
|
+
refuseClosingDirective(squash.commit_title);
|
|
1005
|
+
refuseClosingDirective(squash.commit_message);
|
|
1006
|
+
}
|
|
437
1007
|
if (pr.head.sha !== reviewedHead ||
|
|
438
1008
|
pr.state !== 'open' ||
|
|
439
1009
|
pr.draft ||
|
|
@@ -445,16 +1015,26 @@ export async function mergePr(context, input) {
|
|
|
445
1015
|
if (!validSha(currentBase) || currentBase !== pr.base.sha)
|
|
446
1016
|
throw new DeliveryError('GitHub base moved; refresh clean eligibility and independent review before merge.');
|
|
447
1017
|
if (input.dryRun)
|
|
448
|
-
return {
|
|
1018
|
+
return {
|
|
1019
|
+
dryRun: true,
|
|
1020
|
+
headSha: reviewedHead,
|
|
1021
|
+
prNumber: pr.number,
|
|
1022
|
+
...(squash === undefined ? {} : { nonClosing: true, ...squash }),
|
|
1023
|
+
};
|
|
449
1024
|
const result = (await context.clients.rest.pulls.merge({
|
|
450
1025
|
...context.repo,
|
|
451
1026
|
pull_number: input.prNumber,
|
|
452
1027
|
sha: reviewedHead,
|
|
453
1028
|
merge_method: input.strategy ?? 'squash',
|
|
1029
|
+
...squash,
|
|
454
1030
|
})).data;
|
|
455
1031
|
if (!result.merged || !validSha(result.sha))
|
|
456
1032
|
throw new DeliveryError('GitHub refused the reviewed-head merge.');
|
|
457
|
-
const readback = await verifyIssuePr(context, input.issueNumber, input.prNumber);
|
|
1033
|
+
const readback = await verifyIssuePr(context, input.issueNumber, input.prNumber, nonClosing);
|
|
1034
|
+
if (nonClosing) {
|
|
1035
|
+
sameReferencePr(initialPr, readback, true);
|
|
1036
|
+
sameReferenceIssue(issue, await openReferenceIssue(context, input.issueNumber));
|
|
1037
|
+
}
|
|
458
1038
|
if (!readback.merged ||
|
|
459
1039
|
readback.state !== 'closed' ||
|
|
460
1040
|
readback.head.sha !== reviewedHead ||
|
|
@@ -463,6 +1043,8 @@ export async function mergePr(context, input) {
|
|
|
463
1043
|
return { mergeSha: result.sha, headSha: reviewedHead, reused: false };
|
|
464
1044
|
}
|
|
465
1045
|
export async function finishIssue(context, input) {
|
|
1046
|
+
if (nonClosingMode(input.nonClosing))
|
|
1047
|
+
throw new DeliveryError('Non-closing delivery cannot finish or close its retained issue.');
|
|
466
1048
|
const result = await mergePr(context, input);
|
|
467
1049
|
if (input.dryRun)
|
|
468
1050
|
return result;
|
|
@@ -485,4 +1067,231 @@ export async function finishIssue(context, input) {
|
|
|
485
1067
|
return { ...result, issueClosed: true };
|
|
486
1068
|
}
|
|
487
1069
|
export { startIssueBranch };
|
|
1070
|
+
function investigationRoot(context) {
|
|
1071
|
+
const root = primaryGitRoot(context.root);
|
|
1072
|
+
const repo = resolveRepoFromRemote(root, 'origin');
|
|
1073
|
+
if (`${repo.owner}/${repo.repo}`.toLowerCase() !== context.config.repository.toLowerCase() ||
|
|
1074
|
+
`${context.repo.owner}/${context.repo.repo}`.toLowerCase() !== context.config.repository.toLowerCase())
|
|
1075
|
+
throw new DeliveryError('Investigation repository identity disagrees.');
|
|
1076
|
+
return root;
|
|
1077
|
+
}
|
|
1078
|
+
async function withInvestigationExclusion(root, target, operation) {
|
|
1079
|
+
const common = gitCommonDir(root);
|
|
1080
|
+
for (const directory of [join(common, 'ai-delivery'), join(common, 'ai-delivery', 'writers@1')]) {
|
|
1081
|
+
try {
|
|
1082
|
+
const metadata = lstatSync(directory);
|
|
1083
|
+
if (!metadata.isDirectory() || metadata.isSymbolicLink())
|
|
1084
|
+
throw new DeliveryError('Investigation writer namespace is unsafe.');
|
|
1085
|
+
}
|
|
1086
|
+
catch (error) {
|
|
1087
|
+
if (error.code !== 'ENOENT')
|
|
1088
|
+
throw error;
|
|
1089
|
+
}
|
|
1090
|
+
ensurePrivateDirectoryDurably(directory);
|
|
1091
|
+
}
|
|
1092
|
+
return withWorktreeTransitionWriterAbsent(root, target, operation);
|
|
1093
|
+
}
|
|
1094
|
+
async function investigationNativeSource(context, issueNumber, headSha, state) {
|
|
1095
|
+
const issue = (await context.clients.rest.issues.get({ ...context.repo, issue_number: issueNumber })).data;
|
|
1096
|
+
if (issue.number !== issueNumber ||
|
|
1097
|
+
'pull_request' in issue ||
|
|
1098
|
+
issue.state !== state ||
|
|
1099
|
+
new URL(issue.html_url).pathname.toLowerCase() !==
|
|
1100
|
+
`/${context.config.repository}/issues/${String(issueNumber)}`.toLowerCase())
|
|
1101
|
+
throw new DeliveryError('Investigation requires the exact native issue and expected state.');
|
|
1102
|
+
const branches = await linkedIssueBranches(context, issueNumber);
|
|
1103
|
+
if (branches.length !== 1 || branches[0].name !== `issue/${String(issueNumber)}` || branches[0].sha !== headSha)
|
|
1104
|
+
throw new DeliveryError('Investigation requires one exact unchanged native linked branch.');
|
|
1105
|
+
const prs = (await context.clients.rest.pulls.list({
|
|
1106
|
+
...context.repo,
|
|
1107
|
+
state: 'all',
|
|
1108
|
+
head: `${context.repo.owner}:${branches[0].name}`,
|
|
1109
|
+
per_page: 100,
|
|
1110
|
+
})).data;
|
|
1111
|
+
if (prs.length !== 0)
|
|
1112
|
+
throw new DeliveryError('Investigation completion cannot substitute for PR delivery.');
|
|
1113
|
+
}
|
|
1114
|
+
export function investigationPreparationBody(preparation) {
|
|
1115
|
+
return stableJson({
|
|
1116
|
+
schemaVersion: 'ai-delivery.investigation-preparation-acknowledgement@1',
|
|
1117
|
+
preparation,
|
|
1118
|
+
authority: 'prospective-registration-before-investigation-no-legacy-adoption',
|
|
1119
|
+
});
|
|
1120
|
+
}
|
|
1121
|
+
export function investigationAuthorityBody(preparation, resultDigest) {
|
|
1122
|
+
return stableJson({
|
|
1123
|
+
schemaVersion: 'ai-delivery.investigation-removal-authority@1',
|
|
1124
|
+
preparationId: preparation.preparationId,
|
|
1125
|
+
repository: preparation.repository,
|
|
1126
|
+
issueNumber: preparation.row.issueNumber,
|
|
1127
|
+
head: preparation.head,
|
|
1128
|
+
worktree: preparation.row.path,
|
|
1129
|
+
branch: preparation.row.branch,
|
|
1130
|
+
resultDigest,
|
|
1131
|
+
disposition: 'remove-only-this-unchanged-investigation-worktree-retain-branch-and-evidence',
|
|
1132
|
+
operationalClosure: 'all-investigation-producers-quiescent-and-excluded-through-completion-and-resume-no-outstanding-holds',
|
|
1133
|
+
});
|
|
1134
|
+
}
|
|
1135
|
+
export function investigationAcceptanceBody(preparation, resultDigest, authorityCommentId) {
|
|
1136
|
+
return stableJson({
|
|
1137
|
+
schemaVersion: 'ai-delivery.investigation-result-acceptance@1',
|
|
1138
|
+
authority: JSON.parse(investigationAuthorityBody(preparation, resultDigest)),
|
|
1139
|
+
authorityCommentId,
|
|
1140
|
+
verdict: 'approved',
|
|
1141
|
+
independentlyReviewed: 'complete-durable-result-exact-source-and-removal-scope',
|
|
1142
|
+
});
|
|
1143
|
+
}
|
|
1144
|
+
/** Explicit author mode for a host-created worktree; ordinary start remains remote-only. */
|
|
1145
|
+
export async function registerInvestigation(context, input) {
|
|
1146
|
+
if (!Number.isSafeInteger(input.issueNumber) || input.issueNumber <= 0 || !Sha.test(input.expectedHeadSha))
|
|
1147
|
+
throw new DeliveryError('Investigation preparation requires a positive issue and expected commit.');
|
|
1148
|
+
const root = investigationRoot(context);
|
|
1149
|
+
const target = join(root, '.worktrees', `issue-${String(input.issueNumber)}`);
|
|
1150
|
+
if (input.investigationWorktree !== target || realpathSync(target) !== target)
|
|
1151
|
+
throw new DeliveryError('Investigation worktree must be the exact canonical .worktrees/issue-N path.');
|
|
1152
|
+
if (existsSync(join(root, '.issue-cli', 'issues', String(input.issueNumber))))
|
|
1153
|
+
throw new DeliveryError('Historical issue custody remains preservation-only; investigation cannot adopt it.');
|
|
1154
|
+
const actors = await authenticatedWorktreeActors(context);
|
|
1155
|
+
const observed = investigationSourceIdentity(root, input.issueNumber);
|
|
1156
|
+
const prior = readInvestigationPreparation(root, input.issueNumber);
|
|
1157
|
+
await investigationNativeSource(context, input.issueNumber, observed.head.sha, 'open');
|
|
1158
|
+
if (observed.head.sha !== input.expectedHeadSha)
|
|
1159
|
+
throw new DeliveryError('Investigation preparation source drifted from the expected head.');
|
|
1160
|
+
if (prior === undefined) {
|
|
1161
|
+
const repository = (await context.clients.rest.repos.get({ ...context.repo })).data;
|
|
1162
|
+
if (repository.full_name.toLowerCase() !== context.config.repository.toLowerCase() || !repository.default_branch)
|
|
1163
|
+
throw new DeliveryError('Investigation native repository identity is incomplete.');
|
|
1164
|
+
const base = (await context.clients.rest.git.getRef({ ...context.repo, ref: `heads/${repository.default_branch}` }))
|
|
1165
|
+
.data;
|
|
1166
|
+
if (base.object.sha !== observed.head.sha)
|
|
1167
|
+
throw new DeliveryError('Prospective investigation must begin at the current native default commit.');
|
|
1168
|
+
}
|
|
1169
|
+
const date = new Date().toISOString();
|
|
1170
|
+
const content = {
|
|
1171
|
+
schemaVersion: 'ai-delivery.investigation-preparation@1',
|
|
1172
|
+
repository: context.config.repository,
|
|
1173
|
+
repoRoot: root,
|
|
1174
|
+
row: prior?.row ?? {
|
|
1175
|
+
branch: `issue/${String(input.issueNumber)}`,
|
|
1176
|
+
createdAt: date,
|
|
1177
|
+
identity: context.config.roles.author.identity,
|
|
1178
|
+
issueNumber: input.issueNumber,
|
|
1179
|
+
path: target,
|
|
1180
|
+
status: 'active',
|
|
1181
|
+
type: 'issue',
|
|
1182
|
+
updatedAt: date,
|
|
1183
|
+
},
|
|
1184
|
+
...observed,
|
|
1185
|
+
operator: { actorLogin: actors.operator.actorLogin, credentialIdentity: actors.operator.credentialIdentity },
|
|
1186
|
+
reviewerActor: actors.reviewerActor,
|
|
1187
|
+
};
|
|
1188
|
+
const preparation = InvestigationPreparationSchema.parse({ ...content, preparationId: digestValue(content) });
|
|
1189
|
+
await withInvestigationExclusion(root, target, () => registerInvestigationCustody(preparation, async () => {
|
|
1190
|
+
if (stableJson(investigationSourceIdentity(root, input.issueNumber)) !== stableJson(observed))
|
|
1191
|
+
throw new DeliveryError('Investigation source changed before preparation acknowledgement.');
|
|
1192
|
+
await investigationNativeSource(context, input.issueNumber, observed.head.sha, 'open');
|
|
1193
|
+
const { commentId, url, body } = await commentIssue(context, {
|
|
1194
|
+
issueNumber: input.issueNumber,
|
|
1195
|
+
body: investigationPreparationBody(preparation),
|
|
1196
|
+
});
|
|
1197
|
+
if (stableJson(investigationSourceIdentity(root, input.issueNumber)) !== stableJson(observed))
|
|
1198
|
+
throw new DeliveryError('Investigation source changed during preparation acknowledgement.');
|
|
1199
|
+
return { commentId, url, body };
|
|
1200
|
+
}));
|
|
1201
|
+
return { preparation, acknowledgement: readInvestigationAcknowledgement(root, input.issueNumber), registered: true };
|
|
1202
|
+
}
|
|
1203
|
+
/** One non-mergeable completion path. No PR, issue close, reviewer submission or branch deletion occurs. */
|
|
1204
|
+
export async function finishInvestigation(context, input) {
|
|
1205
|
+
if (['prNumber', 'strategy', 'reviewedHeadSha', 'nonClosing'].some((key) => key in input))
|
|
1206
|
+
throw new DeliveryError('Investigation completion refuses PR or merge options.');
|
|
1207
|
+
const root = investigationRoot(context);
|
|
1208
|
+
const preparation = readInvestigationPreparation(root, input.issueNumber);
|
|
1209
|
+
if (!preparation)
|
|
1210
|
+
throw new DeliveryError('No prospective investigation preparation; historical custody cannot be reconstructed.');
|
|
1211
|
+
const resultPath = resolve(input.resultPath);
|
|
1212
|
+
const resultRelative = relative(preparation.row.path, resultPath);
|
|
1213
|
+
const resultOutside = resultRelative === '..' || resultRelative.startsWith(`..${sep}`) || isAbsolute(resultRelative);
|
|
1214
|
+
if (realpathSync(resultPath) !== resultPath || !resultOutside)
|
|
1215
|
+
throw new DeliveryError('Durable investigation result must be canonical and outside the removable worktree.');
|
|
1216
|
+
const result = assertPrivateFile(resultPath, { maxBytes: 512 * 1024 });
|
|
1217
|
+
if (result.length === 0)
|
|
1218
|
+
throw new DeliveryError('Investigation result must contain durable evidence.');
|
|
1219
|
+
const resultDigest = digestBytes(result);
|
|
1220
|
+
const authorityBody = investigationAuthorityBody(preparation, resultDigest);
|
|
1221
|
+
const acceptanceBody = input.authorityCommentId === undefined
|
|
1222
|
+
? undefined
|
|
1223
|
+
: investigationAcceptanceBody(preparation, resultDigest, input.authorityCommentId);
|
|
1224
|
+
const authorize = async () => {
|
|
1225
|
+
await investigationNativeSource(context, input.issueNumber, preparation.head.sha, 'closed');
|
|
1226
|
+
const closure = await readIssueClosure(context, input.issueNumber);
|
|
1227
|
+
if (closure.state !== 'closed' || closure.closeReason !== 'completed' || closure.duplicateOf !== null)
|
|
1228
|
+
throw new DeliveryError('Investigation requires native completed closed-issue authority.');
|
|
1229
|
+
const acknowledgement = readInvestigationAcknowledgement(root, input.issueNumber);
|
|
1230
|
+
const native = (await context.clients.rest.issues.getComment({ ...context.repo, comment_id: acknowledgement.commentId })).data;
|
|
1231
|
+
if (native.id !== acknowledgement.commentId ||
|
|
1232
|
+
native.body !== investigationPreparationBody(preparation) ||
|
|
1233
|
+
native.body !== acknowledgement.body ||
|
|
1234
|
+
native.issue_url.toLowerCase() !==
|
|
1235
|
+
`https://api.github.com/repos/${preparation.repository}/issues/${String(input.issueNumber)}`.toLowerCase() ||
|
|
1236
|
+
native.user?.type !== 'User' ||
|
|
1237
|
+
native.user.login.toLowerCase() !== preparation.operator.actorLogin.toLowerCase() ||
|
|
1238
|
+
`user:${String(native.user.id)}` !== preparation.operator.credentialIdentity)
|
|
1239
|
+
throw new DeliveryError('Native prospective preparation acknowledgement changed.');
|
|
1240
|
+
if (input.authorityCommentId === undefined ||
|
|
1241
|
+
input.acceptanceCommentId === undefined ||
|
|
1242
|
+
acceptanceBody === undefined)
|
|
1243
|
+
throw new DeliveryError('Existing authentic operator and independent App acceptance comments are required.');
|
|
1244
|
+
const authority = await assertNativeWorktreeAcceptance(context, {
|
|
1245
|
+
repository: preparation.repository,
|
|
1246
|
+
subject: input.issueNumber,
|
|
1247
|
+
operator: preparation.operator,
|
|
1248
|
+
reviewerActor: preparation.reviewerActor,
|
|
1249
|
+
authorityCommentId: input.authorityCommentId,
|
|
1250
|
+
acceptanceCommentId: input.acceptanceCommentId,
|
|
1251
|
+
operatorBody: authorityBody,
|
|
1252
|
+
reviewerBody: acceptanceBody,
|
|
1253
|
+
revokedBody: stableJson({
|
|
1254
|
+
schemaVersion: 'ai-delivery.investigation-removal-revocation@1',
|
|
1255
|
+
preparationId: preparation.preparationId,
|
|
1256
|
+
resultDigest,
|
|
1257
|
+
authorityCommentId: input.authorityCommentId,
|
|
1258
|
+
}),
|
|
1259
|
+
});
|
|
1260
|
+
const preparedAt = Date.parse(native.created_at);
|
|
1261
|
+
for (const value of [authority.relinquishment, authority.acceptance]) {
|
|
1262
|
+
const createdAt = Date.parse(value.created_at);
|
|
1263
|
+
if (!Number.isFinite(preparedAt) || !Number.isFinite(createdAt) || createdAt < preparedAt)
|
|
1264
|
+
throw new DeliveryError('Investigation acceptance must follow its prospective preparation.');
|
|
1265
|
+
}
|
|
1266
|
+
return { acknowledgement: native, closure, ...authority };
|
|
1267
|
+
};
|
|
1268
|
+
if (input.dryRun && (input.authorityCommentId === undefined || input.acceptanceCommentId === undefined)) {
|
|
1269
|
+
assertInvestigationSource(preparation, context.root);
|
|
1270
|
+
await investigationNativeSource(context, input.issueNumber, preparation.head.sha, 'closed');
|
|
1271
|
+
return {
|
|
1272
|
+
completed: false,
|
|
1273
|
+
ready: false,
|
|
1274
|
+
preparationId: preparation.preparationId,
|
|
1275
|
+
resultDigest,
|
|
1276
|
+
authorityBody,
|
|
1277
|
+
...(acceptanceBody === undefined ? {} : { acceptanceBody }),
|
|
1278
|
+
missingEvidence: 'Existing authentic operator and configured-App exact-result acceptance are prerequisites; no submission route is supplied.',
|
|
1279
|
+
};
|
|
1280
|
+
}
|
|
1281
|
+
if (!Number.isSafeInteger(input.authorityCommentId) ||
|
|
1282
|
+
!Number.isSafeInteger(input.acceptanceCommentId) ||
|
|
1283
|
+
input.authorityCommentId <= 0 ||
|
|
1284
|
+
input.acceptanceCommentId <= 0)
|
|
1285
|
+
throw new DeliveryError('Investigation evidence requires positive native comment IDs.');
|
|
1286
|
+
return withInvestigationExclusion(root, preparation.row.path, () => completeInvestigationCustody({
|
|
1287
|
+
preparation,
|
|
1288
|
+
result,
|
|
1289
|
+
authorityCommentId: input.authorityCommentId,
|
|
1290
|
+
acceptanceCommentId: input.acceptanceCommentId,
|
|
1291
|
+
dryRun: input.dryRun === true,
|
|
1292
|
+
authorize,
|
|
1293
|
+
assertSource: (allowAbsent) => assertInvestigationSource(preparation, context.root, allowAbsent),
|
|
1294
|
+
removeSource: () => removeInvestigationSource(preparation, context.root),
|
|
1295
|
+
}));
|
|
1296
|
+
}
|
|
488
1297
|
//# sourceMappingURL=pr.js.map
|