|
| 1 | +// types.ts |
| 2 | +export interface GitHubContributor { |
| 3 | + username: string; |
| 4 | + avatarUrl: string; |
| 5 | + profileUrl: string; |
| 6 | + commitCount: number; |
| 7 | + firstCommit: string; |
| 8 | + lastCommit: string; |
| 9 | +} |
| 10 | + |
| 11 | +interface GitHubCommit { |
| 12 | + sha: string; |
| 13 | + commit: { |
| 14 | + author: { |
| 15 | + name: string; |
| 16 | + date: string; |
| 17 | + }; |
| 18 | + }; |
| 19 | + author: { |
| 20 | + login: string; |
| 21 | + avatar_url: string; |
| 22 | + html_url: string; |
| 23 | + } | null; |
| 24 | +} |
| 25 | + |
| 26 | +// utils/github.ts |
| 27 | +export async function getFileContributors( |
| 28 | + owner: string, |
| 29 | + repo: string, |
| 30 | + filePath: string, |
| 31 | + githubToken?: string, |
| 32 | +): Promise<GitHubContributor[]> { |
| 33 | + const headers: HeadersInit = { |
| 34 | + Accept: "application/vnd.github.v3+json", |
| 35 | + ...(githubToken && { Authorization: `token ${githubToken}` }), |
| 36 | + }; |
| 37 | + |
| 38 | + try { |
| 39 | + const response = await fetch( |
| 40 | + `https://api.github.com/repos/${owner}/${repo}/commits?path=${encodeURIComponent(filePath)}`, |
| 41 | + { headers }, |
| 42 | + ); |
| 43 | + |
| 44 | + if (!response.ok) { |
| 45 | + throw new Error(`GitHub API error: ${response.statusText}`); |
| 46 | + } |
| 47 | + |
| 48 | + const commits: GitHubCommit[] = await response.json(); |
| 49 | + const contributorsMap = new Map<string, GitHubContributor>(); |
| 50 | + |
| 51 | + commits.forEach((commit) => { |
| 52 | + const author = commit.author || { |
| 53 | + login: commit.commit.author.name, |
| 54 | + avatar_url: "", |
| 55 | + html_url: "", |
| 56 | + }; |
| 57 | + |
| 58 | + const username = author.login; |
| 59 | + |
| 60 | + if (!contributorsMap.has(username)) { |
| 61 | + contributorsMap.set(username, { |
| 62 | + username, |
| 63 | + avatarUrl: author.avatar_url, |
| 64 | + profileUrl: author.html_url, |
| 65 | + commitCount: 0, |
| 66 | + firstCommit: commit.commit.author.date, |
| 67 | + lastCommit: commit.commit.author.date, |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + const contributor = contributorsMap.get(username)!; |
| 72 | + contributor.commitCount += 1; |
| 73 | + |
| 74 | + // Update first and last commit dates |
| 75 | + if ( |
| 76 | + new Date(commit.commit.author.date) < new Date(contributor.firstCommit) |
| 77 | + ) { |
| 78 | + contributor.firstCommit = commit.commit.author.date; |
| 79 | + } |
| 80 | + if ( |
| 81 | + new Date(commit.commit.author.date) > new Date(contributor.lastCommit) |
| 82 | + ) { |
| 83 | + contributor.lastCommit = commit.commit.author.date; |
| 84 | + } |
| 85 | + }); |
| 86 | + |
| 87 | + return Array.from(contributorsMap.values()).sort( |
| 88 | + (a, b) => b.commitCount - a.commitCount, |
| 89 | + ); |
| 90 | + } catch (error) { |
| 91 | + console.error("Error fetching file contributors:", error); |
| 92 | + throw error; |
| 93 | + } |
| 94 | +} |
| 95 | + |
| 96 | +// Example usage in a Next.js API route (app/api/contributors/route.ts): |
| 97 | +// import { NextResponse } from "next/server"; |
| 98 | +// |
| 99 | +// export async function GET(request: Request) { |
| 100 | +// const { searchParams } = new URL(request.url); |
| 101 | +// const owner = searchParams.get("owner"); |
| 102 | +// const repo = searchParams.get("repo"); |
| 103 | +// const filePath = searchParams.get("filePath"); |
| 104 | +// |
| 105 | +// if (!owner || !repo || !filePath) { |
| 106 | +// return NextResponse.json( |
| 107 | +// { error: "Missing required parameters" }, |
| 108 | +// { status: 400 }, |
| 109 | +// ); |
| 110 | +// } |
| 111 | +// |
| 112 | +// try { |
| 113 | +// const contributors = await getFileContributors(owner, repo, filePath); |
| 114 | +// return NextResponse.json(contributors); |
| 115 | +// } catch (error) { |
| 116 | +// return NextResponse.json( |
| 117 | +// { error: "Failed to fetch contributors" }, |
| 118 | +// { status: 500 }, |
| 119 | +// ); |
| 120 | +// } |
| 121 | +// } |
0 commit comments