You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

utils.ts 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2024 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. import { differenceInYears } from 'date-fns';
  21. import { sortBy } from 'lodash';
  22. import { queryToSearchString } from '~sonar-aligned/helpers/urls';
  23. import { Profile as BaseProfile } from '../../api/quality-profiles';
  24. import { isValidDate, parseDate } from '../../helpers/dates';
  25. import { PROFILE_COMPARE_PATH, PROFILE_PATH } from './constants';
  26. import { Profile } from './types';
  27. export function sortProfiles(profiles: BaseProfile[]): Profile[] {
  28. const result: Profile[] = [];
  29. const sorted = sortBy(profiles, 'name');
  30. function retrieveChildren(parent: BaseProfile | null) {
  31. return sorted.filter(
  32. (p) =>
  33. (parent == null && p.parentKey == null) || (parent != null && p.parentKey === parent.key),
  34. );
  35. }
  36. function putProfile(profile: BaseProfile | null = null, depth: number = 1) {
  37. const children = retrieveChildren(profile);
  38. if (profile != null) {
  39. result.push({ ...profile, childrenCount: children.length, depth });
  40. }
  41. children.forEach((child) => putProfile(child, depth + 1));
  42. }
  43. sorted
  44. .filter(
  45. (profile) =>
  46. profile.parentKey == null || sorted.find((p) => p.key === profile.parentKey) == null,
  47. )
  48. .forEach((profile) => putProfile(profile));
  49. return result;
  50. }
  51. export function isStagnant(profile: Profile): boolean {
  52. if (profile.rulesUpdatedAt) {
  53. const updateDate = parseDate(profile.rulesUpdatedAt);
  54. if (isValidDate(updateDate)) {
  55. return differenceInYears(new Date(), updateDate) >= 1;
  56. }
  57. }
  58. return false;
  59. }
  60. export const getProfilesForLanguagePath = (language: string) => ({
  61. pathname: PROFILE_PATH,
  62. search: queryToSearchString({ language }),
  63. });
  64. export const getProfilePath = (name: string, language: string) => ({
  65. pathname: `${PROFILE_PATH}/show`,
  66. search: queryToSearchString({ name, language }),
  67. });
  68. export const getProfileComparePath = (name: string, language: string, withKey?: string) => {
  69. const query = { language, name };
  70. if (withKey) {
  71. Object.assign(query, { withKey });
  72. }
  73. return {
  74. pathname: PROFILE_COMPARE_PATH,
  75. search: queryToSearchString(query),
  76. };
  77. };
  78. export const getProfileChangelogPath = (
  79. name: string,
  80. language: string,
  81. filter?: { since?: string; to?: string },
  82. ) => {
  83. const query = { language, name };
  84. if (filter) {
  85. if (filter.since) {
  86. Object.assign(query, { since: filter.since });
  87. }
  88. if (filter.to) {
  89. Object.assign(query, { to: filter.to });
  90. }
  91. }
  92. return {
  93. pathname: `${PROFILE_PATH}/changelog`,
  94. search: queryToSearchString(query),
  95. };
  96. };
  97. export const isProfileComparePath = (pathname: string): boolean => {
  98. return pathname === PROFILE_COMPARE_PATH;
  99. };