-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsyncOrg.ts
More file actions
58 lines (45 loc) · 1.87 KB
/
syncOrg.ts
File metadata and controls
58 lines (45 loc) · 1.87 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
import { Context } from "openapi-backend";
import type { Request, Response } from "express";
import { GetClient } from "../services/gitHub.ts";
import { SyncOrg } from "../services/githubSync.ts";
import { AsyncReturnType } from "../utility.ts";
import axios from 'axios';
import { Log } from "../logging.ts";
import { GetInvitationsClient } from "../services/githubInvitations.ts";
async function forwardToProxy(installationId: number) {
Log(`Forwarding request to '${process.env.GITHUB_PROXY}'`);
const requestUrl = `${process.env.GITHUB_PROXY}/api/sync/SynchronizeOrg?installationId=${installationId}`
const result = await axios.post(requestUrl);
if(result.status >= 200 && result.status < 300) {
return result.data;
}
return {
status: "failed",
installationId: installationId
}
}
export async function syncOrgHandler(
c: Context,
_req: Request,
res: Response
) {
const potentialIds = c.request.query.installationId;
const installationIds = potentialIds instanceof Array ? potentialIds : [potentialIds];
const distinctIds = new Set(installationIds.map(i => {
return Number.parseInt(i)
}));
if (process.env.GITHUB_PROXY) {
const orgSyncPromises = Array.from(distinctIds).map(forwardToProxy);
const results = await Promise.allSettled(orgSyncPromises);
return res.status(200).json(results);
}
const client = GetClient();
const syncOrgResponses : AsyncReturnType<typeof SyncOrg>[] = [];
for (const i of distinctIds) {
const orgClient = await client.GetOrgClient(i);
const appConfig = await client.GetAppConfig();
const invitationsClient = GetInvitationsClient(orgClient);
syncOrgResponses.push(await SyncOrg(orgClient, appConfig, invitationsClient));
}
return res.status(200).json(syncOrgResponses);
}