Skip to content

Commit b209302

Browse files
committed
fix: handle missing commits in PushEvent payload
GitHub Events API no longer returns `commits` array in PushEvent payload (only `push_id`, `ref`, `head`, `before`). Fall back to fetching the `head` commit directly when `commits` is unavailable.
1 parent d33005b commit b209302

1 file changed

Lines changed: 30 additions & 54 deletions

File tree

src/index.ts

Lines changed: 30 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -31,49 +31,46 @@ const fetchCommits = async (
3131
per_page: perPage,
3232
page,
3333
});
34-
console.log(
35-
`Page ${page}: ${events.data.length} total events, types: ${[...new Set(events.data.map((e) => e.type))].join(', ')}`,
36-
);
37-
// Debug: print first 3 events
38-
for (const e of events.data.slice(0, 3)) {
39-
console.log(
40-
` event: type=${e.type} actor=${e.actor.login} created_at=${e.created_at}`,
41-
);
42-
}
4334
const pushEvents = events.data.filter(
4435
(event): event is PushEvent =>
4536
event.type === 'PushEvent' && event.actor.login === username,
4637
);
47-
console.log(
48-
`Page ${page}: ${pushEvents.length} PushEvents from ${username}`,
49-
);
50-
// Debug: print first 3 PushEvents details
51-
for (const pe of pushEvents.slice(0, 3)) {
52-
console.log(
53-
` push: repo=${pe.repo.name} created_at=${pe.created_at} passes_date=${new Date(pe.created_at) > fromDate} has_commits=${!!pe.payload.commits} is_array=${Array.isArray(pe.payload.commits)} payload_keys=${JSON.stringify(Object.keys(pe.payload))}`,
54-
);
55-
}
5638

5739
const recentPushEvents = pushEvents.filter(
58-
({ created_at, payload }) =>
59-
new Date(created_at) > fromDate &&
60-
payload.commits &&
61-
Array.isArray(payload.commits),
40+
({ created_at }) => new Date(created_at) > fromDate,
6241
);
63-
console.log(`${recentPushEvents.length} recent events fetched.`);
42+
console.log(`${recentPushEvents.length} events fetched.`);
6443

6544
const newCommits = await Promise.allSettled(
66-
recentPushEvents.flatMap(({ repo, payload }) =>
67-
payload.commits
68-
.filter((commit) => commit.distinct === true)
69-
.map((commit) =>
45+
recentPushEvents.flatMap(({ repo, payload }) => {
46+
const [owner, repoName] = repo.name.split('/');
47+
48+
// Use payload.commits if available, otherwise fall back to Compare API
49+
if (payload.commits && Array.isArray(payload.commits)) {
50+
return payload.commits
51+
.filter((commit) => commit.distinct === true)
52+
.map((commit) =>
53+
githubRequest('GET /repos/{owner}/{repo}/commits/{ref}', {
54+
owner,
55+
repo: repoName,
56+
ref: commit.sha,
57+
}),
58+
);
59+
}
60+
61+
// Fallback: use head SHA from payload to fetch the commit directly
62+
if (payload.head) {
63+
return [
7064
githubRequest('GET /repos/{owner}/{repo}/commits/{ref}', {
71-
owner: repo.name.split('/')[0],
72-
repo: repo.name.split('/')[1],
73-
ref: commit.sha,
65+
owner,
66+
repo: repoName,
67+
ref: payload.head,
7468
}),
75-
),
76-
),
69+
];
70+
}
71+
72+
return [];
73+
}),
7774
);
7875

7976
commits.push(
@@ -121,7 +118,7 @@ const updateGist = async (gistId: string, content: string) => {
121118
gist_id: gistId,
122119
files: {
123120
[filename]: {
124-
filename: 'Bryans Recent Coding Languages',
121+
filename: "Bryan's Recent Coding Languages",
125122
content,
126123
},
127124
},
@@ -137,27 +134,6 @@ const main = async () => {
137134
const days = Math.max(1, Math.min(30, Number(DAYS || 14)));
138135
const fromDate = new Date(Date.now() - days * 24 * 60 * 60 * 1000);
139136

140-
// Debug: verify token type
141-
const tokenCheck = await fetch(
142-
'https://api.github.com/users/liby/events?per_page=1',
143-
{
144-
headers: {
145-
authorization: `bearer ${GH_TOKEN}`,
146-
accept: 'application/vnd.github.v3+json',
147-
},
148-
},
149-
);
150-
console.log(`Token scopes: ${tokenCheck.headers.get('x-oauth-scopes')}`);
151-
console.log(
152-
`Rate limit: ${tokenCheck.headers.get('x-ratelimit-limit')}`,
153-
);
154-
const tokenCheckData = (await tokenCheck.json()) as Array<{
155-
created_at: string;
156-
}>;
157-
if (tokenCheckData.length > 0) {
158-
console.log(`Latest event: ${tokenCheckData[0].created_at}`);
159-
}
160-
161137
console.log(`Username: ${username}`);
162138
console.log(`Fetching data for the last ${days} days`);
163139

0 commit comments

Comments
 (0)