From: Jeremy Davis Date: Mon, 31 Oct 2022 13:14:26 +0000 (+0100) Subject: SONAR-17465 Handle missing port number for SonarLint Connection X-Git-Tag: 9.8.0.63668~167 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e028637e1ab4ac6715eb56a6898c1c9758e5ec87;p=sonarqube.git SONAR-17465 Handle missing port number for SonarLint Connection --- diff --git a/server/sonar-web/src/main/js/app/components/SonarLintConnection.tsx b/server/sonar-web/src/main/js/app/components/SonarLintConnection.tsx index 591a14d4ee3..1a997e66bc7 100644 --- a/server/sonar-web/src/main/js/app/components/SonarLintConnection.tsx +++ b/server/sonar-web/src/main/js/app/components/SonarLintConnection.tsx @@ -19,7 +19,7 @@ */ import * as React from 'react'; import { FormattedMessage } from 'react-intl'; -import { useNavigate, useSearchParams } from 'react-router-dom'; +import { useSearchParams } from 'react-router-dom'; import { generateToken, getTokens } from '../../api/user-tokens'; import Link from '../../components/common/Link'; import { Button } from '../../components/controls/buttons'; @@ -40,8 +40,8 @@ import './SonarLintConnection.css'; enum Status { request, tokenError, - connectionError, - success + tokenCreated, + tokenSent } interface Props { @@ -64,7 +64,6 @@ async function computeExpirationDate() { } export function SonarLintConnection({ currentUser }: Props) { - const navigate = useNavigate(); const [searchParams] = useSearchParams(); const [status, setStatus] = React.useState(Status.request); const [newToken, setNewToken] = React.useState(undefined); @@ -72,13 +71,6 @@ export function SonarLintConnection({ currentUser }: Props) { const port = parseInt(searchParams.get('port') ?? '0', 10); const ideName = searchParams.get('ideName') ?? translate('sonarlint-connection.unspecified-ide'); - // If the port is not in the expected range, redirect to home page - React.useEffect(() => { - if (!portIsValid(port)) { - navigate('/'); - } - }, [navigate, port]); - const { login } = currentUser; const authorize = React.useCallback(async () => { @@ -95,11 +87,16 @@ export function SonarLintConnection({ currentUser }: Props) { setNewToken(token); + if (!portIsValid(port)) { + setStatus(Status.tokenCreated); + return; + } + try { await sendUserToken(port, token); - setStatus(Status.success); + setStatus(Status.tokenSent); } catch (_) { - setStatus(Status.connectionError); + setStatus(Status.tokenCreated); } }, [port, ideName, login]); @@ -162,7 +159,7 @@ export function SonarLintConnection({ currentUser }: Props) { )} - {status === Status.connectionError && newToken && ( + {status === Status.tokenCreated && newToken && ( <> )} - {status === Status.success && newToken && ( + {status === Status.tokenSent && newToken && ( <>

{translate('sonarlint-connection.success.title')} diff --git a/server/sonar-web/src/main/js/app/components/__tests__/SonarLintConnection-test.tsx b/server/sonar-web/src/main/js/app/components/__tests__/SonarLintConnection-test.tsx index 2137385bb1b..4c1daef425d 100644 --- a/server/sonar-web/src/main/js/app/components/__tests__/SonarLintConnection-test.tsx +++ b/server/sonar-web/src/main/js/app/components/__tests__/SonarLintConnection-test.tsx @@ -123,12 +123,18 @@ it('should require authentication if user is not logged in', () => { expect(handleRequiredAuthentication).toHaveBeenCalled(); }); -it('should redirect if port is not provided', () => { +it('should let the user copy the token if the port is not valid', async () => { + const user = userEvent.setup(); + renderSonarLintConnection({ port: '' }); + await user.click( + await screen.findByRole('button', { name: 'sonarlint-connection.request.action' }) + ); + expect( - screen.queryByRole('heading', { name: 'sonarlint-connection.title' }) - ).not.toBeInTheDocument(); + await screen.findByText('sonarlint-connection.connection-error.description') + ).toBeInTheDocument(); }); function renderSonarLintConnection(overrides: { currentUser?: CurrentUser; port?: string } = {}) {