@@ -16,9 +16,7 @@ export default defineEventHandler(async (event) => {
16
16
17
17
if ( 'action' in body && 'installation' in body && ! ( 'client_payload' in body ) ) {
18
18
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 || [ ] ) )
22
20
}
23
21
if ( body . action === 'deleted' && 'repositories' in body ) {
24
22
for ( const repo of body . repositories || [ ] ) {
@@ -27,9 +25,7 @@ export default defineEventHandler(async (event) => {
27
25
}
28
26
if ( ( body . action === 'added' || body . action === 'removed' ) ) {
29
27
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 ) )
33
29
}
34
30
if ( 'repositories_removed' in body ) {
35
31
for ( const repo of body . repositories_removed ) {
@@ -38,7 +34,7 @@ export default defineEventHandler(async (event) => {
38
34
}
39
35
}
40
36
if ( body . action === 'publicized' && body . installation ) {
41
- promises . push ( addRepo ( event , body . installation , body . repository ) )
37
+ promises . push ( addRepos ( event , body . installation , [ body . repository ] ) )
42
38
}
43
39
if ( body . action === 'privatized' ) {
44
40
promises . push ( deleteRepo ( event , body . repository ) )
@@ -74,44 +70,49 @@ export type InstallationRepo = {
74
70
private : boolean
75
71
}
76
72
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 [ ] ) {
81
74
const config = useRuntimeConfig ( event )
82
75
const app = new App ( {
83
76
appId : config . github . appId ,
84
77
privateKey : config . github . privateKey ,
85
78
} )
86
79
const octokit = await app . getInstallationOctokit ( installation . id )
87
80
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
102
84
}
103
- return [ ]
104
- } )
105
85
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 ( '/' )
111
90
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
+ } )
113
105
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
+ }
115
116
}
116
117
117
118
async function deleteRepo ( event : H3Event , repo : InstallationRepo ) {
0 commit comments