From da21bb7e20058ad4c2dfabbb9ed84230e0998cd7 Mon Sep 17 00:00:00 2001 From: Viktor Vorona Date: Thu, 25 Apr 2024 10:27:05 +0200 Subject: [PATCH] SONAR-22049 Align getJSON --- .../src/main/js/api/alm-integrations.ts | 3 +- .../sonar-web/src/main/js/api/alm-settings.ts | 3 +- .../sonar-web/src/main/js/api/application.ts | 3 +- server/sonar-web/src/main/js/api/branches.ts | 3 +- server/sonar-web/src/main/js/api/ce.ts | 3 +- .../src/main/js/api/component-report.ts | 3 +- .../sonar-web/src/main/js/api/components.ts | 3 +- server/sonar-web/src/main/js/api/editions.ts | 2 +- server/sonar-web/src/main/js/api/favorites.ts | 3 +- server/sonar-web/src/main/js/api/features.ts | 2 +- .../src/main/js/api/github-provisioning.ts | 3 +- server/sonar-web/src/main/js/api/issues.ts | 11 +--- server/sonar-web/src/main/js/api/l10n.ts | 2 +- server/sonar-web/src/main/js/api/languages.ts | 2 +- server/sonar-web/src/main/js/api/measures.ts | 2 +- server/sonar-web/src/main/js/api/messages.ts | 3 +- server/sonar-web/src/main/js/api/metrics.ts | 2 +- .../sonar-web/src/main/js/api/navigation.ts | 4 +- .../src/main/js/api/newCodeDefinition.ts | 3 +- .../src/main/js/api/notifications.ts | 3 +- .../sonar-web/src/main/js/api/permissions.ts | 3 +- server/sonar-web/src/main/js/api/plugins.ts | 3 +- .../src/main/js/api/project-badges.ts | 3 +- .../sonar-web/src/main/js/api/project-dump.ts | 3 +- .../src/main/js/api/project-management.ts | 3 +- .../src/main/js/api/projectActivity.ts | 3 +- .../sonar-web/src/main/js/api/projectLinks.ts | 3 +- .../src/main/js/api/quality-gates.ts | 3 +- .../src/main/js/api/quality-profiles.ts | 3 +- server/sonar-web/src/main/js/api/rules.ts | 3 +- .../src/main/js/api/scim-provisioning.ts | 3 +- .../src/main/js/api/security-hotspots.ts | 3 +- server/sonar-web/src/main/js/api/settings.ts | 3 +- server/sonar-web/src/main/js/api/system.ts | 3 +- .../sonar-web/src/main/js/api/time-machine.ts | 2 +- .../sonar-web/src/main/js/api/user-tokens.ts | 3 +- server/sonar-web/src/main/js/api/users.ts | 5 +- server/sonar-web/src/main/js/api/web-api.ts | 2 +- server/sonar-web/src/main/js/api/webhooks.ts | 3 +- .../components/extensions/exposeLibraries.ts | 2 +- .../main/js/helpers/__tests__/request-test.ts | 21 ------- .../sonar-web/src/main/js/helpers/request.ts | 7 --- .../helpers/__tests__/request-test.ts | 58 +++++++++++++++++++ .../main/js/sonar-aligned/helpers/request.ts | 44 ++++++++++++++ 44 files changed, 174 insertions(+), 78 deletions(-) create mode 100644 server/sonar-web/src/main/js/sonar-aligned/helpers/__tests__/request-test.ts create mode 100644 server/sonar-web/src/main/js/sonar-aligned/helpers/request.ts diff --git a/server/sonar-web/src/main/js/api/alm-integrations.ts b/server/sonar-web/src/main/js/api/alm-integrations.ts index ce7986c0c9c..d949954dbb7 100644 --- a/server/sonar-web/src/main/js/api/alm-integrations.ts +++ b/server/sonar-web/src/main/js/api/alm-integrations.ts @@ -18,7 +18,8 @@ * 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, diff --git a/server/sonar-web/src/main/js/api/alm-settings.ts b/server/sonar-web/src/main/js/api/alm-settings.ts index cd7a986bdb6..08f5b1b17a5 100644 --- a/server/sonar-web/src/main/js/api/alm-settings.ts +++ b/server/sonar-web/src/main/js/api/alm-settings.ts @@ -18,7 +18,8 @@ * 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, diff --git a/server/sonar-web/src/main/js/api/application.ts b/server/sonar-web/src/main/js/api/application.ts index 6c51411b58e..679961cfda1 100644 --- a/server/sonar-web/src/main/js/api/application.ts +++ b/server/sonar-web/src/main/js/api/application.ts @@ -18,7 +18,8 @@ * 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'; diff --git a/server/sonar-web/src/main/js/api/branches.ts b/server/sonar-web/src/main/js/api/branches.ts index e0a9fb6f8ed..213091b3eb5 100644 --- a/server/sonar-web/src/main/js/api/branches.ts +++ b/server/sonar-web/src/main/js/api/branches.ts @@ -18,7 +18,8 @@ * 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 { diff --git a/server/sonar-web/src/main/js/api/ce.ts b/server/sonar-web/src/main/js/api/ce.ts index 62b36056571..7acd1af27c4 100644 --- a/server/sonar-web/src/main/js/api/ce.ts +++ b/server/sonar-web/src/main/js/api/ce.ts @@ -18,7 +18,8 @@ * 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'; diff --git a/server/sonar-web/src/main/js/api/component-report.ts b/server/sonar-web/src/main/js/api/component-report.ts index 20fd7bf0432..0f3cd52dc1a 100644 --- a/server/sonar-web/src/main/js/api/component-report.ts +++ b/server/sonar-web/src/main/js/api/component-report.ts @@ -18,7 +18,8 @@ * 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'; diff --git a/server/sonar-web/src/main/js/api/components.ts b/server/sonar-web/src/main/js/api/components.ts index 29b5a4c4e1b..375e6a8ea19 100644 --- a/server/sonar-web/src/main/js/api/components.ts +++ b/server/sonar-web/src/main/js/api/components.ts @@ -18,7 +18,8 @@ * 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, diff --git a/server/sonar-web/src/main/js/api/editions.ts b/server/sonar-web/src/main/js/api/editions.ts index 83a16612b68..5b8ba747a45 100644 --- a/server/sonar-web/src/main/js/api/editions.ts +++ b/server/sonar-web/src/main/js/api/editions.ts @@ -18,7 +18,7 @@ * 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 }> { diff --git a/server/sonar-web/src/main/js/api/favorites.ts b/server/sonar-web/src/main/js/api/favorites.ts index 2f8be5fcb45..5b59a269231 100644 --- a/server/sonar-web/src/main/js/api/favorites.ts +++ b/server/sonar-web/src/main/js/api/favorites.ts @@ -17,7 +17,8 @@ * 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 { return getJSON('/api/favorites/search'); diff --git a/server/sonar-web/src/main/js/api/features.ts b/server/sonar-web/src/main/js/api/features.ts index 1e4857006e0..a5856b65c29 100644 --- a/server/sonar-web/src/main/js/api/features.ts +++ b/server/sonar-web/src/main/js/api/features.ts @@ -17,7 +17,7 @@ * 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 { diff --git a/server/sonar-web/src/main/js/api/github-provisioning.ts b/server/sonar-web/src/main/js/api/github-provisioning.ts index 713f9b123a8..8ba2bba4961 100644 --- a/server/sonar-web/src/main/js/api/github-provisioning.ts +++ b/server/sonar-web/src/main/js/api/github-provisioning.ts @@ -19,7 +19,8 @@ */ 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'; diff --git a/server/sonar-web/src/main/js/api/issues.ts b/server/sonar-web/src/main/js/api/issues.ts index 6f490cc80e7..6b87eaea6e4 100644 --- a/server/sonar-web/src/main/js/api/issues.ts +++ b/server/sonar-web/src/main/js/api/issues.ts @@ -18,16 +18,9 @@ * 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'; diff --git a/server/sonar-web/src/main/js/api/l10n.ts b/server/sonar-web/src/main/js/api/l10n.ts index c1ac5eaa760..604d7bccd91 100644 --- a/server/sonar-web/src/main/js/api/l10n.ts +++ b/server/sonar-web/src/main/js/api/l10n.ts @@ -17,7 +17,7 @@ * 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( diff --git a/server/sonar-web/src/main/js/api/languages.ts b/server/sonar-web/src/main/js/api/languages.ts index f76605f2ea8..6c23e1e73e9 100644 --- a/server/sonar-web/src/main/js/api/languages.ts +++ b/server/sonar-web/src/main/js/api/languages.ts @@ -18,7 +18,7 @@ * 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 { diff --git a/server/sonar-web/src/main/js/api/measures.ts b/server/sonar-web/src/main/js/api/measures.ts index b30bce138bb..093b0df065d 100644 --- a/server/sonar-web/src/main/js/api/measures.ts +++ b/server/sonar-web/src/main/js/api/measures.ts @@ -18,7 +18,7 @@ * 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, diff --git a/server/sonar-web/src/main/js/api/messages.ts b/server/sonar-web/src/main/js/api/messages.ts index 3184cdb4c75..a5081180e20 100644 --- a/server/sonar-web/src/main/js/api/messages.ts +++ b/server/sonar-web/src/main/js/api/messages.ts @@ -18,7 +18,8 @@ * 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', diff --git a/server/sonar-web/src/main/js/api/metrics.ts b/server/sonar-web/src/main/js/api/metrics.ts index 894ef7a4a65..f6b35e44858 100644 --- a/server/sonar-web/src/main/js/api/metrics.ts +++ b/server/sonar-web/src/main/js/api/metrics.ts @@ -18,7 +18,7 @@ * 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 { diff --git a/server/sonar-web/src/main/js/api/navigation.ts b/server/sonar-web/src/main/js/api/navigation.ts index 05099d42fce..da908049ae6 100644 --- a/server/sonar-web/src/main/js/api/navigation.ts +++ b/server/sonar-web/src/main/js/api/navigation.ts @@ -18,7 +18,7 @@ * 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'; @@ -41,5 +41,5 @@ export function getSettingsNavigation(): Promise<{ } export function getGlobalNavigation(): Promise { - return getJSON('/api/navigation/global', undefined, true); + return getJSON('/api/navigation/global', undefined, { bypassRedirect: true }); } diff --git a/server/sonar-web/src/main/js/api/newCodeDefinition.ts b/server/sonar-web/src/main/js/api/newCodeDefinition.ts index 57a553eb71f..63936d5bd15 100644 --- a/server/sonar-web/src/main/js/api/newCodeDefinition.ts +++ b/server/sonar-web/src/main/js/api/newCodeDefinition.ts @@ -18,7 +18,8 @@ * 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, diff --git a/server/sonar-web/src/main/js/api/notifications.ts b/server/sonar-web/src/main/js/api/notifications.ts index aab9b5df561..f146613a72b 100644 --- a/server/sonar-web/src/main/js/api/notifications.ts +++ b/server/sonar-web/src/main/js/api/notifications.ts @@ -18,7 +18,8 @@ * 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 { diff --git a/server/sonar-web/src/main/js/api/permissions.ts b/server/sonar-web/src/main/js/api/permissions.ts index 06c554e293f..e13899e8a1f 100644 --- a/server/sonar-web/src/main/js/api/permissions.ts +++ b/server/sonar-web/src/main/js/api/permissions.ts @@ -18,7 +18,8 @@ * 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, diff --git a/server/sonar-web/src/main/js/api/plugins.ts b/server/sonar-web/src/main/js/api/plugins.ts index db78fea70d7..9850b5d8775 100644 --- a/server/sonar-web/src/main/js/api/plugins.ts +++ b/server/sonar-web/src/main/js/api/plugins.ts @@ -18,7 +18,8 @@ * 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, diff --git a/server/sonar-web/src/main/js/api/project-badges.ts b/server/sonar-web/src/main/js/api/project-badges.ts index d73ff348980..84b1ea6132b 100644 --- a/server/sonar-web/src/main/js/api/project-badges.ts +++ b/server/sonar-web/src/main/js/api/project-badges.ts @@ -18,7 +18,8 @@ * 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 }) diff --git a/server/sonar-web/src/main/js/api/project-dump.ts b/server/sonar-web/src/main/js/api/project-dump.ts index e7ce3574b60..f76a9d5c503 100644 --- a/server/sonar-web/src/main/js/api/project-dump.ts +++ b/server/sonar-web/src/main/js/api/project-dump.ts @@ -18,7 +18,8 @@ * 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 { diff --git a/server/sonar-web/src/main/js/api/project-management.ts b/server/sonar-web/src/main/js/api/project-management.ts index 9d10c8834f0..7ac68b3f756 100644 --- a/server/sonar-web/src/main/js/api/project-management.ts +++ b/server/sonar-web/src/main/js/api/project-management.ts @@ -18,7 +18,8 @@ * 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'; diff --git a/server/sonar-web/src/main/js/api/projectActivity.ts b/server/sonar-web/src/main/js/api/projectActivity.ts index 52edbc66f0f..f6aa6224ad2 100644 --- a/server/sonar-web/src/main/js/api/projectActivity.ts +++ b/server/sonar-web/src/main/js/api/projectActivity.ts @@ -18,7 +18,8 @@ * 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, diff --git a/server/sonar-web/src/main/js/api/projectLinks.ts b/server/sonar-web/src/main/js/api/projectLinks.ts index fe8880f72d2..223d326811c 100644 --- a/server/sonar-web/src/main/js/api/projectLinks.ts +++ b/server/sonar-web/src/main/js/api/projectLinks.ts @@ -18,7 +18,8 @@ * 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 { diff --git a/server/sonar-web/src/main/js/api/quality-gates.ts b/server/sonar-web/src/main/js/api/quality-gates.ts index a0767938280..2c8f490222b 100644 --- a/server/sonar-web/src/main/js/api/quality-gates.ts +++ b/server/sonar-web/src/main/js/api/quality-gates.ts @@ -18,7 +18,8 @@ * 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, diff --git a/server/sonar-web/src/main/js/api/quality-profiles.ts b/server/sonar-web/src/main/js/api/quality-profiles.ts index 120dd6a33cd..58bfb8f6961 100644 --- a/server/sonar-web/src/main/js/api/quality-profiles.ts +++ b/server/sonar-web/src/main/js/api/quality-profiles.ts @@ -19,9 +19,10 @@ */ 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'; diff --git a/server/sonar-web/src/main/js/api/rules.ts b/server/sonar-web/src/main/js/api/rules.ts index ef3991bb259..7339a43998e 100644 --- a/server/sonar-web/src/main/js/api/rules.ts +++ b/server/sonar-web/src/main/js/api/rules.ts @@ -19,7 +19,8 @@ */ 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'; diff --git a/server/sonar-web/src/main/js/api/scim-provisioning.ts b/server/sonar-web/src/main/js/api/scim-provisioning.ts index b9678e9c6dc..6c002fe777c 100644 --- a/server/sonar-web/src/main/js/api/scim-provisioning.ts +++ b/server/sonar-web/src/main/js/api/scim-provisioning.ts @@ -18,7 +18,8 @@ * 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 { return getJSON('/api/scim_management/status') diff --git a/server/sonar-web/src/main/js/api/security-hotspots.ts b/server/sonar-web/src/main/js/api/security-hotspots.ts index 75a743ab118..866a0dcbd44 100644 --- a/server/sonar-web/src/main/js/api/security-hotspots.ts +++ b/server/sonar-web/src/main/js/api/security-hotspots.ts @@ -18,7 +18,8 @@ * 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, diff --git a/server/sonar-web/src/main/js/api/settings.ts b/server/sonar-web/src/main/js/api/settings.ts index 8ca01a42f9d..52b3a0b03ca 100644 --- a/server/sonar-web/src/main/js/api/settings.ts +++ b/server/sonar-web/src/main/js/api/settings.ts @@ -19,8 +19,9 @@ */ 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, diff --git a/server/sonar-web/src/main/js/api/system.ts b/server/sonar-web/src/main/js/api/system.ts index 7014f71feea..9bf9ca49e6f 100644 --- a/server/sonar-web/src/main/js/api/system.ts +++ b/server/sonar-web/src/main/js/api/system.ts @@ -18,7 +18,8 @@ * 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'; diff --git a/server/sonar-web/src/main/js/api/time-machine.ts b/server/sonar-web/src/main/js/api/time-machine.ts index 45f17998588..0dd1b2b6b1e 100644 --- a/server/sonar-web/src/main/js/api/time-machine.ts +++ b/server/sonar-web/src/main/js/api/time-machine.ts @@ -18,7 +18,7 @@ * 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'; diff --git a/server/sonar-web/src/main/js/api/user-tokens.ts b/server/sonar-web/src/main/js/api/user-tokens.ts index 35f8402155b..c315abd9bf4 100644 --- a/server/sonar-web/src/main/js/api/user-tokens.ts +++ b/server/sonar-web/src/main/js/api/user-tokens.ts @@ -18,7 +18,8 @@ * 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 */ diff --git a/server/sonar-web/src/main/js/api/users.ts b/server/sonar-web/src/main/js/api/users.ts index e4387df03af..597fa6da3b8 100644 --- a/server/sonar-web/src/main/js/api/users.ts +++ b/server/sonar-web/src/main/js/api/users.ts @@ -19,7 +19,8 @@ */ 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, @@ -33,7 +34,7 @@ import { const USERS_ENDPOINT = '/api/v2/users-management/users'; export function getCurrentUser(): Promise { - return getJSON('/api/users/current', undefined, true); + return getJSON('/api/users/current', undefined, { bypassRedirect: true }); } export function dismissNotice(notice: NoticeType) { diff --git a/server/sonar-web/src/main/js/api/web-api.ts b/server/sonar-web/src/main/js/api/web-api.ts index b7119a8fb20..778df47f539 100644 --- a/server/sonar-web/src/main/js/api/web-api.ts +++ b/server/sonar-web/src/main/js/api/web-api.ts @@ -19,7 +19,7 @@ */ 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 { diff --git a/server/sonar-web/src/main/js/api/webhooks.ts b/server/sonar-web/src/main/js/api/webhooks.ts index 9b59e82a542..70683aaabab 100644 --- a/server/sonar-web/src/main/js/api/webhooks.ts +++ b/server/sonar-web/src/main/js/api/webhooks.ts @@ -18,7 +18,8 @@ * 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, diff --git a/server/sonar-web/src/main/js/app/components/extensions/exposeLibraries.ts b/server/sonar-web/src/main/js/app/components/extensions/exposeLibraries.ts index c14038522de..8c1830be553 100644 --- a/server/sonar-web/src/main/js/app/components/extensions/exposeLibraries.ts +++ b/server/sonar-web/src/main/js/app/components/extensions/exposeLibraries.ts @@ -20,10 +20,10 @@ 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, diff --git a/server/sonar-web/src/main/js/helpers/__tests__/request-test.ts b/server/sonar-web/src/main/js/helpers/__tests__/request-test.ts index 81dfb5bfecb..916919a42ac 100644 --- a/server/sonar-web/src/main/js/helpers/__tests__/request-test.ts +++ b/server/sonar-web/src/main/js/helpers/__tests__/request-test.ts @@ -24,7 +24,6 @@ import { Dict } from '../../types/types'; import handleRequiredAuthentication from '../handleRequiredAuthentication'; import { checkStatus, - getJSON, getText, HttpStatus, isSuccessStatus, @@ -46,26 +45,6 @@ beforeEach(() => { 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, ''); diff --git a/server/sonar-web/src/main/js/helpers/request.ts b/server/sonar-web/src/main/js/helpers/request.ts index 693c1d9a12f..cd437aac6f3 100644 --- a/server/sonar-web/src/main/js/helpers/request.ts +++ b/server/sonar-web/src/main/js/helpers/request.ts @@ -221,13 +221,6 @@ export function get(url: string, data?: RequestData, bypassRedirect = false): Pr .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 { - return get(url, data, bypassRedirect).then(parseJSON); -} - /** * Shortcut to do a GET request and return response text */ diff --git a/server/sonar-web/src/main/js/sonar-aligned/helpers/__tests__/request-test.ts b/server/sonar-web/src/main/js/sonar-aligned/helpers/__tests__/request-test.ts new file mode 100644 index 00000000000..f065fcb0c24 --- /dev/null +++ b/server/sonar-web/src/main/js/sonar-aligned/helpers/__tests__/request-test.ts @@ -0,0 +1,58 @@ +/* + * 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 = {}, 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; +} diff --git a/server/sonar-web/src/main/js/sonar-aligned/helpers/request.ts b/server/sonar-web/src/main/js/sonar-aligned/helpers/request.ts new file mode 100644 index 00000000000..efbef3dac20 --- /dev/null +++ b/server/sonar-web/src/main/js/sonar-aligned/helpers/request.ts @@ -0,0 +1,44 @@ +/* + * 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 { + return get(url, data, options.bypassRedirect).then(parseJSON); +} -- 2.39.5