Skip to content

Commit f6a9d16

Browse files
committed
perf: create single app token
1 parent 15fa0d5 commit f6a9d16

File tree

1 file changed

+35
-34
lines changed

1 file changed

+35
-34
lines changed

server/routes/github/webhook.post.ts

+35-34
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,7 @@ export default defineEventHandler(async (event) => {
1616

1717
if ('action' in body && 'installation' in body && !('client_payload' in body)) {
1818
if (body.action === 'created' && 'repositories' in body) {
19-
for (const repo of body.repositories || []) {
20-
promises.push(addRepo(event, body.installation, repo))
21-
}
19+
promises.push(addRepos(event, body.installation, body.repositories || []))
2220
}
2321
if (body.action === 'deleted' && 'repositories' in body) {
2422
for (const repo of body.repositories || []) {
@@ -27,9 +25,7 @@ export default defineEventHandler(async (event) => {
2725
}
2826
if ((body.action === 'added' || body.action === 'removed')) {
2927
if ('repositories_added' in body) {
30-
for (const repo of body.repositories_added) {
31-
promises.push(addRepo(event, body.installation, repo))
32-
}
28+
promises.push(addRepos(event, body.installation, body.repositories_added))
3329
}
3430
if ('repositories_removed' in body) {
3531
for (const repo of body.repositories_removed) {
@@ -38,7 +34,7 @@ export default defineEventHandler(async (event) => {
3834
}
3935
}
4036
if (body.action === 'publicized' && body.installation) {
41-
promises.push(addRepo(event, body.installation, body.repository))
37+
promises.push(addRepos(event, body.installation, [body.repository]))
4238
}
4339
if (body.action === 'privatized') {
4440
promises.push(deleteRepo(event, body.repository))
@@ -74,44 +70,49 @@ export type InstallationRepo = {
7470
private: boolean
7571
}
7672

77-
async function addRepo(event: H3Event, installation: Installation | InstallationLite, repo: InstallationRepo) {
78-
if (repo.private) {
79-
return
80-
}
73+
async function addRepos(event: H3Event, installation: Installation | InstallationLite, repos: InstallationRepo[]) {
8174
const config = useRuntimeConfig(event)
8275
const app = new App({
8376
appId: config.github.appId,
8477
privateKey: config.github.privateKey,
8578
})
8679
const octokit = await app.getInstallationOctokit(installation.id)
8780

88-
const kv = hubKV()
89-
const [owner, name] = repo.full_name.split('/')
90-
91-
const promises: Array<Promise<unknown>> = []
92-
promises.push(kv.setItem(`repo:${owner}:${name}`, { ...repo, indexed: false }))
93-
94-
await octokit.paginate(octokit.rest.issues.listForRepo, {
95-
owner: owner!,
96-
repo: name!,
97-
state: 'open',
98-
per_page: 100,
99-
}, (response) => {
100-
for (const issue of response.data) {
101-
promises.push(indexIssue(issue, { owner: { login: owner! }, name: name! }))
81+
for (const repo of repos) {
82+
if (repo.private) {
83+
continue
10284
}
103-
return []
104-
})
10585

106-
await Promise.allSettled(promises).then((r) => {
107-
if (r.some(p => p.status === 'rejected')) {
108-
console.error('Failed to fetch some issues from', `${owner}/${name}`)
109-
}
110-
})
86+
console.log('starting to index', `${repo.full_name}`)
87+
88+
const kv = hubKV()
89+
const [owner, name] = repo.full_name.split('/')
11190

112-
console.log('added', promises.length - 1, 'issues from', `${owner}/${name}`, 'to the index')
91+
const promises: Array<Promise<unknown>> = []
92+
promises.push(kv.setItem(`repo:${owner}:${name}`, { ...repo, indexed: false }))
93+
94+
await octokit.paginate(octokit.rest.issues.listForRepo, {
95+
owner: owner!,
96+
repo: name!,
97+
state: 'open',
98+
per_page: 100,
99+
}, (response) => {
100+
for (const issue of response.data) {
101+
promises.push(indexIssue(issue, { owner: { login: owner! }, name: name! }))
102+
}
103+
return []
104+
})
113105

114-
event.waitUntil(kv.setItem(`repo:${owner}:${name}`, { ...repo, indexed: true }))
106+
await Promise.allSettled(promises).then((r) => {
107+
if (r.some(p => p.status === 'rejected')) {
108+
console.error('Failed to fetch some issues from', `${owner}/${name}`)
109+
}
110+
})
111+
112+
console.log('added', promises.length - 1, 'issues from', `${owner}/${name}`, 'to the index')
113+
114+
event.waitUntil(kv.setItem(`repo:${owner}:${name}`, { ...repo, indexed: true }))
115+
}
115116
}
116117

117118
async function deleteRepo(event: H3Event, repo: InstallationRepo) {

0 commit comments

Comments
 (0)