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 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2018 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 { memoize } from 'lodash';
  21. import { translateWithParameters } from '../../../helpers/l10n';
  22. import { formatMeasure } from '../../../helpers/measures';
  23. import {
  24. RawQuery,
  25. parseAsOptionalString,
  26. cleanQuery,
  27. serializeString
  28. } from '../../../helpers/query';
  29. import { isBitbucket, isGithub } from '../../../helpers/almIntegrations';
  30. import { decodeJwt } from '../../../helpers/strings';
  31. export const ORGANIZATION_IMPORT_BINDING_IN_PROGRESS_TIMESTAMP =
  32. 'sonarcloud.import_org.binding_in_progress';
  33. export const ORGANIZATION_IMPORT_REDIRECT_TO_PROJECT_TIMESTAMP =
  34. 'sonarcloud.import_org.redirect_to_projects';
  35. export const TRIAL_DURATION_DAYS = 14;
  36. export enum Step {
  37. OrganizationDetails,
  38. Plan
  39. }
  40. export function formatPrice(price?: number, noSign?: boolean) {
  41. const priceFormatted = formatMeasure(price, 'FLOAT')
  42. .replace(/[.|,]0$/, '')
  43. .replace(/([.|,]\d)$/, '$10');
  44. return noSign ? priceFormatted : translateWithParameters('billing.price_format', priceFormatted);
  45. }
  46. export interface Query {
  47. almInstallId?: string;
  48. almKey?: string;
  49. }
  50. export const parseQuery = memoize(
  51. (urlQuery: RawQuery = {}): Query => {
  52. let almInstallId = undefined;
  53. let almKey = undefined;
  54. if (urlQuery['installation_id']) {
  55. almKey = 'github';
  56. almInstallId = parseAsOptionalString(urlQuery['installation_id']);
  57. } else if (urlQuery['clientKey']) {
  58. almKey = 'bitbucket';
  59. almInstallId = parseAsOptionalString(urlQuery['clientKey']);
  60. } else if (urlQuery['jwt']) {
  61. const jwt = decodeJwt(urlQuery['jwt']);
  62. if (jwt && jwt.iss) {
  63. almKey = 'bitbucket';
  64. almInstallId = jwt.iss;
  65. }
  66. }
  67. return { almInstallId, almKey };
  68. }
  69. );
  70. export const serializeQuery = (query: Query): RawQuery =>
  71. cleanQuery({
  72. installation_id: isGithub(query.almKey) ? serializeString(query.almInstallId) : undefined,
  73. clientKey: isBitbucket(query.almKey) ? serializeString(query.almInstallId) : undefined
  74. });