aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/quality-profiles/utils.ts
blob: cc8ddea8634e0bd614d77875502beb567cfbc2e1 (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
/*
 * SonarQube
 * Copyright (C) 2009-2021 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 * as differenceInYears from 'date-fns/difference_in_years';
import { sortBy } from 'lodash';
import { Profile as BaseProfile } from '../../api/quality-profiles';
import { isValidDate, parseDate } from '../../helpers/dates';
import { Profile } from './types';

export function sortProfiles(profiles: BaseProfile[]): Profile[] {
  const result: Profile[] = [];
  const sorted = sortBy(profiles, 'name');

  function retrieveChildren(parent: BaseProfile | null) {
    return sorted.filter(
      p => (parent == null && p.parentKey == null) || (parent != null && p.parentKey === parent.key)
    );
  }

  function putProfile(profile: BaseProfile | null = null, depth: number = 1) {
    const children = retrieveChildren(profile);

    if (profile != null) {
      result.push({ ...profile, childrenCount: children.length, depth });
    }

    children.forEach(child => putProfile(child, depth + 1));
  }

  sorted
    .filter(
      profile => profile.parentKey == null || sorted.find(p => p.key === profile.parentKey) == null
    )
    .forEach(profile => putProfile(profile));

  return result;
}

export function isStagnant(profile: Profile): boolean {
  if (profile.rulesUpdatedAt) {
    const updateDate = parseDate(profile.rulesUpdatedAt);
    if (isValidDate(updateDate)) {
      return differenceInYears(new Date(), updateDate) >= 1;
    }
  }
  return false;
}

export const PROFILE_PATH = '/profiles';

export const getProfilesForLanguagePath = (language: string) => ({
  pathname: PROFILE_PATH,
  query: { language }
});

export const getProfilePath = (name: string, language: string) => ({
  pathname: `${PROFILE_PATH}/show`,
  query: { name, language }
});

export const getProfileComparePath = (name: string, language: string, withKey?: string) => {
  const query = { language, name };
  if (withKey) {
    Object.assign(query, { withKey });
  }
  return {
    pathname: `${PROFILE_PATH}/compare`,
    query
  };
};

export const getProfileChangelogPath = (
  name: string,
  language: string,
  filter?: { since?: string; to?: string }
) => {
  const query = { language, name };
  if (filter) {
    if (filter.since) {
      Object.assign(query, { since: filter.since });
    }
    if (filter.to) {
      Object.assign(query, { to: filter.to });
    }
  }
  return {
    pathname: `${PROFILE_PATH}/changelog`,
    query
  };
};