@notation/factory-github 0.1.3 → 0.1.6

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.
@@ -1,3 +1,4 @@
1
+ import { readFileSync } from "node:fs";
1
2
  import { ResourceGroup } from "@notation/resource";
2
3
  import type { CodeHost as CodeHostContract, RepoPattern } from "@notation/factory";
3
4
  import * as v from "valibot";
@@ -15,7 +16,8 @@ export type CodeHostOptions = CodeHostConfig & { connection: Connection };
15
16
  * that match `allow` get outposts, and the router mints their containers'
16
17
  * installation tokens through the connection's App. Those last an hour and can
17
18
  * be limited to one repo and a set of permissions, so every `github.Policy`
18
- * passes.
19
+ * passes. Its runner module clones and pushes with those tokens, and gives
20
+ * the image's `gh` the run's token.
19
21
  */
20
22
  export class CodeHost
21
23
  extends ResourceGroup<CodeHostConfig>
@@ -29,6 +31,9 @@ export class CodeHost
29
31
  super("github/CodeHost", id, config);
30
32
  this.connection = connection;
31
33
  this.allow = allow;
34
+ const { router } = this.connection;
35
+ if (router.codeHost) throw new Error(`An infra has one code host, and ${router.codeHost.id} is declared already.`);
36
+ router.codeHost = this;
32
37
  this.connection.router.store.contribute({
33
38
  codeHost: {
34
39
  provider: "github",
@@ -37,4 +42,8 @@ export class CodeHost
37
42
  },
38
43
  });
39
44
  }
45
+
46
+ get runner() {
47
+ return readFileSync(new URL("../runner/index.ts", import.meta.url), "utf8");
48
+ }
40
49
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@notation/factory-github",
3
- "version": "0.1.3",
3
+ "version": "0.1.6",
4
4
  "author": "Daniel Grant",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -21,7 +21,8 @@
21
21
  "implementations",
22
22
  "lib",
23
23
  "resources",
24
- "router"
24
+ "router",
25
+ "runner"
25
26
  ],
26
27
  "publishConfig": {
27
28
  "access": "public"
@@ -31,9 +32,9 @@
31
32
  "valibot": "^1.1.0"
32
33
  },
33
34
  "peerDependencies": {
34
- "@notation/factory": "^0.1.3"
35
+ "@notation/factory": "^0.1.6"
35
36
  },
36
37
  "devDependencies": {
37
- "@notation/factory": "0.1.3"
38
+ "@notation/factory": "0.1.6"
38
39
  }
39
40
  }
@@ -0,0 +1,40 @@
1
+ import { execFile } from "node:child_process";
2
+ import { mkdir, writeFile } from "node:fs/promises";
3
+ import { join } from "node:path";
4
+ import { promisify } from "node:util";
5
+ import type { CodeHostRunner } from "@notation/factory";
6
+
7
+ const run = promisify(execFile);
8
+
9
+ /** How the runner clones from and pushes to GitHub. Containers import it with no packages, so it imports only Node's. */
10
+ const github: CodeHostRunner = {
11
+ cloneUrl: (repo) => `https://github.com/${repo}.git`,
12
+ gitAuth: (token) => [
13
+ "-c",
14
+ `http.extraHeader=Authorization: Basic ${Buffer.from(`x-access-token:${token}`).toString("base64")}`,
15
+ ],
16
+ setup: async ({ bin }) => {
17
+ await run("git", [
18
+ "config",
19
+ "--global",
20
+ "credential.https://github.com.helper",
21
+ '!f() { test -n "$FACTORY_TOKEN_FILE" || exit 0; echo username=x-access-token; echo "password=$(cat "$FACTORY_TOKEN_FILE")"; }; f',
22
+ ]);
23
+ // A `gh` in `bin` that runs the image's `gh` with the token in
24
+ // `FACTORY_TOKEN_FILE`. `GH_TOKEN` would keep the first token after the
25
+ // router refreshes it.
26
+ const gh = await run("sh", ["-c", "command -v gh"]).then(
27
+ ({ stdout }) => stdout.trim(),
28
+ () => "",
29
+ );
30
+ if (!gh) return;
31
+ await mkdir(bin, { recursive: true });
32
+ const script = `#!/bin/sh
33
+ if [ -n "$FACTORY_TOKEN_FILE" ]; then GH_TOKEN="$(cat "$FACTORY_TOKEN_FILE")"; export GH_TOKEN; fi
34
+ exec ${JSON.stringify(gh)} "$@"
35
+ `;
36
+ await writeFile(join(bin, "gh"), script, { mode: 0o755 });
37
+ },
38
+ };
39
+
40
+ export default github;