|
| 1 | +import { readFile } from 'fs/promises'; |
| 2 | +import { type NextRequest, NextResponse } from 'next/server'; |
| 3 | +import { join } from 'path'; |
| 4 | + |
| 5 | +export async function GET(request: NextRequest) { |
| 6 | + const searchParams = request.nextUrl.searchParams; |
| 7 | + const path = searchParams.get('path'); |
| 8 | + |
| 9 | + if (!path) { |
| 10 | + return NextResponse.json( |
| 11 | + { error: 'Path parameter is required' }, |
| 12 | + { status: 400 } |
| 13 | + ); |
| 14 | + } |
| 15 | + |
| 16 | + // Dev override to fetch the monster patch without required GitHub |
| 17 | + if (path === '/nodejs/node/pull/59805') { |
| 18 | + try { |
| 19 | + const localPatchPath = join( |
| 20 | + process.cwd(), |
| 21 | + 'app/api/fetch-pr-patch', |
| 22 | + 'larg2.patch' |
| 23 | + // 'smol.patch' |
| 24 | + ); |
| 25 | + const patchContent = await readFile(localPatchPath, 'utf-8'); |
| 26 | + return NextResponse.json({ |
| 27 | + content: patchContent, |
| 28 | + url: 'local', |
| 29 | + }); |
| 30 | + } catch (error) { |
| 31 | + return NextResponse.json( |
| 32 | + { |
| 33 | + error: `Failed to read local patch: ${error instanceof Error ? error.message : 'Unknown error'}`, |
| 34 | + }, |
| 35 | + { status: 500 } |
| 36 | + ); |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + try { |
| 41 | + // Validate the path format (should be /org/repo/pull/{number}) |
| 42 | + const pathSegments = path.split('/').filter(Boolean); |
| 43 | + if (pathSegments.length < 4 || pathSegments[2] !== 'pull') { |
| 44 | + return NextResponse.json( |
| 45 | + { error: 'Invalid GitHub PR path format' }, |
| 46 | + { status: 400 } |
| 47 | + ); |
| 48 | + } |
| 49 | + |
| 50 | + // Ensure the path ends with .patch |
| 51 | + let patchPath = path; |
| 52 | + if (!patchPath.endsWith('.patch')) { |
| 53 | + patchPath += '.patch'; |
| 54 | + } |
| 55 | + |
| 56 | + // Construct the full GitHub URL server-side |
| 57 | + const patchURL = `https://github.com${patchPath}`; |
| 58 | + |
| 59 | + // Fetch the patch from GitHub |
| 60 | + const response = await fetch(patchURL, { |
| 61 | + headers: { |
| 62 | + 'User-Agent': 'pierre-js', |
| 63 | + }, |
| 64 | + }); |
| 65 | + |
| 66 | + if (!response.ok) { |
| 67 | + return NextResponse.json( |
| 68 | + { error: `Failed to fetch patch: ${response.statusText}` }, |
| 69 | + { status: response.status } |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + const patchContent = await response.text(); |
| 74 | + |
| 75 | + return NextResponse.json({ |
| 76 | + content: patchContent, |
| 77 | + url: patchURL, |
| 78 | + }); |
| 79 | + } catch (error) { |
| 80 | + return NextResponse.json( |
| 81 | + { error: error instanceof Error ? error.message : 'Unknown error' }, |
| 82 | + { status: 500 } |
| 83 | + ); |
| 84 | + } |
| 85 | +} |
0 commit comments