diff --git a/backend/src/ee/routes/v1/github-org-sync-router.ts b/backend/src/ee/routes/v1/github-org-sync-router.ts index 9aa226d94..1d7c85c4d 100644 --- a/backend/src/ee/routes/v1/github-org-sync-router.ts +++ b/backend/src/ee/routes/v1/github-org-sync-router.ts @@ -179,18 +179,20 @@ export const registerGithubOrgSyncRouter = async (server: FastifyZodProvider) => onRequest: verifyAuth([AuthMode.JWT]), schema: { 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: { 200: z.object({ valid: z.boolean(), - organizationInfo: z.object({ - id: z.number(), - login: z.string(), - name: z.string(), - publicRepos: z.number().optional(), - privateRepos: z.number().optional() - }).optional() + organizationInfo: z + .object({ + id: z.number(), + login: z.string(), + name: z.string(), + publicRepos: z.number().optional(), + privateRepos: z.number().optional() + }) + .optional() }) } }, diff --git a/backend/src/ee/services/github-org-sync/github-org-sync-service.ts b/backend/src/ee/services/github-org-sync/github-org-sync-service.ts index 50f9e2a9c..141c243f9 100644 --- a/backend/src/ee/services/github-org-sync/github-org-sync-service.ts +++ b/backend/src/ee/services/github-org-sync/github-org-sync-service.ts @@ -330,8 +330,8 @@ export const githubOrgSyncServiceFactory = ({ const removeFromTeams = infisicalUserGroups.filter((el) => !githubUserTeamSet.has(el.groupName)); 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( newTeams.map((newGroupName) => ({ name: newGroupName, @@ -348,9 +348,11 @@ export const githubOrgSyncServiceFactory = ({ })), tx ); - } + }); + } - if (updateTeams.length) { + if (updateTeams.length) { + await groupDAL.transaction(async (tx) => { await userGroupMembershipDAL.insertMany( updateTeams.map((el) => ({ groupId: githubUserTeamOnInfisicalGroupByName[el][0].id, @@ -358,15 +360,17 @@ export const githubOrgSyncServiceFactory = ({ })), tx ); - } + }); + } - if (removeFromTeams.length) { + if (removeFromTeams.length) { + await groupDAL.transaction(async (tx) => { await userGroupMembershipDAL.delete( { userId, $in: { groupId: removeFromTeams.map((el) => el.groupId) } }, tx ); - } - }); + }); + } } }; @@ -683,15 +687,15 @@ export const githubOrgSyncServiceFactory = ({ const removeFromTeams = infisicalUserGroups.filter((el) => !githubUserTeamSet.has(el.groupName)); if (newTeams.length || updateTeams.length || removeFromTeams.length) { - return await groupDAL.transaction(async (tx) => { - const result = { - createdTeams: [] as string[], - updatedTeams: [] as string[], - removedMemberships: 0 - }; + const result = { + createdTeams: [] as string[], + updatedTeams: [] as string[], + removedMemberships: 0 + }; - try { - if (newTeams.length) { + try { + if (newTeams.length) { + await groupDAL.transaction(async (tx) => { logger.info({ userId, githubUsername, newTeams, orgId }, "Creating new teams for user"); const newGroups = await groupDAL.insertMany( @@ -711,11 +715,13 @@ export const githubOrgSyncServiceFactory = ({ })), 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"); await userGroupMembershipDAL.insertMany( @@ -725,11 +731,13 @@ export const githubOrgSyncServiceFactory = ({ })), tx ); + }); - result.updatedTeams = updateTeams; - } + result.updatedTeams = updateTeams; + } - if (removeFromTeams.length) { + if (removeFromTeams.length) { + await groupDAL.transaction(async (tx) => { logger.info( { userId, githubUsername, removeFromTeams: removeFromTeams.map((t) => t.groupName), orgId }, "Removing user from teams" @@ -739,16 +747,16 @@ export const githubOrgSyncServiceFactory = ({ { userId, $in: { groupId: removeFromTeams.map((el) => el.groupId) } }, tx ); + }); - result.removedMemberships = removeFromTeams.length; - } - - return result; - } catch (error) { - logger.error(error, `Failed to update team memberships for user ${userId} (${githubUsername})`); - throw error; + result.removedMemberships = removeFromTeams.length; } - }); + + return result; + } catch (error) { + logger.error(error, `Failed to update team memberships for user ${userId} (${githubUsername})`); + throw error; + } } return null; diff --git a/frontend/src/pages/organization/SettingsPage/components/OrgProvisioningTab/OrgGithubSyncSection.tsx b/frontend/src/pages/organization/SettingsPage/components/OrgProvisioningTab/OrgGithubSyncSection.tsx index 486605530..61eb6fb06 100644 --- a/frontend/src/pages/organization/SettingsPage/components/OrgProvisioningTab/OrgGithubSyncSection.tsx +++ b/frontend/src/pages/organization/SettingsPage/components/OrgProvisioningTab/OrgGithubSyncSection.tsx @@ -130,7 +130,7 @@ export const OrgGithubSyncSection = () => { text: "Please enter a GitHub access token", type: "error" }); - return; + return false; } setIsValidatingToken(true); @@ -147,6 +147,8 @@ export const OrgGithubSyncSection = () => { type: "success" }); } + + return result.valid; } catch (error) { const errorMessage = (error as any)?.response?.data?.message || @@ -157,6 +159,7 @@ export const OrgGithubSyncSection = () => { type: "error" }); setTokenValidationResult({ valid: false }); + return false; } finally { setIsValidatingToken(false); } @@ -171,9 +174,10 @@ export const OrgGithubSyncSection = () => { return; } - if (!tokenValidationResult?.valid) { - await validateToken(); - if (!tokenValidationResult?.valid) { + let isTokenValid = tokenValidationResult?.valid ?? false; + if (!isTokenValid) { + isTokenValid = await validateToken(); + if (!isTokenValid) { return; } }