@artieeez/cf-plugin-github 0.0.0-stage → 0.2.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 +144 -2
- package/dist/ci.d.ts +93 -0
- package/dist/ci.d.ts.map +1 -0
- package/dist/ci.js +141 -0
- package/dist/ci.js.map +1 -0
- package/dist/diff.d.ts +7 -0
- package/dist/diff.d.ts.map +1 -0
- package/dist/diff.js +39 -0
- package/dist/diff.js.map +1 -0
- package/dist/index.d.ts +539 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +1021 -0
- package/dist/index.js.map +1 -0
- package/dist/setup.d.ts +40 -0
- package/dist/setup.d.ts.map +1 -0
- package/dist/setup.js +73 -0
- package/dist/setup.js.map +1 -0
- package/package.json +42 -3
- package/src/ci.ts +228 -0
- package/src/diff.ts +35 -0
- package/src/index.ts +1102 -0
- package/src/setup.ts +99 -0
package/src/setup.ts
ADDED
|
@@ -0,0 +1,99 @@
|
|
|
1
|
+
import { createAppAuth } from '@octokit/auth-app';
|
|
2
|
+
import { Octokit } from '@octokit/rest';
|
|
3
|
+
import { cut } from '@artieeez/cf-core';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* What the server calls on GitHub to set up an instance (integration-setup S2, S3, S6), beside the
|
|
7
|
+
* plugin's own actions: the manifest code conversion, the App's slug and an installation's repos.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
const apiUrl = () => process.env['CF_GITHUB_API_URL'] || 'https://api.github.com';
|
|
11
|
+
|
|
12
|
+
/** A setup call's answer: its value, or why GitHub refused it. */
|
|
13
|
+
export type SetupCall<T> =
|
|
14
|
+
| { ok: true; value: T }
|
|
15
|
+
/** `rejected`: GitHub refused the key or code (401); `not_found`: no such installation for this App (404); `failed`: anything else */
|
|
16
|
+
| { ok: false; reason: 'rejected' | 'not_found' | 'failed'; message: string };
|
|
17
|
+
|
|
18
|
+
/** The App GitHub made from a manifest: what an instance stores of it. */
|
|
19
|
+
export interface ConvertedApp {
|
|
20
|
+
appId: string;
|
|
21
|
+
slug: string;
|
|
22
|
+
privateKey: string;
|
|
23
|
+
webhookSecret: string;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface AppKey {
|
|
27
|
+
appId: string;
|
|
28
|
+
privateKey: string;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
/**
|
|
32
|
+
* An Octokit error as a refused call. `signing`: the call signs a JWT with the App's key first, so
|
|
33
|
+
* an error with no HTTP status there is a key that cannot be read (a bad paste), not GitHub.
|
|
34
|
+
*/
|
|
35
|
+
function refused(err: unknown, what: string, signing = false): SetupCall<never> {
|
|
36
|
+
const status = (err as { status?: number }).status;
|
|
37
|
+
const message = cut(`${what}: ${err instanceof Error ? err.message : String(err)}`, 300);
|
|
38
|
+
if (status === 401 || (signing && status === undefined)) return { ok: false, reason: 'rejected', message };
|
|
39
|
+
if (status === 404) return { ok: false, reason: 'not_found', message };
|
|
40
|
+
return { ok: false, reason: 'failed', message };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const appClient = (key: AppKey, fetch: typeof globalThis.fetch) =>
|
|
44
|
+
new Octokit({ authStrategy: createAppAuth, auth: { appId: key.appId, privateKey: key.privateKey }, baseUrl: apiUrl(), request: { fetch }, userAgent: 'code-factory' });
|
|
45
|
+
|
|
46
|
+
/** `POST /app-manifests/{code}/conversions`: the App GitHub made, with its key and webhook secret. */
|
|
47
|
+
export async function convertManifest(code: string, fetch: typeof globalThis.fetch): Promise<SetupCall<ConvertedApp>> {
|
|
48
|
+
const octokit = new Octokit({ baseUrl: apiUrl(), request: { fetch }, userAgent: 'code-factory' });
|
|
49
|
+
try {
|
|
50
|
+
const { data } = await octokit.request('POST /app-manifests/{code}/conversions', { code });
|
|
51
|
+
const d = data as unknown as { id: number; slug?: string; pem: string; webhook_secret: string | null };
|
|
52
|
+
return { ok: true, value: { appId: String(d.id), slug: d.slug ?? '', privateKey: d.pem, webhookSecret: d.webhook_secret ?? '' } };
|
|
53
|
+
} catch (err) {
|
|
54
|
+
return refused(err, 'GitHub POST /app-manifests/{code}/conversions');
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** `GET /app` as the App: its slug, which also proves the key. */
|
|
59
|
+
export async function appSlug(key: AppKey, fetch: typeof globalThis.fetch): Promise<SetupCall<string>> {
|
|
60
|
+
try {
|
|
61
|
+
const { data } = await appClient(key, fetch).request('GET /app');
|
|
62
|
+
return { ok: true, value: (data as { slug?: string } | null)?.slug ?? '' };
|
|
63
|
+
} catch (err) {
|
|
64
|
+
return refused(err, 'GitHub GET /app', true);
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
/** A repository an installation reaches. */
|
|
69
|
+
export interface InstallationRepository {
|
|
70
|
+
/** `owner/name` */
|
|
71
|
+
fullName: string;
|
|
72
|
+
private: boolean;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The repositories an installation of the App can reach, sorted by name: an installation token is
|
|
77
|
+
* minted (a 404 is another App's installation), then `GET /installation/repositories`.
|
|
78
|
+
*/
|
|
79
|
+
export async function installationRepositories(key: AppKey & { installationId: string }, fetch: typeof globalThis.fetch): Promise<SetupCall<InstallationRepository[]>> {
|
|
80
|
+
let token: string;
|
|
81
|
+
try {
|
|
82
|
+
const auth = createAppAuth({ appId: key.appId, privateKey: key.privateKey, request: new Octokit({ baseUrl: apiUrl(), request: { fetch } }).request });
|
|
83
|
+
token = (await auth({ type: 'installation', installationId: Number(key.installationId) })).token;
|
|
84
|
+
} catch (err) {
|
|
85
|
+
return refused(err, `GitHub POST /app/installations/${key.installationId}/access_tokens`, true);
|
|
86
|
+
}
|
|
87
|
+
try {
|
|
88
|
+
const octokit = new Octokit({ auth: token, baseUrl: apiUrl(), request: { fetch }, userAgent: 'code-factory' });
|
|
89
|
+
const all: InstallationRepository[] = [];
|
|
90
|
+
for (let page = 1; ; page++) {
|
|
91
|
+
const { data } = await octokit.request('GET /installation/repositories', { per_page: 100, page });
|
|
92
|
+
all.push(...data.repositories.map((r) => ({ fullName: r.full_name, private: r.private })));
|
|
93
|
+
if (data.repositories.length < 100 || all.length >= data.total_count) break;
|
|
94
|
+
}
|
|
95
|
+
return { ok: true, value: all.sort((a, b) => a.fullName.localeCompare(b.fullName)) };
|
|
96
|
+
} catch (err) {
|
|
97
|
+
return refused(err, 'GitHub GET /installation/repositories');
|
|
98
|
+
}
|
|
99
|
+
}
|