123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236 |
- /*
- * 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 { useMutation, useQuery, useQueryClient } from '@tanstack/react-query';
- import { addGlobalSuccessMessage } from 'design-system';
- import {
- copyQualityGate,
- createCondition,
- createQualityGate,
- deleteCondition,
- deleteQualityGate,
- fetchQualityGate,
- fetchQualityGates,
- getGateForProject,
- renameQualityGate,
- setQualityGateAsDefault,
- updateCondition,
- } from '../api/quality-gates';
- import { getCorrectCaycCondition } from '../apps/quality-gates/utils';
- 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) {
- return useQuery({
- queryKey: getQualityGateQueryKey('name', name),
- queryFn: ({ queryKey: [, , name] }) => {
- return fetchQualityGate({ name });
- },
- });
- }
-
- export function useComponentQualityGateQuery(project: string) {
- return useQuery({
- queryKey: getQualityGateQueryKey('project', project),
- queryFn: async ({ queryKey: [, , project] }) => {
- const qualityGatePreview = await getGateForProject({ project });
- return fetchQualityGate({ name: qualityGatePreview.name });
- },
- });
- }
-
- export function useQualityGatesQuery() {
- return useQuery({
- queryKey: getQualityGateQueryKey(),
- queryFn: () => {
- return fetchQualityGates();
- },
- staleTime: 2 * 60 * 1000,
- });
- }
-
- export function useCreateQualityGateMutation() {
- const queryClient = useQueryClient();
-
- return useMutation({
- mutationFn: (name: string) => {
- return createQualityGate({ name });
- },
- onSuccess: () => {
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey() });
- },
- });
- }
-
- export function useSetQualityGateAsDefaultMutation(gateName: string) {
- const queryClient = useQueryClient();
-
- return useMutation({
- mutationFn: (qualityGate: QualityGate) => {
- return setQualityGateAsDefault({ name: qualityGate.name });
- },
- onSuccess: () => {
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey() });
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey('name', gateName) });
- },
- });
- }
-
- export function useRenameQualityGateMutation(currentName: string) {
- const queryClient = useQueryClient();
-
- return useMutation({
- mutationFn: (newName: string) => {
- return renameQualityGate({ currentName, name: newName });
- },
- onSuccess: (_, newName: string) => {
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey() });
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey('name', newName) });
- },
- });
- }
-
- export function useCopyQualityGateMutation(sourceName: string) {
- const queryClient = useQueryClient();
-
- return useMutation({
- mutationFn: (newName: string) => {
- return copyQualityGate({ sourceName, name: newName });
- },
- onSuccess: () => {
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey() });
- },
- });
- }
-
- export function useDeleteQualityGateMutation(name: string) {
- const queryClient = useQueryClient();
-
- return useMutation({
- mutationFn: () => {
- return deleteQualityGate({ name });
- },
- onSuccess: () => {
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey() });
- },
- });
- }
-
- export function useFixQualityGateMutation(gateName: string) {
- const queryClient = useQueryClient();
-
- return useMutation({
- mutationFn: ({
- weakConditions,
- missingConditions,
- }: {
- weakConditions: Condition[];
- missingConditions: Condition[];
- }) => {
- const promiseArr = weakConditions
- .map((condition) => {
- return updateCondition({
- ...getCorrectCaycCondition(condition),
- id: condition.id,
- });
- })
- .concat(
- missingConditions.map((condition) => {
- return createCondition({
- ...getCorrectCaycCondition(condition),
- gateName,
- });
- }),
- );
-
- return Promise.all(promiseArr);
- },
- onSuccess: () => {
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey() });
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey('name', gateName) });
- addGlobalSuccessMessage(translate('quality_gates.conditions_updated'));
- },
- });
- }
-
- export function useCreateConditionMutation(gateName: string) {
- const queryClient = useQueryClient();
-
- return useMutation({
- mutationFn: (condition: Omit<Condition, 'id'>) => {
- 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) });
- addGlobalSuccessMessage(translate('quality_gates.condition_added'));
- },
- });
- }
-
- export function useUpdateConditionMutation(gateName: string) {
- const queryClient = useQueryClient();
-
- return useMutation({
- mutationFn: (condition: Condition) => {
- return updateCondition(condition);
- },
- onSuccess: () => {
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey() });
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey('name', gateName) });
- addGlobalSuccessMessage(translate('quality_gates.condition_updated'));
- },
- });
- }
-
- export function useDeleteConditionMutation(gateName: string) {
- const queryClient = useQueryClient();
-
- return useMutation({
- mutationFn: (condition: Condition) => {
- return deleteCondition({
- id: condition.id,
- });
- },
- onSuccess: () => {
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey() });
- queryClient.invalidateQueries({ queryKey: getQualityGateQueryKey('name', gateName) });
- addGlobalSuccessMessage(translate('quality_gates.condition_deleted'));
- },
- });
- }
|