Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a Check To Validate Proper Demo Link #859

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
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
53 changes: 53 additions & 0 deletions src/app/api/check-repo/route.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { NextResponse } from 'next/server'

async function fetchAllBranches(owner: string, repo: string) {
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/branches`)
const branches = await response.json()
return branches.map((branch: any) => branch.name)
}

async function fetchAllFiles(owner: string, repo: string, branch: string, path: string = ''): Promise<any[]> {
const response = await fetch(`https://api.github.com/repos/${owner}/${repo}/contents/${path}?ref=${branch}`)
const contents = await response.json()

let files: any[] = []
for (const item of contents) {
if (item.type === 'file') {
files.push(item)
} else if (item.type === 'dir') {
const subDirFiles = await fetchAllFiles(owner, repo, branch, item.path)
files = files.concat(subDirFiles)
}
}
return files
}

export async function POST(request: Request) {
const { repoUrl } = await request.json()

const repoMatch = repoUrl.match(/github\.com\/([^/]+)\/([^/]+)/)
if (!repoMatch) {
return NextResponse.json({ error: 'Invalid GitHub repository URL' }, { status: 400 })
}

const [_, owner, repo] = repoMatch

try {
const branches = await fetchAllBranches(owner, repo)
let allFiles: any[] = []

for (const branch of branches) {
const branchFiles = await fetchAllFiles(owner, repo, branch)
allFiles = allFiles.concat(branchFiles)
}

const hasWebsiteFiles = allFiles.some((file) =>
['index.html', 'index.htm', 'index.php', 'index.asp', 'index.css'].includes(file.name),
)

return NextResponse.json({ hasWebsiteFiles })
} catch (error) {
console.error('Error fetching repository contents:', error)
return NextResponse.json({ error: 'Failed to fetch repository contents' }, { status: 500 })
}
}
21 changes: 20 additions & 1 deletion src/app/harbor/shipyard/new-ship-form.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,18 @@ async function getReadmeFromRepo(url: string) {
return (await testReadmeLink(readmeURI)) ? readmeURI : null
}

async function checkRepoForPossibleWebsite(repoUrl: string) {
const response = await fetch('/api/check-repo', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ repoUrl }),
})
const data = await response.json()
return data.hasPossibleWebsite
}

export default function NewShipForm({
ships,
canvasRef,
Expand Down Expand Up @@ -121,8 +133,15 @@ export default function NewShipForm({
}

const repoUrl = formData.get('repo_url') as string
const assumedReadmeUrl = await getReadmeFromRepo(repoUrl)
const hasPossibleWebsite = await checkRepoForPossibleWebsite(repoUrl)
if (hasPossibleWebsite) {
toast({
title: "Warning",
description: 'We detected possible website files in your repo. Please submit a hosted demo link using vercel over a Youtube link.',
})
}

const assumedReadmeUrl = await getReadmeFromRepo(repoUrl)
if (!!assumedReadmeUrl) {
formData.set('readme_url', assumedReadmeUrl)
}
Expand Down
Loading