@notation/factory 0.2.0 → 0.2.2
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/deployer/index.ts +12 -5
- package/implementations/outpost-secret.ts +35 -0
- package/index.ts +2 -0
- package/lib/repos.test.ts +17 -0
- package/lib/repos.ts +19 -0
- package/package.json +1 -1
- package/router/channel.ts +8 -3
- package/router/handle.test.ts +2 -2
- package/router/handle.ts +3 -9
- package/router/index.ts +1 -0
package/deployer/index.ts
CHANGED
|
@@ -22,9 +22,16 @@ export interface DeployerContext {
|
|
|
22
22
|
/**
|
|
23
23
|
* Stores the repo's outpost config, where the router reads it, and the
|
|
24
24
|
* `.factory/` files it was evaluated from. `ref` is the commit they were
|
|
25
|
-
* read at
|
|
25
|
+
* read at, and `executables` are the paths of the files that are
|
|
26
|
+
* executable. Resolves to the containers whose images started building.
|
|
26
27
|
*/
|
|
27
|
-
putOutpost(
|
|
28
|
+
putOutpost(
|
|
29
|
+
repo: string,
|
|
30
|
+
outpost: OutpostConfig,
|
|
31
|
+
files: Record<string, string>,
|
|
32
|
+
ref: string,
|
|
33
|
+
executables?: string[],
|
|
34
|
+
): Promise<string[]>;
|
|
28
35
|
/** The `.factory/` files of the repo's outpost. */
|
|
29
36
|
getFiles(repo: string): Promise<Record<string, string> | undefined>;
|
|
30
37
|
deleteOutpost(repo: string): Promise<void>;
|
|
@@ -136,7 +143,7 @@ const message = (error: unknown) => (error instanceof Error ? error.message : St
|
|
|
136
143
|
*/
|
|
137
144
|
export const deploy = async (
|
|
138
145
|
repo: string,
|
|
139
|
-
{ ref, files }: SourceFiles,
|
|
146
|
+
{ ref, files, executables }: SourceFiles,
|
|
140
147
|
context: DeployerContext,
|
|
141
148
|
): Promise<FactoryStatus> => {
|
|
142
149
|
if (!("infra.ts" in files)) {
|
|
@@ -144,8 +151,8 @@ export const deploy = async (
|
|
|
144
151
|
return { state: "failed", ref, error: `There's no ${FACTORY_DIR}/infra.ts.`, at: new Date().toISOString() };
|
|
145
152
|
}
|
|
146
153
|
try {
|
|
147
|
-
const
|
|
148
|
-
return { state: "deployed", ref, at: new Date().toISOString(), ...(
|
|
154
|
+
const builds = await context.putOutpost(repo, await evaluateOutpost(repo, files, context), files, ref, executables);
|
|
155
|
+
return { state: "deployed", ref, at: new Date().toISOString(), ...(builds.length && { builds }) };
|
|
149
156
|
} catch (error) {
|
|
150
157
|
return { state: "failed", ref, error: message(error), at: new Date().toISOString() };
|
|
151
158
|
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { type CredentialRef, ResourceGroup } from "@notation/resource";
|
|
2
|
+
import type { RepoPattern } from "../contracts/router.ts";
|
|
3
|
+
|
|
4
|
+
export type OutpostSecretOptions = {
|
|
5
|
+
/** The env var name that containers read the secret as, such as `DATABASE_URL`. */
|
|
6
|
+
name: string;
|
|
7
|
+
/** A credential, such as `source.credentials.value`. The deployer writes it to the hub's vault. */
|
|
8
|
+
value: CredentialRef<string>;
|
|
9
|
+
/** The repos whose containers get the secret, in the form of the code host's `allow`. */
|
|
10
|
+
allowedRepos: RepoPattern[];
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* `new OutpostSecret(id, ...)`: a secret that the containers of
|
|
15
|
+
* `allowedRepos` get as the env var `name`. Pass it to the deployer's
|
|
16
|
+
* `secrets`, which writes it to the hub's vault and binds it.
|
|
17
|
+
*/
|
|
18
|
+
export class OutpostSecret extends ResourceGroup {
|
|
19
|
+
readonly name: string;
|
|
20
|
+
readonly value: CredentialRef<string>;
|
|
21
|
+
readonly allowedRepos: RepoPattern[];
|
|
22
|
+
|
|
23
|
+
constructor(id: string, { name, value, allowedRepos }: OutpostSecretOptions) {
|
|
24
|
+
if (!/^[A-Za-z_][A-Za-z0-9_]*$/.test(name)) {
|
|
25
|
+
throw new Error(`The outpost secret ${id} has the name ${JSON.stringify(name)}, which isn't an env var name.`);
|
|
26
|
+
}
|
|
27
|
+
if (allowedRepos.length === 0) {
|
|
28
|
+
throw new Error(`The outpost secret ${id} has no allowedRepos, so no container would get it.`);
|
|
29
|
+
}
|
|
30
|
+
super("factory/OutpostSecret", id, { name, allowedRepos });
|
|
31
|
+
this.name = name;
|
|
32
|
+
this.value = value;
|
|
33
|
+
this.allowedRepos = allowedRepos;
|
|
34
|
+
}
|
|
35
|
+
}
|
package/index.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
1
|
export * from "./outpost.ts";
|
|
2
2
|
export { RandomKey, type RandomKeyInstance } from "./resources/random-key.ts";
|
|
3
3
|
export { RouterSync } from "./resources/router-sync.ts";
|
|
4
|
+
export { OutpostSecret, type OutpostSecretOptions } from "./implementations/outpost-secret.ts";
|
|
5
|
+
export { reposOverlap } from "./lib/repos.ts";
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import assert from "node:assert/strict";
|
|
2
|
+
import { test } from "node:test";
|
|
3
|
+
import { allowsRepo, reposOverlap } from "./repos.ts";
|
|
4
|
+
|
|
5
|
+
test("a pattern without a repo allows every repo of its owner, whatever the case", () => {
|
|
6
|
+
assert.equal(allowsRepo([{ owner: "acme" }], "Acme/Web"), true);
|
|
7
|
+
assert.equal(allowsRepo([{ owner: "acme", repo: "web" }], "acme/WEB"), true);
|
|
8
|
+
assert.equal(allowsRepo([{ owner: "acme", repo: "web" }], "acme/api"), false);
|
|
9
|
+
assert.equal(allowsRepo([{ owner: "acme" }], "other/web"), false);
|
|
10
|
+
});
|
|
11
|
+
|
|
12
|
+
test("patterns overlap when one repo matches both", () => {
|
|
13
|
+
assert.equal(reposOverlap([{ owner: "acme" }], [{ owner: "ACME", repo: "web" }]), true);
|
|
14
|
+
assert.equal(reposOverlap([{ owner: "acme", repo: "web" }], [{ owner: "acme", repo: "Web" }]), true);
|
|
15
|
+
assert.equal(reposOverlap([{ owner: "acme", repo: "web" }], [{ owner: "acme", repo: "api" }]), false);
|
|
16
|
+
assert.equal(reposOverlap([{ owner: "acme" }], [{ owner: "other" }]), false);
|
|
17
|
+
});
|
package/lib/repos.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { RepoPattern } from "../contracts/router.ts";
|
|
2
|
+
|
|
3
|
+
/** Whether `repo`, as `owner/name`, matches one of `allow`. A pattern without `repo` matches every repo of its owner. Case doesn't matter. */
|
|
4
|
+
export const allowsRepo = (allow: RepoPattern[], repo: string) => {
|
|
5
|
+
const [owner, name] = repo.toLowerCase().split("/");
|
|
6
|
+
return allow.some(
|
|
7
|
+
(p) => p.owner.toLowerCase() === owner && (!p.repo || p.repo.toLowerCase() === name),
|
|
8
|
+
);
|
|
9
|
+
};
|
|
10
|
+
|
|
11
|
+
/** Whether some repo matches a pattern in `a` and a pattern in `b`. */
|
|
12
|
+
export const reposOverlap = (a: RepoPattern[], b: RepoPattern[]) =>
|
|
13
|
+
a.some((p) =>
|
|
14
|
+
b.some(
|
|
15
|
+
(q) =>
|
|
16
|
+
p.owner.toLowerCase() === q.owner.toLowerCase() &&
|
|
17
|
+
(!p.repo || !q.repo || p.repo.toLowerCase() === q.repo.toLowerCase()),
|
|
18
|
+
),
|
|
19
|
+
);
|
package/package.json
CHANGED
package/router/channel.ts
CHANGED
|
@@ -56,7 +56,12 @@ export type Received<E> =
|
|
|
56
56
|
| { status: "rejected"; reason: string };
|
|
57
57
|
|
|
58
58
|
/** The files under a directory of a repo at one commit, by their path in the directory. */
|
|
59
|
-
export type SourceFiles = {
|
|
59
|
+
export type SourceFiles = {
|
|
60
|
+
ref: string;
|
|
61
|
+
files: Record<string, string>;
|
|
62
|
+
/** The paths of the files that the repo marks executable. */
|
|
63
|
+
executables?: string[];
|
|
64
|
+
};
|
|
60
65
|
|
|
61
66
|
/**
|
|
62
67
|
* Whether a selector's `from` lets the delivery start a run. `check` holds
|
|
@@ -101,8 +106,8 @@ export type FactoryStatus = {
|
|
|
101
106
|
error?: string;
|
|
102
107
|
/** When the deploy finished, as an ISO date. */
|
|
103
108
|
at: string;
|
|
104
|
-
/** The containers whose images started building. */
|
|
105
|
-
|
|
109
|
+
/** The containers whose images this deploy started building. Each build's progress is in its `factory/build <container>` check, not here. */
|
|
110
|
+
builds?: string[];
|
|
106
111
|
};
|
|
107
112
|
|
|
108
113
|
/** A container image's build, as a check on the commit it was built from. */
|
package/router/handle.test.ts
CHANGED
|
@@ -219,7 +219,7 @@ test("a deploy that starts builds shows a queued check on the commit for each co
|
|
|
219
219
|
writeCheck: async (repo, ref, check) => void calls.push(`check ${repo} ${ref} ${check.name} ${check.state}`),
|
|
220
220
|
};
|
|
221
221
|
const s = sourceStore(channel, calls);
|
|
222
|
-
s.deployer!.deploy = async (_repo, source) => ({ state: "deployed", ref: source.ref, at: "now",
|
|
222
|
+
s.deployer!.deploy = async (_repo, source) => ({ state: "deployed", ref: source.ref, at: "now", builds: ["dev", "ci"] });
|
|
223
223
|
await call(s);
|
|
224
224
|
assert.deepEqual(calls, [
|
|
225
225
|
"read acme/api .factory abc",
|
|
@@ -238,7 +238,7 @@ test("a check that the channel can't write doesn't fail the deploy", async () =>
|
|
|
238
238
|
},
|
|
239
239
|
};
|
|
240
240
|
const s = sourceStore(channel, calls);
|
|
241
|
-
s.deployer!.deploy = async (_repo, source) => ({ state: "deployed", ref: source.ref, at: "now",
|
|
241
|
+
s.deployer!.deploy = async (_repo, source) => ({ state: "deployed", ref: source.ref, at: "now", builds: ["dev"] });
|
|
242
242
|
const { status } = await call(s);
|
|
243
243
|
assert.equal(status, 202);
|
|
244
244
|
assert.deepEqual(calls, ["read acme/api .factory abc", "status acme/api deployed"]);
|
package/router/handle.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { OutpostConfig } from "../contracts/outpost.ts";
|
|
2
2
|
import type { RepoPattern, RouterConfig } from "../contracts/router.ts";
|
|
3
|
+
import { allowsRepo } from "../lib/repos.ts";
|
|
3
4
|
import type {
|
|
4
5
|
AuthorCheck,
|
|
5
6
|
BuildCheck,
|
|
@@ -331,13 +332,6 @@ const writeCheck = async (channel: RemoteChannel, repo: string, ref: string, che
|
|
|
331
332
|
}
|
|
332
333
|
};
|
|
333
334
|
|
|
334
|
-
const allows = (allow: RepoPattern[], repo: string) => {
|
|
335
|
-
const [owner, name] = repo.toLowerCase().split("/");
|
|
336
|
-
return allow.some(
|
|
337
|
-
(p) => p.owner.toLowerCase() === owner && (!p.repo || p.repo.toLowerCase() === name),
|
|
338
|
-
);
|
|
339
|
-
};
|
|
340
|
-
|
|
341
335
|
const message = (error: unknown) => (error instanceof Error ? error.message : String(error));
|
|
342
336
|
|
|
343
337
|
/**
|
|
@@ -360,7 +354,7 @@ const redeploy = async (
|
|
|
360
354
|
if (change.removed) return deployer.remove(repo);
|
|
361
355
|
const at = () => new Date().toISOString();
|
|
362
356
|
let status: FactoryStatus;
|
|
363
|
-
if (!
|
|
357
|
+
if (!allowsRepo(allow, repo)) {
|
|
364
358
|
await deployer.remove(repo);
|
|
365
359
|
status = { state: "failed", error: `The hub doesn't serve ${repo}: the code host's allow list doesn't include it.`, at: at() };
|
|
366
360
|
} else {
|
|
@@ -371,7 +365,7 @@ const redeploy = async (
|
|
|
371
365
|
}
|
|
372
366
|
}
|
|
373
367
|
await channel.writeStatus(repo, status);
|
|
374
|
-
for (const container of status.
|
|
368
|
+
for (const container of status.builds ?? []) {
|
|
375
369
|
if (status.ref) await writeCheck(channel, repo, status.ref, { name: buildCheckName(container), state: "queued" });
|
|
376
370
|
}
|
|
377
371
|
console.log(JSON.stringify({ repo, ...status }));
|