]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-20532 Rename parameters and update react-query cache
authorViktor Vorona <viktor.vorona@sonarsource.com>
Wed, 27 Sep 2023 08:35:30 +0000 (10:35 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 28 Sep 2023 20:03:11 +0000 (20:03 +0000)
server/sonar-web/src/main/js/api/mocks/AuthenticationServiceMock.ts
server/sonar-web/src/main/js/apps/settings/components/authentication/GitHubMappingModal.tsx
server/sonar-web/src/main/js/queries/identity-provider.ts
server/sonar-web/src/main/js/types/provisioning.ts

index 03c59cede6bbfed8b350afc12a6f876393d6475b..1141cb3f29680da44e4d5a5ff969d361a493167e 100644 (file)
@@ -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,
     },
index 7c4026ceba5fddc63f28e9b3e05aa478f17ce83d..795d3c419c1287fa418ca55c6cbda832de16d9fb 100644 (file)
@@ -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<PermissionCellProps>) {
         <div className="sw-flex sw-max-w-[150px] sw-items-center">
           <b
             className={mapping.isBaseRole ? 'sw-capitalize' : 'sw-truncate'}
-            title={mapping.roleName}
+            title={mapping.githubRole}
           >
-            {mapping.roleName}
+            {mapping.githubRole}
           </b>
           {!mapping.isBaseRole && (
             <DeleteButton
               onClick={() => {
-                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 ?? []),
index 8a20c11108d04381133bb5b4a64c412521820d70..6017978687edfd0ac3cb5027f9c25b020d441196 100644 (file)
@@ -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<GitHubMapping[]>(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<GitHubMapping[]>(queryKey);
+      if (state) {
+        const newData = unionBy(
+          addedOrChanged,
+          state.filter((s) => !deleted.find((id) => id === s.id)),
+          (el) => el.id,
+        );
+        client.setQueryData(queryKey, newData);
+      }
     },
   });
 }
index f59b1b610de71b54bf72c39683ee11a3353de15d..e3ff30b3b9cd3f1f88fa1d8acc26449cdff877a8 100644 (file)
@@ -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;
   };