|
| 1 | +import { extractRepositoryEvent, defineEvent } from "./events"; |
| 2 | +import { getRepository } from "@acme/extract-functions"; |
| 3 | +import type { Context, GetRepositorySourceControl, GetRepositoryEntities } from "@acme/extract-functions"; |
| 4 | +import { GitlabSourceControl } from "@acme/source-control"; |
| 5 | +import { repositories, namespaces } from "@acme/extract-schema"; |
| 6 | +import { createClient } from '@libsql/client'; |
| 7 | +import { drizzle } from 'drizzle-orm/libsql'; |
| 8 | +import type { APIGatewayProxyHandlerV2 } from "aws-lambda"; |
| 9 | +import { z } from "zod"; |
| 10 | +import { Config } from "sst/node/config"; |
| 11 | + |
| 12 | +const client = createClient({ url: Config.DATABASE_URL, authToken: Config.DATABASE_AUTH_TOKEN }); |
| 13 | + |
| 14 | +const db = drizzle(client); |
| 15 | + |
| 16 | +const event = defineEvent(extractRepositoryEvent); |
| 17 | + |
| 18 | + |
| 19 | +const context: Context<GetRepositorySourceControl, GetRepositoryEntities> = { |
| 20 | + entities: { |
| 21 | + repositories, |
| 22 | + namespaces, |
| 23 | + }, |
| 24 | + integrations: { |
| 25 | + sourceControl: new GitlabSourceControl(Config.GITLAB_TOKEN), |
| 26 | + }, |
| 27 | + db, |
| 28 | +}; |
| 29 | + |
| 30 | +const inputSchema = z.object({ |
| 31 | + repositoryId: z.number(), |
| 32 | + repositoryName: z.string(), |
| 33 | + namespaceName: z.string(), |
| 34 | +}); |
| 35 | + |
| 36 | +type Input = z.infer<typeof inputSchema>; |
| 37 | + |
| 38 | +export const handler: APIGatewayProxyHandlerV2 = async (apiGatewayEvent) => { |
| 39 | + |
| 40 | + let input: Input; |
| 41 | + |
| 42 | + try { |
| 43 | + input = inputSchema.parse(apiGatewayEvent); |
| 44 | + } catch (error) { |
| 45 | + return { |
| 46 | + statusCode: 400, |
| 47 | + body: JSON.stringify({ error: (error as Error).message }), |
| 48 | + }; |
| 49 | + } |
| 50 | + |
| 51 | + const { repositoryId, repositoryName, namespaceName } = input; |
| 52 | + |
| 53 | + const { repository, namespace } = await getRepository({ externalRepositoryId: repositoryId, repositoryName, namespaceName }, context); |
| 54 | + |
| 55 | + await event.publish({ repository, namespace }, { caller: 'extract-repository', timestamp: new Date().getTime(), version: 1 }); |
| 56 | + |
| 57 | + return { |
| 58 | + statusCode: 200, |
| 59 | + body: JSON.stringify({}) |
| 60 | + }; |
| 61 | +} |
0 commit comments