aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/quality-profiles/utils.js
blob: a98b28b9acbc47264921e764d443f80fc514d5d2 (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
113
114
115
116
117
118
119
120
121
/*
 * SonarQube
 * Copyright (C) 2009-2017 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.
 */
// @flow
import { sortBy } from 'lodash';
import moment from 'moment';
import type { Profile } from './propTypes';

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

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

  function putProfile(profile = null, depth = 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 createFakeProfile(overrides: {}) {
  return {
    key: 'key',
    name: 'name',
    isDefault: false,
    isInherited: false,
    language: 'js',
    languageName: 'JavaScript',
    activeRuleCount: 10,
    activeDeprecatedRuleCount: 2,
    projectCount: 3,
    ...overrides
  };
}

export function isStagnant(profile: Profile) {
  return moment().diff(moment(profile.userUpdatedAt), 'years') >= 1;
}

export const getProfilesPath = (organization: ?string) =>
  organization ? `/organizations/${organization}/quality_profiles` : '/profiles';

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

export const getProfilePath = (name: string, language: string, organization: ?string) => ({
  pathname: getProfilesPath(organization) + '/show',
  query: { name, language }
});

export const getProfileComparePath = (
  name: string,
  language: string,
  organization: ?string,
  withKey?: string
) => {
  const query: Object = { language, name };
  if (withKey) {
    Object.assign(query, { withKey });
  }
  return {
    pathname: getProfilesPath(organization) + '/compare',
    query
  };
};

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