Skip to content

Commit 24b9e49

Browse files
💡 Expose endpoint for syncing specific team
1 parent 8fb4ba8 commit 24b9e49

4 files changed

Lines changed: 89 additions & 9 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import { Context } from "openapi-backend";
2+
import type { Request, Response } from "express";
3+
import { GetClient } from "../services/gitHub";
4+
import { SyncOrg, SyncTeam } from "../services/githubSync";
5+
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+
}
23+
24+
export async function syncSpecificTeamHandler(
25+
c: Context,
26+
_req: Request,
27+
res: Response
28+
) {
29+
const orgId = c.request.query.orgId! as unknown as number;
30+
const teamName = c.request.query.teamName! as unknown as string;
31+
32+
const client = GetClient();
33+
const orgClient = await client.GetOrgClient(orgId);
34+
const appConfig = await client.GetAppConfig();
35+
36+
// const existingOrgMembers = await orgClient.GetOrgMembers();
37+
38+
// if(!existingOrgMembers.successful) {
39+
// return res.status(500).json("Unable to fetch org members");
40+
// }
41+
42+
const response = await SyncTeam(teamName, orgClient, appConfig);
43+
44+
return res.status(200).json(response);
45+
}

src/openapi.yaml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,28 @@ paths:
6666
responses:
6767
"200":
6868
description: A successful response
69+
/api/sync/SynchronizeSpecificTeam:
70+
post:
71+
operationId: syncSpecificTeam
72+
description: Triggers a synchronization job for all orgs. **Note**, at this point in time, this endpoint will NOT check org membership first.
73+
tags:
74+
- sync
75+
parameters:
76+
- in: query
77+
name: orgId
78+
description: This value can be retrieved from the `/metadata/getInstalledOrgs` endpoint
79+
schema:
80+
type: number
81+
required: true
82+
- in: query
83+
name: teamName
84+
description: The name of the team to synchronize
85+
schema:
86+
type: string
87+
required: true
88+
responses:
89+
"200":
90+
description: A successful response
6991
/api/get-source-team:
7092
get:
7193
operationId: getSourceTeam

src/routes.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { getInstalledOrgsHandler } from "./handlers/getInstalledOrgs";
33
import { getSourceTeamHandler } from "./handlers/getSourceTeam";
44
import { syncAllHandler } from "./handlers/syncAll";
55
import { syncOrgHandler } from "./handlers/syncOrg";
6+
import { syncSpecificTeamHandler } from "./handlers/syncSpecificTeamHandler";
67

78
function notImplementedHandler(c: any, req: any, res: any) {
89
return res
@@ -16,5 +17,6 @@ export const routes = {
1617
syncOrg: syncOrgHandler,
1718
syncAllOrgs: syncAllHandler,
1819
notImplemented: notImplementedHandler,
19-
getSourceTeam: getSourceTeamHandler
20+
getSourceTeam: getSourceTeamHandler,
21+
syncSpecificTeam: syncSpecificTeamHandler
2022
}

src/services/githubSync.ts

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ async function SynchronizeOrgMembers(installedGitHubClient: InstalledClient, tea
7373
return orgMembers;
7474
}
7575

76-
async function SynchronizeGitHubTeam(installedGitHubClient: InstalledClient, teamName: string, config: AppConfig, existingMembers: GitHubId[]) {
76+
async function SynchronizeGitHubTeam(installedGitHubClient: InstalledClient, teamName: string, config: AppConfig, existingMembers: GitHubId[], checkOrgMembers:boolean = true) {
7777
await installedGitHubClient.UpdateTeamDetails(teamName, teamDescription);
7878

7979
const trueMembersList = await GetGitHubIds(teamName, config);
@@ -96,14 +96,19 @@ async function SynchronizeGitHubTeam(installedGitHubClient: InstalledClient, tea
9696
};
9797
}
9898

99-
const isMember = existingMembers.filter(em => em == gitHubId);
99+
if(checkOrgMembers) {
100+
const isMember = existingMembers.filter(em => em == gitHubId);
100101

101-
if (!isMember) {
102-
return {
103-
successful: false,
104-
gitHubId: gitHubId,
105-
message: `User '${gitHubId} is not an Org Member of ${orgName}`
106-
};
102+
if (!isMember) {
103+
return {
104+
successful: false,
105+
gitHubId: gitHubId,
106+
message: `User '${gitHubId} is not an Org Member of ${orgName}`
107+
};
108+
}
109+
}
110+
else {
111+
console.log(`Skipping Org Membership check for ${gitHubId}`);
107112
}
108113

109114
return {
@@ -246,6 +251,12 @@ async function syncOrg(installedGitHubClient: InstalledClient, config: AppConfig
246251
}
247252
}
248253

254+
export async function SyncTeam(teamName:string, client: InstalledClient, config: AppConfig) {
255+
const response = await SynchronizeGitHubTeam(client, teamName, config, [], false);
256+
257+
return response;
258+
}
259+
249260
export async function SyncOrg(installedGitHubClient: InstalledClient, config: AppConfig) : ReturnTypeOfSyncOrg {
250261
try {
251262
return await syncOrg(installedGitHubClient, config);

0 commit comments

Comments
 (0)