-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgithub.js
More file actions
62 lines (53 loc) · 1.92 KB
/
Copy pathgithub.js
File metadata and controls
62 lines (53 loc) · 1.92 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
57
58
59
60
61
62
const { getGithubUsername } = require("./config");
const GITHUB_HEADERS = {
Accept: "application/vnd.github+json",
"User-Agent": "github-tracker",
};
async function fetchPublicEvents() {
const GITHUB_USERNAME = getGithubUsername();
const url = `https://api.github.com/users/${GITHUB_USERNAME}/events/public?per_page=30`;
const res = await fetch(url, { headers: GITHUB_HEADERS });
if (!res.ok) {
console.error(`GitHub API error: ${res.status} ${res.statusText}`);
return [];
}
return res.json();
}
async function fetchCommitsForPush(repoName, beforeSha, headSha) {
const url = `https://api.github.com/repos/${repoName}/compare/${beforeSha}...${headSha}`;
try {
const res = await fetch(url, { headers: GITHUB_HEADERS });
if (!res.ok) {
console.error(` Compare API error for ${repoName}: ${res.status}`);
return [];
}
const data = await res.json();
return (data.commits || []).map((c) => ({
sha: c.sha,
message: c.commit?.message || "(no message)",
author: {
name: c.commit?.author?.name || c.author?.login || "unknown",
email: c.commit?.author?.email || "",
},
url: c.html_url,
}));
} catch (err) {
console.error(` Failed to fetch commits for ${repoName}:`, err.message);
return [];
}
}
async function enrichPushEvents(events) {
for (const ev of events) {
if (ev.type !== "PushEvent") continue;
if (ev.payload.commits && ev.payload.commits.length > 0) continue;
const { before, head } = ev.payload;
if (!before || !head) continue;
console.log(` Fetching commits for ${ev.repo.name} (${before.substring(0, 7)}...${head.substring(0, 7)})`);
const commits = await fetchCommitsForPush(ev.repo.name, before, head);
ev.payload.commits = commits;
ev.payload.size = commits.length;
ev.payload.distinct_size = commits.length;
}
return events;
}
module.exports = { fetchPublicEvents, enrichPushEvents };