import { translate } from '../helpers/l10n';
import { Condition, QualityGate } from '../types/types';
-function getQualityGateQueryKey(type: 'name', data: string): string[];
-function getQualityGateQueryKey(type: 'project', data: string): string[];
-function getQualityGateQueryKey(): string[];
-function getQualityGateQueryKey(type?: string, data?: string) {
- return ['quality-gate', type ?? '', data ?? ''].filter(Boolean);
-}
-
-export function useQualityGateQuery(name: string) {
+const QUERY_STALE_TIME = 5 * 60 * 1000;
+
+const qualityQuery = {
+ all: () => ['quality-gate'] as const,
+ list: () => ['quality-gate', 'list'] as const,
+ details: () => ['quality-gate', 'details'] as const,
+ detail: (name?: string) => [...qualityQuery.details(), name ?? ''] as const,
+ projectsAssoc: () => ['quality-gate', 'project-assoc'] as const,
+ projectAssoc: (project: string) => [...qualityQuery.projectsAssoc(), project] as const,
+};
+
+// This is internal to "enable" query when searching from the project page
+function useQualityGateQueryInner(name?: string) {
return useQuery({
- queryKey: getQualityGateQueryKey('name', name),
+ queryKey: qualityQuery.detail(name),
queryFn: ({ queryKey: [, , name] }) => {
return fetchQualityGate({ name });
},
+ enabled: name !== undefined,
+ staleTime: QUERY_STALE_TIME,
});
}
-export function useComponentQualityGateQuery(project: string) {
+export function useQualityGateQuery(name: string) {
+ return useQualityGateQueryInner(name);
+}
+
+function useQualityGateForProjectQuery(project: string) {
return useQuery({
- queryKey: getQualityGateQueryKey('project', project),
+ queryKey: qualityQuery.projectAssoc(project),
queryFn: async ({ queryKey: [, , project] }) => {
const qualityGatePreview = await getGateForProject({ project });
- return fetchQualityGate({ name: qualityGatePreview.name });
+ return qualityGatePreview.name;
},
});
}
+export function useComponentQualityGateQuery(project: string) {
+ const { data: name } = useQualityGateForProjectQuery(project);
+ return useQualityGateQueryInner(name);
+}
+
export function useQualityGatesQuery() {
return useQuery({
- queryKey: getQualityGateQueryKey(),
+ queryKey: qualityQuery.list(),
queryFn: () => {
return fetchQualityGates();
},
- staleTime: 2 * 60 * 1000,
+ staleTime: QUERY_STALE_TIME,
});
}
return createQualityGate({ name });
},
onSuccess: () => {
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey() });
+ queryClient.invalidateQueries({ queryKey: qualityQuery.list() });
},
});
}
-export function useSetQualityGateAsDefaultMutation(gateName: string) {
+export function useSetQualityGateAsDefaultMutation() {
const queryClient = useQueryClient();
return useMutation({
return setQualityGateAsDefault({ name: qualityGate.name });
},
onSuccess: () => {
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey() });
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey('name', gateName) });
+ queryClient.invalidateQueries({ queryKey: qualityQuery.list() });
+ queryClient.invalidateQueries({ queryKey: qualityQuery.details() });
},
});
}
mutationFn: (newName: string) => {
return renameQualityGate({ currentName, name: newName });
},
- onSuccess: (_, newName: string) => {
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey() });
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey('name', newName) });
+ onSuccess: () => {
+ queryClient.invalidateQueries({ queryKey: qualityQuery.list() });
+ queryClient.invalidateQueries({ queryKey: qualityQuery.projectsAssoc() });
+ queryClient.removeQueries({ queryKey: qualityQuery.detail(currentName) });
},
});
}
return copyQualityGate({ sourceName, name: newName });
},
onSuccess: () => {
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey() });
+ queryClient.invalidateQueries({ queryKey: qualityQuery.list() });
},
});
}
return deleteQualityGate({ name });
},
onSuccess: () => {
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey() });
+ queryClient.invalidateQueries({ queryKey: qualityQuery.list() });
+ queryClient.invalidateQueries({ queryKey: qualityQuery.projectsAssoc() });
+ queryClient.removeQueries({ queryKey: qualityQuery.detail(name) });
},
});
}
return Promise.all(promiseArr);
},
onSuccess: () => {
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey() });
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey('name', gateName) });
+ queryClient.invalidateQueries({ queryKey: qualityQuery.detail(gateName) });
+ queryClient.invalidateQueries({ queryKey: qualityQuery.list() });
addGlobalSuccessMessage(translate('quality_gates.conditions_updated'));
},
});
return createCondition({ ...condition, gateName });
},
onSuccess: (_, condition) => {
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey() });
- queryClient.setQueryData(
- getQualityGateQueryKey('name', gateName),
- (oldData?: QualityGate) => {
- return oldData?.conditions
- ? {
- ...oldData,
- conditions: [...oldData.conditions, condition],
- }
- : undefined;
- },
- );
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey('name', gateName) });
+ queryClient.setQueryData(qualityQuery.detail(gateName), (oldData?: QualityGate) => {
+ return oldData?.conditions
+ ? {
+ ...oldData,
+ conditions: [...oldData.conditions, condition],
+ }
+ : undefined;
+ });
+ queryClient.invalidateQueries({ queryKey: qualityQuery.detail(gateName) });
+ queryClient.invalidateQueries({ queryKey: qualityQuery.list() });
addGlobalSuccessMessage(translate('quality_gates.condition_added'));
},
});
return updateCondition(condition);
},
onSuccess: () => {
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey() });
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey('name', gateName) });
+ queryClient.invalidateQueries({ queryKey: qualityQuery.list() });
+ queryClient.invalidateQueries({ queryKey: qualityQuery.detail(gateName) });
addGlobalSuccessMessage(translate('quality_gates.condition_updated'));
},
});
});
},
onSuccess: () => {
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey() });
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey('name', gateName) });
+ queryClient.invalidateQueries({ queryKey: qualityQuery.list() });
+ queryClient.invalidateQueries({ queryKey: qualityQuery.detail(gateName) });
addGlobalSuccessMessage(translate('quality_gates.condition_deleted'));
},
});