@artieeez/cf-plugin-linear 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/src/setup.ts ADDED
@@ -0,0 +1,60 @@
1
+ import { cut } from '@artieeez/cf-core';
2
+
3
+ /**
4
+ * What the server calls on Linear to set up an instance (integration-setup S4, S7): the key's
5
+ * teams, and the webhook it creates and deletes. Plain GraphQL over the server's `fetch`.
6
+ */
7
+
8
+ const apiUrl = () => process.env['CF_LINEAR_API_URL'] || 'https://api.linear.app/graphql';
9
+
10
+ /** A setup call's answer: its value, or why Linear refused it (`rejected`: the key). */
11
+ export type LinearCall<T> = { ok: true; value: T } | { ok: false; reason: 'rejected' | 'failed'; message: string };
12
+
13
+ export interface LinearTeam {
14
+ id: string;
15
+ key: string;
16
+ name: string;
17
+ }
18
+
19
+ async function gql<T>(apiKey: string, fetch: typeof globalThis.fetch, query: string, variables: Record<string, unknown> = {}): Promise<LinearCall<T>> {
20
+ let res: Response;
21
+ try {
22
+ res = await fetch(apiUrl(), { method: 'POST', headers: { 'content-type': 'application/json', authorization: apiKey }, body: JSON.stringify({ query, variables }) });
23
+ } catch (err) {
24
+ return { ok: false, reason: 'failed', message: cut(`Linear could not be reached: ${err instanceof Error ? err.message : String(err)}`, 300) };
25
+ }
26
+ const body = (await res.json().catch(() => ({}))) as { data?: T; errors?: { message: string; extensions?: { code?: string } }[] };
27
+ const errors = body.errors ?? [];
28
+ const message = cut(errors.map((e) => e.message).join('; ') || `Linear answered ${res.status}`, 300);
29
+ const auth = res.status === 401 || errors.some((e) => e.extensions?.code === 'AUTHENTICATION_ERROR' || /authenticat/i.test(e.message));
30
+ if (auth) return { ok: false, reason: 'rejected', message };
31
+ if (errors.length > 0 || body.data === undefined || !res.ok) return { ok: false, reason: 'failed', message };
32
+ return { ok: true, value: body.data };
33
+ }
34
+
35
+ /** The teams the key can see, by key. */
36
+ export async function linearTeams(apiKey: string, fetch: typeof globalThis.fetch): Promise<LinearCall<LinearTeam[]>> {
37
+ const r = await gql<{ teams: { nodes: LinearTeam[] } }>(apiKey, fetch, 'query SetupTeams { teams(first: 250) { nodes { id key name } } }');
38
+ return r.ok ? { ok: true, value: [...r.value.teams.nodes].sort((a, b) => a.key.localeCompare(b.key)) } : r;
39
+ }
40
+
41
+ /** A webhook on the team for `Issue` and `Comment` to `url`: its id and signing secret. */
42
+ export async function createLinearWebhook(apiKey: string, fetch: typeof globalThis.fetch, input: { teamId: string; url: string; label: string }): Promise<LinearCall<{ id: string; secret: string }>> {
43
+ const r = await gql<{ webhookCreate: { success: boolean; webhook: { id: string; secret: string | null } | null } }>(
44
+ apiKey,
45
+ fetch,
46
+ 'mutation SetupWebhookCreate($input: WebhookCreateInput!) { webhookCreate(input: $input) { success webhook { id secret } } }',
47
+ { input: { url: input.url, teamId: input.teamId, label: input.label, resourceTypes: ['Issue', 'Comment'], enabled: true } },
48
+ );
49
+ if (!r.ok) return r;
50
+ const w = r.value.webhookCreate.webhook;
51
+ if (!r.value.webhookCreate.success || w === null || !w.secret) return { ok: false, reason: 'failed', message: 'Linear did not create the webhook' };
52
+ return { ok: true, value: { id: w.id, secret: w.secret } };
53
+ }
54
+
55
+ /** Deletes the webhook `id`. */
56
+ export async function deleteLinearWebhook(apiKey: string, fetch: typeof globalThis.fetch, id: string): Promise<LinearCall<true>> {
57
+ const r = await gql<{ webhookDelete: { success: boolean } }>(apiKey, fetch, 'mutation SetupWebhookDelete($id: String!) { webhookDelete(id: $id) { success } }', { id });
58
+ if (!r.ok) return r;
59
+ return r.value.webhookDelete.success ? { ok: true, value: true } : { ok: false, reason: 'failed', message: 'Linear did not delete the webhook' };
60
+ }