Skip to content

Commit 18e7a0a

Browse files
committed
fix: incrementally index repos
1 parent 5a33720 commit 18e7a0a

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

server/routes/github/webhook.post.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ export default defineEventHandler(async (event) => {
1515
const body = await readBody<WebhookEvent>(event)
1616
const promises: Promise<unknown>[] = []
1717
if ('action' in body && 'installation' in body && !('client_payload' in body)) {
18+
if ('repository' in body && body.installation && body.repository) {
19+
const repoMetadata = await hubKV().getItem<RepoMetadata>(`repo:${body.repository.owner.login}:${body.repository.name}`)
20+
if (repoMetadata && !repoMetadata.indexed) {
21+
promises.push(addRepos(event, body.installation, [body.repository]))
22+
}
23+
}
1824
if (body.action === 'created' && 'repositories' in body) {
1925
promises.push(addRepos(event, body.installation, body.repositories || []))
2026
}
@@ -70,6 +76,10 @@ export type InstallationRepo = {
7076
private: boolean
7177
}
7278

79+
type RepoMetadata = InstallationRepo & {
80+
indexed: boolean
81+
}
82+
7383
async function addRepos(event: H3Event, installation: Installation | InstallationLite, repos: InstallationRepo[]) {
7484
const config = useRuntimeConfig(event)
7585
const octokit = new Octokit({
@@ -92,7 +102,7 @@ async function addRepos(event: H3Event, installation: Installation | Installatio
92102
const [owner, name] = repo.full_name.split('/')
93103

94104
const promises: Array<Promise<unknown>> = []
95-
promises.push(kv.setItem(`repo:${owner}:${name}`, { ...repo, indexed: false }))
105+
promises.push(kv.setItem(`repo:${owner}:${name}`, { ...repo, indexed: false } satisfies RepoMetadata))
96106

97107
await octokit.paginate(octokit.rest.issues.listForRepo, {
98108
owner: owner!,
@@ -115,7 +125,7 @@ async function addRepos(event: H3Event, installation: Installation | Installatio
115125

116126
console.log('added', promises.length - 1, 'issues from', `${owner}/${name}`, 'to the index')
117127

118-
event.waitUntil(kv.setItem(`repo:${owner}:${name}`, { ...repo, indexed: true }))
128+
event.waitUntil(kv.setItem(`repo:${owner}:${name}`, { ...repo, indexed: true } satisfies RepoMetadata))
119129
}
120130
}
121131

0 commit comments

Comments
 (0)