1
1
import type { Installation , InstallationLite , WebhookEvent } from '@octokit/webhooks-types'
2
2
import type { H3Event } from 'h3'
3
- import { createAppAuth } from '@octokit/auth-app'
4
- import { Octokit } from '@octokit/rest'
3
+ import { App } from 'octokit'
5
4
6
5
import { indexIssue , removeIssue , storagePrefixForRepo } from '../../utils/embeddings'
7
6
@@ -14,58 +13,52 @@ export default defineEventHandler(async (event) => {
14
13
15
14
const body = await readBody < WebhookEvent > ( event )
16
15
const promises : Promise < unknown > [ ] = [ ]
17
- try {
18
- if ( 'action' in body && 'installation' in body && ! ( 'client_payload' in body ) ) {
19
- if ( body . action === 'created' && 'repositories' in body ) {
20
- await addRepos ( event , body . installation , body . repositories || [ ] )
16
+ if ( 'action' in body && 'installation' in body && ! ( 'client_payload' in body ) ) {
17
+ if ( body . action === 'created' && 'repositories' in body ) {
18
+ await addRepos ( event , body . installation , body . repositories || [ ] )
19
+ }
20
+ if ( body . action === 'deleted' && 'repositories' in body ) {
21
+ for ( const repo of body . repositories || [ ] ) {
22
+ promises . push ( deleteRepo ( event , repo ) )
21
23
}
22
- if ( body . action === 'deleted' && 'repositories' in body ) {
23
- for ( const repo of body . repositories || [ ] ) {
24
- promises . push ( deleteRepo ( event , repo ) )
25
- }
24
+ }
25
+ if ( ( body . action === 'added' || body . action === 'removed' ) ) {
26
+ if ( 'repositories_added' in body ) {
27
+ promises . push ( addRepos ( event , body . installation , body . repositories_added ) )
26
28
}
27
- if ( ( body . action === 'added' || body . action === 'removed' ) ) {
28
- if ( 'repositories_added' in body ) {
29
- await addRepos ( event , body . installation , body . repositories_added )
30
- }
31
- if ( 'repositories_removed' in body ) {
32
- for ( const repo of body . repositories_removed ) {
33
- promises . push ( deleteRepo ( event , repo ) )
34
- }
29
+ if ( 'repositories_removed' in body ) {
30
+ for ( const repo of body . repositories_removed ) {
31
+ promises . push ( deleteRepo ( event , repo ) )
35
32
}
36
33
}
37
- if ( body . action === 'publicized' && body . installation ) {
38
- await addRepos ( event , body . installation , [ body . repository ] )
39
- }
40
- if ( body . action === 'privatized' ) {
41
- promises . push ( deleteRepo ( event , body . repository ) )
42
- }
43
34
}
35
+ if ( body . action === 'publicized' && body . installation ) {
36
+ await addRepos ( event , body . installation , [ body . repository ] )
37
+ }
38
+ if ( body . action === 'privatized' ) {
39
+ promises . push ( deleteRepo ( event , body . repository ) )
40
+ }
41
+ }
44
42
45
- if ( 'issue' in body ) {
46
- switch ( body . action ) {
47
- case 'created' :
48
- case 'edited' :
49
- case 'opened' :
50
- case 'reopened' :
51
- promises . push ( indexIssue ( body . issue , body . repository ) )
52
- break
53
-
54
- case 'closed' :
55
- case 'deleted' :
56
- promises . push ( removeIssue ( body . issue , body . repository ) )
57
- break
58
- }
43
+ if ( 'issue' in body ) {
44
+ switch ( body . action ) {
45
+ case 'created' :
46
+ case 'edited' :
47
+ case 'opened' :
48
+ case 'reopened' :
49
+ promises . push ( indexIssue ( body . issue , body . repository ) )
50
+ break
51
+
52
+ case 'closed' :
53
+ case 'deleted' :
54
+ promises . push ( removeIssue ( body . issue , body . repository ) )
55
+ break
59
56
}
57
+ }
60
58
61
- event . waitUntil ( Promise . allSettled ( promises ) )
59
+ event . waitUntil ( Promise . allSettled ( promises ) )
62
60
63
- return null
64
- }
65
- catch ( err ) {
66
- console . error ( err )
67
- throw err
68
- }
61
+ return null
69
62
} )
70
63
71
64
export type InstallationRepo = {
@@ -78,14 +71,11 @@ export type InstallationRepo = {
78
71
79
72
async function addRepos ( event : H3Event , installation : Installation | InstallationLite , repos : InstallationRepo [ ] ) {
80
73
const config = useRuntimeConfig ( event )
81
- const octokit = new Octokit ( {
82
- authStrategy : createAppAuth ,
83
- auth : {
84
- appId : config . github . appId ,
85
- privateKey : config . github . privateKey ,
86
- installationId : installation . id ,
87
- } ,
74
+ const app = new App ( {
75
+ appId : config . github . appId ,
76
+ privateKey : config . github . privateKey ,
88
77
} )
78
+ const octokit = await app . getInstallationOctokit ( installation . id )
89
79
90
80
for ( const repo of repos ) {
91
81
if ( repo . private ) {
0 commit comments