Skip to content

Commit

Permalink
Add ability to handle users with periods in names (#92)
Browse files Browse the repository at this point in the history
This also adds the ability to dry run a team specific sync when calling
the endpoints directly.
  • Loading branch information
JoshuaTheMiller authored Sep 19, 2024
1 parent f77cd5a commit 2a98c9a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 5 deletions.
3 changes: 2 additions & 1 deletion src/handlers/syncSpecificTeamHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export async function syncSpecificTeamHandler(
) {
const orgId = c.request.query.orgId! as unknown as number;
const teamName = c.request.query.teamName! as unknown as string;
const dryRun = c.request.query.dryRun as unknown as boolean ?? true;

const client = GetClient();
const orgClient = await client.GetOrgClient(orgId);
Expand All @@ -54,7 +55,7 @@ export async function syncSpecificTeamHandler(

const sourceTeamMap = orgConfig.data.DisplayNameToSourceMap;

const response = await SyncTeam(teamName, orgClient, appConfig, invites, sourceTeamMap);
const response = await SyncTeam(teamName, orgClient, appConfig, invites, sourceTeamMap, dryRun);

return res.status(200).json(response);
}
7 changes: 7 additions & 0 deletions src/openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ paths:
schema:
type: string
required: true
- in: query
name: dryRun
description: Whether to run the sync, or simply return what actions will be taken. Defaults to True (i.e., perform as Dry Run).
schema:
type: boolean
default: true
required: false
responses:
"200":
description: A successful response
Expand Down
14 changes: 10 additions & 4 deletions src/services/githubSync.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ async function GetGitHubIds(teamName: string, config: AppConfig): Promise<GitHub
return {
Succeeded: true,
Ids: membersFromSourceOfTruth.entries.map(e => {
return replaceAll(e.cn, '_', '-') + config.GitHubIdAppend;
const replace1 = replaceAll(e.cn, '_', '-');
const replace2 = replace1.replaceAll(".", "-");
return replace2 + config.GitHubIdAppend;
})
}
}
Expand Down Expand Up @@ -108,7 +110,7 @@ async function SynchronizeOrgMembers(installedGitHubClient: InstalledClient, tea
};
}

async function SynchronizeGitHubTeam(installedGitHubClient: InstalledClient, teamName: string, config: AppConfig, existingInvites: OrgInvite[], sourceTeamMap: Map<string, string>, checkOrgMembers: boolean = true) {
async function SynchronizeGitHubTeam(installedGitHubClient: InstalledClient, teamName: string, config: AppConfig, existingInvites: OrgInvite[], sourceTeamMap: Map<string, string>, checkOrgMembers: boolean = true, dryRun: boolean = false) {
function GetSourceOrReturn(teamName: string) {
return sourceTeamMap.get(teamName) ?? teamName;
}
Expand Down Expand Up @@ -170,6 +172,10 @@ async function SynchronizeGitHubTeam(installedGitHubClient: InstalledClient, tea

Log(JSON.stringify(teamSyncNotes));

if(dryRun === true) {
return teamSyncNotes;
}

const deleteResponses = await Promise.all(teamMembersToRemove.map(mtr => installedGitHubClient.RemoveTeamMemberAsync(teamName, mtr)));
const addResponses = await Promise.all(teamMembersToAdd.map(mta => installedGitHubClient.AddTeamMember(teamName, mta)));

Expand Down Expand Up @@ -473,8 +479,8 @@ async function syncOrg(installedGitHubClient: InstalledClient, appConfig: AppCon
}


export async function SyncTeam(teamName: string, client: InstalledClient, config: AppConfig, invites: OrgInvite[], sourceTeamMap: Map<string, string>) {
const response = await SynchronizeGitHubTeam(client, teamName, config, invites, sourceTeamMap, true);
export async function SyncTeam(teamName: string, client: InstalledClient, config: AppConfig, invites: OrgInvite[], sourceTeamMap: Map<string, string>, dryRun: boolean = false) {
const response = await SynchronizeGitHubTeam(client, teamName, config, invites, sourceTeamMap, true, dryRun);

return response;
}
Expand Down

0 comments on commit 2a98c9a

Please sign in to comment.