* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { get, getJSON, parseError, post, postJSON } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { get, parseError, post, postJSON } from '../helpers/request';
import {
AzureProject,
AzureRepository,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { get, getJSON, HttpStatus, parseError, parseJSON, post } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { get, HttpStatus, parseError, parseJSON, post } from '../helpers/request';
import {
AlmSettingsBindingDefinitions,
AlmSettingsInstance,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON, post, postJSON } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post, postJSON } from '../helpers/request';
import { Application, ApplicationPeriod } from '../types/application';
import { Visibility } from '../types/component';
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON, post } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post } from '../helpers/request';
import { Branch, PullRequest } from '../types/branch-like';
export function getBranches(project: string): Promise<Branch[]> {
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON, post } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post } from '../helpers/request';
import { IndexationStatus } from '../types/indexation';
import { ActivityRequestParameters, Task, TaskWarning } from '../types/tasks';
import { Paging } from '../types/types';
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON, post } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post } from '../helpers/request';
import { getBaseUrl } from '../helpers/system';
import { ComponentReportStatus } from '../types/component-report';
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON, post, RequestData } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post, RequestData } from '../helpers/request';
import { BranchParameters } from '../types/branch-like';
import {
ComponentQualifier,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
import { License } from '../types/editions';
export function isValidLicense(): Promise<{ isValidLicense: boolean }> {
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
-import { getJSON, post } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post } from '../helpers/request';
export function getFavorites(): Promise<any> {
return getJSON('/api/favorites/search');
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
-import { getJSON } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
import { Feature } from '../types/features';
export function getAvailableFeatures(): Promise<Feature[]> {
*/
import axios from 'axios';
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON, post, postJSON } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post, postJSON } from '../helpers/request';
import { GitHubConfigurationStatus, GitHubMapping, GithubStatus } from '../types/provisioning';
const GITHUB_PERMISSION_MAPPINGS = '/api/v2/dop-translation/github-permission-mappings';
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
+import { getJSON } from '~sonar-aligned/helpers/request';
import getCoverageStatus from '../components/SourceViewer/helpers/getCoverageStatus';
-import {
- get,
- getJSON,
- HttpStatus,
- parseJSON,
- post,
- postJSON,
- RequestData,
-} from '../helpers/request';
+import { get, HttpStatus, parseJSON, post, postJSON, RequestData } from '../helpers/request';
import { FacetName, IssueResponse, ListIssuesResponse, RawIssuesResponse } from '../types/issues';
import { Dict, FacetValue, IssueChangelog, SnippetsByComponent, SourceLine } from '../types/types';
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
-import { getJSON } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
import { L10nBundleRequestParams, L10nBundleRequestResponse } from '../types/l10nBundle';
export function fetchL10nBundle(
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
import { Language } from '../types/languages';
export function getLanguages(): Promise<Language[]> {
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
import { BranchParameters } from '../types/branch-like';
import {
MeasuresAndMetaWithMetrics,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON, post } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post } from '../helpers/request';
export enum MessageTypes {
GlobalNcd90 = 'GLOBAL_NCD_90',
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
import { Metric } from '../types/types';
export interface MetricsResponse {
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
import { AppState } from '../types/appstate';
import { BranchParameters } from '../types/branch-like';
import { Extension, NavigationComponent } from '../types/types';
}
export function getGlobalNavigation(): Promise<AppState> {
- return getJSON('/api/navigation/global', undefined, true);
+ return getJSON('/api/navigation/global', undefined, { bypassRedirect: true });
}
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON, post } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post } from '../helpers/request';
import {
NewCodeDefinition,
NewCodeDefinitionBranch,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON, post } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post } from '../helpers/request';
import { AddRemoveNotificationParameters, NotificationsResponse } from '../types/notifications';
export function getNotifications(): Promise<NotificationsResponse> {
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON, post, postJSON, RequestData } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post, postJSON, RequestData } from '../helpers/request';
import { Visibility } from '../types/component';
import {
Paging,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON, post } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post } from '../helpers/request';
import {
AvailablePlugin,
InstalledPlugin,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON, post } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post } from '../helpers/request';
export function getProjectBadgesToken(project: string) {
return getJSON('/api/project_badges/token', { project })
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON, post } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post } from '../helpers/request';
import { DumpStatus } from '../types/project-dump';
export function getStatus(componentKey: string): Promise<DumpStatus> {
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON, post, postJSON } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post, postJSON } from '../helpers/request';
import { ComponentQualifier, Visibility } from '../types/component';
import { Paging } from '../types/types';
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON, post, postJSON } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post, postJSON } from '../helpers/request';
import { BranchParameters } from '../types/branch-like';
import {
Analysis,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON, post, postJSON } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post, postJSON } from '../helpers/request';
import { ProjectLink } from '../types/types';
export function getProjectLinks(projectKey: string): Promise<ProjectLink[]> {
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON, post, postJSON } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post, postJSON } from '../helpers/request';
import { BranchParameters } from '../types/branch-like';
import {
AddDeleteGroupPermissionsParameters,
*/
import { map } from 'lodash';
import { throwGlobalError } from '~sonar-aligned/helpers/error';
+import { getJSON } from '~sonar-aligned/helpers/request';
import { Exporter, ProfileChangelogEvent } from '../apps/quality-profiles/types';
import { csvEscape } from '../helpers/csv';
-import { RequestData, getJSON, post, postJSON } from '../helpers/request';
+import { RequestData, post, postJSON } from '../helpers/request';
import { CleanCodeAttributeCategory, SoftwareImpact } from '../types/clean-code-taxonomy';
import { Dict, Paging, ProfileInheritanceDetails, UserSelected } from '../types/types';
*/
import { HttpStatusCode } from 'axios';
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { axiosToCatch, getJSON, post, postJSON } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { axiosToCatch, post, postJSON } from '../helpers/request';
import { CleanCodeAttribute, SoftwareImpact } from '../types/clean-code-taxonomy';
import { GetRulesAppResponse, SearchRulesResponse } from '../types/coding-rules';
import { SearchRulesQuery } from '../types/rules';
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON, post } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post } from '../helpers/request';
export function fetchIsScimEnabled(): Promise<boolean> {
return getJSON('/api/scim_management/status')
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON, post } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post } from '../helpers/request';
import { BranchParameters } from '../types/branch-like';
import {
Hotspot,
*/
import { omitBy } from 'lodash';
import { throwGlobalError } from '~sonar-aligned/helpers/error';
+import { getJSON } from '~sonar-aligned/helpers/request';
import { isCategoryDefinition } from '../apps/settings/utils';
-import { getJSON, post, postJSON, RequestData } from '../helpers/request';
+import { post, postJSON, RequestData } from '../helpers/request';
import { BranchParameters } from '../types/branch-like';
import {
ExtendedSettingDefinition,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON, post, postJSON, requestTryAndRepeatUntil } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post, postJSON, requestTryAndRepeatUntil } from '../helpers/request';
import { SystemUpgrade } from '../types/system';
import { SysInfoCluster, SysInfoStandalone, SysStatus } from '../types/types';
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
import { BranchParameters } from '../types/branch-like';
import { MetricKey } from '../types/metrics';
import { Paging } from '../types/types';
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON, post, postJSON } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post, postJSON } from '../helpers/request';
import { NewUserToken, UserToken } from '../types/token';
/** List tokens for given user login */
*/
import axios from 'axios';
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { HttpStatus, axiosToCatch, getJSON, parseJSON, post } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { HttpStatus, axiosToCatch, parseJSON, post } from '../helpers/request';
import { IdentityProvider, Paging } from '../types/types';
import {
ChangePasswordResults,
const USERS_ENDPOINT = '/api/v2/users-management/users';
export function getCurrentUser(): Promise<CurrentUser> {
- return getJSON('/api/users/current', undefined, true);
+ return getJSON('/api/users/current', undefined, { bypassRedirect: true });
}
export function dismissNotice(notice: NoticeType) {
*/
import { OpenAPIV3 } from 'openapi-types';
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
import { WebApi } from '../types/types';
interface RawDomain {
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import { throwGlobalError } from '~sonar-aligned/helpers/error';
-import { getJSON, post, postJSON } from '../helpers/request';
+import { getJSON } from '~sonar-aligned/helpers/request';
+import { post, postJSON } from '../helpers/request';
import { Paging } from '../types/types';
import {
WebhookCreatePayload,
import { addGlobalSuccessMessage } from 'design-system';
import { throwGlobalError } from '~sonar-aligned/helpers/error';
+import { getJSON } from '~sonar-aligned/helpers/request';
import { translate, translateWithParameters } from '../../../helpers/l10n';
import {
get,
- getJSON,
getText,
omitNil,
parseError,
import handleRequiredAuthentication from '../handleRequiredAuthentication';
import {
checkStatus,
- getJSON,
getText,
HttpStatus,
isSuccessStatus,
window.fetch = jest.fn().mockResolvedValue(mockResponse({}, HttpStatus.Ok, {}));
});
-describe('getJSON', () => {
- it('should get json without parameters', async () => {
- const response = mockResponse({}, HttpStatus.Ok, {});
- window.fetch = jest.fn().mockResolvedValue(response);
- getJSON(url);
- await new Promise(setImmediate);
-
- expect(window.fetch).toHaveBeenCalledWith(url, expect.objectContaining({ method: 'GET' }));
- expect(response.json).toHaveBeenCalled();
- });
-
- it('should get json with parameters', () => {
- getJSON(url, { data: 'test' });
- expect(window.fetch).toHaveBeenCalledWith(
- url + '?data=test',
- expect.objectContaining({ method: 'GET' }),
- );
- });
-});
-
describe('getText', () => {
it('should get text without parameters', async () => {
const response = mockResponse({}, HttpStatus.Ok, '');
.then((response) => checkStatus(response, bypassRedirect));
}
-/**
- * Shortcut to do a GET request and return response json
- */
-export function getJSON(url: string, data?: RequestData, bypassRedirect = false): Promise<any> {
- return get(url, data, bypassRedirect).then(parseJSON);
-}
-
/**
* Shortcut to do a GET request and return response text
*/
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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 { setImmediate } from 'timers';
+import { HttpStatus } from '../../../helpers/request';
+import { Dict } from '../../../types/types';
+import { getJSON } from '../request';
+
+const url = '/my-url';
+
+beforeEach(() => {
+ jest.clearAllMocks();
+ window.fetch = jest.fn().mockResolvedValue(mockResponse({}, HttpStatus.Ok, {}));
+});
+
+describe('getJSON', () => {
+ it('should get json without parameters', async () => {
+ const response = mockResponse({}, HttpStatus.Ok, {});
+ window.fetch = jest.fn().mockResolvedValue(response);
+ getJSON(url);
+ await new Promise(setImmediate);
+
+ expect(window.fetch).toHaveBeenCalledWith(url, expect.objectContaining({ method: 'GET' }));
+ expect(response.json).toHaveBeenCalled();
+ });
+
+ it('should get json with parameters', () => {
+ getJSON(url, { data: 'test' });
+ expect(window.fetch).toHaveBeenCalledWith(
+ url + '?data=test',
+ expect.objectContaining({ method: 'GET' }),
+ );
+ });
+});
+
+function mockResponse(headers: Dict<string> = {}, status = HttpStatus.Ok, value?: any): Response {
+ const body = value && value instanceof Object ? JSON.stringify(value) : value;
+ const response = new Response(body, { headers, status });
+ response.json = jest.fn().mockResolvedValue(value);
+ response.text = jest.fn().mockResolvedValue(value);
+ return response;
+}
--- /dev/null
+/*
+ * SonarQube
+ * Copyright (C) 2009-2024 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 { RequestData, get, parseJSON } from '../../helpers/request';
+
+interface CustomHeader {
+ accept?: string;
+ apiVersion?: string;
+ isJSON?: boolean;
+}
+
+interface RequestOptions {
+ bypassRedirect?: boolean;
+ customHeaders?: CustomHeader; // used only in SonarCloud
+ isExternal?: boolean; // used only in SonarCloud
+ useQueryParams?: boolean; // used only in SonarCloud
+}
+
+/**
+ * Shortcut to do a GET request and return response json
+ */
+export function getJSON(
+ url: string,
+ data?: RequestData,
+ options: RequestOptions = {},
+): Promise<any> {
+ return get(url, data, options.bypassRedirect).then(parseJSON);
+}