|
1 | 1 | import { extractRepositoryEvent, defineEvent } from "./events"; |
2 | 2 | import { getRepository } from "@acme/extract-functions"; |
3 | 3 | import type { Context, GetRepositorySourceControl, GetRepositoryEntities } from "@acme/extract-functions"; |
4 | | -import { GitlabSourceControl } from "@acme/source-control"; |
| 4 | +import { GitlabSourceControl, GitHubSourceControl } from "@acme/source-control"; |
5 | 5 | import { repositories, namespaces } from "@acme/extract-schema"; |
6 | 6 | import { createClient } from '@libsql/client'; |
7 | 7 | import { drizzle } from 'drizzle-orm/libsql'; |
8 | | -import type { APIGatewayProxyHandlerV2 } from "aws-lambda"; |
9 | 8 | import { z } from "zod"; |
10 | 9 | import { Config } from "sst/node/config"; |
| 10 | +import { Clerk } from "@clerk/clerk-sdk-node"; |
| 11 | +import { ApiHandler, useJsonBody } from 'sst/node/api'; |
11 | 12 |
|
| 13 | +const clerkClient = Clerk({ secretKey: Config.CLERK_SECRET_KEY }); |
12 | 14 | const client = createClient({ url: Config.DATABASE_URL, authToken: Config.DATABASE_AUTH_TOKEN }); |
13 | 15 |
|
14 | 16 | const db = drizzle(client); |
15 | 17 |
|
16 | 18 | const event = defineEvent(extractRepositoryEvent); |
17 | 19 |
|
| 20 | +const fetchSourceControlAccessToken = async (userId: string, forgeryIdProvider: 'oauth_github' | 'oauth_gitlab') => { |
| 21 | + const [userOauthAccessTokenPayload, ...rest] = await clerkClient.users.getUserOauthAccessToken(userId, forgeryIdProvider); |
| 22 | + if (!userOauthAccessTokenPayload) throw new Error("Failed to get token"); |
| 23 | + if (rest.length !== 0) throw new Error("wtf ?"); |
| 24 | + |
| 25 | + return userOauthAccessTokenPayload.token; |
| 26 | +} |
18 | 27 |
|
19 | 28 | const context: Context<GetRepositorySourceControl, GetRepositoryEntities> = { |
20 | 29 | entities: { |
21 | 30 | repositories, |
22 | 31 | namespaces, |
23 | 32 | }, |
24 | 33 | integrations: { |
25 | | - sourceControl: new GitlabSourceControl(Config.GITLAB_TOKEN), |
| 34 | + sourceControl: null, |
26 | 35 | }, |
27 | 36 | db, |
28 | 37 | }; |
29 | 38 |
|
| 39 | +const contextSchema = z.object({ |
| 40 | + authorizer: z.object({ |
| 41 | + jwt: z.object({ |
| 42 | + claims: z.object({ |
| 43 | + sub: z.string(), |
| 44 | + }), |
| 45 | + }), |
| 46 | + }), |
| 47 | +}); |
| 48 | + |
| 49 | +type CTX = z.infer<typeof contextSchema>; |
| 50 | + |
30 | 51 | const inputSchema = z.object({ |
31 | 52 | repositoryId: z.number(), |
32 | 53 | repositoryName: z.string(), |
33 | 54 | namespaceName: z.string(), |
| 55 | + sourceControl: z.literal("gitlab").or(z.literal("github")), |
34 | 56 | }); |
35 | 57 |
|
36 | 58 | type Input = z.infer<typeof inputSchema>; |
37 | 59 |
|
38 | | -export const handler: APIGatewayProxyHandlerV2 = async (apiGatewayEvent) => { |
| 60 | +export const handler = ApiHandler(async (ev) => { |
| 61 | + |
| 62 | + const body = useJsonBody() as unknown; |
| 63 | + |
| 64 | + let lambdaContext: CTX; |
| 65 | + |
| 66 | + try { |
| 67 | + lambdaContext = contextSchema.parse(ev.requestContext); |
| 68 | + } catch (error) { |
| 69 | + return { |
| 70 | + statusCode: 401, |
| 71 | + body: JSON.stringify({ error: (error as Error).message }), |
| 72 | + }; |
| 73 | + } |
39 | 74 |
|
40 | 75 | let input: Input; |
| 76 | + let sourceControlAccessToken: string; |
41 | 77 |
|
42 | 78 | try { |
43 | | - input = inputSchema.parse(apiGatewayEvent); |
| 79 | + input = inputSchema.parse(body); |
| 80 | + |
44 | 81 | } catch (error) { |
45 | 82 | return { |
46 | 83 | statusCode: 400, |
47 | 84 | body: JSON.stringify({ error: (error as Error).message }), |
48 | 85 | }; |
49 | 86 | } |
50 | 87 |
|
51 | | - const { repositoryId, repositoryName, namespaceName } = input; |
| 88 | + const { sub } = lambdaContext.authorizer.jwt.claims; |
| 89 | + |
| 90 | + |
| 91 | + const { repositoryId, repositoryName, namespaceName, sourceControl } = input; |
| 92 | + |
| 93 | + try { |
| 94 | + sourceControlAccessToken = await fetchSourceControlAccessToken(sub, `oauth_${sourceControl}`); |
| 95 | + } catch (error) { |
| 96 | + return { |
| 97 | + statusCode: 500, |
| 98 | + body: JSON.stringify({ error: (error as Error).message }), |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + if (sourceControl === "gitlab") { |
| 103 | + context.integrations.sourceControl = new GitlabSourceControl(sourceControlAccessToken); |
| 104 | + } else if (sourceControl === "github") { |
| 105 | + context.integrations.sourceControl = new GitHubSourceControl(sourceControlAccessToken); |
| 106 | + } |
52 | 107 |
|
53 | 108 | const { repository, namespace } = await getRepository({ externalRepositoryId: repositoryId, repositoryName, namespaceName }, context); |
54 | 109 |
|
55 | | - await event.publish({ repository, namespace }, { caller: 'extract-repository', timestamp: new Date().getTime(), version: 1 }); |
| 110 | + await event.publish({ repository, namespace }, { caller: 'extract-repository', timestamp: new Date().getTime(), version: 1, sourceControl, userId: sub }); |
56 | 111 |
|
57 | 112 | return { |
58 | 113 | statusCode: 200, |
59 | 114 | body: JSON.stringify({}) |
60 | 115 | }; |
61 | | -} |
| 116 | +}); |
0 commit comments