From ef39d482e771289803b5bc33c90b4fcca6362489 Mon Sep 17 00:00:00 2001 From: Scooter Date: Mon, 25 Nov 2024 16:22:05 -0500 Subject: [PATCH] Add a Check To Validate Proper Demo Link Adds an API route to check if a GitHub repo has possible website files and shows a warning if they posted a Youtube Link instead of a vercel etc. link. --- src/app/api/check-repo/route.ts | 53 +++++++++++++++++++++++ src/app/harbor/shipyard/new-ship-form.tsx | 21 ++++++++- 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 src/app/api/check-repo/route.ts diff --git a/src/app/api/check-repo/route.ts b/src/app/api/check-repo/route.ts new file mode 100644 index 00000000..698b5ba3 --- /dev/null +++ b/src/app/api/check-repo/route.ts @@ -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 { + 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 }) + } +} diff --git a/src/app/harbor/shipyard/new-ship-form.tsx b/src/app/harbor/shipyard/new-ship-form.tsx index b646da51..e775c8d5 100644 --- a/src/app/harbor/shipyard/new-ship-form.tsx +++ b/src/app/harbor/shipyard/new-ship-form.tsx @@ -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, @@ -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) }