@moo.md/hum 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/defaults/hum.json +321 -0
- package/defaults/kinds/check.json +183 -0
- package/defaults/kinds/docs.json +151 -0
- package/defaults/kinds/feature.json +202 -0
- package/defaults/kinds/fix.json +183 -0
- package/defaults/kinds/hotfix.json +164 -0
- package/defaults/roles/build.md +3 -0
- package/defaults/roles/fix.md +1 -0
- package/defaults/roles/plan.md +3 -0
- package/defaults/roles/review.md +3 -0
- package/evals/alignment/RESULTS.md +8 -0
- package/evals/alignment/cases.jsonl +128 -0
- package/evals/alignment/sample.jsonl +20 -0
- package/evals/kind/RESULTS.md +5 -0
- package/evals/kind/cases.jsonl +24 -0
- package/evals/plan-outcome/RESULTS.md +4 -0
- package/evals/plan-outcome/cases.jsonl +12 -0
- package/evals/repeat-pair/RESULTS.md +4 -0
- package/evals/repeat-pair/cases.jsonl +51 -0
- package/evals/reply-option/RESULTS.md +3 -0
- package/evals/reply-option/cases.jsonl +45 -0
- package/evals/review-verdict/RESULTS.md +4 -0
- package/evals/review-verdict/cases.jsonl +12 -0
- package/package.json +88 -4
- package/schema/hum.schema.json +1355 -0
- package/scripts/eval.ts +244 -0
- package/src/adapter-api.ts +134 -0
- package/src/adapters/admit.ts +111 -0
- package/src/adapters/argument.ts +11 -0
- package/src/adapters/claim.ts +93 -0
- package/src/adapters/claude-bg.ts +392 -0
- package/src/adapters/claude-p.ts +599 -0
- package/src/adapters/contract.ts +231 -0
- package/src/adapters/fake.ts +157 -0
- package/src/adapters/git.ts +156 -0
- package/src/adapters/github.ts +479 -0
- package/src/adapters/guard.ts +152 -0
- package/src/adapters/history.ts +215 -0
- package/src/adapters/item-file.ts +102 -0
- package/src/adapters/limit.ts +67 -0
- package/src/adapters/markdown.ts +365 -0
- package/src/adapters/marked.ts +127 -0
- package/src/adapters/model-call.ts +75 -0
- package/src/adapters/once.ts +45 -0
- package/src/adapters/owner-commit.ts +60 -0
- package/src/adapters/pull-requests.ts +253 -0
- package/src/adapters/registry.ts +156 -0
- package/src/adapters/roundup.ts +293 -0
- package/src/adapters/serial.ts +22 -0
- package/src/adapters/transcript.ts +99 -0
- package/src/cli/commands.ts +46 -0
- package/src/cli/main.ts +8 -0
- package/src/cli/ops.ts +224 -0
- package/src/cli/service.ts +156 -0
- package/src/cli/setup.ts +141 -0
- package/src/cli/shared-watch.ts +127 -0
- package/src/config/change.ts +54 -0
- package/src/config/checks.ts +217 -0
- package/src/config/git-ref-name.d.ts +3 -0
- package/src/config/hash.ts +6 -0
- package/src/config/json.ts +6 -0
- package/src/config/load.ts +88 -0
- package/src/config/machine.ts +20 -0
- package/src/config/migrate.ts +61 -0
- package/src/config/refusal.ts +58 -0
- package/src/config/schema.ts +465 -0
- package/src/config/settings.ts +6 -0
- package/src/config/state-map.ts +76 -0
- package/src/decide/alignment.ts +79 -0
- package/src/decide/answer.ts +131 -0
- package/src/decide/kind.ts +39 -0
- package/src/decide/labels.ts +137 -0
- package/src/decide/merge.ts +102 -0
- package/src/decide/prose.ts +109 -0
- package/src/decide/question.ts +159 -0
- package/src/decide/review.ts +10 -0
- package/src/decide/risk.ts +216 -0
- package/src/decide/taste.ts +37 -0
- package/src/exec.ts +85 -0
- package/src/index.ts +200 -0
- package/src/loop/breaks.ts +8 -0
- package/src/loop/check-item.ts +76 -0
- package/src/loop/ci.ts +8 -0
- package/src/loop/github.ts +81 -0
- package/src/loop/groups.ts +108 -0
- package/src/loop/memory.ts +70 -0
- package/src/loop/notes.ts +76 -0
- package/src/loop/pair.ts +39 -0
- package/src/loop/pass.ts +99 -0
- package/src/loop/proof.ts +85 -0
- package/src/loop/prove-item.ts +33 -0
- package/src/loop/records.ts +75 -0
- package/src/loop/signals.ts +130 -0
- package/src/measure/inputs.ts +28 -0
- package/src/measure/measure.ts +68 -0
- package/src/service/cards.ts +24 -0
- package/src/service/ci-tick.ts +28 -0
- package/src/service/launchd.ts +16 -0
- package/src/service/supervise.ts +145 -0
- package/src/service/systemd.ts +44 -0
- package/src/service/token.ts +21 -0
- package/src/service/unit.ts +29 -0
- package/src/service/wake.ts +30 -0
- package/src/service/workflows.ts +97 -0
- package/src/setup/propose.ts +120 -0
- package/src/setup/prove.ts +216 -0
- package/src/setup/scan.ts +186 -0
- package/src/setup/scratch.ts +81 -0
- package/src/setup/shipped.ts +15 -0
- package/src/setup/tables.ts +101 -0
- package/src/setup/waits.ts +58 -0
- package/src/setup/write.ts +161 -0
- package/src/slots/env.ts +42 -0
- package/src/slots/pool.ts +329 -0
- package/src/slots/scan.ts +26 -0
- package/src/slots/worktree.ts +17 -0
- package/src/state/events.ts +52 -0
- package/src/state/faults.ts +73 -0
- package/src/state/format.ts +50 -0
- package/src/state/fs.ts +100 -0
- package/src/state/items.ts +195 -0
- package/src/state/open.ts +80 -0
- package/src/state/ops.ts +76 -0
- package/src/state/records.ts +35 -0
- package/src/state/runs.ts +99 -0
- package/src/state/saved.ts +119 -0
- package/src/status/file.ts +49 -0
- package/src/status/format.ts +132 -0
- package/src/status/sections.ts +14 -0
- package/src/status/version.ts +2 -0
- package/src/time.ts +7 -0
- package/src/worker/alignment-report.ts +18 -0
- package/src/worker/answers.ts +153 -0
- package/src/worker/base-check.ts +45 -0
- package/src/worker/checks.ts +40 -0
- package/src/worker/kind.ts +68 -0
- package/src/worker/land.ts +509 -0
- package/src/worker/lifecycle.ts +97 -0
- package/src/worker/merged.ts +29 -0
- package/src/worker/open.ts +100 -0
- package/src/worker/prose-run.ts +56 -0
- package/src/worker/questions.ts +89 -0
- package/src/worker/scheduler.ts +135 -0
- package/src/worker/signature.ts +41 -0
- package/src/worker/tally.ts +25 -0
- package/src/worker/taste.ts +38 -0
- package/src/worker/thin-path.ts +886 -0
- package/src/worker/worker.ts +750 -0
- package/surface.json +8787 -0
- package/README.md +0 -3
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { GraphqlResponseError } from "@octokit/graphql";
|
|
2
|
+
import pMemoize from "p-memoize";
|
|
3
|
+
import { z } from "zod";
|
|
4
|
+
import type { Json } from "../config/json.ts";
|
|
5
|
+
import { MERGE_METHODS, type MergeMethod } from "../config/schema.ts";
|
|
6
|
+
import {
|
|
7
|
+
type GitHubClient,
|
|
8
|
+
gitHubClient,
|
|
9
|
+
MERGE_METHOD,
|
|
10
|
+
ownerToken,
|
|
11
|
+
raise,
|
|
12
|
+
} from "./github.ts";
|
|
13
|
+
|
|
14
|
+
/** What the target branch's rules ask of a change that lands on it. */
|
|
15
|
+
export type BranchRules = {
|
|
16
|
+
readonly requiresPullRequest: boolean;
|
|
17
|
+
/** The merge methods a pull request may use; undefined when no rule narrows them. */
|
|
18
|
+
readonly mergeMethods: readonly MergeMethod[] | undefined;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type Pull = {
|
|
22
|
+
readonly id: string;
|
|
23
|
+
readonly number: number;
|
|
24
|
+
readonly url: string;
|
|
25
|
+
readonly state: "OPEN" | "MERGED" | "CLOSED";
|
|
26
|
+
readonly autoMerge: boolean;
|
|
27
|
+
/** Its branch is behind the base, which a strict required check makes wait. */
|
|
28
|
+
readonly behind: boolean;
|
|
29
|
+
};
|
|
30
|
+
|
|
31
|
+
/** Pull requests on the repo a run's changes land in. */
|
|
32
|
+
export type PullRequests = {
|
|
33
|
+
/** The rules read once and kept; `fresh` reads them again, as after a push the remote refused. */
|
|
34
|
+
readonly rules: (base: string, fresh?: boolean) => Promise<BranchRules>;
|
|
35
|
+
/** The newest pull request from `head`, in any state. */
|
|
36
|
+
readonly find: (head: string) => Promise<Pull | undefined>;
|
|
37
|
+
readonly open: (change: {
|
|
38
|
+
head: string;
|
|
39
|
+
base: string;
|
|
40
|
+
title: string;
|
|
41
|
+
body: string;
|
|
42
|
+
}) => Promise<Pull>;
|
|
43
|
+
/** Turns auto-merge on or off. A pull request whose checks already passed takes no auto-merge, so it merges now. */
|
|
44
|
+
readonly autoMerge: (
|
|
45
|
+
pull: Pull,
|
|
46
|
+
on: boolean,
|
|
47
|
+
method: MergeMethod,
|
|
48
|
+
) => Promise<"armed" | "merged" | "off">;
|
|
49
|
+
readonly update: (pull: Pull) => Promise<void>;
|
|
50
|
+
/** One comment on the pull request. */
|
|
51
|
+
readonly comment: (pull: Pull, body: string) => Promise<void>;
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
const Rule = z.looseObject({
|
|
55
|
+
type: z.string(),
|
|
56
|
+
parameters: z
|
|
57
|
+
.looseObject({
|
|
58
|
+
allowed_merge_methods: z.array(z.enum(MERGE_METHODS)).optional(),
|
|
59
|
+
})
|
|
60
|
+
.optional(),
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
const PULL_REQUEST_RULE = "pull_request";
|
|
64
|
+
|
|
65
|
+
const PullNode = z.object({
|
|
66
|
+
id: z.string(),
|
|
67
|
+
number: z.int(),
|
|
68
|
+
url: z.string(),
|
|
69
|
+
state: z.enum(["OPEN", "MERGED", "CLOSED"]),
|
|
70
|
+
mergeStateStatus: z.string(),
|
|
71
|
+
autoMergeRequest: z.object({ enabledAt: z.string().nullable() }).nullable(),
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
const PULL_FIELDS =
|
|
75
|
+
"id number url state mergeStateStatus autoMergeRequest { enabledAt }";
|
|
76
|
+
|
|
77
|
+
const BEHIND = "BEHIND";
|
|
78
|
+
|
|
79
|
+
const PULLS = `query PullsFrom($owner: String!, $name: String!, $head: String!) {
|
|
80
|
+
repository(owner: $owner, name: $name) { id pullRequests(headRefName: $head, first: 1, orderBy: { field: CREATED_AT, direction: DESC }) { nodes { ${PULL_FIELDS} } } }
|
|
81
|
+
}`;
|
|
82
|
+
|
|
83
|
+
const OPEN_PULL = `mutation OpenPull($repository: ID!, $base: String!, $head: String!, $title: String!, $body: String!) {
|
|
84
|
+
createPullRequest(input: { repositoryId: $repository, baseRefName: $base, headRefName: $head, title: $title, body: $body }) { pullRequest { ${PULL_FIELDS} } }
|
|
85
|
+
}`;
|
|
86
|
+
|
|
87
|
+
const ENABLE_AUTO_MERGE = `mutation EnableAutoMerge($pull: ID!, $mergeMethod: PullRequestMergeMethod!) {
|
|
88
|
+
enablePullRequestAutoMerge(input: { pullRequestId: $pull, mergeMethod: $mergeMethod }) { clientMutationId }
|
|
89
|
+
}`;
|
|
90
|
+
|
|
91
|
+
const DISABLE_AUTO_MERGE = `mutation DisableAutoMerge($pull: ID!) {
|
|
92
|
+
disablePullRequestAutoMerge(input: { pullRequestId: $pull }) { clientMutationId }
|
|
93
|
+
}`;
|
|
94
|
+
|
|
95
|
+
const MERGE_NOW = `mutation MergeNow($pull: ID!, $mergeMethod: PullRequestMergeMethod!) {
|
|
96
|
+
mergePullRequest(input: { pullRequestId: $pull, mergeMethod: $mergeMethod }) { clientMutationId }
|
|
97
|
+
}`;
|
|
98
|
+
|
|
99
|
+
const COMMENT = `mutation Comment($pull: ID!, $body: String!) {
|
|
100
|
+
addComment(input: { subjectId: $pull, body: $body }) { clientMutationId }
|
|
101
|
+
}`;
|
|
102
|
+
|
|
103
|
+
const UPDATE_BRANCH = `mutation UpdateBranch($pull: ID!) {
|
|
104
|
+
updatePullRequestBranch(input: { pullRequestId: $pull }) { clientMutationId }
|
|
105
|
+
}`;
|
|
106
|
+
|
|
107
|
+
const PullsReply = z.object({
|
|
108
|
+
repository: z.object({
|
|
109
|
+
id: z.string(),
|
|
110
|
+
pullRequests: z.object({ nodes: z.array(PullNode) }),
|
|
111
|
+
}),
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
const Opened = z.object({
|
|
115
|
+
createPullRequest: z.object({ pullRequest: PullNode }),
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
const Acknowledged = z.looseObject({});
|
|
119
|
+
|
|
120
|
+
/** GitHub refuses auto-merge on a pull request that may merge now. */
|
|
121
|
+
const isMergeableNow = (cause: unknown): boolean =>
|
|
122
|
+
cause instanceof GraphqlResponseError && /clean status/i.test(cause.message);
|
|
123
|
+
|
|
124
|
+
const pullOf = (node: z.infer<typeof PullNode>): Pull => ({
|
|
125
|
+
id: node.id,
|
|
126
|
+
number: node.number,
|
|
127
|
+
url: node.url,
|
|
128
|
+
state: node.state,
|
|
129
|
+
autoMerge: node.autoMergeRequest !== null,
|
|
130
|
+
behind: node.mergeStateStatus === BEHIND,
|
|
131
|
+
});
|
|
132
|
+
|
|
133
|
+
/** The method the owner set when the branch's rules allow it, else the first they allow. */
|
|
134
|
+
export const methodFor = (
|
|
135
|
+
preferred: MergeMethod,
|
|
136
|
+
allowed: readonly MergeMethod[] | undefined,
|
|
137
|
+
): MergeMethod =>
|
|
138
|
+
allowed === undefined || allowed.includes(preferred)
|
|
139
|
+
? preferred
|
|
140
|
+
: (allowed[0] ?? preferred);
|
|
141
|
+
|
|
142
|
+
/** Pull requests and branch rules of a GitHub repo, on the owner's own login unless a client is given. `timeoutMs` bounds the `gh auth token` that reads that login. */
|
|
143
|
+
export const gitHubPullRequests = (
|
|
144
|
+
repo: string,
|
|
145
|
+
{ timeoutMs, client }: { readonly timeoutMs: number; readonly client?: GitHubClient },
|
|
146
|
+
): PullRequests => {
|
|
147
|
+
const [owner = "", name = ""] = repo.split("/");
|
|
148
|
+
|
|
149
|
+
const connect = pMemoize(
|
|
150
|
+
async () => client ?? gitHubClient({ token: await ownerToken(timeoutMs), timeoutMs }),
|
|
151
|
+
);
|
|
152
|
+
|
|
153
|
+
const rulesCache = new Map<string, BranchRules>();
|
|
154
|
+
|
|
155
|
+
const graphql = async <T>(
|
|
156
|
+
schema: z.ZodType<T>,
|
|
157
|
+
document: string,
|
|
158
|
+
variables: Readonly<Record<string, Json>>,
|
|
159
|
+
): Promise<T> =>
|
|
160
|
+
schema.parse(
|
|
161
|
+
await (
|
|
162
|
+
await connect()
|
|
163
|
+
)
|
|
164
|
+
.graphql(document, { owner, name, ...variables })
|
|
165
|
+
.catch(raise),
|
|
166
|
+
);
|
|
167
|
+
|
|
168
|
+
const readRules = pMemoize(
|
|
169
|
+
async (base: string): Promise<BranchRules> => {
|
|
170
|
+
const reply = await (
|
|
171
|
+
await connect()
|
|
172
|
+
)
|
|
173
|
+
.request("GET /repos/{owner}/{repo}/rules/branches/{branch}", {
|
|
174
|
+
owner,
|
|
175
|
+
repo: name,
|
|
176
|
+
branch: base,
|
|
177
|
+
})
|
|
178
|
+
.catch(raise);
|
|
179
|
+
|
|
180
|
+
const rules = z.array(Rule).parse(reply.data);
|
|
181
|
+
const pullRule = rules.find((rule) => rule.type === PULL_REQUEST_RULE);
|
|
182
|
+
|
|
183
|
+
return {
|
|
184
|
+
requiresPullRequest: pullRule !== undefined,
|
|
185
|
+
mergeMethods: pullRule?.parameters?.allowed_merge_methods,
|
|
186
|
+
};
|
|
187
|
+
},
|
|
188
|
+
{ cache: rulesCache },
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
const find = async (head: string): Promise<Pull | undefined> => {
|
|
192
|
+
const node = (await graphql(PullsReply, PULLS, { head })).repository
|
|
193
|
+
.pullRequests.nodes[0];
|
|
194
|
+
|
|
195
|
+
return node && pullOf(node);
|
|
196
|
+
};
|
|
197
|
+
|
|
198
|
+
return {
|
|
199
|
+
rules: (base, fresh = false) => {
|
|
200
|
+
if (fresh) rulesCache.delete(base);
|
|
201
|
+
|
|
202
|
+
return readRules(base);
|
|
203
|
+
},
|
|
204
|
+
find,
|
|
205
|
+
open: async ({ head, base, title, body }) => {
|
|
206
|
+
const repository = (await graphql(PullsReply, PULLS, { head })).repository
|
|
207
|
+
.id;
|
|
208
|
+
|
|
209
|
+
return pullOf(
|
|
210
|
+
(
|
|
211
|
+
await graphql(Opened, OPEN_PULL, {
|
|
212
|
+
repository,
|
|
213
|
+
base,
|
|
214
|
+
head,
|
|
215
|
+
title,
|
|
216
|
+
body,
|
|
217
|
+
})
|
|
218
|
+
).createPullRequest.pullRequest,
|
|
219
|
+
);
|
|
220
|
+
},
|
|
221
|
+
autoMerge: async (pull, on, method) => {
|
|
222
|
+
if (!on) {
|
|
223
|
+
if (pull.autoMerge)
|
|
224
|
+
await graphql(Acknowledged, DISABLE_AUTO_MERGE, { pull: pull.id });
|
|
225
|
+
|
|
226
|
+
return "off";
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
if (pull.autoMerge) return "armed";
|
|
230
|
+
|
|
231
|
+
const mergeMethod = MERGE_METHOD[method];
|
|
232
|
+
|
|
233
|
+
const armed = await (
|
|
234
|
+
await connect()
|
|
235
|
+
)
|
|
236
|
+
.graphql(ENABLE_AUTO_MERGE, { pull: pull.id, mergeMethod })
|
|
237
|
+
.then(
|
|
238
|
+
() => true,
|
|
239
|
+
(cause: unknown) => (isMergeableNow(cause) ? false : raise(cause)),
|
|
240
|
+
);
|
|
241
|
+
|
|
242
|
+
if (armed) return "armed";
|
|
243
|
+
|
|
244
|
+
await graphql(Acknowledged, MERGE_NOW, { pull: pull.id, mergeMethod });
|
|
245
|
+
|
|
246
|
+
return "merged";
|
|
247
|
+
},
|
|
248
|
+
update: async (pull) =>
|
|
249
|
+
void (await graphql(Acknowledged, UPDATE_BRANCH, { pull: pull.id })),
|
|
250
|
+
comment: async (pull, body) =>
|
|
251
|
+
void (await graphql(Acknowledged, COMMENT, { pull: pull.id, body })),
|
|
252
|
+
};
|
|
253
|
+
};
|
|
@@ -0,0 +1,156 @@
|
|
|
1
|
+
import { existsSync } from "node:fs";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
import { pathToFileURL } from "node:url";
|
|
4
|
+
import { z } from "zod";
|
|
5
|
+
import { ADAPTER_API_VERSION, type Adapter, type AdapterContext, type AdapterFactory, PermanentError } from "../adapter-api.ts";
|
|
6
|
+
import { readSettings } from "../config/settings.ts";
|
|
7
|
+
import { HumConfig } from "../config/schema.ts";
|
|
8
|
+
import { commandTimeoutMs } from "../exec.ts";
|
|
9
|
+
import type { Faults } from "../state/faults.ts";
|
|
10
|
+
import type { Card } from "../status/format.ts";
|
|
11
|
+
import { githubAdapter, repoFromRemote } from "./github.ts";
|
|
12
|
+
import { gitIn, whenGitFailed } from "./git.ts";
|
|
13
|
+
import { type GuardOptions, guardTimings, guarded } from "./guard.ts";
|
|
14
|
+
import { markdownAdapter } from "./markdown.ts";
|
|
15
|
+
import { gitHubPullRequests, type PullRequests } from "./pull-requests.ts";
|
|
16
|
+
import { roundupAdapter } from "./roundup.ts";
|
|
17
|
+
|
|
18
|
+
/** The integration slots `hum.json` `integrations` names, each to an adapter. */
|
|
19
|
+
export const SLOTS = ["work-source", "questions", "answers", "release"] as const;
|
|
20
|
+
|
|
21
|
+
export type Slot = (typeof SLOTS)[number];
|
|
22
|
+
|
|
23
|
+
const MARKDOWN = "markdown";
|
|
24
|
+
const FALLBACK_BASE = "main";
|
|
25
|
+
|
|
26
|
+
/** Slots that use the markdown source when `hum.json` names none; release has no default. */
|
|
27
|
+
const MARKDOWN_SLOTS: readonly Slot[] = ["work-source", "questions", "answers"];
|
|
28
|
+
|
|
29
|
+
/** What the adapters read from `hum.json`; every other key belongs to the worker. */
|
|
30
|
+
const AdapterSettings = z.looseObject({
|
|
31
|
+
integrations: z.partialRecord(z.enum(SLOTS), z.string().min(1)).optional(),
|
|
32
|
+
claimPrefix: HumConfig.shape.claimPrefix,
|
|
33
|
+
itemsDir: HumConfig.shape.itemsDir,
|
|
34
|
+
commitIdentity: HumConfig.shape.commitIdentity,
|
|
35
|
+
readyLabel: HumConfig.shape.readyLabel,
|
|
36
|
+
mergeMethod: HumConfig.shape.mergeMethod,
|
|
37
|
+
ownerBranchPrefix: HumConfig.shape.ownerBranchPrefix,
|
|
38
|
+
writeRaceRetries: HumConfig.shape.writeRaceRetries,
|
|
39
|
+
itemReadsAtOnce: HumConfig.shape.itemReadsAtOnce,
|
|
40
|
+
providerTimeoutSeconds: HumConfig.shape.providerTimeoutSeconds,
|
|
41
|
+
providerBackoff: HumConfig.shape.providerBackoff,
|
|
42
|
+
commandTimeoutSeconds: HumConfig.shape.commandTimeoutSeconds,
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
type AdapterSettings = z.infer<typeof AdapterSettings>;
|
|
46
|
+
|
|
47
|
+
const AdapterModule = z.looseObject({ default: z.custom<AdapterFactory>((value) => value instanceof Function, "must export an adapter factory as its default") });
|
|
48
|
+
|
|
49
|
+
/** The adapter a slot uses: the one `hum.json` names, else the markdown source where that is the default. */
|
|
50
|
+
const adapterNameFor = (slot: Slot, integrations: AdapterSettings["integrations"]): string | undefined =>
|
|
51
|
+
integrations?.[slot] ?? (MARKDOWN_SLOTS.includes(slot) ? MARKDOWN : undefined);
|
|
52
|
+
|
|
53
|
+
type Repo = { readonly path: string; readonly settings: AdapterSettings; readonly context: AdapterContext };
|
|
54
|
+
|
|
55
|
+
const defaultBase = async (repo: string, timeoutMs: number): Promise<string> => {
|
|
56
|
+
const head = await gitIn(repo, { timeoutMs })
|
|
57
|
+
.raw(["symbolic-ref", "--short", "refs/remotes/origin/HEAD"])
|
|
58
|
+
.catch(whenGitFailed(`origin/${FALLBACK_BASE}`));
|
|
59
|
+
|
|
60
|
+
return head.trim().replace(/^origin\//, "");
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
/** The pull requests of the repo's `origin` when it is on GitHub. */
|
|
64
|
+
const originPulls = async (repo: string, timeoutMs: number): Promise<PullRequests | undefined> => {
|
|
65
|
+
const found = repoFromRemote(await gitIn(repo, { timeoutMs }).raw(["remote", "get-url", "origin"]).catch(whenGitFailed("")));
|
|
66
|
+
|
|
67
|
+
return found === undefined ? undefined : gitHubPullRequests(found, { timeoutMs });
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
const githubRepo = async (repo: string, timeoutMs: number): Promise<string> => {
|
|
71
|
+
const found = repoFromRemote(await gitIn(repo, { timeoutMs }).raw(["remote", "get-url", "origin"]));
|
|
72
|
+
|
|
73
|
+
if (!found) throw new PermanentError("the github adapter needs an `origin` remote on github.com");
|
|
74
|
+
|
|
75
|
+
return found;
|
|
76
|
+
};
|
|
77
|
+
|
|
78
|
+
/** The adapters the engine ships, by the name `hum.json` gives them. */
|
|
79
|
+
const BUILT_IN = {
|
|
80
|
+
[MARKDOWN]: async ({ path, settings, context }) => {
|
|
81
|
+
const timeoutMs = commandTimeoutMs(settings);
|
|
82
|
+
|
|
83
|
+
return markdownAdapter({ repo: path, base: await defaultBase(path, timeoutMs), claimPrefix: settings.claimPrefix, itemsDir: settings.itemsDir, identity: settings.commitIdentity, writeRaceRetries: settings.writeRaceRetries, itemReadsAtOnce: settings.itemReadsAtOnce, pulls: await originPulls(path, timeoutMs), mergeMethod: settings.mergeMethod, ownerBranchPrefix: settings.ownerBranchPrefix, timeoutMs }, context);
|
|
84
|
+
},
|
|
85
|
+
github: async ({ path, settings, context }) => {
|
|
86
|
+
const timeoutMs = commandTimeoutMs(settings);
|
|
87
|
+
|
|
88
|
+
return githubAdapter({ repo: await githubRepo(path, timeoutMs), base: await defaultBase(path, timeoutMs), claimPrefix: settings.claimPrefix, readyLabel: settings.readyLabel, mergeMethod: settings.mergeMethod, timeoutMs }, context);
|
|
89
|
+
},
|
|
90
|
+
roundup: async ({ context }) => roundupAdapter({}, context),
|
|
91
|
+
} satisfies Readonly<Record<string, (repo: Repo) => Promise<Adapter>>>;
|
|
92
|
+
|
|
93
|
+
const isBuiltIn = (name: string): name is keyof typeof BUILT_IN => Object.hasOwn(BUILT_IN, name);
|
|
94
|
+
|
|
95
|
+
/** An owner's adapter: the default export of `adapters/<name>.ts` in the repo. */
|
|
96
|
+
const ownerAdapter = async (name: string, { path, context }: Repo): Promise<Adapter> => {
|
|
97
|
+
const file = join(path, "adapters", `${name}.ts`);
|
|
98
|
+
|
|
99
|
+
if (!existsSync(file)) throw new PermanentError(`no adapter "${name}": the engine ships ${Object.keys(BUILT_IN).join(", ")}, and ${file} does not exist`);
|
|
100
|
+
|
|
101
|
+
const loaded = AdapterModule.safeParse(await import(pathToFileURL(file).href));
|
|
102
|
+
|
|
103
|
+
if (!loaded.success) throw new PermanentError(`${file}: ${loaded.error.issues[0]?.message}`);
|
|
104
|
+
|
|
105
|
+
return loaded.data.default(context);
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
/** The card that refuses an adapter written for another API version; undefined for one written for this engine. */
|
|
109
|
+
export const refuseOtherVersion = (name: string, adapter: Adapter, faults: Faults): Card | undefined => {
|
|
110
|
+
if (adapter.apiVersion === ADAPTER_API_VERSION) return undefined;
|
|
111
|
+
|
|
112
|
+
return faults.raise({
|
|
113
|
+
code: "adapter-api-mismatch",
|
|
114
|
+
item: name,
|
|
115
|
+
cause: `Adapter "${name}" implements adapter API ${adapter.apiVersion}; this engine runs ${ADAPTER_API_VERSION}.`,
|
|
116
|
+
fix: `Update ${name} to adapter API ${ADAPTER_API_VERSION}.`,
|
|
117
|
+
});
|
|
118
|
+
};
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* The adapter `hum.json` names for each slot, guarded: one the engine ships, else an owner's `adapters/<name>.ts`.
|
|
122
|
+
* A slot with no entry uses the markdown source in `repo`, except release, which has no default. Slots that name
|
|
123
|
+
* the same adapter share one instance.
|
|
124
|
+
*/
|
|
125
|
+
export const resolveAdapters = async (
|
|
126
|
+
repo: string,
|
|
127
|
+
{ faults, guard, context }: { faults: Faults; guard: Pick<GuardOptions, "lastReady">; context: AdapterContext },
|
|
128
|
+
): Promise<Partial<Record<Slot, Adapter>>> => {
|
|
129
|
+
const settings = readSettings(repo, AdapterSettings);
|
|
130
|
+
|
|
131
|
+
const slots = SLOTS.flatMap((slot) => {
|
|
132
|
+
const name = adapterNameFor(slot, settings.integrations);
|
|
133
|
+
|
|
134
|
+
return name === undefined ? [] : [[slot, name] as const];
|
|
135
|
+
});
|
|
136
|
+
|
|
137
|
+
const source: Repo = { path: repo, settings, context };
|
|
138
|
+
|
|
139
|
+
const build = async (name: string): Promise<Adapter> => {
|
|
140
|
+
const adapter = await (isBuiltIn(name) ? BUILT_IN[name](source) : ownerAdapter(name, source));
|
|
141
|
+
|
|
142
|
+
if (refuseOtherVersion(name, adapter, faults)) throw new PermanentError(`adapter "${name}" implements another adapter API version`);
|
|
143
|
+
|
|
144
|
+
return guarded(adapter, {
|
|
145
|
+
...guard,
|
|
146
|
+
provider: name,
|
|
147
|
+
faults,
|
|
148
|
+
...guardTimings(settings),
|
|
149
|
+
});
|
|
150
|
+
};
|
|
151
|
+
|
|
152
|
+
const names = [...new Set(slots.map(([, name]) => name))];
|
|
153
|
+
const built = new Map(await Promise.all(names.map(async (name) => [name, await build(name)] as const)));
|
|
154
|
+
|
|
155
|
+
return Object.fromEntries(slots.map(([slot, name]) => [slot, built.get(name)]));
|
|
156
|
+
};
|