-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathapi-backend.js
More file actions
56 lines (46 loc) · 1.94 KB
/
api-backend.js
File metadata and controls
56 lines (46 loc) · 1.94 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
export default async function handler(req, res) {
const GITHUB_TOKEN = process.env.GITHUB_TOKEN; // From Vercel
const ORG_NAME = "Krypto-Hashers-Community";
res.setHeader("Access-Control-Allow-Origin", "*");
res.setHeader("Access-Control-Allow-Methods", "GET, OPTIONS");
res.setHeader("Access-Control-Allow-Headers", "Content-Type, Authorization");
const headers = {
Authorization: `Bearer ${GITHUB_TOKEN}`,
"Content-Type": "application/json",
};
try {
// 1. Fetch repositories (public + private)
const reposResponse = await fetch(`https://api.github.com/user/repos?per_page=100&affiliation=owner,organization_member`, {
headers,
});
const reposData = await reposResponse.json();
if (!Array.isArray(reposData)) {
throw new Error("Failed to fetch repositories. Check GitHub token permissions.");
}
// 2. Fetch members (both public and private)
const membersResponse = await fetch(`https://api.github.com/orgs/${ORG_NAME}/members?per_page=100`, {
headers,
});
const membersData = await membersResponse.json();
if (!Array.isArray(membersData)) {
throw new Error("Failed to fetch members. Check GitHub token permissions.");
}
// 3. Calculate total stars and forks for repos belonging to the org
let totalStars = 0;
let totalForks = 0;
reposData.forEach((repo) => {
if (repo.owner.login === ORG_NAME) {
totalStars += repo.stargazers_count;
totalForks += repo.forks_count;
}
});
res.status(200).json({
totalStars,
totalForks,
totalMembers: membersData.length,
});
} catch (error) {
console.error("Error fetching GitHub data:", error.message);
res.status(500).json({ error: "Failed to fetch GitHub data" });
}
}