Lint fix and greptile comments addressed

This commit is contained in:
Carlos Monastyrski
2025-08-28 01:27:07 -03:00
parent 7d74dce82b
commit e8ef0191d6
3 changed files with 56 additions and 42 deletions
@@ -179,18 +179,20 @@ export const registerGithubOrgSyncRouter = async (server: FastifyZodProvider) =>
onRequest: verifyAuth([AuthMode.JWT]), onRequest: verifyAuth([AuthMode.JWT]),
schema: { schema: {
body: z.object({ body: z.object({
githubOrgAccessToken: z.string().trim().min(1, "GitHub access token is required") githubOrgAccessToken: z.string().trim().min(1, "GitHub access token is required").max(1000)
}), }),
response: { response: {
200: z.object({ 200: z.object({
valid: z.boolean(), valid: z.boolean(),
organizationInfo: z.object({ organizationInfo: z
id: z.number(), .object({
login: z.string(), id: z.number(),
name: z.string(), login: z.string(),
publicRepos: z.number().optional(), name: z.string(),
privateRepos: z.number().optional() publicRepos: z.number().optional(),
}).optional() privateRepos: z.number().optional()
})
.optional()
}) })
} }
}, },
@@ -330,8 +330,8 @@ export const githubOrgSyncServiceFactory = ({
const removeFromTeams = infisicalUserGroups.filter((el) => !githubUserTeamSet.has(el.groupName)); const removeFromTeams = infisicalUserGroups.filter((el) => !githubUserTeamSet.has(el.groupName));
if (newTeams.length || updateTeams.length || removeFromTeams.length) { if (newTeams.length || updateTeams.length || removeFromTeams.length) {
await groupDAL.transaction(async (tx) => { if (newTeams.length) {
if (newTeams.length) { await groupDAL.transaction(async (tx) => {
const newGroups = await groupDAL.insertMany( const newGroups = await groupDAL.insertMany(
newTeams.map((newGroupName) => ({ newTeams.map((newGroupName) => ({
name: newGroupName, name: newGroupName,
@@ -348,9 +348,11 @@ export const githubOrgSyncServiceFactory = ({
})), })),
tx tx
); );
} });
}
if (updateTeams.length) { if (updateTeams.length) {
await groupDAL.transaction(async (tx) => {
await userGroupMembershipDAL.insertMany( await userGroupMembershipDAL.insertMany(
updateTeams.map((el) => ({ updateTeams.map((el) => ({
groupId: githubUserTeamOnInfisicalGroupByName[el][0].id, groupId: githubUserTeamOnInfisicalGroupByName[el][0].id,
@@ -358,15 +360,17 @@ export const githubOrgSyncServiceFactory = ({
})), })),
tx tx
); );
} });
}
if (removeFromTeams.length) { if (removeFromTeams.length) {
await groupDAL.transaction(async (tx) => {
await userGroupMembershipDAL.delete( await userGroupMembershipDAL.delete(
{ userId, $in: { groupId: removeFromTeams.map((el) => el.groupId) } }, { userId, $in: { groupId: removeFromTeams.map((el) => el.groupId) } },
tx tx
); );
} });
}); }
} }
}; };
@@ -683,15 +687,15 @@ export const githubOrgSyncServiceFactory = ({
const removeFromTeams = infisicalUserGroups.filter((el) => !githubUserTeamSet.has(el.groupName)); const removeFromTeams = infisicalUserGroups.filter((el) => !githubUserTeamSet.has(el.groupName));
if (newTeams.length || updateTeams.length || removeFromTeams.length) { if (newTeams.length || updateTeams.length || removeFromTeams.length) {
return await groupDAL.transaction(async (tx) => { const result = {
const result = { createdTeams: [] as string[],
createdTeams: [] as string[], updatedTeams: [] as string[],
updatedTeams: [] as string[], removedMemberships: 0
removedMemberships: 0 };
};
try { try {
if (newTeams.length) { if (newTeams.length) {
await groupDAL.transaction(async (tx) => {
logger.info({ userId, githubUsername, newTeams, orgId }, "Creating new teams for user"); logger.info({ userId, githubUsername, newTeams, orgId }, "Creating new teams for user");
const newGroups = await groupDAL.insertMany( const newGroups = await groupDAL.insertMany(
@@ -711,11 +715,13 @@ export const githubOrgSyncServiceFactory = ({
})), })),
tx tx
); );
});
result.createdTeams = newTeams; result.createdTeams = newTeams;
} }
if (updateTeams.length) { if (updateTeams.length) {
await groupDAL.transaction(async (tx) => {
logger.info({ userId, githubUsername, updateTeams, orgId }, "Adding user to existing teams"); logger.info({ userId, githubUsername, updateTeams, orgId }, "Adding user to existing teams");
await userGroupMembershipDAL.insertMany( await userGroupMembershipDAL.insertMany(
@@ -725,11 +731,13 @@ export const githubOrgSyncServiceFactory = ({
})), })),
tx tx
); );
});
result.updatedTeams = updateTeams; result.updatedTeams = updateTeams;
} }
if (removeFromTeams.length) { if (removeFromTeams.length) {
await groupDAL.transaction(async (tx) => {
logger.info( logger.info(
{ userId, githubUsername, removeFromTeams: removeFromTeams.map((t) => t.groupName), orgId }, { userId, githubUsername, removeFromTeams: removeFromTeams.map((t) => t.groupName), orgId },
"Removing user from teams" "Removing user from teams"
@@ -739,16 +747,16 @@ export const githubOrgSyncServiceFactory = ({
{ userId, $in: { groupId: removeFromTeams.map((el) => el.groupId) } }, { userId, $in: { groupId: removeFromTeams.map((el) => el.groupId) } },
tx tx
); );
});
result.removedMemberships = removeFromTeams.length; result.removedMemberships = removeFromTeams.length;
}
return result;
} catch (error) {
logger.error(error, `Failed to update team memberships for user ${userId} (${githubUsername})`);
throw error;
} }
});
return result;
} catch (error) {
logger.error(error, `Failed to update team memberships for user ${userId} (${githubUsername})`);
throw error;
}
} }
return null; return null;
@@ -130,7 +130,7 @@ export const OrgGithubSyncSection = () => {
text: "Please enter a GitHub access token", text: "Please enter a GitHub access token",
type: "error" type: "error"
}); });
return; return false;
} }
setIsValidatingToken(true); setIsValidatingToken(true);
@@ -147,6 +147,8 @@ export const OrgGithubSyncSection = () => {
type: "success" type: "success"
}); });
} }
return result.valid;
} catch (error) { } catch (error) {
const errorMessage = const errorMessage =
(error as any)?.response?.data?.message || (error as any)?.response?.data?.message ||
@@ -157,6 +159,7 @@ export const OrgGithubSyncSection = () => {
type: "error" type: "error"
}); });
setTokenValidationResult({ valid: false }); setTokenValidationResult({ valid: false });
return false;
} finally { } finally {
setIsValidatingToken(false); setIsValidatingToken(false);
} }
@@ -171,9 +174,10 @@ export const OrgGithubSyncSection = () => {
return; return;
} }
if (!tokenValidationResult?.valid) { let isTokenValid = tokenValidationResult?.valid ?? false;
await validateToken(); if (!isTokenValid) {
if (!tokenValidationResult?.valid) { isTokenValid = await validateToken();
if (!isTokenValid) {
return; return;
} }
} }