@@ -2,6 +2,15 @@ import { Context } from "openapi-backend";
22import type { Request , Response } from "express" ;
33import { GetClient } from "../services/gitHub" ;
44import { SyncOrg } from "../services/githubSync" ;
5+ import { GitHubClient } from "../services/gitHubTypes" ;
6+ import axios from 'axios' ;
7+
8+ async function syncOrgLocal ( installationId : number , client : GitHubClient ) {
9+ const orgClient = await client . GetOrgClient ( installationId ) ;
10+ const appConfig = await client . GetAppConfig ( ) ;
11+
12+ return await SyncOrg ( orgClient , appConfig )
13+ }
514
615export async function syncAllHandler (
716 c : Context ,
@@ -12,26 +21,40 @@ export async function syncAllHandler(
1221
1322 const client = GetClient ( ) ;
1423 const installations = await client . GetInstallations ( ) ;
24+
25+ console . log ( `Syncing the following orgs: ${ JSON . stringify ( installations ) } ` )
1526
16- async function syncOrg ( installationId :number ) {
17- const orgClient = await client . GetOrgClient ( installationId ) ;
18- const appConfig = await client . GetAppConfig ( ) ;
27+ if ( process . env . GITHUB_PROXY ) {
28+ // TODO: clean this up... Such forwarding logic should not be included in
29+ // "handlers"
30+ console . log ( `Forwarding request to '${ process . env . GITHUB_PROXY } '` ) ;
31+ const requestUrl = `${ process . env . GITHUB_PROXY } /api/sync/SynchronizeOrg?installationId=`
32+ const orgSyncPromises = installations . map ( i => axios . post ( `${ requestUrl } i` ) ) ;
1933
20- return await SyncOrg ( orgClient , appConfig )
21- }
34+ const results = await Promise . allSettled ( orgSyncPromises ) ;
2235
23- console . log ( `Syncing the following orgs: ${ JSON . stringify ( installations ) } ` )
36+ const end = Date . now ( ) ;
2437
25- const orgSyncPromises = installations . map ( i => syncOrg ( i . id ) )
38+ const resultObject = {
39+ orgSyncResults : results ,
40+ timeToCompleteInMilliseconds : end - start
41+ }
42+
43+ return res . status ( 200 ) . json ( resultObject ) ;
44+ }
45+ else {
46+ const orgSyncPromises = installations . map ( i => syncOrgLocal ( i . id , client ) )
47+ const results = await Promise . allSettled ( orgSyncPromises ) ;
2648
27- const results = await Promise . allSettled ( orgSyncPromises ) ;
49+ const end = Date . now ( ) ;
2850
29- const end = Date . now ( ) ;
51+ const resultObject = {
52+ orgSyncResults : results ,
53+ timeToCompleteInMilliseconds : end - start
54+ }
3055
31- const resultObject = {
32- orgSyncResults : results ,
33- timeToCompleteInMilliseconds : end - start
56+ return res . status ( 200 ) . json ( resultObject ) ;
3457 }
3558
36- return res . status ( 200 ) . json ( resultObject ) ;
37- }
59+ return res . status ( 500 ) . json ( "An error occurred" ) ;
60+ }
0 commit comments