From 8b8a54d89c3acae3b0e693b55f0348436ad5a513 Mon Sep 17 00:00:00 2001 From: David Cho-Lerat Date: Fri, 3 Nov 2023 08:28:59 +0100 Subject: [PATCH] SONAR-20447 Pass extra 'pullRequest' parameter to the SonarLint call --- .../issues/components/IssueOpenInIdeButton.tsx | 10 ++++++++-- .../__tests__/IssueOpenInIdeButton-test.tsx | 2 ++ .../IssueSourceViewerHeader.tsx | 15 ++++++++++----- .../main/js/helpers/__tests__/sonarlint-test.ts | 15 +++++++++++++++ server/sonar-web/src/main/js/helpers/sonarlint.ts | 5 +++++ 5 files changed, 40 insertions(+), 7 deletions(-) diff --git a/server/sonar-web/src/main/js/apps/issues/components/IssueOpenInIdeButton.tsx b/server/sonar-web/src/main/js/apps/issues/components/IssueOpenInIdeButton.tsx index 3e89755df69..f391b2ff87a 100644 --- a/server/sonar-web/src/main/js/apps/issues/components/IssueOpenInIdeButton.tsx +++ b/server/sonar-web/src/main/js/apps/issues/components/IssueOpenInIdeButton.tsx @@ -39,6 +39,7 @@ export interface Props { branchName?: string; issueKey: string; projectKey: string; + pullRequestID?: string; } interface State { @@ -62,7 +63,12 @@ const showError = () => const showSuccess = () => addGlobalSuccessMessage(translate('issues.open_in_ide.success')); -export function IssueOpenInIdeButton({ branchName, issueKey, projectKey }: Readonly) { +export function IssueOpenInIdeButton({ + branchName, + issueKey, + projectKey, + pullRequestID, +}: Readonly) { const [state, setState] = React.useState({ ides: [], mounted: false }); React.useEffect(() => { @@ -83,7 +89,7 @@ export function IssueOpenInIdeButton({ branchName, issueKey, projectKey }: Reado const openIssue = (ide: Ide) => { setState({ ...state, ides: [] }); - return openSonarLintIssue(ide.port, projectKey, issueKey, branchName) + return openSonarLintIssue(ide.port, projectKey, issueKey, branchName, pullRequestID) .then(showSuccess) .catch(showError) .finally(cleanState); diff --git a/server/sonar-web/src/main/js/apps/issues/components/__tests__/IssueOpenInIdeButton-test.tsx b/server/sonar-web/src/main/js/apps/issues/components/__tests__/IssueOpenInIdeButton-test.tsx index adda8abf21a..004d5647a5c 100644 --- a/server/sonar-web/src/main/js/apps/issues/components/__tests__/IssueOpenInIdeButton-test.tsx +++ b/server/sonar-web/src/main/js/apps/issues/components/__tests__/IssueOpenInIdeButton-test.tsx @@ -118,6 +118,7 @@ it('handles button click with one ide found', async () => { MOCK_PROJECT_KEY, MOCK_ISSUE_KEY, undefined, + undefined, ); expect(addGlobalSuccessMessage).toHaveBeenCalledWith('issues.open_in_ide.success'); @@ -163,6 +164,7 @@ it('handles button click with several ides found', async () => { MOCK_PROJECT_KEY, MOCK_ISSUE_KEY, undefined, + undefined, ); expect(addGlobalSuccessMessage).toHaveBeenCalledWith('issues.open_in_ide.success'); diff --git a/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/IssueSourceViewerHeader.tsx b/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/IssueSourceViewerHeader.tsx index 2f920ddb0f6..e97d9794092 100644 --- a/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/IssueSourceViewerHeader.tsx +++ b/server/sonar-web/src/main/js/apps/issues/crossComponentSourceViewer/IssueSourceViewerHeader.tsx @@ -90,16 +90,16 @@ export function IssueSourceViewerHeader(props: Readonly) { border-bottom: none; `; - const branchName = React.useMemo(() => { + const [branchName, pullRequestID] = React.useMemo(() => { if (isBranch(branchLike)) { - return branchLike.name; + return [branchLike.name, undefined]; } if (isPullRequest(branchLike)) { - return branchLike.branch; + return [branchLike.branch, branchLike.key]; } - return undefined; // should never end up here, but needed for consistent returns + return [undefined, undefined]; // should never end up here, but needed for consistent returns }, [branchLike]); return ( @@ -163,7 +163,12 @@ export function IssueSourceViewerHeader(props: Readonly) { {!isProjectRoot && isLoggedIn(currentUser) && ( - + )} {!isProjectRoot && measures.issues !== undefined && ( diff --git a/server/sonar-web/src/main/js/helpers/__tests__/sonarlint-test.ts b/server/sonar-web/src/main/js/helpers/__tests__/sonarlint-test.ts index 6db7f0bb4a0..af8b0b8c078 100644 --- a/server/sonar-web/src/main/js/helpers/__tests__/sonarlint-test.ts +++ b/server/sonar-web/src/main/js/helpers/__tests__/sonarlint-test.ts @@ -96,6 +96,7 @@ describe('openHotspot', () => { describe('openIssue', () => { it('should send the correct request to the IDE to open an issue', async () => { let branchName: string | undefined = undefined; + let pullRequestID: string | undefined = undefined; const issueKey = 'my-issue-key'; const resp = new Response(); @@ -108,6 +109,8 @@ describe('openIssue', () => { expect(calledUrl.searchParams.get('issue')).toStrictEqual(issueKey); // eslint-disable-next-line jest/no-conditional-in-test expect(calledUrl.searchParams.get('branch') ?? undefined).toStrictEqual(branchName); + // eslint-disable-next-line jest/no-conditional-in-test + expect(calledUrl.searchParams.get('pullRequest') ?? undefined).toStrictEqual(pullRequestID); } catch (error) { return Promise.reject(error); } @@ -124,6 +127,18 @@ describe('openIssue', () => { result = await openIssue(SONARLINT_PORT_START, PROJECT_KEY, issueKey, branchName); expect(result).toBe(resp); + + pullRequestID = 'pr-1'; + + result = await openIssue( + SONARLINT_PORT_START, + PROJECT_KEY, + issueKey, + branchName, + pullRequestID, + ); + + expect(result).toBe(resp); }); }); diff --git a/server/sonar-web/src/main/js/helpers/sonarlint.ts b/server/sonar-web/src/main/js/helpers/sonarlint.ts index b4d9dbccd33..07dbf341ab5 100644 --- a/server/sonar-web/src/main/js/helpers/sonarlint.ts +++ b/server/sonar-web/src/main/js/helpers/sonarlint.ts @@ -60,6 +60,7 @@ export function openIssue( projectKey: string, issueKey: string, branchName?: string, + pullRequestID?: string, ) { const showUrl = new URL(buildSonarLintEndpoint(calledPort, '/issues/show')); @@ -71,6 +72,10 @@ export function openIssue( showUrl.searchParams.set('branch', branchName); } + if (pullRequestID !== undefined) { + showUrl.searchParams.set('pullRequest', pullRequestID); + } + return fetch(showUrl.toString()).then((response: Response) => checkStatus(response, true)); } -- 2.39.5