diff options
author | Jeremy Davis <jeremy.davis@sonarsource.com> | 2022-10-31 14:14:26 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-11-01 20:03:09 +0000 |
commit | e028637e1ab4ac6715eb56a6898c1c9758e5ec87 (patch) | |
tree | 911a56cfa3a1292414414320eb4caf22b4748665 /server/sonar-web/src | |
parent | 493b522f0ac52122ace3fa12fa5d53e823b3cb20 (diff) | |
download | sonarqube-e028637e1ab4ac6715eb56a6898c1c9758e5ec87.tar.gz sonarqube-e028637e1ab4ac6715eb56a6898c1c9758e5ec87.zip |
SONAR-17465 Handle missing port number for SonarLint Connection
Diffstat (limited to 'server/sonar-web/src')
-rw-r--r-- | server/sonar-web/src/main/js/app/components/SonarLintConnection.tsx | 27 | ||||
-rw-r--r-- | server/sonar-web/src/main/js/app/components/__tests__/SonarLintConnection-test.tsx | 12 |
2 files changed, 21 insertions, 18 deletions
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<NewUserToken | undefined>(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 && ( <> <img alt="" @@ -200,7 +197,7 @@ export function SonarLintConnection({ currentUser }: Props) { </> )} - {status === Status.success && newToken && ( + {status === Status.tokenSent && newToken && ( <> <h1 className="big-spacer-top big-spacer-bottom"> {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 } = {}) { |