Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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 { isWebUri } from 'valid-url';
  21. import { getGithubClientId } from '../../../../api/alm-integrations';
  22. import { getHostUrl } from '../../../../helpers/urls';
  23. import { AlmKeys } from '../../../../types/alm-settings';
  24. import { DopSetting } from '../../../../types/dop-translation';
  25. export async function redirectToGithub(params: {
  26. isMonorepoSetup: boolean;
  27. selectedDopSetting?: DopSetting;
  28. }) {
  29. const { isMonorepoSetup, selectedDopSetting } = params;
  30. if (selectedDopSetting?.url === undefined) {
  31. return;
  32. }
  33. const { clientId } = await getGithubClientId(selectedDopSetting.key);
  34. if (clientId === undefined) {
  35. throw new Error('Received no GitHub client id');
  36. }
  37. const queryParams = [
  38. { param: 'client_id', value: clientId },
  39. {
  40. param: 'redirect_uri',
  41. value: encodeURIComponent(
  42. `${getHostUrl()}/projects/create?mode=${AlmKeys.GitHub}&dopSetting=${
  43. selectedDopSetting.id
  44. }${isMonorepoSetup ? '&mono=true' : ''}`,
  45. ),
  46. },
  47. ]
  48. .map(({ param, value }) => `${param}=${value}`)
  49. .join('&');
  50. let instanceRootUrl;
  51. // Strip the api section from the url, since we're not hitting the api here.
  52. if (selectedDopSetting.url.includes('/api/v3')) {
  53. // GitHub Enterprise
  54. instanceRootUrl = selectedDopSetting.url.replace('/api/v3', '');
  55. } else {
  56. // github.com
  57. instanceRootUrl = selectedDopSetting.url.replace('api.', '');
  58. }
  59. // strip the trailing /
  60. instanceRootUrl = instanceRootUrl.replace(/\/$/, '');
  61. if (isWebUri(instanceRootUrl) === undefined) {
  62. throw new Error('Invalid GitHub URL');
  63. } else {
  64. window.location.replace(`${instanceRootUrl}/login/oauth/authorize?${queryParams}`);
  65. }
  66. }