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.

AzureProjectCreate.tsx 8.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  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 * as React from 'react';
  21. import {
  22. getAzureProjects,
  23. getAzureRepositories,
  24. searchAzureRepositories,
  25. } from '../../../../api/alm-integrations';
  26. import { Location, Router } from '../../../../components/hoc/withRouter';
  27. import { AzureProject, AzureRepository } from '../../../../types/alm-integration';
  28. import { AlmSettingsInstance } from '../../../../types/alm-settings';
  29. import { Dict } from '../../../../types/types';
  30. import { ImportProjectParam } from '../CreateProjectPage';
  31. import { CreateProjectModes } from '../types';
  32. import AzureCreateProjectRenderer from './AzureProjectCreateRenderer';
  33. interface Props {
  34. canAdmin: boolean;
  35. loadingBindings: boolean;
  36. almInstances: AlmSettingsInstance[];
  37. location: Location;
  38. router: Router;
  39. onProjectSetupDone: (importProjects: ImportProjectParam) => void;
  40. }
  41. interface State {
  42. loading: boolean;
  43. loadingRepositories: Dict<boolean>;
  44. projects?: AzureProject[];
  45. repositories: Dict<AzureRepository[]>;
  46. searching?: boolean;
  47. searchResults?: AzureRepository[];
  48. searchQuery?: string;
  49. selectedAlmInstance?: AlmSettingsInstance;
  50. showPersonalAccessTokenForm: boolean;
  51. }
  52. export default class AzureProjectCreate extends React.PureComponent<Props, State> {
  53. mounted = false;
  54. constructor(props: Props) {
  55. super(props);
  56. this.state = {
  57. selectedAlmInstance: props.almInstances[0],
  58. loading: false,
  59. showPersonalAccessTokenForm: true,
  60. loadingRepositories: {},
  61. repositories: {},
  62. };
  63. }
  64. componentDidMount() {
  65. this.mounted = true;
  66. this.fetchData();
  67. }
  68. componentDidUpdate(prevProps: Props) {
  69. if (prevProps.almInstances.length === 0 && this.props.almInstances.length > 0) {
  70. this.setState({ selectedAlmInstance: this.props.almInstances[0] }, () => {
  71. this.fetchData().catch(() => {
  72. /* noop */
  73. });
  74. });
  75. }
  76. }
  77. componentWillUnmount() {
  78. this.mounted = false;
  79. }
  80. fetchData = async () => {
  81. const { showPersonalAccessTokenForm } = this.state;
  82. if (!showPersonalAccessTokenForm) {
  83. this.setState({ loading: true });
  84. let projects: AzureProject[] | undefined;
  85. try {
  86. projects = await this.fetchAzureProjects();
  87. } catch (_) {
  88. if (this.mounted) {
  89. this.setState({ showPersonalAccessTokenForm: true, loading: false });
  90. }
  91. }
  92. const { repositories } = this.state;
  93. let firstProjectName: string;
  94. if (projects && projects.length > 0) {
  95. firstProjectName = projects[0].name;
  96. this.setState(({ loadingRepositories }) => ({
  97. loadingRepositories: { ...loadingRepositories, [firstProjectName]: true },
  98. }));
  99. const repos = await this.fetchAzureRepositories(firstProjectName);
  100. repositories[firstProjectName] = repos;
  101. }
  102. if (this.mounted) {
  103. this.setState(({ loadingRepositories }) => {
  104. if (firstProjectName !== '') {
  105. loadingRepositories[firstProjectName] = false;
  106. }
  107. return {
  108. loading: false,
  109. loadingRepositories: { ...loadingRepositories },
  110. projects,
  111. repositories,
  112. };
  113. });
  114. }
  115. }
  116. };
  117. fetchAzureProjects = (): Promise<AzureProject[] | undefined> => {
  118. const { selectedAlmInstance } = this.state;
  119. if (!selectedAlmInstance) {
  120. return Promise.resolve(undefined);
  121. }
  122. return getAzureProjects(selectedAlmInstance.key).then(({ projects }) => projects);
  123. };
  124. fetchAzureRepositories = (projectName: string): Promise<AzureRepository[]> => {
  125. const { selectedAlmInstance } = this.state;
  126. if (!selectedAlmInstance) {
  127. return Promise.resolve([]);
  128. }
  129. return getAzureRepositories(selectedAlmInstance.key, projectName)
  130. .then(({ repositories }) => repositories)
  131. .catch(() => []);
  132. };
  133. cleanUrl = () => {
  134. const { location, router } = this.props;
  135. delete location.query.resetPat;
  136. router.replace(location);
  137. };
  138. handleOpenProject = async (projectName: string) => {
  139. if (this.state.searchResults) {
  140. return;
  141. }
  142. this.setState(({ loadingRepositories }) => ({
  143. loadingRepositories: { ...loadingRepositories, [projectName]: true },
  144. }));
  145. const projectRepos = await this.fetchAzureRepositories(projectName);
  146. this.setState(({ loadingRepositories, repositories }) => ({
  147. loadingRepositories: { ...loadingRepositories, [projectName]: false },
  148. repositories: { ...repositories, [projectName]: projectRepos },
  149. }));
  150. };
  151. handleSearchRepositories = async (searchQuery: string) => {
  152. const { selectedAlmInstance } = this.state;
  153. if (!selectedAlmInstance) {
  154. return;
  155. }
  156. if (searchQuery.length === 0) {
  157. this.setState({ searchResults: undefined, searchQuery: undefined });
  158. return;
  159. }
  160. this.setState({ searching: true });
  161. const searchResults: AzureRepository[] = await searchAzureRepositories(
  162. selectedAlmInstance.key,
  163. searchQuery,
  164. )
  165. .then(({ repositories }) => repositories)
  166. .catch(() => []);
  167. if (this.mounted) {
  168. this.setState({
  169. searching: false,
  170. searchResults,
  171. searchQuery,
  172. });
  173. }
  174. };
  175. handleImportRepository = (selectedRepository: AzureRepository) => {
  176. const { selectedAlmInstance } = this.state;
  177. if (selectedAlmInstance && selectedRepository) {
  178. this.props.onProjectSetupDone({
  179. creationMode: CreateProjectModes.AzureDevOps,
  180. almSetting: selectedAlmInstance.key,
  181. monorepo: false,
  182. projects: [
  183. {
  184. projectName: selectedRepository.projectName,
  185. repositoryName: selectedRepository.name,
  186. },
  187. ],
  188. });
  189. }
  190. };
  191. handlePersonalAccessTokenCreate = () => {
  192. this.cleanUrl();
  193. this.setState({ showPersonalAccessTokenForm: false }, () => {
  194. this.fetchData();
  195. });
  196. };
  197. onSelectedAlmInstanceChange = (instance: AlmSettingsInstance) => {
  198. this.setState(
  199. {
  200. selectedAlmInstance: instance,
  201. searchResults: undefined,
  202. searchQuery: '',
  203. showPersonalAccessTokenForm: true,
  204. },
  205. () => {
  206. this.fetchData().catch(() => {
  207. /* noop */
  208. });
  209. },
  210. );
  211. };
  212. render() {
  213. const { canAdmin, loadingBindings, location, almInstances } = this.props;
  214. const {
  215. loading,
  216. loadingRepositories,
  217. showPersonalAccessTokenForm,
  218. projects,
  219. repositories,
  220. searching,
  221. searchResults,
  222. searchQuery,
  223. selectedAlmInstance,
  224. } = this.state;
  225. return (
  226. <AzureCreateProjectRenderer
  227. canAdmin={canAdmin}
  228. loading={loading || loadingBindings}
  229. loadingRepositories={loadingRepositories}
  230. onImportRepository={this.handleImportRepository}
  231. onOpenProject={this.handleOpenProject}
  232. onPersonalAccessTokenCreate={this.handlePersonalAccessTokenCreate}
  233. onSearch={this.handleSearchRepositories}
  234. projects={projects}
  235. repositories={repositories}
  236. searching={searching}
  237. searchResults={searchResults}
  238. searchQuery={searchQuery}
  239. almInstances={almInstances}
  240. selectedAlmInstance={selectedAlmInstance}
  241. resetPat={Boolean(location.query.resetPat)}
  242. showPersonalAccessTokenForm={
  243. showPersonalAccessTokenForm || Boolean(location.query.resetPat)
  244. }
  245. onSelectedAlmInstanceChange={this.onSelectedAlmInstanceChange}
  246. />
  247. );
  248. }
  249. }