From 0c3f9e70e84945aba2952d047f4ecb31654fd2b5 Mon Sep 17 00:00:00 2001 From: Jeremy Davis Date: Thu, 12 Sep 2024 09:14:59 +0200 Subject: [PATCH] SONAR-22970 Fix openinIDE --- .../js/apps/issues/components/OpenFixInIde.tsx | 14 ++++++++------ .../components/__tests__/OpenFixInIde-test.tsx | 18 ++++++++++++++++-- 2 files changed, 24 insertions(+), 8 deletions(-) diff --git a/server/sonar-web/src/main/js/apps/issues/components/OpenFixInIde.tsx b/server/sonar-web/src/main/js/apps/issues/components/OpenFixInIde.tsx index 92c1c52fced..adfe1b0cbed 100644 --- a/server/sonar-web/src/main/js/apps/issues/components/OpenFixInIde.tsx +++ b/server/sonar-web/src/main/js/apps/issues/components/OpenFixInIde.tsx @@ -20,10 +20,11 @@ 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) { const [ides, setIdes] = useState([]); - 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) { 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) { }; await openFixInIde({ - branchLike: data?.branchLike, + branchLike, ide, fix, issue, @@ -95,7 +97,7 @@ export function OpenFixInIde({ aiSuggestion, issue }: Readonly) { 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) { } }; - if (!isLoggedIn || data?.branchLike === undefined || sourceViewerFile === undefined) { + if (!isLoggedIn || branchLike === undefined || sourceViewerFile === undefined) { return null; } diff --git a/server/sonar-web/src/main/js/apps/issues/components/__tests__/OpenFixInIde-test.tsx b/server/sonar-web/src/main/js/apps/issues/components/__tests__/OpenFixInIde-test.tsx index 0b7ddd1bcd6..d64448a24f5 100644 --- a/server/sonar-web/src/main/js/apps/issues/components/__tests__/OpenFixInIde-test.tsx +++ b/server/sonar-web/src/main/js/apps/issues/components/__tests__/OpenFixInIde-test.tsx @@ -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 ; + + return ( + + + + ); } return renderComponent(, '/?id=mycomponent', { currentUser: mockLoggedInUser() }); -- 2.39.5