-
-
Notifications
You must be signed in to change notification settings - Fork 361
Expand file tree
/
Copy pathgithub.ts
More file actions
229 lines (207 loc) · 6.62 KB
/
Copy pathgithub.ts
File metadata and controls
229 lines (207 loc) · 6.62 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
import { createFileRoute } from '@tanstack/react-router'
import { getAuthService } from '~/auth/index.server'
import { getGitHubAuthState } from '~/auth/github.server'
import { compileHandler } from '~/builder/api'
import {
createRepository,
pushFiles,
validateRepoName,
generateRepoDescription,
} from '~/utils/github-repo.server'
import { checkIpRateLimit, rateLimitedResponse, RATE_LIMITS } from '~/utils/rateLimit.server'
interface DeployRequest {
repoName: string
isPrivate: boolean
projectName: string
features: Array<string>
featureOptions: Record<string, Record<string, unknown>>
tailwind: boolean
}
interface DeployResponse {
success: true
repoUrl: string
owner: string
repoName: string
}
interface DeployError {
success: false
error: string
code:
| 'NOT_AUTHENTICATED'
| 'NO_GITHUB_ACCOUNT'
| 'MISSING_REPO_SCOPE'
| 'INVALID_REPO_NAME'
| 'REPO_NAME_TAKEN'
| 'REPO_CREATION_FAILED'
| 'PUSH_FAILED'
| 'COMPILE_FAILED'
| 'INVALID_REQUEST'
}
export const Route = createFileRoute('/api/builder/deploy/github')({
server: {
handlers: {
/**
* GET: Check GitHub auth state for current user
*/
GET: async ({ request }: { request: Request }) => {
const authService = getAuthService()
const user = await authService.getCurrentUser(request)
if (!user) {
return Response.json({
authenticated: false,
hasGitHubAccount: false,
hasRepoScope: false,
})
}
const authState = await getGitHubAuthState(user.userId)
return Response.json({
authenticated: true,
...authState,
})
},
/**
* POST: Create GitHub repo and push project files
*/
POST: async ({ request }: { request: Request }) => {
// Rate limiting (10 requests/minute per IP)
const rateLimit = await checkIpRateLimit(request, RATE_LIMITS.deploy)
if (!rateLimit.allowed) {
return rateLimitedResponse(rateLimit)
}
const authService = getAuthService()
const user = await authService.getCurrentUser(request)
if (!user) {
return Response.json(
{
success: false,
error: 'You must be logged in to deploy',
code: 'NOT_AUTHENTICATED',
} satisfies DeployError,
{ status: 401 },
)
}
const authState = await getGitHubAuthState(user.userId)
if (!authState.hasGitHubAccount) {
return Response.json(
{
success: false,
error: 'No GitHub account linked',
code: 'NO_GITHUB_ACCOUNT',
} satisfies DeployError,
{ status: 403 },
)
}
if (!authState.hasRepoScope || !authState.accessToken) {
console.log('[Deploy] Auth state check failed:', {
hasRepoScope: authState.hasRepoScope,
hasToken: !!authState.accessToken,
})
return Response.json(
{
success: false,
error: 'Missing public_repo scope. Please re-authenticate with GitHub.',
code: 'MISSING_REPO_SCOPE',
} satisfies DeployError,
{ status: 403 },
)
}
console.log('[Deploy] Auth state OK, hasRepoScope:', authState.hasRepoScope)
let body: DeployRequest
try {
body = await request.json()
} catch {
return Response.json(
{
success: false,
error: 'Invalid request body',
code: 'INVALID_REQUEST',
} satisfies DeployError,
{ status: 400 },
)
}
const { repoName, isPrivate, projectName, features, featureOptions, tailwind } = body
// Validate repo name
const validation = validateRepoName(repoName)
if (!validation.valid) {
return Response.json(
{
success: false,
error: validation.error ?? 'Invalid repository name',
code: 'INVALID_REPO_NAME',
} satisfies DeployError,
{ status: 400 },
)
}
// Compile the project
let compiledFiles: Record<string, string>
try {
const result = await compileHandler({
name: projectName,
tailwind,
features,
featureOptions,
})
compiledFiles = result.files
} catch (error) {
console.error('[Deploy] Compile failed:', error)
return Response.json(
{
success: false,
error: 'Failed to compile project',
code: 'COMPILE_FAILED',
} satisfies DeployError,
{ status: 500 },
)
}
// Create the repository
const description = generateRepoDescription(features)
console.log('[Deploy] Creating repository:', { repoName, isPrivate, description })
const createResult = await createRepository(authState.accessToken, {
name: repoName,
description,
isPrivate,
})
if (!createResult.success) {
console.error('[Deploy] Repository creation failed:', {
error: createResult.error,
code: createResult.code,
})
const code = createResult.code === 'NAME_TAKEN' ? 'REPO_NAME_TAKEN' : 'REPO_CREATION_FAILED'
return Response.json(
{
success: false,
error: createResult.error,
code,
} satisfies DeployError,
{ status: code === 'REPO_NAME_TAKEN' ? 409 : 500 },
)
}
console.log('[Deploy] Repository created:', createResult.repoUrl)
// Push files to the repository
const pushResult = await pushFiles(authState.accessToken, {
owner: createResult.owner,
repo: createResult.name,
files: compiledFiles,
message: 'Initial commit from TanStack Builder',
})
if (!pushResult.success) {
console.error('[Deploy] Push failed:', pushResult.error)
return Response.json(
{
success: false,
error: `Repository created but failed to push files: ${pushResult.error}`,
code: 'PUSH_FAILED',
} satisfies DeployError,
{ status: 500 },
)
}
return Response.json({
success: true,
repoUrl: createResult.repoUrl,
owner: createResult.owner,
repoName: createResult.name,
} satisfies DeployResponse)
},
},
},
})