|
| 1 | +import { NextRequest, NextResponse } from 'next/server' |
| 2 | + |
| 3 | +export const dynamic = 'force-dynamic' |
| 4 | + |
| 5 | +export async function GET(request: NextRequest) { |
| 6 | + try { |
| 7 | + if (!process.env.GITHUB_TOKEN) { |
| 8 | + return NextResponse.json({ error: 'GitHub token not configured' }, { status: 500 }) |
| 9 | + } |
| 10 | + |
| 11 | + const owner = request.nextUrl.searchParams.get('owner') |
| 12 | + |
| 13 | + if (!owner) { |
| 14 | + return NextResponse.json({ error: 'Owner parameter is required' }, { status: 400 }) |
| 15 | + } |
| 16 | + |
| 17 | + // First, get the authenticated user to check if this is their repos |
| 18 | + const userResponse = await fetch('https://api.github.com/user', { |
| 19 | + headers: { |
| 20 | + Authorization: `Bearer ${process.env.GITHUB_TOKEN}`, |
| 21 | + Accept: 'application/vnd.github.v3+json', |
| 22 | + }, |
| 23 | + }) |
| 24 | + |
| 25 | + let isAuthenticatedUser = false |
| 26 | + if (userResponse.ok) { |
| 27 | + const user = await userResponse.json() |
| 28 | + isAuthenticatedUser = user.login === owner |
| 29 | + } |
| 30 | + |
| 31 | + // Fetch all repositories by paginating through all pages |
| 32 | + const allRepos: any[] = [] |
| 33 | + let page = 1 |
| 34 | + const perPage = 100 // GitHub's maximum per page |
| 35 | + |
| 36 | + while (true) { |
| 37 | + let apiUrl: string |
| 38 | + |
| 39 | + if (isAuthenticatedUser) { |
| 40 | + // Use /user/repos for authenticated user to get private repos, but only owned repos |
| 41 | + apiUrl = `https://api.github.com/user/repos?sort=name&direction=asc&per_page=${perPage}&page=${page}&visibility=all&affiliation=owner` |
| 42 | + } else { |
| 43 | + // Check if it's an organization |
| 44 | + const orgResponse = await fetch(`https://api.github.com/orgs/${owner}`, { |
| 45 | + headers: { |
| 46 | + Authorization: `Bearer ${process.env.GITHUB_TOKEN}`, |
| 47 | + Accept: 'application/vnd.github.v3+json', |
| 48 | + }, |
| 49 | + }) |
| 50 | + |
| 51 | + if (orgResponse.ok) { |
| 52 | + // Use /orgs/{org}/repos for organizations to get private repos |
| 53 | + apiUrl = `https://api.github.com/orgs/${owner}/repos?sort=name&direction=asc&per_page=${perPage}&page=${page}` |
| 54 | + } else { |
| 55 | + // Fallback to /users/{owner}/repos (public only) |
| 56 | + apiUrl = `https://api.github.com/users/${owner}/repos?sort=name&direction=asc&per_page=${perPage}&page=${page}` |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + const response = await fetch(apiUrl, { |
| 61 | + headers: { |
| 62 | + Authorization: `Bearer ${process.env.GITHUB_TOKEN}`, |
| 63 | + Accept: 'application/vnd.github.v3+json', |
| 64 | + }, |
| 65 | + }) |
| 66 | + |
| 67 | + if (!response.ok) { |
| 68 | + throw new Error(`GitHub API error: ${response.status}`) |
| 69 | + } |
| 70 | + |
| 71 | + const repos = await response.json() |
| 72 | + |
| 73 | + // If we get fewer repos than the per_page limit, we've reached the end |
| 74 | + if (repos.length === 0) { |
| 75 | + break |
| 76 | + } |
| 77 | + |
| 78 | + allRepos.push(...repos) |
| 79 | + |
| 80 | + // If we got fewer than the max per page, we've reached the end |
| 81 | + if (repos.length < perPage) { |
| 82 | + break |
| 83 | + } |
| 84 | + |
| 85 | + page++ |
| 86 | + } |
| 87 | + |
| 88 | + // Remove duplicates based on full_name (owner/repo) |
| 89 | + const uniqueRepos = allRepos.filter( |
| 90 | + (repo, index, self) => index === self.findIndex((r) => r.full_name === repo.full_name), |
| 91 | + ) |
| 92 | + |
| 93 | + // Sort alphabetically by name (GitHub API sort might not be perfect) |
| 94 | + uniqueRepos.sort((a, b) => a.name.toLowerCase().localeCompare(b.name.toLowerCase())) |
| 95 | + |
| 96 | + return NextResponse.json( |
| 97 | + uniqueRepos.map((repo: any) => ({ |
| 98 | + name: repo.name, |
| 99 | + full_name: repo.full_name, |
| 100 | + description: repo.description, |
| 101 | + private: repo.private, |
| 102 | + clone_url: repo.clone_url, |
| 103 | + updated_at: repo.updated_at, |
| 104 | + language: repo.language, |
| 105 | + })), |
| 106 | + ) |
| 107 | + } catch (error) { |
| 108 | + console.error('Error fetching GitHub repositories:', error) |
| 109 | + return NextResponse.json({ error: 'Failed to fetch repositories' }, { status: 500 }) |
| 110 | + } |
| 111 | +} |
0 commit comments