@comforge/github 0.1.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +55 -4
- package/dist/errors.d.ts +1 -1
- package/dist/errors.js +4 -0
- package/dist/evidence.d.ts +56 -2
- package/dist/evidence.js +109 -9
- package/dist/index.d.ts +3 -1
- package/dist/index.js +2 -0
- package/dist/operations.d.ts +147 -0
- package/dist/operations.js +328 -0
- package/dist/request.d.ts +49 -0
- package/dist/request.js +88 -0
- package/dist/transport.js +7 -1
- package/dist/writer.d.ts +183 -0
- package/dist/writer.js +877 -0
- package/llms.txt +5 -3
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -9,7 +9,7 @@
|
|
|
9
9
|
- reviews, optionally only those submitted on an exact commit;
|
|
10
10
|
- check runs recorded for an exact commit, judged against the check names you require.
|
|
11
11
|
|
|
12
|
-
|
|
12
|
+
The evidence API **performs no writes** and grants nothing. A separate, opt-in writer (`createGitHubWriter`, [below](#caller-authorized-writes)) performs a fixed set of GitHub writes only when your own policy authorizes each exact request. GitHub is an external evidence source: a successful response is never an authorization, and every person-written text field (titles, bodies, review text) is returned as `{ text, truncated, trust: "untrusted" }` data, never as an instruction or a proof.
|
|
13
13
|
|
|
14
14
|
> **Pre-1.0.** Releases are tagged `github/vX.Y.Z` on the `releases/github` branch of the public Comforge repository (`github.com/vikramrao1994/comforge`), each with a `RELEASE.json` naming its source commit and checksums. The TypeScript API may still change before 1.0.
|
|
15
15
|
|
|
@@ -41,7 +41,7 @@ const files = await github.listPullRequestFiles(7, pr.head.sha); // refuses if t
|
|
|
41
41
|
if (!checks.ok) throw new Error("required checks are not green for this exact commit");
|
|
42
42
|
```
|
|
43
43
|
|
|
44
|
-
A complete, runnable consumer with a fake GitHub transport: [examples/factory-github-consumer](https://github.com/vikramrao1994/comforge/tree/github/v0.
|
|
44
|
+
A complete, runnable consumer with a fake GitHub transport: [examples/factory-github-consumer](https://github.com/vikramrao1994/comforge/tree/github/v0.3.0/examples/factory-github-consumer/).
|
|
45
45
|
|
|
46
46
|
## The transport
|
|
47
47
|
|
|
@@ -76,11 +76,62 @@ With a `SpecificationProtocol` from `@comforge/core` (`createSpecificationProtoc
|
|
|
76
76
|
- `readSpecificationIssue(github, protocol, n)` returns the issue, the marker, the delimited specification text and `verified` (the issue still hashes to its marker). A pull request is `wrong-target`; a body cut by the text limit is `incomplete`.
|
|
77
77
|
- `reconcileSpecification(github, protocol, { ticketId, digest, keyDigest })` reads every issue and answers `absent`, `existing` or `replayed`; a key or ticket already used for other content is a `conflict`. Call it before publishing so a retry never duplicates an issue.
|
|
78
78
|
|
|
79
|
-
Publishing the issue
|
|
79
|
+
Publishing the issue is not part of the specification protocol: `@comforge/core` has no authorization that covers creating an issue. If you publish with this package's writer, the authority is your own `authorize` policy, not core.
|
|
80
|
+
|
|
81
|
+
## Caller-authorized writes
|
|
82
|
+
|
|
83
|
+
`createGitHubWriter` is separate from everything above: the read transport and evidence objects have no write method and never gain one. Construct a writer only where you mean to write.
|
|
84
|
+
|
|
85
|
+
```ts
|
|
86
|
+
import { createGitHubWriter } from "@comforge/github";
|
|
87
|
+
|
|
88
|
+
const writer = createGitHubWriter({
|
|
89
|
+
repository: { owner: "acme", name: "widgets" },
|
|
90
|
+
token: () => mintInstallationToken(), // yours; never stored, logged or returned
|
|
91
|
+
capabilities: ["issues:comment"], // the only writes this writer performs
|
|
92
|
+
authorize: (descriptor) => myPolicy(descriptor), // yours: allow one exact descriptor, briefly
|
|
93
|
+
ledger: myDurableLedger, // yours: atomic create + version-checked replace
|
|
94
|
+
clock: Date.now,
|
|
95
|
+
expectedAuthor: "acme-bot[bot]", // the login the token writes as
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const outcome = await writer.execute({
|
|
99
|
+
kind: "issue.comment", operationId: "acme-2026-0042-c1", issueNumber: 42, body: "Thanks.",
|
|
100
|
+
});
|
|
101
|
+
// outcome.status: "verified" | "accepted" | "indeterminate"
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
**Operations and capabilities.** Each operation needs exactly one capability; none implies another, and none is inferred from what the token permits.
|
|
105
|
+
|
|
106
|
+
| `kind` | Capability | GitHub request | Checked before the write |
|
|
107
|
+
|---|---|---|---|
|
|
108
|
+
| `issue.create` | `issues:create` | `POST /repos/{o}/{r}/issues` | — |
|
|
109
|
+
| `issue.comment` | `issues:comment` | `POST …/issues/{n}/comments` | the thread is an issue (or `thread: "pull-request"`), open, not locked |
|
|
110
|
+
| `pull.review` | `pulls:review` (COMMENT), `pulls:review:approve`, `pulls:review:request-changes` | `POST …/pulls/{n}/reviews` with `commit_id` | open, head is exactly `expectedHeadSha` |
|
|
111
|
+
| `pull.create` | `pulls:create` | `POST …/pulls` (same-repository head, `maintainer_can_modify: false`) | head (and optionally base) branch at the expected commit; no open PR for head and base |
|
|
112
|
+
| `issue.update` | `issues:update` | `PATCH …/issues/{n}` | `updatedAt`, and optionally body digest and state, unchanged |
|
|
113
|
+
| `comment.update` | `issues:comment:update` | `PATCH …/issues/comments/{id}` | on the expected issue; `updatedAt`, optionally body digest and author, unchanged |
|
|
114
|
+
|
|
115
|
+
Not supported, by design: merging, tags and releases, workflow dispatch, deletion, inline review comments, pending reviews and cross-repository pull requests.
|
|
116
|
+
|
|
117
|
+
**Authorization is yours and is bound to bytes.** `prepare(op)` returns the frozen `OperationDescriptor` (repository, target, capability, exact JSON payload, preconditions, and a `sha256:` digest of all of them) without sending anything. `execute(op)` builds the same descriptor and calls `authorize(descriptor)`. The decision must be `{ allow: true, descriptorDigest, expiresAt }`, with the digest of exactly this descriptor and an expiry no more than 15 minutes ahead (`limits.maxAuthorizationTtlMs`). Anything else, including a thrown error, is `authorization-denied` and nothing is sent; the expiry is checked again just before sending. The request body is derived from the descriptor only, so changing your input object after authorization changes nothing. A callback that returns `allow` is your policy, not evidence that a person approved anything.
|
|
118
|
+
|
|
119
|
+
**The ledger is what makes retries safe.** GitHub offers no idempotency key and no compare-and-swap for these endpoints, so the writer records every operation in your `OperationLedger` before sending: `reserved` → `sending` → `succeeded` | `failed` | `indeterminate` | `abandoned`. It relies on two atomic primitives — insert-if-absent (`create`) and replace-if-version (`replace`) — so two processes sharing the store cannot both send one operation. A process-local map gives that only within one process.
|
|
120
|
+
|
|
121
|
+
- The same `operationId` with the same content replays the recorded outcome without a request; with other content it is a `conflict`.
|
|
122
|
+
- Created bodies end with a correlation marker (`<!-- comforge-op v1 ns=… op=… fp=… -->`, digests only). A write whose answer is lost (timeout, network, 5xx, 408, unreadable 2xx) is searched for once by that marker, within `limits.reconcileMaxPages`. Found: the outcome is `recovered`. Not found: `indeterminate`, never resent automatically. Only markers by `expectedAuthor` count, since anyone with write access can copy one.
|
|
123
|
+
- `reconcile(operationId)` searches again; `abandon(operationId, reason)` ends the operation by your decision. A retry after a rate limit waits for `retry-after` and searches before its one resend.
|
|
124
|
+
- Updates carry no marker: reconciliation compares the target with the requested change.
|
|
125
|
+
|
|
126
|
+
Nothing here is exactly-once: a write can still be duplicated if it becomes visible only after you abandon it, and an expected-state check reads before it writes, so a change in between is not prevented (an edit within the same second as `updatedAt` is not even detected — pass `bodyDigest`, from `bodyDigest(body)`, to narrow that).
|
|
127
|
+
|
|
128
|
+
**Outcomes.** `verified`: a fresh read shows exactly what was authorized. `accepted`: the resource exists but differs or could not be re-read (`mismatches`, e.g. labels GitHub silently dropped for a caller without push access, or a branch that moved during a pull-request create). `indeterminate`: unknown; reconcile. Refusals throw a `GitHubAdapterError` and mean nothing was written.
|
|
129
|
+
|
|
130
|
+
**Permissions** (fine-grained token or GitHub App installation token; the writer cannot verify a token's permissions and does not try): Issues write for issue writes and issue comments; Pull requests write for reviews, pull requests and comments on pull requests; plus read access to issues, pull requests and contents (branch refs) for the checks.
|
|
80
131
|
|
|
81
132
|
## Errors
|
|
82
133
|
|
|
83
|
-
Every refusal is a `GitHubAdapterError` with a `code`: `invalid-input`, `unapproved-origin`, `not-found`, `unauthorized` (401), `forbidden` (403), `rate-limited` (429, or 403 with `retry-after` / an exhausted rate limit; `detail.retryAfterSeconds` when GitHub said), `http-error`, `timeout`, `network`, `too-large`, `malformed-response`, `incomplete`, `stale`, `wrong-target`, `conflict`.
|
|
134
|
+
Every refusal is a `GitHubAdapterError` with a `code`: `invalid-input`, `unapproved-origin`, `not-found`, `unauthorized` (401), `forbidden` (403), `rate-limited` (429, or 403 with `retry-after` / an exhausted rate limit; `detail.retryAfterSeconds` when GitHub said), `http-error`, `timeout`, `network`, `too-large`, `malformed-response`, `incomplete`, `stale`, `wrong-target`, `conflict`; and from the writer, refused before anything was sent: `capability-denied`, `authorization-denied`, `ledger-failure`.
|
|
84
135
|
|
|
85
136
|
## License
|
|
86
137
|
|
package/dist/errors.d.ts
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
* Every refusal the adapter makes, with a stable code. A refusal is never a governance
|
|
3
3
|
* decision: it says GitHub could not supply trustworthy evidence, so the caller stops.
|
|
4
4
|
*/
|
|
5
|
-
export declare const GITHUB_ERROR_CODES: readonly ["invalid-input", "unapproved-origin", "not-found", "unauthorized", "forbidden", "rate-limited", "http-error", "timeout", "network", "too-large", "malformed-response", "incomplete", "stale", "wrong-target", "conflict"];
|
|
5
|
+
export declare const GITHUB_ERROR_CODES: readonly ["invalid-input", "unapproved-origin", "not-found", "unauthorized", "forbidden", "rate-limited", "http-error", "timeout", "network", "too-large", "malformed-response", "incomplete", "stale", "wrong-target", "conflict", "capability-denied", "authorization-denied", "ledger-failure"];
|
|
6
6
|
export type GitHubErrorCode = (typeof GITHUB_ERROR_CODES)[number];
|
|
7
7
|
export interface GitHubErrorDetail {
|
|
8
8
|
/** HTTP status, when GitHub answered. */
|
package/dist/errors.js
CHANGED
|
@@ -18,6 +18,10 @@ export const GITHUB_ERROR_CODES = [
|
|
|
18
18
|
"stale",
|
|
19
19
|
"wrong-target",
|
|
20
20
|
"conflict",
|
|
21
|
+
// The writer refuses with these before anything is sent.
|
|
22
|
+
"capability-denied",
|
|
23
|
+
"authorization-denied",
|
|
24
|
+
"ledger-failure",
|
|
21
25
|
];
|
|
22
26
|
export class GitHubAdapterError extends Error {
|
|
23
27
|
name = "GitHubAdapterError";
|
package/dist/evidence.d.ts
CHANGED
|
@@ -60,6 +60,7 @@ export interface PullRequestEvidence {
|
|
|
60
60
|
repository: string | null;
|
|
61
61
|
};
|
|
62
62
|
}
|
|
63
|
+
export type PatchStatus = "included" | "truncated" | "omitted-budget" | "omitted-by-request" | "unavailable";
|
|
63
64
|
export interface ChangedFile {
|
|
64
65
|
path: string;
|
|
65
66
|
previousPath: string | null;
|
|
@@ -67,6 +68,34 @@ export interface ChangedFile {
|
|
|
67
68
|
additions: number;
|
|
68
69
|
deletions: number;
|
|
69
70
|
changes: number;
|
|
71
|
+
/** GitHub sent no patch and the name is a binary format: there is no text to show. */
|
|
72
|
+
binary: boolean;
|
|
73
|
+
/**
|
|
74
|
+
* Why `patch` is or is not here. `unavailable`: GitHub sent none (binary or too large for it);
|
|
75
|
+
* never an empty patch. `omitted-*`: GitHub sent one and the caller's request or budget left it out.
|
|
76
|
+
*/
|
|
77
|
+
patchStatus: PatchStatus;
|
|
78
|
+
patch: UntrustedText | null;
|
|
79
|
+
}
|
|
80
|
+
export interface PatchLimits {
|
|
81
|
+
/** Characters kept of one file's patch. */
|
|
82
|
+
perFileMaxChars: number;
|
|
83
|
+
/** Characters kept across all patches, in GitHub's file order. */
|
|
84
|
+
totalMaxChars: number;
|
|
85
|
+
}
|
|
86
|
+
export declare const DEFAULT_PATCH_LIMITS: PatchLimits;
|
|
87
|
+
export interface WorkflowRunEvidence {
|
|
88
|
+
repository: string;
|
|
89
|
+
sha: string;
|
|
90
|
+
/** Workflow runs for exactly `sha`. Evidence of what ran, never a verdict on required checks. */
|
|
91
|
+
runs: {
|
|
92
|
+
id: number;
|
|
93
|
+
name: string | null;
|
|
94
|
+
status: string | null;
|
|
95
|
+
conclusion: string | null;
|
|
96
|
+
event: string;
|
|
97
|
+
createdAt: string;
|
|
98
|
+
}[];
|
|
70
99
|
}
|
|
71
100
|
export interface PullRequestFilesEvidence {
|
|
72
101
|
repository: string;
|
|
@@ -90,6 +119,7 @@ export interface CheckEvidence {
|
|
|
90
119
|
status: string;
|
|
91
120
|
conclusion: string | null;
|
|
92
121
|
app: string | null;
|
|
122
|
+
startedAt: string | null;
|
|
93
123
|
completedAt: string | null;
|
|
94
124
|
}[];
|
|
95
125
|
required: {
|
|
@@ -100,6 +130,18 @@ export interface CheckEvidence {
|
|
|
100
130
|
/** Every required check has a successful run on exactly `sha`. False when nothing is required. */
|
|
101
131
|
ok: boolean;
|
|
102
132
|
}
|
|
133
|
+
export interface CommitComparison {
|
|
134
|
+
repository: string;
|
|
135
|
+
base: string;
|
|
136
|
+
head: string;
|
|
137
|
+
/** How `head` relates to `base`: `ahead` means `head` has commits `base` lacks. */
|
|
138
|
+
status: "identical" | "ahead" | "behind" | "diverged";
|
|
139
|
+
/** Commits on `head` that are not on `base`. */
|
|
140
|
+
aheadBy: number;
|
|
141
|
+
/** Commits on `base` that are not on `head`. */
|
|
142
|
+
behindBy: number;
|
|
143
|
+
mergeBase: string;
|
|
144
|
+
}
|
|
103
145
|
export interface GitHubEvidence {
|
|
104
146
|
readonly repository: string;
|
|
105
147
|
getIssue(number: number): Promise<IssueEvidence>;
|
|
@@ -110,14 +152,26 @@ export interface GitHubEvidence {
|
|
|
110
152
|
getPullRequest(number: number, options?: {
|
|
111
153
|
expectedHeadSha?: string;
|
|
112
154
|
}): Promise<PullRequestEvidence>;
|
|
113
|
-
/**
|
|
114
|
-
|
|
155
|
+
/**
|
|
156
|
+
* The changed files of a pull request whose head is exactly `headSha`, before and after the
|
|
157
|
+
* read. With `patches`, each file's patch text within the per-file and total budgets.
|
|
158
|
+
*/
|
|
159
|
+
listPullRequestFiles(number: number, headSha: string, options?: {
|
|
160
|
+
patches?: Partial<PatchLimits> | true;
|
|
161
|
+
}): Promise<PullRequestFilesEvidence>;
|
|
115
162
|
/** Reviews; with `commitSha`, only those submitted on that exact commit. */
|
|
116
163
|
listReviews(number: number, options?: {
|
|
117
164
|
commitSha?: string;
|
|
118
165
|
}): Promise<ReviewEvidence[]>;
|
|
119
166
|
/** Check runs GitHub recorded for exactly `sha`, judged against `required` check names. */
|
|
120
167
|
getChecks(sha: string, required?: readonly string[]): Promise<CheckEvidence>;
|
|
168
|
+
/**
|
|
169
|
+
* Workflow runs GitHub recorded for exactly `sha`: a read for credentials without check-run
|
|
170
|
+
* access. Which one to trust when is the caller's policy; this never reports success.
|
|
171
|
+
*/
|
|
172
|
+
listWorkflowRuns(sha: string): Promise<WorkflowRunEvidence>;
|
|
173
|
+
/** How commit `head` relates to commit `base` (both full SHAs), as GitHub's compare reports it. */
|
|
174
|
+
compareCommits(base: string, head: string): Promise<CommitComparison>;
|
|
121
175
|
}
|
|
122
176
|
export declare function createGitHubEvidence(options: {
|
|
123
177
|
transport: GitHubTransport;
|
package/dist/evidence.js
CHANGED
|
@@ -52,6 +52,7 @@ const FileSchema = z.object({
|
|
|
52
52
|
additions: z.number().int(),
|
|
53
53
|
deletions: z.number().int(),
|
|
54
54
|
changes: z.number().int(),
|
|
55
|
+
patch: z.string().optional(),
|
|
55
56
|
});
|
|
56
57
|
const ReviewSchema = z.object({
|
|
57
58
|
id: z.number().int(),
|
|
@@ -69,10 +70,36 @@ const CheckRunsSchema = z.object({
|
|
|
69
70
|
head_sha: z.string(),
|
|
70
71
|
status: z.string(),
|
|
71
72
|
conclusion: z.string().nullable(),
|
|
73
|
+
started_at: z.string().nullable().optional(),
|
|
72
74
|
completed_at: z.string().nullable().optional(),
|
|
73
75
|
app: z.object({ slug: z.string() }).nullable().optional(),
|
|
74
76
|
})),
|
|
75
77
|
});
|
|
78
|
+
const WorkflowRunsSchema = z.object({
|
|
79
|
+
total_count: z.number().int().nonnegative(),
|
|
80
|
+
workflow_runs: z.array(z.object({
|
|
81
|
+
id: z.number().int(),
|
|
82
|
+
name: z.string().nullable(),
|
|
83
|
+
head_sha: z.string(),
|
|
84
|
+
status: z.string().nullable(),
|
|
85
|
+
conclusion: z.string().nullable(),
|
|
86
|
+
event: z.string(),
|
|
87
|
+
created_at: z.string(),
|
|
88
|
+
})),
|
|
89
|
+
});
|
|
90
|
+
/** Extensions whose missing patch means binary content, not an unreadable one. */
|
|
91
|
+
const BINARY_EXTENSIONS = /\.(png|jpe?g|gif|webp|avif|ico|pdf|zip|gz|woff2?|ttf|bin|db|sqlite)$/i;
|
|
92
|
+
const CompareSchema = z.object({
|
|
93
|
+
status: z.enum(["identical", "ahead", "behind", "diverged"]),
|
|
94
|
+
ahead_by: z.number().int().nonnegative(),
|
|
95
|
+
behind_by: z.number().int().nonnegative(),
|
|
96
|
+
base_commit: z.object({ sha: z.string().regex(SHA) }),
|
|
97
|
+
merge_base_commit: z.object({ sha: z.string().regex(SHA) }),
|
|
98
|
+
});
|
|
99
|
+
export const DEFAULT_PATCH_LIMITS = {
|
|
100
|
+
perFileMaxChars: 20_000,
|
|
101
|
+
totalMaxChars: 100_000,
|
|
102
|
+
};
|
|
76
103
|
function positive(value, what) {
|
|
77
104
|
if (!Number.isSafeInteger(value) || value < 1)
|
|
78
105
|
throw new GitHubAdapterError("invalid-input", `Invalid ${what}`);
|
|
@@ -173,26 +200,60 @@ export function createGitHubEvidence(options) {
|
|
|
173
200
|
return all.map(issueEvidence).filter((i) => !i.isPullRequest);
|
|
174
201
|
},
|
|
175
202
|
getPullRequest,
|
|
176
|
-
async listPullRequestFiles(number, headSha) {
|
|
203
|
+
async listPullRequestFiles(number, headSha, opts = {}) {
|
|
177
204
|
sha(headSha, "headSha");
|
|
205
|
+
const patchLimits = opts.patches === undefined
|
|
206
|
+
? null
|
|
207
|
+
: { ...DEFAULT_PATCH_LIMITS, ...(opts.patches === true ? {} : opts.patches) };
|
|
208
|
+
if (patchLimits &&
|
|
209
|
+
!(Number.isSafeInteger(patchLimits.perFileMaxChars) &&
|
|
210
|
+
Number.isSafeInteger(patchLimits.totalMaxChars) &&
|
|
211
|
+
patchLimits.perFileMaxChars >= 1 &&
|
|
212
|
+
patchLimits.totalMaxChars >= 1))
|
|
213
|
+
throw new GitHubAdapterError("invalid-input", "Invalid patch limits");
|
|
178
214
|
await getPullRequest(number, { expectedHeadSha: headSha });
|
|
179
215
|
const files = await pages(["pulls", number, "files"], {}, (body) => ({
|
|
180
216
|
items: parse(z.array(FileSchema), body, "pull request files"),
|
|
181
217
|
}));
|
|
182
218
|
// The listing is of whatever the head was while it was read; prove it did not move.
|
|
183
219
|
await getPullRequest(number, { expectedHeadSha: headSha });
|
|
220
|
+
let budget = patchLimits?.totalMaxChars ?? 0;
|
|
184
221
|
return {
|
|
185
222
|
repository,
|
|
186
223
|
number,
|
|
187
224
|
headSha,
|
|
188
|
-
files: files.map((f) =>
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
225
|
+
files: files.map((f) => {
|
|
226
|
+
let patchStatus = "unavailable";
|
|
227
|
+
let patch = null;
|
|
228
|
+
if (f.patch !== undefined) {
|
|
229
|
+
if (!patchLimits)
|
|
230
|
+
patchStatus = "omitted-by-request";
|
|
231
|
+
else if (budget <= 0)
|
|
232
|
+
patchStatus = "omitted-budget";
|
|
233
|
+
else {
|
|
234
|
+
const max = Math.min(patchLimits.perFileMaxChars, budget);
|
|
235
|
+
const truncated = f.patch.length > max;
|
|
236
|
+
patch = {
|
|
237
|
+
text: truncated ? f.patch.slice(0, max) : f.patch,
|
|
238
|
+
truncated,
|
|
239
|
+
trust: UNTRUSTED,
|
|
240
|
+
};
|
|
241
|
+
budget -= patch.text.length;
|
|
242
|
+
patchStatus = truncated ? "truncated" : "included";
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
return {
|
|
246
|
+
path: f.filename,
|
|
247
|
+
previousPath: f.previous_filename ?? null,
|
|
248
|
+
status: f.status,
|
|
249
|
+
additions: f.additions,
|
|
250
|
+
deletions: f.deletions,
|
|
251
|
+
changes: f.changes,
|
|
252
|
+
binary: f.patch === undefined && BINARY_EXTENSIONS.test(f.filename),
|
|
253
|
+
patchStatus,
|
|
254
|
+
patch,
|
|
255
|
+
};
|
|
256
|
+
}),
|
|
196
257
|
};
|
|
197
258
|
},
|
|
198
259
|
async listReviews(number, opts = {}) {
|
|
@@ -224,6 +285,7 @@ export function createGitHubEvidence(options) {
|
|
|
224
285
|
status: r.status,
|
|
225
286
|
conclusion: r.conclusion,
|
|
226
287
|
app: r.app?.slug ?? null,
|
|
288
|
+
startedAt: r.started_at ?? null,
|
|
227
289
|
completedAt: r.completed_at ?? null,
|
|
228
290
|
}));
|
|
229
291
|
// Any successful run of a required name on exactly this commit proves it passed on this tree.
|
|
@@ -244,5 +306,43 @@ export function createGitHubEvidence(options) {
|
|
|
244
306
|
ok: requiredEvidence.length > 0 && requiredEvidence.every((r) => r.satisfied),
|
|
245
307
|
};
|
|
246
308
|
},
|
|
309
|
+
async listWorkflowRuns(commit) {
|
|
310
|
+
sha(commit);
|
|
311
|
+
const runs = await pages(["actions", "runs"], { head_sha: commit }, (body) => {
|
|
312
|
+
const page = parse(WorkflowRunsSchema, body, "workflow runs");
|
|
313
|
+
return { items: page.workflow_runs, total: page.total_count };
|
|
314
|
+
});
|
|
315
|
+
if (runs.some((r) => r.head_sha !== commit))
|
|
316
|
+
throw new GitHubAdapterError("malformed-response", "A workflow run belongs to another commit");
|
|
317
|
+
return {
|
|
318
|
+
repository,
|
|
319
|
+
sha: commit,
|
|
320
|
+
runs: runs.map((r) => ({
|
|
321
|
+
id: r.id,
|
|
322
|
+
name: r.name,
|
|
323
|
+
status: r.status,
|
|
324
|
+
conclusion: r.conclusion,
|
|
325
|
+
event: r.event,
|
|
326
|
+
createdAt: r.created_at,
|
|
327
|
+
})),
|
|
328
|
+
};
|
|
329
|
+
},
|
|
330
|
+
async compareCommits(baseSha, headSha) {
|
|
331
|
+
sha(baseSha, "base");
|
|
332
|
+
sha(headSha, "head");
|
|
333
|
+
const c = parse(CompareSchema, await get(["compare", `${baseSha}...${headSha}`]), "a commit comparison");
|
|
334
|
+
// GitHub resolves the range itself; an answer about another base is not this comparison.
|
|
335
|
+
if (c.base_commit.sha !== baseSha)
|
|
336
|
+
throw new GitHubAdapterError("malformed-response", "The comparison names another base");
|
|
337
|
+
return {
|
|
338
|
+
repository,
|
|
339
|
+
base: baseSha,
|
|
340
|
+
head: headSha,
|
|
341
|
+
status: c.status,
|
|
342
|
+
aheadBy: c.ahead_by,
|
|
343
|
+
behindBy: c.behind_by,
|
|
344
|
+
mergeBase: c.merge_base_commit.sha,
|
|
345
|
+
};
|
|
346
|
+
},
|
|
247
347
|
});
|
|
248
348
|
}
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export { GITHUB_ERROR_CODES, GitHubAdapterError, type GitHubErrorCode, type GitHubErrorDetail, } from "./errors.ts";
|
|
2
|
-
export { type ChangedFile, type CheckEvidence, createGitHubEvidence, type EvidenceLimits, type GitHubEvidence, type IssueEvidence, type PullRequestEvidence, type PullRequestFilesEvidence, type RepositoryRef, type ReviewEvidence, type UntrustedText, } from "./evidence.ts";
|
|
2
|
+
export { type ChangedFile, type CheckEvidence, type CommitComparison, createGitHubEvidence, type EvidenceLimits, type GitHubEvidence, type IssueEvidence, type PatchLimits, type PatchStatus, type PullRequestEvidence, type PullRequestFilesEvidence, type RepositoryRef, type ReviewEvidence, type UntrustedText, type WorkflowRunEvidence, } from "./evidence.ts";
|
|
3
|
+
export { bodyDigest, type DescriptorTarget, type OperationDescriptor, type ReviewEvent, WRITE_CAPABILITIES, type WriteCapability, type WriteKind, type WriteOperation, } from "./operations.ts";
|
|
3
4
|
export { type Reconciliation, readSpecificationIssue, reconcileSpecification, type SpecificationIssueEvidence, } from "./specification.ts";
|
|
4
5
|
export { createFetchTransport, type FetchTransportOptions, type GitHubTransport, } from "./transport.ts";
|
|
6
|
+
export { type AuthorizationDecision, createGitHubWriter, DEFAULT_WRITER_LIMITS, type GitHubWriter, type GitHubWriterOptions, type OperationLedger, type OperationRecord, type OperationState, type ReconciliationReport, type WriteOutcome, type WriterLimits, type WrittenResource, } from "./writer.ts";
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
1
|
export { GITHUB_ERROR_CODES, GitHubAdapterError, } from "./errors.js";
|
|
2
2
|
export { createGitHubEvidence, } from "./evidence.js";
|
|
3
|
+
export { bodyDigest, WRITE_CAPABILITIES, } from "./operations.js";
|
|
3
4
|
export { readSpecificationIssue, reconcileSpecification, } from "./specification.js";
|
|
4
5
|
export { createFetchTransport, } from "./transport.js";
|
|
6
|
+
export { createGitHubWriter, DEFAULT_WRITER_LIMITS, } from "./writer.js";
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Write operations as canonical, frozen descriptors. A descriptor is the one object a consumer
|
|
3
|
+
* authorizes: the request the writer sends is derived from it and nothing else, so an
|
|
4
|
+
* authorization of one descriptor can never be spent on other bytes, another target or another
|
|
5
|
+
* capability. This module only validates and canonicalizes; it sends nothing.
|
|
6
|
+
*/
|
|
7
|
+
/**
|
|
8
|
+
* Capabilities a writer may be constructed with. Each names exactly one kind of write; nothing
|
|
9
|
+
* is implied by a broader one, and none is inferred from what a token happens to permit.
|
|
10
|
+
*/
|
|
11
|
+
export declare const WRITE_CAPABILITIES: readonly ["issues:create", "issues:comment", "issues:update", "issues:comment:update", "pulls:create", "pulls:review", "pulls:review:approve", "pulls:review:request-changes"];
|
|
12
|
+
export type WriteCapability = (typeof WRITE_CAPABILITIES)[number];
|
|
13
|
+
export type ReviewEvent = "COMMENT" | "APPROVE" | "REQUEST_CHANGES";
|
|
14
|
+
export type WriteOperation = {
|
|
15
|
+
kind: "issue.create";
|
|
16
|
+
operationId: string;
|
|
17
|
+
title: string;
|
|
18
|
+
body: string;
|
|
19
|
+
labels?: readonly string[];
|
|
20
|
+
assignees?: readonly string[];
|
|
21
|
+
} | {
|
|
22
|
+
kind: "issue.comment";
|
|
23
|
+
operationId: string;
|
|
24
|
+
issueNumber: number;
|
|
25
|
+
body: string;
|
|
26
|
+
/** The thread the number must be. Defaults to `issue`; a mismatch is `wrong-target`. */
|
|
27
|
+
thread?: "issue" | "pull-request";
|
|
28
|
+
/** Defaults to false: a closed thread is refused. */
|
|
29
|
+
allowClosed?: boolean;
|
|
30
|
+
/** Defaults to false: a locked thread is refused. */
|
|
31
|
+
allowLocked?: boolean;
|
|
32
|
+
} | {
|
|
33
|
+
kind: "pull.review";
|
|
34
|
+
operationId: string;
|
|
35
|
+
pullNumber: number;
|
|
36
|
+
/** The head the review is of; sent as `commit_id`, and the pull request must still be at it. */
|
|
37
|
+
expectedHeadSha: string;
|
|
38
|
+
event: ReviewEvent;
|
|
39
|
+
body: string;
|
|
40
|
+
} | {
|
|
41
|
+
kind: "pull.create";
|
|
42
|
+
operationId: string;
|
|
43
|
+
title: string;
|
|
44
|
+
body: string;
|
|
45
|
+
/** A branch of this repository (cross-repository heads are not supported). */
|
|
46
|
+
head: string;
|
|
47
|
+
base: string;
|
|
48
|
+
/** The commit `head` must point at, checked before and after the create. */
|
|
49
|
+
expectedHeadSha: string;
|
|
50
|
+
/** When given, the commit `base` must point at before the create. */
|
|
51
|
+
expectedBaseSha?: string;
|
|
52
|
+
draft?: boolean;
|
|
53
|
+
} | {
|
|
54
|
+
kind: "issue.update";
|
|
55
|
+
operationId: string;
|
|
56
|
+
issueNumber: number;
|
|
57
|
+
/** The issue as the caller last read it; any difference before the write is `stale`. */
|
|
58
|
+
expected: {
|
|
59
|
+
updatedAt: string;
|
|
60
|
+
bodyDigest?: string;
|
|
61
|
+
state?: "open" | "closed";
|
|
62
|
+
};
|
|
63
|
+
changes: {
|
|
64
|
+
title?: string;
|
|
65
|
+
body?: string;
|
|
66
|
+
state?: "open" | "closed";
|
|
67
|
+
stateReason?: "completed" | "not_planned" | "reopened";
|
|
68
|
+
labels?: readonly string[];
|
|
69
|
+
};
|
|
70
|
+
} | {
|
|
71
|
+
kind: "comment.update";
|
|
72
|
+
operationId: string;
|
|
73
|
+
commentId: number;
|
|
74
|
+
/** The comment as the caller last read it; any difference before the write is `stale`. */
|
|
75
|
+
expected: {
|
|
76
|
+
issueNumber: number;
|
|
77
|
+
updatedAt: string;
|
|
78
|
+
bodyDigest?: string;
|
|
79
|
+
author?: string;
|
|
80
|
+
};
|
|
81
|
+
body: string;
|
|
82
|
+
};
|
|
83
|
+
export type WriteKind = WriteOperation["kind"];
|
|
84
|
+
/** The capability an operation needs. A review's event chooses among three. */
|
|
85
|
+
export declare function capabilityOf(op: Pick<WriteOperation, "kind"> & {
|
|
86
|
+
event?: ReviewEvent;
|
|
87
|
+
}): WriteCapability;
|
|
88
|
+
export type DescriptorTarget = {
|
|
89
|
+
type: "repository";
|
|
90
|
+
} | {
|
|
91
|
+
type: "issue";
|
|
92
|
+
number: number;
|
|
93
|
+
} | {
|
|
94
|
+
type: "pull";
|
|
95
|
+
number: number;
|
|
96
|
+
} | {
|
|
97
|
+
type: "comment";
|
|
98
|
+
id: number;
|
|
99
|
+
} | {
|
|
100
|
+
type: "branch";
|
|
101
|
+
head: string;
|
|
102
|
+
base: string;
|
|
103
|
+
};
|
|
104
|
+
/** What the consumer authorizes. Deeply frozen; `digest` covers every other field. */
|
|
105
|
+
export interface OperationDescriptor {
|
|
106
|
+
readonly schema: "comforge-github-write/v1";
|
|
107
|
+
readonly operationId: string;
|
|
108
|
+
readonly kind: WriteKind;
|
|
109
|
+
readonly capability: WriteCapability;
|
|
110
|
+
readonly repository: string;
|
|
111
|
+
readonly target: Readonly<DescriptorTarget>;
|
|
112
|
+
/** The exact JSON body the writer sends, before its correlation marker is appended. */
|
|
113
|
+
readonly payload: Readonly<Record<string, unknown>>;
|
|
114
|
+
/** What must hold on GitHub immediately before the write. */
|
|
115
|
+
readonly precondition: Readonly<Record<string, unknown>>;
|
|
116
|
+
/** `sha256:` of the canonical JSON of every field above. */
|
|
117
|
+
readonly digest: string;
|
|
118
|
+
}
|
|
119
|
+
/** GitHub's documented body limit for issues, comments, reviews and pull requests. */
|
|
120
|
+
export declare const MAX_BODY_CHARS = 65536;
|
|
121
|
+
export declare const MAX_TITLE_CHARS = 256;
|
|
122
|
+
/** Reserved for the writer's own correlation marker; refused in caller text. */
|
|
123
|
+
export declare const MARKER_PREFIX = "<!-- comforge-op ";
|
|
124
|
+
/** A branch name git and GitHub both accept, kept to a conservative subset. */
|
|
125
|
+
export declare function branch(value: unknown, what: string): string;
|
|
126
|
+
/** JSON with object keys sorted at every depth: the digest input. */
|
|
127
|
+
export declare function canonicalJson(value: unknown): string;
|
|
128
|
+
/** `sha256:<hex>` of a string's UTF-8 bytes, through Web Crypto (Node ≥ 22 and Bun). */
|
|
129
|
+
export declare function sha256(input: string): Promise<string>;
|
|
130
|
+
/** The digest a caller passes as `expected.bodyDigest`: of the body exactly as GitHub returned it. */
|
|
131
|
+
export declare const bodyDigest: (body: string | null | undefined) => Promise<string>;
|
|
132
|
+
/**
|
|
133
|
+
* Validates an operation and returns its descriptor. Only the named fields are read, each is
|
|
134
|
+
* copied, and the result is frozen: mutating the caller's object afterwards changes nothing.
|
|
135
|
+
*/
|
|
136
|
+
export declare function describe(repository: string, input: WriteOperation): Promise<OperationDescriptor>;
|
|
137
|
+
/**
|
|
138
|
+
* The correlation marker appended to a created body: digests only, never the raw operation id.
|
|
139
|
+
* Anyone who can write to the repository can copy it, so a match is evidence that needs the
|
|
140
|
+
* author check (`expectedAuthor`), not proof.
|
|
141
|
+
*/
|
|
142
|
+
export declare function correlationMarker(namespace: string, descriptor: OperationDescriptor): Promise<string>;
|
|
143
|
+
/** True when `body` carries exactly this operation's marker (same operation, same fingerprint). */
|
|
144
|
+
export declare function carriesMarker(body: string | null | undefined, marker: string): boolean;
|
|
145
|
+
/** The body sent for a create: the authorized body, a blank line, and the marker. */
|
|
146
|
+
export declare function markedBody(body: string, marker: string | null): string;
|
|
147
|
+
export declare const isSha: (value: unknown) => value is string;
|