Skip to content

Commit db9341e

Browse files
⏩ check membership as needed (#84)
* General org membership checking is being removed and replaced by just in time checks to save on the amount of calls going to GitHub's API, which will help both general sync performance (as now there is no up-front need to fetch all members, which for large orgs is intense) as well as with issues where the sync bot was running into GitHub API rate limits (less calls == less chance of hitting the API rate limit)
1 parent 84e4395 commit db9341e

5 files changed

Lines changed: 21 additions & 49 deletions

File tree

src/handlers/syncSpecificTeamHandler.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -36,12 +36,6 @@ export async function syncSpecificTeamHandler(
3636

3737
const invitationsClient = GetInvitationsClient(orgClient);
3838

39-
const existingOrgMembers = await orgClient.GetOrgMembers();
40-
41-
if(!existingOrgMembers.successful) {
42-
return res.status(500).json("Unable to fetch org members");
43-
}
44-
4539
const invitesResponse = await invitationsClient.ListInvites();
4640

4741
if(!invitesResponse.successful) {
@@ -60,7 +54,7 @@ export async function syncSpecificTeamHandler(
6054

6155
const sourceTeamMap = orgConfig.data.DisplayNameToSourceMap;
6256

63-
const response = await SyncTeam(teamName, orgClient, appConfig, existingOrgMembers.data, invites, sourceTeamMap);
57+
const response = await SyncTeam(teamName, orgClient, appConfig, invites, sourceTeamMap);
6458

6559
return res.status(200).json(response);
6660
}

src/services/gitHub.ts

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -364,19 +364,6 @@ class InstalledGitHubClient implements InstalledClient {
364364
}
365365
}
366366

367-
public async GetOrgMembers(): Response<GitHubId[]> {
368-
const response = await this.gitHubClient.paginate(this.gitHubClient.rest.orgs.listMembers, {
369-
org: this.orgName
370-
})
371-
372-
return {
373-
successful: true,
374-
data: response.map(i => {
375-
return i.login
376-
})
377-
}
378-
}
379-
380367
public GetCurrentOrgName(): string {
381368
return this.orgName;
382369
}

src/services/gitHubCache.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,5 @@ export class GitHubClientCache implements InstalledClient {
148148

149149
GetConfigurationForInstallation(): Response<OrgConfig> {
150150
return this.client.GetConfigurationForInstallation();
151-
}
152-
153-
GetOrgMembers(): Response<string[]> {
154-
return this.client.GetOrgMembers();
155-
}
151+
}
156152
}

src/services/gitHubTypes.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,7 @@ export interface InstalledClient {
3232
RemoveTeamMemberAsync(team: GitHubTeamName, user: GitHubId): RemoveMemberResponse
3333
UpdateTeamDetails(team: GitHubTeamName, description: string): Response
3434
AddSecurityManagerTeam(team: GitHubTeamName): Promise<unknown>
35-
GetConfigurationForInstallation(): Response<OrgConfig>
36-
GetOrgMembers(): Response<GitHubId[]>
35+
GetConfigurationForInstallation(): Response<OrgConfig>
3736
SetOrgRole(id: GitHubId, role: OrgRoles): Response
3837
GetPendingOrgInvites():Response<OrgInvite[]>
3938
CancelOrgInvite(invite:OrgInvite): Response

src/services/githubSync.ts

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ async function SynchronizeOrgMembers(installedGitHubClient: InstalledClient, tea
8686
};
8787
}
8888

89-
async function SynchronizeGitHubTeam(installedGitHubClient: InstalledClient, teamName: string, config: AppConfig, existingMembers: GitHubId[], existingInvites: OrgInvite[], sourceTeamMap: Map<string, string>, checkOrgMembers: boolean = true) {
89+
async function SynchronizeGitHubTeam(installedGitHubClient: InstalledClient, teamName: string, config: AppConfig, existingInvites: OrgInvite[], sourceTeamMap: Map<string, string>, checkOrgMembers: boolean = true) {
9090
function GetSourceOrReturn(teamName: string) {
9191
return sourceTeamMap.get(teamName) ?? teamName;
9292
}
@@ -110,10 +110,16 @@ async function SynchronizeGitHubTeam(installedGitHubClient: InstalledClient, tea
110110

111111
const orgName = installedGitHubClient.GetCurrentOrgName();
112112

113+
const memberCheckFunc = async (id:GitHubId) => {
114+
const response = await installedGitHubClient.IsUserMember(id);
115+
116+
return response.successful && response.data;
117+
}
118+
113119
const validMemberCheckResults = await Promise.all(trueMembersList.map(tm => checkValidOrgMember({
114120
gitHubId: tm,
115121
checkOrgMembers: checkOrgMembers,
116-
existingMembers: existingMembers,
122+
isExistingMember: memberCheckFunc,
117123
idsWithInvites: idsWithInvites,
118124
installedGitHubClient: installedGitHubClient,
119125
orgName: orgName
@@ -210,7 +216,7 @@ async function SyncSecurityManagers(opts: {
210216
}
211217

212218

213-
await SynchronizeGitHubTeam(installedGitHubClient, t, appConfig, orgMembers.OrgMembers, currentInvites, displayNameToSourceMap);
219+
await SynchronizeGitHubTeam(installedGitHubClient, t, appConfig, currentInvites, displayNameToSourceMap);
214220

215221
Log(`Add Security Manager Team for ${installedGitHubClient.GetCurrentOrgName()}: ${t}`)
216222
const addResult = await installedGitHubClient.AddSecurityManagerTeam(t);
@@ -352,17 +358,7 @@ async function syncOrg(installedGitHubClient: InstalledClient, appConfig: AppCon
352358

353359
currentMembers = currentMembersResponse.OrgMembers;
354360

355-
await SynchronizeGitHubTeam(installedGitHubClient, membersGroupName, appConfig, currentMembers, currentInvites, orgConfig.DisplayNameToSourceMap);
356-
}
357-
358-
if (currentMembers.length == 0) {
359-
const getOrgMembersResponse = await installedGitHubClient.GetOrgMembers();
360-
361-
if (!getOrgMembersResponse.successful) {
362-
throw Error("Unable to get current org members");
363-
}
364-
365-
currentMembers = getOrgMembersResponse.data;
361+
await SynchronizeGitHubTeam(installedGitHubClient, membersGroupName, appConfig, currentInvites, orgConfig.DisplayNameToSourceMap);
366362
}
367363

368364
if (!gitHubTeams || gitHubTeams.length < 1) {
@@ -372,7 +368,7 @@ async function syncOrg(installedGitHubClient: InstalledClient, appConfig: AppCon
372368

373369
async function syncTeam(teamName: string, orgConfig: OrgConfig) {
374370
Log(`Syncing Team Members for ${teamName} in ${installedGitHubClient.GetCurrentOrgName()}`)
375-
await SynchronizeGitHubTeam(installedGitHubClient, teamName, appConfig, currentMembers, currentInvites, orgConfig.DisplayNameToSourceMap);
371+
await SynchronizeGitHubTeam(installedGitHubClient, teamName, appConfig, currentInvites, orgConfig.DisplayNameToSourceMap);
376372
}
377373

378374
const teamSyncPromises = gitHubTeams.map(t => syncTeam(t, orgConfig));
@@ -414,8 +410,8 @@ async function syncOrg(installedGitHubClient: InstalledClient, appConfig: AppCon
414410
}
415411

416412

417-
export async function SyncTeam(teamName: string, client: InstalledClient, config: AppConfig, existingMembers: GitHubId[], invites: OrgInvite[], sourceTeamMap: Map<string, string>) {
418-
const response = await SynchronizeGitHubTeam(client, teamName, config, existingMembers, invites, sourceTeamMap, true);
413+
export async function SyncTeam(teamName: string, client: InstalledClient, config: AppConfig, invites: OrgInvite[], sourceTeamMap: Map<string, string>) {
414+
const response = await SynchronizeGitHubTeam(client, teamName, config, invites, sourceTeamMap, true);
419415

420416
return response;
421417
}
@@ -517,9 +513,9 @@ async function checkValidOrgMember(opts: {
517513
checkOrgMembers: boolean,
518514
orgName: string,
519515
idsWithInvites: Set<string>,
520-
existingMembers: GitHubId[]
516+
isExistingMember: (id:GitHubId) => Promise<boolean>
521517
}) {
522-
const { installedGitHubClient, gitHubId, checkOrgMembers, orgName, idsWithInvites, existingMembers } = opts;
518+
const { installedGitHubClient, gitHubId, checkOrgMembers, orgName, idsWithInvites, isExistingMember } = opts;
523519

524520
const isUserReal = await installedGitHubClient.DoesUserExist(gitHubId);
525521

@@ -538,9 +534,9 @@ async function checkValidOrgMember(opts: {
538534
message: `User '${gitHubId} has a Pending Invite to ${orgName}`
539535
};
540536
}
541-
542-
if (checkOrgMembers) {
543-
const isMember = existingMembers.filter(em => em == gitHubId);
537+
538+
if (checkOrgMembers) {
539+
const isMember = await isExistingMember(gitHubId);
544540

545541
if (!isMember) {
546542
return {

0 commit comments

Comments
 (0)