From 3145e899725b53beb88b6562dfa8813a3be1b86e Mon Sep 17 00:00:00 2001 From: Viktor Vorona Date: Wed, 27 Sep 2023 10:35:30 +0200 Subject: [PATCH] SONAR-20532 Rename parameters and update react-query cache --- .../js/api/mocks/AuthenticationServiceMock.ts | 40 ++++++++-------- .../authentication/GitHubMappingModal.tsx | 18 +++---- .../src/main/js/queries/identity-provider.ts | 48 +++++++++---------- .../src/main/js/types/provisioning.ts | 8 ++-- 4 files changed, 58 insertions(+), 56 deletions(-) diff --git a/server/sonar-web/src/main/js/api/mocks/AuthenticationServiceMock.ts b/server/sonar-web/src/main/js/api/mocks/AuthenticationServiceMock.ts index 03c59cede6b..1141cb3f296 100644 --- a/server/sonar-web/src/main/js/api/mocks/AuthenticationServiceMock.ts +++ b/server/sonar-web/src/main/js/api/mocks/AuthenticationServiceMock.ts @@ -64,60 +64,60 @@ const defaultConfigurationStatus: GitHubConfigurationStatus = { const defaultMapping: GitHubMapping[] = [ { id: 'read', - roleName: 'read', + githubRole: 'read', permissions: { user: true, - codeviewer: true, - issueadmin: false, - securityhotspotadmin: false, + codeViewer: true, + issueAdmin: false, + securityHotspotAdmin: false, admin: false, scan: false, }, }, { id: 'write', - roleName: 'write', + githubRole: 'write', permissions: { user: true, - codeviewer: true, - issueadmin: true, - securityhotspotadmin: true, + codeViewer: true, + issueAdmin: true, + securityHotspotAdmin: true, admin: false, scan: true, }, }, { id: 'triage', - roleName: 'triage', + githubRole: 'triage', permissions: { user: true, - codeviewer: true, - issueadmin: false, - securityhotspotadmin: false, + codeViewer: true, + issueAdmin: false, + securityHotspotAdmin: false, admin: false, scan: false, }, }, { id: 'maintain', - roleName: 'maintain', + githubRole: 'maintain', permissions: { user: true, - codeviewer: true, - issueadmin: true, - securityhotspotadmin: true, + codeViewer: true, + issueAdmin: true, + securityHotspotAdmin: true, admin: false, scan: true, }, }, { id: 'admin', - roleName: 'admin', + githubRole: 'admin', permissions: { user: true, - codeviewer: true, - issueadmin: true, - securityhotspotadmin: true, + codeViewer: true, + issueAdmin: true, + securityHotspotAdmin: true, admin: true, scan: true, }, diff --git a/server/sonar-web/src/main/js/apps/settings/components/authentication/GitHubMappingModal.tsx b/server/sonar-web/src/main/js/apps/settings/components/authentication/GitHubMappingModal.tsx index 7c4026ceba5..795d3c419c1 100644 --- a/server/sonar-web/src/main/js/apps/settings/components/authentication/GitHubMappingModal.tsx +++ b/server/sonar-web/src/main/js/apps/settings/components/authentication/GitHubMappingModal.tsx @@ -47,9 +47,9 @@ interface PermissionCellProps { const DEFAULT_CUSTOM_ROLE_PERMISSIONS: GitHubMapping['permissions'] = { user: false, - codeviewer: false, - issueadmin: false, - securityhotspotadmin: false, + codeViewer: false, + issueAdmin: false, + securityHotspotAdmin: false, admin: false, scan: false, }; @@ -63,14 +63,14 @@ function PermissionRow(props: Readonly) {
- {mapping.roleName} + {mapping.githubRole} {!mapping.isBaseRole && ( { - props.setMapping(list?.filter((r) => r.roleName !== mapping.roleName) ?? null); + props.setMapping(list?.filter((r) => r.githubRole !== mapping.githubRole) ?? null); }} /> )} @@ -116,13 +116,15 @@ export default function GitHubMappingModal({ mapping, setMapping, onClose }: Rea const value = customRoleInput.trim(); if ( !list?.some((el) => - el.isBaseRole ? el.roleName.toLowerCase() === value.toLowerCase() : el.roleName === value, + el.isBaseRole + ? el.githubRole.toLowerCase() === value.toLowerCase() + : el.githubRole === value, ) ) { setMapping([ { id: customRoleInput, - roleName: customRoleInput, + githubRole: customRoleInput, permissions: { ...DEFAULT_CUSTOM_ROLE_PERMISSIONS }, }, ...(list ?? []), diff --git a/server/sonar-web/src/main/js/queries/identity-provider.ts b/server/sonar-web/src/main/js/queries/identity-provider.ts index 8a20c11108d..6017978687e 100644 --- a/server/sonar-web/src/main/js/queries/identity-provider.ts +++ b/server/sonar-web/src/main/js/queries/identity-provider.ts @@ -19,7 +19,7 @@ */ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'; -import { isEqual, keyBy, omit, partition } from 'lodash'; +import { isEqual, keyBy, partition, pick, unionBy } from 'lodash'; import { useContext } from 'react'; import { activateGithubProvisioning, @@ -124,6 +124,7 @@ export function useSyncWithGitHubNow() { }; } +// Order is reversed to put non-existing roles at the end (their index is -1) const defaultRoleOrder = ['admin', 'maintain', 'write', 'triage', 'read']; export function useGithubRolesMappingQuery() { @@ -131,11 +132,10 @@ export function useGithubRolesMappingQuery() { staleTime: MAPPING_STALE_TIME, select: (data) => [...data].sort((a, b) => { - // Order is reversed to put non-existing roles at the end (their index is -1) if (defaultRoleOrder.includes(a.id) || defaultRoleOrder.includes(b.id)) { return defaultRoleOrder.indexOf(b.id) - defaultRoleOrder.indexOf(a.id); } - return a.roleName.localeCompare(b.roleName); + return a.githubRole.localeCompare(b.githubRole); }), }); } @@ -145,36 +145,36 @@ export function useGithubRolesMappingMutation() { const queryKey = ['identity_provider', 'github_mapping']; return useMutation({ mutationFn: async (mapping: GitHubMapping[]) => { - const [defaultMapping, customMapping] = partition(mapping, (m) => - defaultRoleOrder.includes(m.roleName), - ); const state = keyBy(client.getQueryData(queryKey), (m) => m.id); - const [customMaybeChangedmRoles, customNewRoles] = partition( - customMapping, - (m) => state[m.id], - ); - - const changeRole = [...defaultMapping, ...customMaybeChangedmRoles].filter( - (item) => !isEqual(item, state[item.id]), - ); - - const customDeleteRole = Object.values(state).filter( - (m) => !defaultRoleOrder.includes(m.roleName) && !changeRole.some((cm) => m.id === cm.id), + const [maybeChangedRoles, newRoles] = partition(mapping, (m) => state[m.id]); + const changedRoles = maybeChangedRoles.filter((item) => !isEqual(item, state[item.id])); + const deletedRoles = Object.values(state).filter( + (m) => !m.isBaseRole && !mapping.some((cm) => m.id === cm.id), ); return { - addedOrChange: await Promise.all([ - ...changeRole.map((data) => - updateGithubRolesMapping(data.id, omit(data, 'id', 'roleName')), + addedOrChanged: await Promise.all([ + ...changedRoles.map((data) => + updateGithubRolesMapping(data.id, pick(data, 'permissions')), ), - ...customNewRoles.map((m) => addGithubRolesMapping(m)), + ...newRoles.map((m) => addGithubRolesMapping(m)), ]), - delete: await Promise.all([customDeleteRole.map((dm) => deleteGithubRolesMapping(dm.id))]), + deleted: await Promise.all([ + deletedRoles.map((dm) => deleteGithubRolesMapping(dm.id)), + ]).then(() => deletedRoles.map((dm) => dm.id)), }; }, - onSuccess: ({ addedOrChange }) => { - client.setQueryData(queryKey, addedOrChange); + onSuccess: ({ addedOrChanged, deleted }) => { + const state = client.getQueryData(queryKey); + if (state) { + const newData = unionBy( + addedOrChanged, + state.filter((s) => !deleted.find((id) => id === s.id)), + (el) => el.id, + ); + client.setQueryData(queryKey, newData); + } }, }); } diff --git a/server/sonar-web/src/main/js/types/provisioning.ts b/server/sonar-web/src/main/js/types/provisioning.ts index f59b1b610de..e3ff30b3b9c 100644 --- a/server/sonar-web/src/main/js/types/provisioning.ts +++ b/server/sonar-web/src/main/js/types/provisioning.ts @@ -78,13 +78,13 @@ export interface GitHubConfigurationStatus { export interface GitHubMapping { readonly id: string; - readonly roleName: string; + readonly githubRole: string; readonly isBaseRole?: boolean; permissions: { user: boolean; - codeviewer: boolean; - issueadmin: boolean; - securityhotspotadmin: boolean; + codeViewer: boolean; + issueAdmin: boolean; + securityHotspotAdmin: boolean; admin: boolean; scan: boolean; }; -- 2.39.5