Skip to content

Commit 0113315

Browse files
💡 allow workers to handle GitHub requests
1 parent 48832a6 commit 0113315

4 files changed

Lines changed: 70 additions & 17 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@
2525
"ts-node": "^10.9.1",
2626
"typescript": "^5.0.2"
2727
},
28-
"dependencies": {
28+
"dependencies": {
2929
"@octokit/plugin-throttling": "^5.0.1",
30+
"axios": "^1.4.0",
3031
"dotenv": "^16.0.3",
3132
"express": "^4.18.2",
3233
"js-yaml": "^4.1.0",

src/app.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { routes } from "./routes";
88

99
const app = express();
1010

11-
const port = 8080;
11+
const port = process.env.PORT;
1212

1313
// TODO: fix/determine why OpenAPIBackend is having issues loading files on its own...
1414
const doc = yaml.load(fs.readFileSync(path.resolve(__dirname, 'openapi.yaml'), 'utf8'));
@@ -35,4 +35,8 @@ app.use((req: any, res: any) => {
3535
})
3636
});
3737

38+
console.log(`Starting server on port '${port}'`);
39+
if(process.env.GITHUB_PROXY) {
40+
console.log(`Forwarding GitHub requests to '${process.env.GITHUB_PROXY}'`);
41+
}
3842
app.listen(port);

src/handlers/syncAll.ts

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ import { Context } from "openapi-backend";
22
import type { Request, Response } from "express";
33
import { GetClient } from "../services/gitHub";
44
import { 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

615
export 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+
}

src/handlers/syncOrg.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,43 @@ import type { Request, Response } from "express";
33
import { GetClient } from "../services/gitHub";
44
import { SyncOrg } from "../services/githubSync";
55
import { AsyncReturnType } from "../utility";
6+
import axios from 'axios';
7+
8+
async function forwardToProxy(installationId: number) {
9+
console.log(`Forwarding request to '${process.env.GITHUB_PROXY}'`);
10+
const requestUrl = `${process.env.GITHUB_PROXY}/api/sync/SynchronizeOrg?installationId=${installationId}`
11+
12+
const result = await axios.post(requestUrl);
13+
14+
if(result.status >= 200 && result.status < 300) {
15+
return result.data;
16+
}
17+
18+
return {
19+
status: "failed",
20+
installationId: installationId
21+
}
22+
}
623

724
export async function syncOrgHandler(
825
c: Context,
926
_req: Request,
1027
res: Response
11-
) {
28+
) {
1229
const potentialIds = c.request.query.installationId;
1330
const installationIds = potentialIds instanceof Array ? potentialIds : [potentialIds];
1431
const distinctIds = new Set(installationIds.map(i => {
1532
return Number.parseInt(i)
1633
}));
1734

35+
if (process.env.GITHUB_PROXY) {
36+
const orgSyncPromises = Array.from(distinctIds).map(forwardToProxy);
37+
38+
const results = await Promise.allSettled(orgSyncPromises);
39+
40+
return res.status(200).json(results);
41+
}
42+
1843
const client = GetClient();
1944

2045
const syncOrgResponses : AsyncReturnType<typeof SyncOrg>[] = [];

0 commit comments

Comments
 (0)