aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/queries/component.ts
blob: 0034d72d883eac85e672f44dda9bf0a3bea42f24 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
/*
 * 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 { queryOptions, useQuery, useQueryClient } from '@tanstack/react-query';
import { groupBy, omit } from 'lodash';
import { BranchParameters } from '~sonar-aligned/types/branch-like';
import { getTasksForComponent } from '../api/ce';
import { getBreadcrumbs, getComponent, getComponentData } from '../api/components';
import { MetricKey } from '../sonar-aligned/types/metrics';
import { Component, Measure } from '../types/types';
import { StaleTime, createQueryHook } from './common';

const NEW_METRICS = [
  MetricKey.software_quality_maintainability_rating,
  MetricKey.software_quality_security_rating,
  MetricKey.software_quality_reliability_rating,
  MetricKey.software_quality_security_review_rating,
  MetricKey.software_quality_releasability_rating,
  MetricKey.new_software_quality_security_rating,
  MetricKey.new_software_quality_reliability_rating,
  MetricKey.new_software_quality_maintainability_rating,
  MetricKey.new_software_quality_security_review_rating,
];

const TASK_RETRY = 10_000;

type QueryKeyData = {
  branchParameters: BranchParameters;
  metricKeys: string[];
};

function extractQueryKeyData(queryKey: string[]): { data?: QueryKeyData; key: string } {
  const [, key, , data] = queryKey;
  return { key, data: JSON.parse(data ?? 'null') };
}

export function useTaskForComponentQuery(component: Component) {
  return useQuery({
    queryKey: ['component', component.key, 'tasks'],
    queryFn: ({ queryKey }) => {
      const { key } = extractQueryKeyData(queryKey);
      return getTasksForComponent(key);
    },
    refetchInterval: TASK_RETRY,
  });
}

export const useComponentQuery = createQueryHook(
  ({ component, metricKeys, ...params }: Parameters<typeof getComponent>[0]) => {
    const queryClient = useQueryClient();

    return queryOptions({
      queryKey: ['component', component, 'measures', { metricKeys, params }],
      queryFn: async () => {
        const result = await getComponent({
          component,
          metricKeys: metricKeys
            .split(',')
            .filter((m) => !NEW_METRICS.includes(m as MetricKey))
            .join(),
          ...params,
        });
        const measuresMapByMetricKey = groupBy(result.component.measures, 'metric');
        metricKeys.split(',').forEach((metricKey) => {
          const measure = measuresMapByMetricKey[metricKey]?.[0] ?? null;
          queryClient.setQueryData<Measure>(
            ['measures', 'details', result.component.key, metricKey],
            measure,
          );
        });
        return result;
      },
      staleTime: StaleTime.LONG,
    });
  },
);

export const useComponentBreadcrumbsQuery = createQueryHook(
  ({ component, ...params }: Parameters<typeof getBreadcrumbs>[0]) => {
    return queryOptions({
      queryKey: ['component', component, 'breadcrumbs', params],
      queryFn: () => getBreadcrumbs({ component, ...params }),
      staleTime: StaleTime.LONG,
    });
  },
);

export const useComponentDataQuery = createQueryHook(
  (data: Parameters<typeof getComponentData>[0]) => {
    return queryOptions({
      queryKey: ['component', data.component, 'component_data', omit(data, 'component')],
      queryFn: () => getComponentData(data),
      staleTime: StaleTime.LONG,
    });
  },
);