[READ-ONLY] Mirror of https://github.com/flo-bit/edge-function-github-contribution.
edge-function-github-contribution.vercel.app
1.6 kB
55 lines
1import { NextRequest } from 'next/server'
2import cors from '../lib/cors';
3
4export const config = {
5 runtime: 'edge',
6}
7
8const corsOptions = {
9 origin: ['https://blento.app', 'https://www.blento.app'],
10};
11
12export async function GET(req: NextRequest) {
13 const { searchParams } = new URL(req.url);
14 const owner = searchParams.get('owner');
15 const repo = searchParams.get('repo');
16 if (!owner || !repo) {
17 return cors(req, new Response('Missing required query parameters: owner and repo', { status: 400 }), corsOptions);
18 }
19
20 const allContributors: any[] = [];
21 let page = 1;
22
23 while (true) {
24 const response = await fetch(
25 `https://api.github.com/repos/${encodeURIComponent(owner)}/${encodeURIComponent(repo)}/contributors?per_page=100&page=${page}`,
26 {
27 headers: {
28 'Accept': 'application/vnd.github+json',
29 'Authorization': `Bearer ${process.env.GITHUB_TOKEN}`,
30 },
31 }
32 );
33
34 if (!response.ok) {
35 return cors(req, new Response(`Error fetching contributors from GitHub: ${response.statusText}`, { status: response.status }), corsOptions);
36 }
37
38 const contributors = await response.json();
39 if (!Array.isArray(contributors) || contributors.length === 0) break;
40
41 allContributors.push(...contributors);
42 if (contributors.length < 100) break;
43 page++;
44 }
45
46 const data = allContributors.map((c: any) => ({
47 username: c.login,
48 avatarUrl: c.avatar_url,
49 contributions: c.contributions,
50 }));
51
52 return cors(req, new Response(JSON.stringify(data), {
53 headers: { 'Content-Type': 'application/json' },
54 }), corsOptions);
55}