diff options
author | Philippe Perrin <philippe.perrin@sonarsource.com> | 2022-01-14 10:07:56 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2022-01-17 20:02:50 +0000 |
commit | 45032fa196523f2df243a21682abd788e64a1062 (patch) | |
tree | 1cb875a7bb260306f022d9eccb7efb538e203d49 /server/sonar-web/src/main/js | |
parent | 1938bdcb2573c9ee9cbeade90d2e5134acd02e7f (diff) | |
download | sonarqube-45032fa196523f2df243a21682abd788e64a1062.tar.gz sonarqube-45032fa196523f2df243a21682abd788e64a1062.zip |
Remove usages of the deprecated package querystring
Diffstat (limited to 'server/sonar-web/src/main/js')
6 files changed, 166 insertions, 15 deletions
diff --git a/server/sonar-web/src/main/js/apps/projectDump/components/Import.tsx b/server/sonar-web/src/main/js/apps/projectDump/components/Import.tsx index 87acd4efea4..9c4160f8cef 100644 --- a/server/sonar-web/src/main/js/apps/projectDump/components/Import.tsx +++ b/server/sonar-web/src/main/js/apps/projectDump/components/Import.tsx @@ -18,15 +18,15 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import classNames from 'classnames'; -import { stringify } from 'querystring'; import * as React from 'react'; +import { Link } from 'react-router'; import { doImport } from '../../../api/project-dump'; import { Button } from '../../../components/controls/buttons'; import DateFromNow from '../../../components/intl/DateFromNow'; import DateTimeFormatter from '../../../components/intl/DateTimeFormatter'; import { Alert } from '../../../components/ui/Alert'; import { translate, translateWithParameters } from '../../../helpers/l10n'; -import { getBaseUrl } from '../../../helpers/system'; +import { getComponentBackgroundTaskUrl } from '../../../helpers/urls'; import { DumpStatus, DumpTask } from '../../../types/project-dump'; import { TaskStatuses, TaskTypes } from '../../../types/tasks'; @@ -119,19 +119,19 @@ export default class Import extends React.Component<Props> { renderWhenImportFailed() { const { componentKey } = this.props; - const detailsUrl = `${getBaseUrl()}/project/background_tasks?${stringify({ - id: encodeURIComponent(componentKey), - status: TaskStatuses.Failed, - taskType: TaskTypes.ProjectImport - })}`; + const detailsUrl = getComponentBackgroundTaskUrl( + componentKey, + TaskStatuses.Failed, + TaskTypes.ProjectImport + ); return ( <div className="boxed-group-inner"> <Alert id="export-in-progress" variant="error"> {translate('project_dump.failed_import')} - <a className="spacer-left" href={detailsUrl}> + <Link className="spacer-left" to={detailsUrl}> {translate('project_dump.see_details')} - </a> + </Link> </Alert> {this.renderImportForm()} diff --git a/server/sonar-web/src/main/js/apps/projectDump/components/__tests__/__snapshots__/Import-test.tsx.snap b/server/sonar-web/src/main/js/apps/projectDump/components/__tests__/__snapshots__/Import-test.tsx.snap index c5cbbc7c15b..6073edbe109 100644 --- a/server/sonar-web/src/main/js/apps/projectDump/components/__tests__/__snapshots__/Import-test.tsx.snap +++ b/server/sonar-web/src/main/js/apps/projectDump/components/__tests__/__snapshots__/Import-test.tsx.snap @@ -41,12 +41,23 @@ exports[`should render correctly: failed 1`] = ` variant="error" > project_dump.failed_import - <a + <Link className="spacer-left" - href="/project/background_tasks?id=key&status=FAILED&taskType=PROJECT_IMPORT" + onlyActiveOnIndex={false} + style={Object {}} + to={ + Object { + "pathname": "/project/background_tasks", + "query": Object { + "id": "key", + "status": "FAILED", + "taskType": "PROJECT_IMPORT", + }, + } + } > project_dump.see_details - </a> + </Link> </Alert> <div> <div diff --git a/server/sonar-web/src/main/js/helpers/__tests__/stringify-queryparams-test.ts b/server/sonar-web/src/main/js/helpers/__tests__/stringify-queryparams-test.ts new file mode 100644 index 00000000000..29b7b43fde0 --- /dev/null +++ b/server/sonar-web/src/main/js/helpers/__tests__/stringify-queryparams-test.ts @@ -0,0 +1,46 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +import { stringify } from '../stringify-queryparams'; + +describe('stringify', () => { + it('should properly format query params object', () => { + const obj = { + prop1: 'a string', + prop2: 123, + prop3: true, + prop4: '', + prop5: [9, 8, 7], + prop6: { test: 'test' } + }; + + expect(stringify(obj)).toEqual( + 'prop1=a%20string&prop2=123&prop3=true&prop4=&prop5=9&prop5=8&prop5=7&prop6=' + ); + }); + + it('should return empty if name is not defined', () => { + expect(stringify('test_obj', undefined, undefined, undefined)).toEqual(''); + }); + + it('should properly format a query param', () => { + expect(stringify('test_obj', undefined, undefined, 'test_name')).toEqual('test_name=test_obj'); + }); +}); diff --git a/server/sonar-web/src/main/js/helpers/request.ts b/server/sonar-web/src/main/js/helpers/request.ts index 686ea3a218c..029ac9c37c4 100644 --- a/server/sonar-web/src/main/js/helpers/request.ts +++ b/server/sonar-web/src/main/js/helpers/request.ts @@ -18,9 +18,9 @@ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ import { isNil, omitBy } from 'lodash'; -import { stringify } from 'querystring'; import { getCookie } from './cookies'; import { translate } from './l10n'; +import { stringify } from './stringify-queryparams'; import { getBaseUrl } from './system'; export function getCSRFTokenName(): string { diff --git a/server/sonar-web/src/main/js/helpers/stringify-queryparams.ts b/server/sonar-web/src/main/js/helpers/stringify-queryparams.ts new file mode 100644 index 00000000000..78273dae4e9 --- /dev/null +++ b/server/sonar-web/src/main/js/helpers/stringify-queryparams.ts @@ -0,0 +1,90 @@ +/* + * SonarQube + * Copyright (C) 2009-2021 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ + +// Copyright Joyent, Inc. and other Node contributors. +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to permit +// persons to whom the Software is furnished to do so, subject to the +// following conditions: +// +// The above copyright notice and this permission notice shall be included +// in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS +// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN +// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, +// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR +// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE +// USE OR OTHER DEALINGS IN THE SOFTWARE. + +function stringifyPrimitive(v: any) { + switch (typeof v) { + case 'string': + return v; + + case 'boolean': + return v ? 'true' : 'false'; + + case 'number': + return isFinite(v) ? v : ''; + + default: + return ''; + } +} + +export function stringify(obj: any, sep?: any, eq?: any, name?: any) { + sep = sep || '&'; + eq = eq || '='; + if (obj === null) { + obj = undefined; + } + + if (typeof obj === 'object') { + return Object.keys(obj) + .map(k => { + const ks = encodeURIComponent(stringifyPrimitive(k)) + eq; + if (Array.isArray(obj[k])) { + return obj[k] + .map((v: any) => { + return ks + encodeURIComponent(stringifyPrimitive(v)); + }) + .join(sep); + } + + return ks + encodeURIComponent(stringifyPrimitive(obj[k])); + }) + .filter(Boolean) + .join(sep); + } + + if (!name) { + return ''; + } + + return ( + encodeURIComponent(stringifyPrimitive(name)) + eq + encodeURIComponent(stringifyPrimitive(obj)) + ); +} diff --git a/server/sonar-web/src/main/js/helpers/urls.ts b/server/sonar-web/src/main/js/helpers/urls.ts index 9a21a7ce814..1d3b88659b1 100644 --- a/server/sonar-web/src/main/js/helpers/urls.ts +++ b/server/sonar-web/src/main/js/helpers/urls.ts @@ -81,8 +81,12 @@ export function getApplicationAdminUrl(key: string) { return { pathname: '/application/console', query: { id: key } }; } -export function getComponentBackgroundTaskUrl(componentKey: string, status?: string): Location { - return { pathname: '/project/background_tasks', query: { id: componentKey, status } }; +export function getComponentBackgroundTaskUrl( + componentKey: string, + status?: string, + taskType?: string +): Location { + return { pathname: '/project/background_tasks', query: { id: componentKey, status, taskType } }; } export function getBranchLikeUrl(project: string, branchLike?: BranchLike): Location { |