Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

CreateProjectPageSonarQube.tsx 4.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2020 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 * as React from 'react';
  21. import { Helmet } from 'react-helmet-async';
  22. import { WithRouterProps } from 'react-router';
  23. import { translate } from 'sonar-ui-common/helpers/l10n';
  24. import { addWhitePageClass, removeWhitePageClass } from 'sonar-ui-common/helpers/pages';
  25. import { getAlmSettings } from '../../../api/alm-settings';
  26. import { whenLoggedIn } from '../../../components/hoc/whenLoggedIn';
  27. import { withAppState } from '../../../components/hoc/withAppState';
  28. import { getProjectUrl } from '../../../helpers/urls';
  29. import { AlmKeys, AlmSettingsInstance } from '../../../types/alm-settings';
  30. import BitbucketProjectCreate from './BitbucketProjectCreate';
  31. import CreateProjectModeSelection from './CreateProjectModeSelection';
  32. import ManualProjectCreate from './ManualProjectCreate';
  33. import './style.css';
  34. import { CreateProjectModes } from './types';
  35. interface Props extends Pick<WithRouterProps, 'router' | 'location'> {
  36. appState: Pick<T.AppState, 'branchesEnabled'>;
  37. currentUser: T.LoggedInUser;
  38. }
  39. interface State {
  40. bitbucketSettings: AlmSettingsInstance[];
  41. loading: boolean;
  42. }
  43. export class CreateProjectPageSonarQube extends React.PureComponent<Props, State> {
  44. mounted = false;
  45. state: State = { bitbucketSettings: [], loading: false };
  46. componentDidMount() {
  47. const {
  48. appState: { branchesEnabled },
  49. location
  50. } = this.props;
  51. this.mounted = true;
  52. if (branchesEnabled) {
  53. this.fetchAlmBindings();
  54. }
  55. if (location.query?.mode || !branchesEnabled) {
  56. addWhitePageClass();
  57. }
  58. }
  59. componentDidUpdate(prevProps: Props) {
  60. if (this.props.location.query?.mode && !prevProps.location.query?.mode) {
  61. addWhitePageClass();
  62. } else if (!this.props.location.query?.mode && prevProps.location.query?.mode) {
  63. removeWhitePageClass();
  64. }
  65. }
  66. componentWillUnmount() {
  67. this.mounted = false;
  68. removeWhitePageClass();
  69. }
  70. fetchAlmBindings = () => {
  71. this.setState({ loading: true });
  72. getAlmSettings()
  73. .then(almSettings => {
  74. if (this.mounted) {
  75. this.setState({
  76. bitbucketSettings: almSettings.filter(s => s.alm === AlmKeys.Bitbucket),
  77. loading: false
  78. });
  79. }
  80. })
  81. .catch(() => {
  82. if (this.mounted) {
  83. this.setState({ loading: false });
  84. }
  85. });
  86. };
  87. handleProjectCreate = (projectKeys: string[]) => {
  88. if (projectKeys.length === 1) {
  89. this.props.router.push(getProjectUrl(projectKeys[0]));
  90. }
  91. };
  92. handleModeSelect = (mode: CreateProjectModes) => {
  93. const { router, location } = this.props;
  94. router.push({
  95. pathname: location.pathname,
  96. query: { mode }
  97. });
  98. };
  99. render() {
  100. const {
  101. appState: { branchesEnabled },
  102. currentUser,
  103. location
  104. } = this.props;
  105. const { bitbucketSettings, loading } = this.state;
  106. const mode: CreateProjectModes | undefined = location.query?.mode;
  107. const showManualForm = !branchesEnabled || mode === CreateProjectModes.Manual;
  108. const showBBSForm = branchesEnabled && mode === CreateProjectModes.BitbucketServer;
  109. return (
  110. <>
  111. <Helmet title={translate('my_account.create_new.TRK')} titleTemplate="%s" />
  112. <div className="page page-limited huge-spacer-bottom position-relative" id="create-project">
  113. {!showBBSForm && !showManualForm && (
  114. <CreateProjectModeSelection
  115. bbsBindingCount={bitbucketSettings.length}
  116. loadingBindings={loading}
  117. onSelectMode={this.handleModeSelect}
  118. />
  119. )}
  120. {showManualForm && (
  121. <ManualProjectCreate
  122. branchesEnabled={branchesEnabled}
  123. currentUser={currentUser}
  124. onProjectCreate={this.handleProjectCreate}
  125. />
  126. )}
  127. {showBBSForm && (
  128. <BitbucketProjectCreate
  129. bitbucketSettings={bitbucketSettings}
  130. loadingBindings={loading}
  131. location={location}
  132. onProjectCreate={this.handleProjectCreate}
  133. />
  134. )}
  135. </div>
  136. </>
  137. );
  138. }
  139. }
  140. export default whenLoggedIn(withAppState(CreateProjectPageSonarQube));