-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
159 lines (149 loc) · 4.55 KB
/
index.ts
File metadata and controls
159 lines (149 loc) · 4.55 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import { getCommit, triggerWorkflow } from "@/lib/github"
import { RequestError } from "@octokit/request-error"
import { ActionError, defineAction, z } from "astro:actions"
import Cloudflare from "cloudflare"
import Stripe from "stripe"
export const server = {
deleteApp: defineAction({
input: z.object({
id: z.string(),
}),
handler: async ({ id }, context) => {
// TODO: protect /_actions/deleteApp in clerk?
const { userId } = context.locals.auth()
if (!userId) {
throw new ActionError({
code: "UNAUTHORIZED",
})
}
const app = await context.locals.db.apps.delete({
id,
userId,
})
if (!app) {
throw new ActionError({
code: "NOT_FOUND",
message: "App not found.",
})
}
if (app.status === "deployed") {
const cloudflare = new Cloudflare({
apiToken: context.locals.runtime.env.CLOUDFLARE_API_TOKEN,
})
await cloudflare.workersForPlatforms.dispatch.namespaces.scripts.delete(
context.locals.runtime.env.CF_DISPATCH_NAMESPACE,
id,
{
account_id: context.locals.runtime.env.CLOUDFLARE_ACCOUNT_ID,
},
)
}
return { success: true }
},
}),
deployApp: defineAction({
accept: "form",
input: z.object({
repoUrl: z
.string()
.min(1)
.refine(
(data) => /^https:\/\/github\.com\/[^\/]+\/[^\/]+$/.test(data),
{
message:
"Repo must be a valid GitHub URL in the format 'https://github.com/username/repository'.",
},
),
branch: z.string().min(1),
directory: z.string().optional(),
}),
// https://github.com/withastro/roadmap/blob/actions/proposals/0046-actions.md#access-api-context
handler: async ({ repoUrl, branch, directory }, context) => {
// TODO: protect /_actions/deployApp in clerk?
const { userId } = context.locals.auth()
if (!userId) {
throw new ActionError({
code: "UNAUTHORIZED",
})
}
try {
const repoPath = new URL(repoUrl).pathname
const [owner, repo] = repoPath.substring(1).split("/")
// fetch the head commit for repo + branch from GitHub
const commit = await getCommit({ owner, repo, branch })
const app = await context.locals.db.apps.create({
userId,
githubOwner: owner,
repo,
branch,
commitHash: commit.sha,
directory,
})
await triggerWorkflow(context.locals.runtime.env.GITHUB_ACCESS_TOKEN, {
appId: app.id,
owner,
repo,
commitHash: commit.sha,
directory,
dispatchNamespace: context.locals.runtime.env.CF_DISPATCH_NAMESPACE,
})
return { success: true }
} catch (e) {
console.error(e)
if (e instanceof Error) {
if (e.message.includes("UNIQUE constraint failed")) {
throw new ActionError({
code: "CONFLICT",
message: "An app with these details already exists.",
})
} else if (e instanceof RequestError) {
throw new ActionError({
code: "BAD_REQUEST",
message: "Invalid repo URL.",
})
}
}
throw new ActionError({
code: "INTERNAL_SERVER_ERROR",
message: "Unexpected issue saving the app.",
})
}
},
}),
createCheckoutSession: defineAction({
input: z.object({
appId: z.string(),
}),
handler: async ({ appId }, context) => {
const stripe = new Stripe(context.locals.runtime.env.STRIPE_SECRET_KEY)
const { origin } = new URL(context.request.url)
const metadata: Stripe.MetadataParam = {
appId,
}
if (context.locals.auth().userId) {
metadata.userId = context.locals.auth().userId
}
const session = await stripe.checkout.sessions.create({
line_items: [
{
price: context.locals.runtime.env.STRIPE_TOPUP_PRICE_ID,
quantity: 1,
},
],
mode: "payment",
// TODO: encrypt
metadata,
success_url: `${origin}/checkout-sessions/{CHECKOUT_SESSION_ID}/success`,
cancel_url: `${origin}?canceled=true`,
})
if (!session.url) {
throw new ActionError({
code: "INTERNAL_SERVER_ERROR",
message: "Failed to create checkout session.",
})
} else {
return session.url
}
},
}),
}