-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsyncAll.ts
More file actions
64 lines (49 loc) · 2.1 KB
/
syncAll.ts
File metadata and controls
64 lines (49 loc) · 2.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import { Context } from "openapi-backend";
import type { Request, Response } from "express";
import { GetClient } from "../services/gitHub.ts";
import { SyncOrg } from "../services/githubSync.ts";
import { GitHubClient } from "../services/gitHubTypes.ts";
import axios from 'axios';
import { Log } from "../logging.ts";
import { GetInvitationsClient } from "../services/githubInvitations.ts";
async function syncOrgLocal(installationId: number, client: GitHubClient) {
const orgClient = await client.GetOrgClient(installationId);
const appConfig = await client.GetAppConfig();
const invitationsClient = GetInvitationsClient(orgClient);
return await SyncOrg(orgClient, appConfig, invitationsClient)
}
export async function syncAllHandler(
c: Context,
_req: Request,
res: Response
) {
const start = Date.now();
const client = GetClient();
const installations = await client.GetInstallations();
Log(`Syncing the following orgs: ${JSON.stringify(installations)}`)
if (process.env.GITHUB_PROXY) {
// TODO: clean this up... Such forwarding logic should not be included in
// "handlers"
Log(`Forwarding request to '${process.env.GITHUB_PROXY}'`);
const requestUrl = `${process.env.GITHUB_PROXY}/api/sync/SynchronizeOrg?installationId=`
const orgSyncPromises = installations.map(i => axios.post(`${requestUrl}${i.id}`));
const results = await Promise.allSettled(orgSyncPromises);
const end = Date.now();
const resultObject = {
orgSyncResults: results,
timeToCompleteInMilliseconds: end - start
}
return res.status(200).json(resultObject);
}
else {
const orgSyncPromises = installations.map(i => syncOrgLocal(i.id, client))
const results = await Promise.allSettled(orgSyncPromises);
const end = Date.now();
const resultObject = {
orgSyncResults: results,
timeToCompleteInMilliseconds: end - start
}
return res.status(200).json(resultObject);
}
return res.status(500).json("An error occurred");
}