aboutsummaryrefslogtreecommitdiffstats
path: root/server
diff options
context:
space:
mode:
authorViktor Vorona <viktor.vorona@sonarsource.com>2024-07-24 12:11:41 +0200
committersonartech <sonartech@sonarsource.com>2024-07-25 20:02:50 +0000
commit89c2d1a6426234d4baa8b9a0e01e45342a3e35d8 (patch)
tree4c353c56ba18d5d20af241109559cb8b17219805 /server
parent76e28ab2760a480d9f092589eb0e7fb6bc450d89 (diff)
downloadsonarqube-89c2d1a6426234d4baa8b9a0e01e45342a3e35d8.tar.gz
sonarqube-89c2d1a6426234d4baa8b9a0e01e45342a3e35d8.zip
[NO JIRA] allow createQueryHook without params
Diffstat (limited to 'server')
-rw-r--r--server/sonar-web/src/main/js/components/tutorials/TutorialSelectionRenderer.tsx2
-rw-r--r--server/sonar-web/src/main/js/components/tutorials/jenkins/MultiBranchPipelineStep.tsx2
-rw-r--r--server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStep.tsx2
-rw-r--r--server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStepBitbucket.tsx2
-rw-r--r--server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStepGithub.tsx2
-rw-r--r--server/sonar-web/src/main/js/queries/common.ts97
-rw-r--r--server/sonar-web/src/main/js/queries/devops-integration.ts25
-rw-r--r--server/sonar-web/src/main/js/queries/system.ts15
8 files changed, 93 insertions, 54 deletions
diff --git a/server/sonar-web/src/main/js/components/tutorials/TutorialSelectionRenderer.tsx b/server/sonar-web/src/main/js/components/tutorials/TutorialSelectionRenderer.tsx
index 675966d6dc1..e5c692ba5cc 100644
--- a/server/sonar-web/src/main/js/components/tutorials/TutorialSelectionRenderer.tsx
+++ b/server/sonar-web/src/main/js/components/tutorials/TutorialSelectionRenderer.tsx
@@ -56,7 +56,7 @@ export interface TutorialSelectionRendererProps {
currentUser: LoggedInUser;
currentUserCanScanProject: boolean;
loading: boolean;
- projectBinding?: ProjectAlmBindingResponse;
+ projectBinding?: ProjectAlmBindingResponse | null;
selectedTutorial?: TutorialModes;
willRefreshAutomatically?: boolean;
}
diff --git a/server/sonar-web/src/main/js/components/tutorials/jenkins/MultiBranchPipelineStep.tsx b/server/sonar-web/src/main/js/components/tutorials/jenkins/MultiBranchPipelineStep.tsx
index 4e1b4ac0f02..9655bc5fc29 100644
--- a/server/sonar-web/src/main/js/components/tutorials/jenkins/MultiBranchPipelineStep.tsx
+++ b/server/sonar-web/src/main/js/components/tutorials/jenkins/MultiBranchPipelineStep.tsx
@@ -41,7 +41,7 @@ export interface MultiBranchPipelineStepProps {
alm: AlmKeys;
almBinding?: AlmSettingsInstance;
- projectBinding?: ProjectAlmBindingResponse;
+ projectBinding?: ProjectAlmBindingResponse | null;
}
/* Capture [workspaceID] from this pattern: https://bitbucket.org/[workspaceId]/ */
diff --git a/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStep.tsx b/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStep.tsx
index 44b74bd6623..504b7693884 100644
--- a/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStep.tsx
+++ b/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStep.tsx
@@ -35,7 +35,7 @@ export interface WebhookStepProps {
alm: AlmKeys;
almBinding?: AlmSettingsInstance;
branchesEnabled: boolean;
- projectBinding?: ProjectAlmBindingResponse;
+ projectBinding?: ProjectAlmBindingResponse | null;
}
function renderAlmSpecificInstructions(props: WebhookStepProps) {
diff --git a/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStepBitbucket.tsx b/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStepBitbucket.tsx
index cddf01e85d2..884e7fb04ba 100644
--- a/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStepBitbucket.tsx
+++ b/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStepBitbucket.tsx
@@ -36,7 +36,7 @@ export interface WebhookStepBitbucketProps {
alm: AlmKeys;
almBinding?: AlmSettingsInstance;
branchesEnabled: boolean;
- projectBinding?: ProjectAlmBindingResponse;
+ projectBinding?: ProjectAlmBindingResponse | null;
}
function buildUrlSnippet(
diff --git a/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStepGithub.tsx b/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStepGithub.tsx
index de5f5bace1a..9574d4c0bd6 100644
--- a/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStepGithub.tsx
+++ b/server/sonar-web/src/main/js/components/tutorials/jenkins/WebhookStepGithub.tsx
@@ -29,7 +29,7 @@ import { buildGithubLink } from '../utils';
export interface WebhookStepGithubProps {
almBinding?: AlmSettingsInstance;
branchesEnabled: boolean;
- projectBinding?: ProjectAlmBindingResponse;
+ projectBinding?: ProjectAlmBindingResponse | null;
}
export default function WebhookStepGithub(props: WebhookStepGithubProps) {
diff --git a/server/sonar-web/src/main/js/queries/common.ts b/server/sonar-web/src/main/js/queries/common.ts
index 9bf450ae683..fe56e4c8c38 100644
--- a/server/sonar-web/src/main/js/queries/common.ts
+++ b/server/sonar-web/src/main/js/queries/common.ts
@@ -29,23 +29,38 @@ import {
useQuery,
} from '@tanstack/react-query';
+const isFnWithoutParams = <T>(fn: ((data: any) => T) | (() => T)): fn is () => T => fn.length === 0;
+
export function createQueryHook<
T = unknown,
TQueryData = unknown,
- TError = Error,
+ TError extends Error = Error,
TData = TQueryData,
TQueryKey extends QueryKey = QueryKey,
>(
- fn: (data: T) => UseQueryOptions<TQueryData, TError, TData, TQueryKey>,
-): <SelectType = TQueryData>(
- data: T,
- options?: Omit<
- UseQueryOptions<TQueryData, TError, SelectType, TQueryKey>,
- 'queryKey' | 'queryFn'
- >,
-) => UseQueryResult<SelectType, TError>;
+ fn:
+ | ((data: T) => UseQueryOptions<TQueryData, TError, TData, TQueryKey>)
+ | (() => UseQueryOptions<TQueryData, TError, TData, TQueryKey>),
+): unknown extends T
+ ? <SelectType = TQueryData>(
+ options?: Omit<
+ UseQueryOptions<TQueryData, TError, SelectType, TQueryKey>,
+ 'queryKey' | 'queryFn'
+ >,
+ ) => UseQueryResult<SelectType, TError>
+ : <SelectType = TQueryData>(
+ data: T,
+ options?: Omit<
+ UseQueryOptions<TQueryData, TError, SelectType, TQueryKey>,
+ 'queryKey' | 'queryFn'
+ >,
+ ) => UseQueryResult<SelectType, TError>;
-export function createQueryHook(fn: (data: any) => UseQueryOptions) {
+export function createQueryHook(fn: ((data: any) => UseQueryOptions) | (() => UseQueryOptions)) {
+ if (isFnWithoutParams(fn)) {
+ return (options?: Omit<UseQueryOptions, 'queryKey' | 'queryFn'>) =>
+ useQuery({ ...fn(), ...options });
+ }
return (data: any, options?: Omit<UseQueryOptions, 'queryKey' | 'queryFn'>) =>
useQuery({ ...fn(data), ...options });
}
@@ -59,18 +74,58 @@ export function createInfiniteQueryHook<
TQueryKey extends QueryKey = QueryKey,
TPageParam = unknown,
>(
- fn: (
- data: T,
- ) => UseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey, TPageParam>,
-): <SelectType = TData>(
- data: T,
- options?: Omit<
- UseInfiniteQueryOptions<TQueryFnData, TError, SelectType, TQueryData, TQueryKey, TPageParam>,
- 'queryKey' | 'queryFn' | 'getNextPageParam' | 'getPreviousPageParam' | 'initialPageParam'
- >,
-) => UseInfiniteQueryResult<SelectType, TError>;
+ fn:
+ | ((
+ data: T,
+ ) => UseInfiniteQueryOptions<TQueryFnData, TError, TData, TQueryData, TQueryKey, TPageParam>)
+ | (() => UseInfiniteQueryOptions<
+ TQueryFnData,
+ TError,
+ TData,
+ TQueryData,
+ TQueryKey,
+ TPageParam
+ >),
+): unknown extends T
+ ? <SelectType = TData>(
+ options?: Omit<
+ UseInfiniteQueryOptions<
+ TQueryFnData,
+ TError,
+ SelectType,
+ TQueryData,
+ TQueryKey,
+ TPageParam
+ >,
+ 'queryKey' | 'queryFn' | 'getNextPageParam' | 'getPreviousPageParam' | 'initialPageParam'
+ >,
+ ) => UseInfiniteQueryResult<SelectType, TError>
+ : <SelectType = TData>(
+ data: T,
+ options?: Omit<
+ UseInfiniteQueryOptions<
+ TQueryFnData,
+ TError,
+ SelectType,
+ TQueryData,
+ TQueryKey,
+ TPageParam
+ >,
+ 'queryKey' | 'queryFn' | 'getNextPageParam' | 'getPreviousPageParam' | 'initialPageParam'
+ >,
+ ) => UseInfiniteQueryResult<SelectType, TError>;
-export function createInfiniteQueryHook(fn: (data: any) => UseInfiniteQueryOptions) {
+export function createInfiniteQueryHook(
+ fn: ((data?: any) => UseInfiniteQueryOptions) | (() => UseInfiniteQueryOptions),
+) {
+ if (isFnWithoutParams(fn)) {
+ return (
+ options?: Omit<
+ UseInfiniteQueryOptions,
+ 'queryKey' | 'queryFn' | 'getNextPageParam' | 'getPreviousPageParam' | 'initialPageParam'
+ >,
+ ) => useInfiniteQuery({ ...fn(), ...options });
+ }
return (
data: any,
options?: Omit<
diff --git a/server/sonar-web/src/main/js/queries/devops-integration.ts b/server/sonar-web/src/main/js/queries/devops-integration.ts
index ff7a6478ea2..fc5824edfcb 100644
--- a/server/sonar-web/src/main/js/queries/devops-integration.ts
+++ b/server/sonar-web/src/main/js/queries/devops-integration.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 { UseQueryOptions, useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
+import { queryOptions, useMutation, useQueryClient } from '@tanstack/react-query';
import { useLocation } from 'react-router-dom';
import {
deleteProjectAlmBinding,
@@ -30,6 +30,7 @@ import {
} from '../api/alm-settings';
import { HttpStatus } from '../helpers/request';
import { AlmKeys, ProjectAlmBindingParams, ProjectAlmBindingResponse } from '../types/alm-settings';
+import { createQueryHook } from './common';
function useProjectKeyFromLocation() {
const location = useLocation();
@@ -42,39 +43,27 @@ function useProjectKeyFromLocation() {
return id as string;
}
-export function useProjectBindingQuery<T = ProjectAlmBindingResponse>(
- project?: string,
- options?: Omit<
- UseQueryOptions<
- ProjectAlmBindingResponse | null,
- Error,
- T,
- ['devops_integration', string, 'binding']
- >,
- 'queryKey' | 'queryFn'
- >,
-) {
+export const useProjectBindingQuery = createQueryHook((project?: string) => {
const keyFromUrl = useProjectKeyFromLocation();
const projectKey = project ?? keyFromUrl;
- return useQuery({
+ return queryOptions({
queryKey: ['devops_integration', projectKey ?? '_blank_', 'binding'],
queryFn: ({ queryKey: [_, key] }) =>
getProjectAlmBinding(key).catch((e: Response) => {
if (e.status === HttpStatus.NotFound) {
return null;
}
- return e;
+ return e as unknown as ProjectAlmBindingResponse;
}),
staleTime: 60_000,
enabled: projectKey !== null,
- ...options,
});
-}
+});
export function useIsGitHubProjectQuery(project?: string) {
- return useProjectBindingQuery<boolean>(project, {
+ return useProjectBindingQuery(project, {
select: (data) => data?.alm === AlmKeys.GitHub,
});
}
diff --git a/server/sonar-web/src/main/js/queries/system.ts b/server/sonar-web/src/main/js/queries/system.ts
index 1f1f41c406e..d5de4ccdf54 100644
--- a/server/sonar-web/src/main/js/queries/system.ts
+++ b/server/sonar-web/src/main/js/queries/system.ts
@@ -17,19 +17,14 @@
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
-import { UseQueryOptions, useQuery } from '@tanstack/react-query';
+import { queryOptions } from '@tanstack/react-query';
import { getSystemUpgrades } from '../api/system';
+import { createQueryHook } from './common';
-export function useSystemUpgrades<T = Awaited<ReturnType<typeof getSystemUpgrades>>>(
- options?: Omit<
- UseQueryOptions<Awaited<ReturnType<typeof getSystemUpgrades>>, Error, T>,
- 'queryKey' | 'queryFn'
- >,
-) {
- return useQuery({
+export const useSystemUpgrades = createQueryHook(() => {
+ return queryOptions({
queryKey: ['system', 'upgrades'],
queryFn: () => getSystemUpgrades(),
staleTime: Infinity,
- ...options,
});
-}
+});