Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions tools/star-tracker/src/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,21 @@ export async function setRepoSyncMode(
.run();
}

// Live update of the repo's star count from a webhook payload. Keeps the
// "Tracked repos — N stars" line in sync with reality between nightly
// reconciles — without this the count is frozen at backfill time and a
// repo that gains stars via webhook reads as stale.
export async function updateStargazersCount(
db: D1Database,
fullName: string,
count: number,
): Promise<void> {
await db
.prepare(`UPDATE repos SET stargazers_count = ? WHERE full_name = ?`)
.bind(count, fullName)
.run();
}

export async function recordEvent(
db: D1Database,
tenant: string,
Expand Down
8 changes: 8 additions & 0 deletions tools/star-tracker/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,14 @@ app.post('/webhook', async (c) => {
// current visibility, so every event keeps our flag in sync even if
// the user didn't subscribe to the 'repository' event.
await db.setRepoPrivacy(c.env.DB, fullName, payload.repository?.private === true);
// GitHub includes the post-action stargazers_count on the repo object,
// so use it to keep our cached count fresh between nightly reconciles.
// Without this the "Tracked repos — N stars" line lags behind the live
// chart by up to a day after a webhook-driven star.
const liveCount = payload.repository?.stargazers_count;
if (typeof liveCount === 'number') {
await db.updateStargazersCount(c.env.DB, fullName, liveCount);
}
await db.recordEvent(c.env.DB, slug, fullName, 'star', payload.action, payload.sender, payload.starred_at);
await db.bumpTenantLastEvent(c.env.DB, slug);

Expand Down
Loading