@notation/factory-github 0.1.6 → 0.1.8

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/lib/app.ts CHANGED
@@ -12,9 +12,11 @@ const github = async <T>(
12
12
  method: string,
13
13
  path: string,
14
14
  token?: string,
15
+ body?: unknown,
15
16
  ): Promise<T> => {
16
17
  const response = await fetch(`${GITHUB_API}${path}`, {
17
18
  method,
19
+ body: body === undefined ? undefined : JSON.stringify(body),
18
20
  headers: {
19
21
  accept: "application/vnd.github+json",
20
22
  "user-agent": "notation-factory",
@@ -64,9 +66,9 @@ export const createApp = async (params: {
64
66
  hookUrl: string;
65
67
  events?: GithubEvent[];
66
68
  permissions?: Permissions;
67
- }): Promise<{ slug: string; id: string; privateKey: string }> => {
69
+ }): Promise<{ id: string; privateKey: string }> => {
68
70
  const state = randomBytes(16).toString("hex");
69
- type Created = { slug: string; id: string; privateKey: string };
71
+ type Created = { id: string; privateKey: string };
70
72
  let resolve!: (app: Created) => void;
71
73
  let reject!: (error: unknown) => void;
72
74
  const promise = new Promise<Created>((res, rej) => {
@@ -116,7 +118,8 @@ export const createApp = async (params: {
116
118
  "POST",
117
119
  `/app-manifests/${encodeURIComponent(code)}/conversions`,
118
120
  );
119
- resolve({ slug: app.slug, id: String(app.id), privateKey: app.pem });
121
+ console.error(`Created the GitHub App ${app.slug}, with the ID ${app.id}. Outposts name the App by this ID in .factory/config.ts.`);
122
+ resolve({ id: String(app.id), privateKey: app.pem });
120
123
  send(200, "App created. You can close this tab.");
121
124
  } catch (error) {
122
125
  reject(error);
@@ -138,28 +141,44 @@ export const createApp = async (params: {
138
141
  }
139
142
  };
140
143
 
141
- /** Reads the App by its slug. GitHub only shows a private App to its owner, so this uses your gh login when there is one. */
142
- export const readApp = async (key: {
143
- slug: string;
144
- }): Promise<{ slug: string; id: string }> => {
145
- const app = await github<{ id: number; slug: string }>(
146
- "GET",
147
- `/apps/${encodeURIComponent(key.slug)}`,
148
- await ghToken(),
144
+ /**
145
+ * The current slug of the App with id `id`, which changes when the App is
146
+ * renamed. GitHub's REST API only finds an App by its slug, so this asks
147
+ * GraphQL for the App's node. GitHub has deprecated this form of node ID, but
148
+ * still resolves it, and its newer form includes the owner's ID.
149
+ */
150
+ export const appSlug = async (id: string): Promise<string> => {
151
+ const token = await ghToken();
152
+ if (!token) {
153
+ throw new Error(`Looking up the GitHub App ${id} needs a GitHub token. Run \`gh auth login\`, or set GH_TOKEN.`);
154
+ }
155
+ const nodeId = Buffer.from(`03:App${id}`).toString("base64");
156
+ const { data, errors } = await github<{
157
+ data?: { node?: { slug?: string } | null };
158
+ errors?: { message: string }[];
159
+ }>(
160
+ "POST",
161
+ "/graphql",
162
+ token,
163
+ { query: "query($id: ID!) { node(id: $id) { ... on App { slug } } }", variables: { id: nodeId } },
149
164
  );
150
- return { slug: app.slug, id: String(app.id) };
165
+ const slug = data?.node?.slug;
166
+ if (!slug) throw new Error(`Looking up the GitHub App ${id} failed: ${errors?.[0]?.message ?? "GitHub returned no App"}`);
167
+ return slug;
168
+ };
169
+
170
+ /** Reads the App by its id, which fails if the App is gone. The lookup needs your gh login or GH_TOKEN. */
171
+ export const readApp = async (key: { id: string }): Promise<{ id: string }> => {
172
+ await appSlug(key.id);
173
+ return { id: key.id };
151
174
  };
152
175
 
153
176
  /** Opens the App's advanced settings, where its owner must delete it. */
154
- export const requestAppDeletion = async (key: { slug: string }): Promise<void> => {
155
- const app = await github<{ owner: { login: string; type: string } }>(
156
- "GET",
157
- `/apps/${encodeURIComponent(key.slug)}`,
158
- await ghToken(),
159
- );
160
- const owner = app.owner.type === "Organization" ? `/organizations/${app.owner.login}` : "";
161
- const url = `${GITHUB_WEB}${owner}/settings/apps/${key.slug}/advanced`;
162
- console.error(`Delete the GitHub App ${key.slug} on its settings page: ${url}`);
177
+ export const requestAppDeletion = async (key: { id: string; org?: string }): Promise<void> => {
178
+ const slug = await appSlug(key.id);
179
+ const owner = key.org ? `/organizations/${key.org}` : "";
180
+ const url = `${GITHUB_WEB}${owner}/settings/apps/${slug}/advanced`;
181
+ console.error(`Delete the GitHub App ${slug} on its settings page: ${url}`);
163
182
  openBrowser(url);
164
183
  };
165
184
 
@@ -192,16 +211,17 @@ const missingFromApp = async (slug: string, change: { events?: GithubEvent[]; pe
192
211
  * App's private key.
193
212
  */
194
213
  export const requestAppChange = async (
195
- key: { slug: string; org?: string },
214
+ key: { id: string; org?: string },
196
215
  change: { events?: GithubEvent[]; permissions?: Permissions },
197
216
  ): Promise<void> => {
198
- let missing = await missingFromApp(key.slug, change);
217
+ const slug = await appSlug(key.id);
218
+ let missing = await missingFromApp(slug, change);
199
219
  if (missing.permissions.length || missing.events.length) {
200
220
  const owner = key.org ? `/organizations/${key.org}` : "";
201
- const url = `${GITHUB_WEB}${owner}/settings/apps/${key.slug}/permissions`;
221
+ const url = `${GITHUB_WEB}${owner}/settings/apps/${slug}/permissions`;
202
222
  console.error(
203
223
  [
204
- `Set the GitHub App ${key.slug}'s permissions and events on ${url}, and save:`,
224
+ `Set the GitHub App ${slug}'s permissions and events on ${url}, and save:`,
205
225
  ` permissions: ${missing.permissions.map(([name, level]) => `${name}: ${level}`).join(", ") || "no change"}`,
206
226
  ` events: ${missing.events.join(", ") || "no change"}`,
207
227
  ].join("\n"),
@@ -210,13 +230,13 @@ export const requestAppChange = async (
210
230
  const deadline = Date.now() + APP_CHANGE_TIMEOUT_MS;
211
231
  while (missing.permissions.length || missing.events.length) {
212
232
  if (Date.now() > deadline) {
213
- throw new Error(`The GitHub App ${key.slug} didn't get its new permissions and events within 15 minutes.`);
233
+ throw new Error(`The GitHub App ${slug} didn't get its new permissions and events within 15 minutes.`);
214
234
  }
215
235
  await new Promise((done) => setTimeout(done, APP_CHANGE_POLL_MS));
216
- missing = await missingFromApp(key.slug, change);
236
+ missing = await missingFromApp(slug, change);
217
237
  }
218
238
  }
219
- const installations = `${GITHUB_WEB}/apps/${key.slug}/installations/new`;
220
- console.error(`Accept the GitHub App ${key.slug}'s new permissions on each installation: ${installations}`);
239
+ const installations = `${GITHUB_WEB}/apps/${slug}/installations/new`;
240
+ console.error(`Accept the GitHub App ${slug}'s new permissions on each installation: ${installations}`);
221
241
  openBrowser(installations);
222
242
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@notation/factory-github",
3
- "version": "0.1.6",
3
+ "version": "0.1.8",
4
4
  "author": "Daniel Grant",
5
5
  "license": "MIT",
6
6
  "repository": {
@@ -14,7 +14,7 @@
14
14
  "factory-outpost": "./implementations/outpost.ts",
15
15
  "default": "./implementations/index.ts"
16
16
  },
17
- "./app-name": "./lib/app-name.ts",
17
+ "./app": "./lib/app.ts",
18
18
  "./router": "./router/index.ts"
19
19
  },
20
20
  "files": [
@@ -32,9 +32,9 @@
32
32
  "valibot": "^1.1.0"
33
33
  },
34
34
  "peerDependencies": {
35
- "@notation/factory": "^0.1.6"
35
+ "@notation/factory": "^0.1.8"
36
36
  },
37
37
  "devDependencies": {
38
- "@notation/factory": "0.1.6"
38
+ "@notation/factory": "0.1.8"
39
39
  }
40
40
  }
package/resources/app.ts CHANGED
@@ -15,7 +15,7 @@ import {
15
15
 
16
16
  /** One GitHub App, which backs a connection. Its channels' rules contribute the events and permissions they need. */
17
17
  type AppApi = {
18
- Key: { slug: string };
18
+ Key: { id: string };
19
19
  CreateParams: {
20
20
  name: string;
21
21
  org?: string;
@@ -28,7 +28,7 @@ type AppApi = {
28
28
  events?: GithubEvent[];
29
29
  permissions?: Permissions;
30
30
  };
31
- ReadResult: { slug: string; id: string };
31
+ ReadResult: { id: string };
32
32
  };
33
33
 
34
34
  const githubEvent = v.picklist([
@@ -57,10 +57,15 @@ export const App = defineResource<AppApi>({
57
57
  provider: "@notation/factory-github",
58
58
  })
59
59
  .defineSchema({
60
+ /**
61
+ * The name the manifest flow suggests for the App. GitHub owns the name
62
+ * after that, and the App keeps working when it's renamed.
63
+ */
60
64
  name: {
61
65
  propertyType: "param",
62
66
  presence: "required",
63
67
  immutable: true,
68
+ volatile: true,
64
69
  value: v.string(),
65
70
  },
66
71
  org: {
@@ -82,13 +87,12 @@ export const App = defineResource<AppApi>({
82
87
  value: permissions,
83
88
  collect: highestPermissions,
84
89
  },
85
- slug: {
90
+ id: {
86
91
  propertyType: "computed",
87
92
  presence: "required",
88
93
  primaryKey: true,
89
94
  value: v.string(),
90
95
  },
91
- id: { propertyType: "computed", presence: "required", value: v.string() },
92
96
  /**
93
97
  * The App's private key, which GitHub returns once, when the manifest
94
98
  * flow creates the App. GitHub has no API that makes another.
@@ -113,7 +117,7 @@ export const App = defineResource<AppApi>({
113
117
  // in as the App with its private key.
114
118
  },
115
119
  // GitHub has no API to delete an App.
116
- delete: async (key) => requestAppDeletion(key),
120
+ delete: async (key, state) => requestAppDeletion({ ...key, org: state.org }),
117
121
  });
118
122
 
119
123
  export type AppInstance = InstanceType<typeof App>;