Skip to content

Commit f77cd5a

Browse files
Add additional logging (#91)
1 parent e8a1a50 commit f77cd5a

4 files changed

Lines changed: 45 additions & 11 deletions

File tree

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
"nocache": "^4.0.0",
3737
"octokit": "3.2.1",
3838
"openapi-backend": "^5.9.1",
39-
"redis": "^4.6.7",
39+
"redis": "^4.7.0",
4040
"swagger-ui-express": "5.0.0"
4141
},
4242
"nodemonConfig": {

src/app.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import {Request} from "openapi-backend";
1414

1515
export const redisClient = createClient({
1616
url: `redis://${process.env.APP_OPTIONS_RedisHost}`
17-
});
17+
}).on('error', err => console.log('Redis Client Error: ', err));
1818

1919
export type CacheClient = typeof redisClient;
2020
Do();

src/services/githubSync.ts

Lines changed: 35 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ type SyncMembersResponse = Promise<SyncMembersFailed | SyncMembersSucceeded>
6969

7070
async function SynchronizeOrgMembers(installedGitHubClient: InstalledClient, teamName: string, config: AppConfig, sourceTeamMap: Map<string, string>): SyncMembersResponse {
7171
const actualTeamName = sourceTeamMap.get(teamName) ?? teamName;
72-
72+
7373
const gitHubIdsResponse = await GetGitHubIds(actualTeamName, config);
7474

7575
if (gitHubIdsResponse.Succeeded == false) {
@@ -90,9 +90,10 @@ async function SynchronizeOrgMembers(installedGitHubClient: InstalledClient, tea
9090

9191
const orgName = installedGitHubClient.GetCurrentOrgName();
9292

93+
Log("Adding Org Members to " + orgName + " via " + actualTeamName + ": Started");
9394
const orgMemberPromises = gitHubIds.map(g => addOrgMember(g, installedGitHubClient));
94-
9595
const responses = await Promise.all(orgMemberPromises);
96+
Log("Adding Org Members to " + orgName + " via " + actualTeamName + ": Completed");
9697

9798
const orgMembers = responses.filter(r => r.successful).map(r => r.user);
9899
const problematicGitHubIds = responses.filter(r => !r.successful);
@@ -252,7 +253,7 @@ async function SyncSecurityManagers(opts: {
252253
}
253254
}
254255

255-
async function syncOrg(installedGitHubClient: InstalledClient, appConfig: AppConfig, invitationsClient: IGitHubInvitations): Promise<ReturnTypeOfSyncOrg> {
256+
async function syncOrg(installedGitHubClient: InstalledClient, appConfig: AppConfig, invitationsClient: IGitHubInvitations, log:(message: string, operation:string, status:string)=>void): Promise<ReturnTypeOfSyncOrg> {
256257
const orgName = installedGitHubClient.GetCurrentOrgName();
257258

258259
let response: ReturnTypeOfSyncOrg = {
@@ -282,6 +283,7 @@ async function syncOrg(installedGitHubClient: InstalledClient, appConfig: AppCon
282283
}
283284
const setOfExistingTeams = new Set(existingTeamsResponse.data.map(t => t.Name.toUpperCase()));
284285

286+
log("", "GetConfiguration", "Started");
285287
const orgConfigResponse = await installedGitHubClient.GetConfigurationForInstallation();
286288

287289
const securityManagersFromOrgConfig = orgConfigResponse.successful ? orgConfigResponse.data.AdditionalSecurityManagerGroups : [];
@@ -291,8 +293,10 @@ async function syncOrg(installedGitHubClient: InstalledClient, appConfig: AppCon
291293
...appConfig.SecurityManagerTeams,
292294
...securityManagersFromOrgConfig
293295
];
294-
296+
log("", "GetConfiguration", "Completed");
297+
295298
if (securityManagerTeams.length > 0) {
299+
log("", "SyncSecurityManagers", "Started");
296300
const syncManagersResponse = await SyncSecurityManagers({
297301
appConfig,
298302
client: installedGitHubClient,
@@ -315,7 +319,8 @@ async function syncOrg(installedGitHubClient: InstalledClient, appConfig: AppCon
315319
...response,
316320
syncedSecurityManagerTeams: syncManagersResponse.SyncedSecurityManagerTeams
317321
}
318-
}
322+
log("", "SyncSecurityManagers", "Completed");
323+
}
319324

320325
if (!orgConfigResponse.successful && orgConfigResponse.state == "NoConfig") {
321326
return {
@@ -359,10 +364,12 @@ async function syncOrg(installedGitHubClient: InstalledClient, appConfig: AppCon
359364
const teamsToCreate = teamsThatShouldExist.filter(t => !setOfExistingTeams.has(t.toUpperCase()))
360365

361366
if (teamsToCreate.length > 0) {
367+
log("", "CreateTeams", "Started");
362368
for (const t of teamsToCreate) {
363369
Log(`Creating team '${orgName}/${t}'`)
364370
await installedGitHubClient.CreateTeam(t, teamDescription(appConfig.Description.ShortLink, t));
365371
}
372+
log("", "CreateTeams", "Completed");
366373
}
367374

368375
async function syncOrgMembersByTeam(teamName: string, sourceTeamMap: Map<string, string>) {
@@ -375,14 +382,17 @@ async function syncOrg(installedGitHubClient: InstalledClient, appConfig: AppCon
375382
// TODO: this method is getting very busy, and most likely could benefit from a larger refactor.
376383
// Benefits most likely include performance gains.
377384
Log(`Syncing Members for ${installedGitHubClient.GetCurrentOrgName()} by individual teams.`);
385+
log("", "MembershipSync_ByTeam", "Started");
378386
const orgMembershipPromises = gitHubTeams.map(t => syncOrgMembersByTeam(t, orgConfig.DisplayNameToSourceMap));
379387
await Promise.all(orgMembershipPromises);
388+
log("", "MembershipSync_ByTeam", "Completed");
380389
}
381390

382391
let currentMembers: GitHubId[] = [];
383392
// TODO: add log message to explain group being skipped if it is included in TeamsToIgnore
384393
if (membersGroupName != undefined && membersGroupName != null && !appConfig.TeamsToIgnore.includes(membersGroupName)) {
385394
Log(`Syncing Members for ${installedGitHubClient.GetCurrentOrgName()}: ${membersGroupName}`)
395+
log("", "MembershipSync_ByOrgMembersGroup", "Started");
386396
const currentMembersResponse = await SynchronizeOrgMembers(installedGitHubClient, membersGroupName, appConfig, orgConfig.DisplayNameToSourceMap)
387397

388398
if (currentMembersResponse.Succeeded == false) {
@@ -405,6 +415,7 @@ async function syncOrg(installedGitHubClient: InstalledClient, appConfig: AppCon
405415
currentMembers = currentMembersResponse.OrgMembers;
406416

407417
await SynchronizeGitHubTeam(installedGitHubClient, membersGroupName, appConfig, currentInvites, orgConfig.DisplayNameToSourceMap);
418+
log("", "MembershipSync_ByOrgMembersGroup", "Completed");
408419
}
409420

410421
if (!gitHubTeams || gitHubTeams.length < 1) {
@@ -419,9 +430,12 @@ async function syncOrg(installedGitHubClient: InstalledClient, appConfig: AppCon
419430

420431
const teamSyncPromises = gitHubTeams.map(t => syncTeam(t, orgConfig));
421432

433+
log("", "TeamSync", "Started");
422434
await Promise.all(teamSyncPromises);
435+
log("", "TeamSync", "Completed");
423436

424437
if (ownerGroupName) {
438+
log("", "OrgOwnerSync", "Started");
425439
const teamMembers = await installedGitHubClient.ListCurrentMembersOfGitHubTeam(ownerGroupName);
426440

427441
if (!teamMembers.successful) {
@@ -444,9 +458,12 @@ async function syncOrg(installedGitHubClient: InstalledClient, appConfig: AppCon
444458
orgOwnersGroup: ownerGroupName
445459
}
446460
}
461+
log("", "OrgOwnerSync", "Completed");
447462
}
448463

464+
log("", "AddCopilotSubscriptions", "Started");
449465
const copilotResult = await installedGitHubClient.AddTeamsToCopilotSubscription(orgConfig.CopilotTeams);
466+
log("", "AddCopilotSubscriptions", "Completed");
450467

451468
return {
452469
...response,
@@ -479,7 +496,19 @@ export async function SyncOrg(installedGitHubClient: InstalledClient, config: Ap
479496
));
480497

481498
try {
482-
const response = await syncOrg(installedGitHubClient, config, invitationsClient);
499+
const log = (message: string, operation:string, status:string) => {
500+
Log(JSON.stringify(
501+
{
502+
data: message,
503+
orgName: orgName,
504+
operation: operation,
505+
status: status,
506+
trace_id: traceKey
507+
}
508+
));
509+
};
510+
511+
const response = await syncOrg(installedGitHubClient, config, invitationsClient, log);
483512

484513
Log(JSON.stringify(
485514
{

src/services/ldapClient.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,14 @@ export async function SearchAllAsync(groupName: string): SearchAllResponse {
4444

4545
// Slightly complex for caching logic, but we don't want to cache useless results
4646
if (actualResult.Succeeded && actualResult.entries.length > 0) {
47-
await redisClient.set(cacheKey, JSON.stringify(actualResult), {
48-
EX: 600 // Expire after 10 minutes
49-
});
47+
try {
48+
await redisClient.set(cacheKey, JSON.stringify(actualResult), {
49+
EX: 600 // Expire after 10 minutes
50+
});
51+
}
52+
catch(e) {
53+
Log(`Error when caching results for ${groupName}: ${JSON.stringify(e)}`);
54+
}
5055
}
5156

5257
return actualResult;

0 commit comments

Comments
 (0)