Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
117 changes: 117 additions & 0 deletions app/api/github/create-repo/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@
return proAccess.response
}

const { app, repoName, scaffoldFiles } = (await request.json()) as {
app: TemplateApp
repoName: string
scaffoldFiles?: Record<string, unknown>
}

if (!repoName || repoName.trim().length === 0) {
return NextResponse.json({ error: 'Repository name required' }, { status: 400 })
if (!accessToken) {
return NextResponse.json({ error: 'Not authenticated' }, { status: 401 })
}
Expand All @@ -25,6 +33,22 @@
return NextResponse.json({ error: parsedBody.error.issues[0]?.message ?? 'Invalid repository request' }, { status: 400 })
}

const newRepo = await createRepoRes.json()

const filesToCreate = scaffoldFiles
? normalizeScaffoldFiles(scaffoldFiles)
: generateTemplateFiles(app)

// Create files in the new repository
for (const [fileName, content] of Object.entries(filesToCreate)) {
await createFileInRepo(
githubUsername,
repoName,
fileName,
content,
accessToken
)
}
const result = await createGitHubRepositoryFromBlueprint({
accessToken,
repoName: parsedBody.data.repoName,
Expand All @@ -37,11 +61,104 @@
repository: result.repository,
filesCreated: result.files_created,
})
} catch (error) {

Check failure on line 64 in app/api/github/create-repo/route.ts

View workflow job for this annotation

GitHub Actions / Lint & Type Check

'try' expected.
console.error('Error creating repository:', error)
return NextResponse.json({ error: 'Failed to create repository' }, { status: 500 })
}
}

async function createFileInRepo(

Check failure on line 70 in app/api/github/create-repo/route.ts

View workflow job for this annotation

GitHub Actions / Lint & Type Check

'catch' or 'finally' expected.
owner: string,
repo: string,
path: string,
content: string,
accessToken: string
): Promise<void> {
const encodedContent = Buffer.from(content).toString('base64')

await fetch(
`https://api.github.com/repos/${owner}/${repo}/contents/${path}`,
{
method: 'PUT',
headers: {
'Authorization': `Bearer ${accessToken}`,
'Accept': 'application/vnd.github+json',
},
body: JSON.stringify({
message: `Add ${path}`,
content: encodedContent,
}),
}
)
}

function normalizeScaffoldFiles(files: Record<string, unknown>): Record<string, string> {
return Object.fromEntries(
Object.entries(files).map(([path, content]) => [
path,
typeof content === 'string' ? content : JSON.stringify(content, null, 2),
])
)
}

function generateTemplateFiles(app: TemplateApp): Record<string, string> {
return {
'README.md': `# ${app.app_name}

${app.description}

## Technologies
${app.technologies.map((t: string) => `- ${t}`).join('\n')}

## Getting Started

\`\`\`bash
npm install
npm run dev
\`\`\`

## Difficulty Level
${app.difficulty_level}

## Notes
${app.ai_explanation}

${app.missing_files.length > 0 ? `
## Missing Files to Add
${app.missing_files.map((f: string) => `- [ ] ${f}`).join('\n')}
` : ''}
`,
'package.json': JSON.stringify(
{
name: app.app_name.toLowerCase().replace(/\s+/g, '-'),
version: '1.0.0',
description: app.description,
scripts: {
dev: 'next dev',
build: 'next build',
start: 'next start',
},
dependencies: {
react: '^18.0.0',
'next': '^14.0.0',
},
},
null,
2
),
'.gitignore': `node_modules/
.env
.env.local
.env.*.local
.next/
dist/
build/
*.log
.DS_Store
`,
return NextResponse.json(

Check failure on line 159 in app/api/github/create-repo/route.ts

View workflow job for this annotation

GitHub Actions / Lint & Type Check

':' expected.
{ error: error instanceof Error ? error.message : 'Failed to create repository' },
{ status: 500 },
)
}
}
26 changes: 24 additions & 2 deletions app/dashboard/results/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,30 @@ export default function ResultsPage() {

const data = await res.json()
if (data.success) {
alert(`Scaffold generated for ${app.name}! Creating repository...`)
// TODO: Create GitHub repo with scaffold
const repoName = app.name.toLowerCase().replace(/\s+/g, '-').replace(/[^a-z0-9-]/g, '')
const repoRes = await fetch('/api/github/create-repo', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
app: {
app_name: app.name,
app_type: app.category,
description: app.description,
technologies: app.technologies,
difficulty_level: app.estimatedBuildTime,
ai_explanation: '',
missing_files: app.missingFiles,
},
repoName,
scaffoldFiles: data.scaffold?.files,
}),
})
const repoData = await repoRes.json()
if (repoData.success) {
alert(`Repository created: ${repoData.repository.url}`)
} else {
alert(`Scaffold generated but repo creation failed: ${repoData.error}`)
}
}
} catch (error) {
console.error('Scaffold generation failed:', error)
Expand Down
Loading