mirror your GitHub repos to tangled.org automatically
1import type { JobEnvelope } from './queue'
2import { useOAuthClient } from './atproto-oauth'
3import { generateAndPublishKey } from './tangled-pubkey'
4
5/**
6 * Map of job kind → handler. Handlers are filled in by later commits:
7 * - 'github.push' → commit 12 (sync push events)
8 * - 'github.create' / 'github.delete' → commit 13 (branch/tag ref ops)
9 * - 'github.repository' → commit 14/15 (description, lifecycle)
10 * - 'tangled.create-repo' → commit 10 (initial enrolment)
11 * - 'atproto.publish-pubkey' → this commit (key rotation)
12 *
13 * Unknown kinds throw so they surface as job failures rather than silent
14 * acknowledgement.
15 */
16const KNOWN_KINDS = new Set([
17 'github.push',
18 'github.create',
19 'github.delete',
20 'github.repository',
21 'github.installation_repositories',
22 'tangled.create-repo',
23 'atproto.publish-pubkey',
24])
25
26interface PublishPubkeyPayload {
27 did: string
28 installationId: number
29}
30
31export async function dispatch(envelope: JobEnvelope): Promise<void> {
32 if (!KNOWN_KINDS.has(envelope.kind)) {
33 throw new Error(`unknown job kind: ${envelope.kind}`)
34 }
35
36 if (envelope.kind === 'atproto.publish-pubkey') {
37 const { did, installationId } = envelope.payload as PublishPubkeyPayload
38 const client = await useOAuthClient()
39 const session = await client.restore(did)
40 await generateAndPublishKey({ oauthSession: session, installationId })
41 return
42 }
43
44 // Other kinds: still no-op until handlers land in their commits.
45}