Skip to content

Commit 1a98d1f

Browse files
authored
templates (#161)
* remove vercel project * fixes * format
1 parent fa6c0cb commit 1a98d1f

File tree

8 files changed

+403
-246
lines changed

8 files changed

+403
-246
lines changed

app/api/github/repos/create/route.ts

Lines changed: 18 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
11
import { NextResponse } from 'next/server'
22
import { getServerSession } from '@/lib/session/get-server-session'
33
import { getUserGitHubToken } from '@/lib/github/user-token'
4-
import { getOAuthToken } from '@/lib/session/get-oauth-token'
5-
import { createProject } from '@/lib/vercel-client/projects'
64
import { Octokit } from '@octokit/rest'
75

86
interface RepoTemplate {
97
id: string
108
name: string
119
description: string
12-
sourceRepo?: string
13-
sourceFolder?: string
10+
cloneUrl?: string
11+
image?: string
1412
}
1513

1614
// Helper function to recursively copy files from a directory
@@ -45,9 +43,11 @@ async function copyFilesRecursively(
4543
const content = await response.text()
4644

4745
// Calculate relative path by removing the base path prefix
48-
const relativePath = item.path.startsWith(basePath + '/')
49-
? item.path.substring(basePath.length + 1)
50-
: item.name
46+
const relativePath = basePath
47+
? item.path.startsWith(basePath + '/')
48+
? item.path.substring(basePath.length + 1)
49+
: item.name
50+
: item.path
5151

5252
// Create file in new repository
5353
await octokit.repos.createOrUpdateFileContents({
@@ -74,27 +74,28 @@ async function copyFilesRecursively(
7474

7575
// Helper function to copy files from template repository
7676
async function populateRepoFromTemplate(octokit: Octokit, repoOwner: string, repoName: string, template: RepoTemplate) {
77-
if (!template.sourceRepo || !template.sourceFolder) {
77+
if (!template.cloneUrl) {
7878
return
7979
}
8080

81-
// Parse source repository
82-
const sourceMatch = template.sourceRepo.match(/github\.com\/([\w-]+)\/([\w-]+)/)
83-
if (!sourceMatch) {
84-
throw new Error('Invalid source repository URL')
81+
// Parse clone URL to get owner and repo name
82+
const cloneMatch = template.cloneUrl.match(/github\.com\/([\w-]+)\/([\w-]+?)(?:\.git)?$/)
83+
if (!cloneMatch) {
84+
throw new Error('Invalid clone URL')
8585
}
8686

87-
const [, sourceOwner, sourceRepoName] = sourceMatch
87+
const [, sourceOwner, sourceRepoName] = cloneMatch
8888

8989
try {
90+
// Get all files from the root of the template repository
9091
await copyFilesRecursively(
9192
octokit,
9293
sourceOwner,
9394
sourceRepoName,
94-
template.sourceFolder,
95+
'', // Root path
9596
repoOwner,
9697
repoName,
97-
template.sourceFolder,
98+
'', // Root path
9899
)
99100
} catch (error) {
100101
console.error('Error populating repository from template:', error)
@@ -122,7 +123,7 @@ export async function POST(request: Request) {
122123
}
123124

124125
// Parse request body
125-
const { name, description, private: isPrivate, owner, template, vercel } = await request.json()
126+
const { name, description, private: isPrivate, owner, template } = await request.json()
126127

127128
if (!name || typeof name !== 'string') {
128129
return NextResponse.json({ error: 'Repository name is required' }, { status: 400 })
@@ -187,7 +188,7 @@ export async function POST(request: Request) {
187188
}
188189

189190
// If a template is selected, populate the repository
190-
if (template && template.id !== 'none') {
191+
if (template) {
191192
try {
192193
await populateRepoFromTemplate(octokit, repo.data.owner.login, repo.data.name, template as RepoTemplate)
193194
} catch (error) {
@@ -197,50 +198,13 @@ export async function POST(request: Request) {
197198
}
198199
}
199200

200-
// Create Vercel project if requested
201-
let vercelProject
202-
if (vercel && vercel.teamId && vercel.projectName && session.authProvider === 'vercel') {
203-
try {
204-
// Get Vercel access token
205-
const tokenData = await getOAuthToken(session.user.id, 'vercel')
206-
if (tokenData) {
207-
vercelProject = await createProject(tokenData.accessToken, vercel.teamId, {
208-
name: vercel.projectName,
209-
gitRepository: {
210-
type: 'github',
211-
repo: repo.data.full_name, // Format: "owner/repo"
212-
},
213-
framework: null, // Let Vercel auto-detect
214-
})
215-
216-
if (vercelProject) {
217-
console.log('Successfully created Vercel project')
218-
} else {
219-
console.error(
220-
'Failed to create Vercel project - user may need to reconnect Vercel account with updated permissions',
221-
)
222-
}
223-
}
224-
} catch (error) {
225-
console.error('Error creating Vercel project:', error)
226-
// Don't fail the entire operation if Vercel project creation fails
227-
// The repository was created successfully
228-
}
229-
}
230-
231201
return NextResponse.json({
232202
success: true,
233203
name: repo.data.name,
234204
full_name: repo.data.full_name,
235205
clone_url: repo.data.clone_url,
236206
html_url: repo.data.html_url,
237207
private: repo.data.private,
238-
vercel_project: vercelProject
239-
? {
240-
id: vercelProject.id,
241-
name: vercelProject.name,
242-
}
243-
: undefined,
244208
})
245209
} catch (error: unknown) {
246210
console.error('GitHub API error:', error)

0 commit comments

Comments
 (0)