-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
113 lines (101 loc) · 2.79 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
import { Hono } from 'hono'
import { cors } from 'hono/cors'
import { z } from 'zod'
import { OrganizationGithubManager, setupGithubUserForOrg } from "./util/github"
import { discordServer } from "./util/discord"
const app = new Hono()
app.use(
'/colony/*',
cors({
origin: process.env.NODE_ENV === 'production' ? 'https://www.ubclaunchpad.com' : ['http://localhost:3000', '*'],
allowHeaders: ['Origin', 'Content-Type', 'Authorization'],
allowMethods: ['POST', 'GET', 'OPTIONS', 'PUT', 'DELETE'],
exposeHeaders: ['Content-Length'],
maxAge: 600,
})
)
const privateKey = process.env.GH_KEY!
const appId = process.env.GH_APP_ID!
const orgClientId = process.env.GH_CLIENT_ID!
const orgAppId = process.env.GH_ORG_APP_ID!
const orgName = process.env.GH_ORG_NAME!
const githubManager = new OrganizationGithubManager({
appId: appId,
privateKey: privateKey,
orgName: orgName,
orgAppId: parseInt(orgAppId),
orgClientId: orgClientId,
})
app.get('/', (c) => {
return c.text('Launch Pad Colony Engine API')
})
type GithubTeam = {
name: string
role: "maintainer" | "member"
}
const GithubIntegrationSchema = z.object({
githubUsername: z.string(),
actions: z.object({
teams: z.array(
z.object({
name: z.string(),
role: z.enum(["maintainer", "member"]),
})
),
}),
})
app.post('/colony/integrations/github', async (c) => {
try {
const body = await c.req.json()
const parseResult = GithubIntegrationSchema.safeParse(body)
if (parseResult.success) {
try {
await setupGithubUserForOrg({
manager: githubManager,
options: parseResult.data,
})
return c.text('All actions completed successfully', 200)
} catch (e) {
console.warn(e)
return c.text((e as Error).message, 400)
}
} else {
return c.json(parseResult.error.errors, 400)
}
} catch (e) {
console.warn(e)
return c.text('Internal server error', 500)
}
})
const DiscordIntegrationSchema = z.object({
discordUsername: z.string(),
actions: z.object({
roles: z.array(z.string()),
}),
})
app.post('/colony/integrations/discord', async (c) => {
try {
const body = await c.req.json()
const parseResult = DiscordIntegrationSchema.safeParse(body)
if (parseResult.success) {
try {
await discordServer.addRolesToUser(
parseResult.data.discordUsername,
parseResult.data.actions.roles
)
return c.text('All roles added successfully', 200)
} catch (e) {
return c.text((e as Error).message, 400)
}
} else {
return c.json(parseResult.error.errors, 400)
}
} catch (e) {
console.warn(e)
return c.text('Internal server error', 500)
}
})
export default {
port: process.env.PORT || 3000,
fetch: app.fetch,
}