]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-22970 Fix openinIDE
authorJeremy Davis <jeremy.davis@sonarsource.com>
Thu, 12 Sep 2024 07:14:59 +0000 (09:14 +0200)
committersonartech <sonartech@sonarsource.com>
Thu, 12 Sep 2024 20:02:54 +0000 (20:02 +0000)
server/sonar-web/src/main/js/apps/issues/components/OpenFixInIde.tsx
server/sonar-web/src/main/js/apps/issues/components/__tests__/OpenFixInIde-test.tsx

index 92c1c52fcede547e591290d1236ebbec7dfaf998..adfe1b0cbed27d59c36322eed9253466fc37eeae 100644 (file)
 import { Button, ButtonVariety, DropdownMenu } from '@sonarsource/echoes-react';
 import { addGlobalErrorMessage } from 'design-system/lib';
 import React, { useCallback, useState } from 'react';
+import { useComponent } from '../../../app/components/componentContext/withComponentContext';
 import { useCurrentUser } from '../../../app/components/current-user/CurrentUserContext';
 import { translate } from '../../../helpers/l10n';
 import { probeSonarLintServers } from '../../../helpers/sonarlint';
-import { useBranchesQuery } from '../../../queries/branch';
+import { useCurrentBranchQuery } from '../../../queries/branch';
 import { useComponentForSourceViewer } from '../../../queries/component';
 import { CodeSuggestion } from '../../../queries/fix-suggestions';
 import { useOpenFixOrIssueInIdeMutation } from '../../../queries/sonarlint';
@@ -39,7 +40,8 @@ const DELAY_AFTER_TOKEN_CREATION = 3000;
 
 export function OpenFixInIde({ aiSuggestion, issue }: Readonly<Props>) {
   const [ides, setIdes] = useState<Ide[]>([]);
-  const { data, isLoading: isBranchLoading } = useBranchesQuery();
+  const { component } = useComponent();
+  const { data: branchLike, isLoading: isBranchLoading } = useCurrentBranchQuery(component);
 
   const {
     currentUser: { isLoggedIn },
@@ -47,7 +49,7 @@ export function OpenFixInIde({ aiSuggestion, issue }: Readonly<Props>) {
 
   const { data: sourceViewerFile } = useComponentForSourceViewer(
     issue.component,
-    data?.branchLike,
+    branchLike,
     !isBranchLoading,
   );
   const { mutateAsync: openFixInIde, isPending } = useOpenFixOrIssueInIdeMutation();
@@ -82,7 +84,7 @@ export function OpenFixInIde({ aiSuggestion, issue }: Readonly<Props>) {
       };
 
       await openFixInIde({
-        branchLike: data?.branchLike,
+        branchLike,
         ide,
         fix,
         issue,
@@ -95,7 +97,7 @@ export function OpenFixInIde({ aiSuggestion, issue }: Readonly<Props>) {
         ide.needsToken ? DELAY_AFTER_TOKEN_CREATION : 0,
       );
     },
-    [aiSuggestion, issue, sourceViewerFile, data, openFixInIde],
+    [aiSuggestion, issue, sourceViewerFile, branchLike, openFixInIde],
   );
 
   const onClick = async () => {
@@ -112,7 +114,7 @@ export function OpenFixInIde({ aiSuggestion, issue }: Readonly<Props>) {
     }
   };
 
-  if (!isLoggedIn || data?.branchLike === undefined || sourceViewerFile === undefined) {
+  if (!isLoggedIn || branchLike === undefined || sourceViewerFile === undefined) {
     return null;
   }
 
index 0b7ddd1bcd6d0879d9cd68a307246068a3eb1ccb..d64448a24f532c288d5e45428ba600f9d098e67f 100644 (file)
@@ -23,10 +23,13 @@ import { screen } from '@testing-library/react';
 import userEvent from '@testing-library/user-event';
 import React, { ComponentProps } from 'react';
 import BranchesServiceMock from '../../../../api/mocks/BranchesServiceMock';
+import { ComponentContext } from '../../../../app/components/componentContext/ComponentContext';
+import { mockComponent } from '../../../../helpers/mocks/component';
 import { openFixOrIssueInSonarLint, probeSonarLintServers } from '../../../../helpers/sonarlint';
 import { mockIssue, mockLoggedInUser } from '../../../../helpers/testMocks';
 import { renderComponent } from '../../../../helpers/testReactTestingUtils';
 import { CodeSuggestion, LineTypeEnum } from '../../../../queries/fix-suggestions';
+import { ComponentContextShape } from '../../../../types/component';
 import { Fix, Ide } from '../../../../types/sonarlint';
 import { OpenFixInIde } from '../OpenFixInIde';
 
@@ -136,7 +139,7 @@ it('handles open in ide button click with several ides found when there is fix s
   await user.click(secondIde);
 
   expect(openFixOrIssueInSonarLint).toHaveBeenCalledWith({
-    branchLike: {},
+    branchLike: expect.objectContaining({ isMain: true, name: 'main' }),
     calledPort: MOCK_IDES_OPEN_FIX[1].port,
     fix: FIX_DATA,
     issueKey: MOCK_ISSUE_KEY,
@@ -153,10 +156,21 @@ function renderComponentOpenIssueInIdeButton(
     projectKey: MOCK_PROJECT_KEY,
   });
 
+  const componentContext: ComponentContextShape = {
+    fetchComponent: jest.fn(),
+    onComponentChange: jest.fn(),
+    component: mockComponent(),
+  };
+
   function Wrapper() {
     const queryClient = useQueryClient();
     queryClient.setQueryData(['branches', 'mycomponent', 'details'], { branchLike: {} });
-    return <OpenFixInIde aiSuggestion={AI_SUGGESTION} issue={mockedIssue} {...props} />;
+
+    return (
+      <ComponentContext.Provider value={componentContext}>
+        <OpenFixInIde aiSuggestion={AI_SUGGESTION} issue={mockedIssue} {...props} />
+      </ComponentContext.Provider>
+    );
   }
 
   return renderComponent(<Wrapper />, '/?id=mycomponent', { currentUser: mockLoggedInUser() });