aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-web/src/main/js/apps/create/project/useProjectCreate.tsx
diff options
context:
space:
mode:
authorShane Findley <shane.findley@sonarsource.com>2024-05-03 08:14:51 +0200
committersonartech <sonartech@sonarsource.com>2024-05-03 20:02:49 +0000
commit4cadd793241b54e5246f32e03fbdc7f838cd2d59 (patch)
treea72f0615061c52651daa7754015d8e7c8d10a4f2 /server/sonar-web/src/main/js/apps/create/project/useProjectCreate.tsx
parentf8755ae02ed58f5b3ce55e33d90589202aadd15e (diff)
downloadsonarqube-4cadd793241b54e5246f32e03fbdc7f838cd2d59.tar.gz
sonarqube-4cadd793241b54e5246f32e03fbdc7f838cd2d59.zip
SONAR 22131 Monorepo for bitbucket server (#11064)
Diffstat (limited to 'server/sonar-web/src/main/js/apps/create/project/useProjectCreate.tsx')
-rw-r--r--server/sonar-web/src/main/js/apps/create/project/useProjectCreate.tsx72
1 files changed, 59 insertions, 13 deletions
diff --git a/server/sonar-web/src/main/js/apps/create/project/useProjectCreate.tsx b/server/sonar-web/src/main/js/apps/create/project/useProjectCreate.tsx
index 1c6c34e3c3e..13486d0d261 100644
--- a/server/sonar-web/src/main/js/apps/create/project/useProjectCreate.tsx
+++ b/server/sonar-web/src/main/js/apps/create/project/useProjectCreate.tsx
@@ -20,31 +20,46 @@
import { useCallback, useEffect, useMemo, useState } from 'react';
import { useLocation, useRouter } from '~sonar-aligned/components/hoc/withRouter';
import { isDefined } from '../../../helpers/types';
+import {
+ AzureRepository,
+ BitbucketCloudRepository,
+ BitbucketRepository,
+ GithubRepository,
+ GitlabProject,
+} from '../../../types/alm-integration';
import { AlmInstanceBase, AlmKeys } from '../../../types/alm-settings';
import { DopSetting } from '../../../types/dop-translation';
-import { Paging } from '../../../types/types';
-
-export function useProjectCreate<RepoType, GroupType>(
- almKey: AlmKeys,
- dopSettings: DopSetting[],
- getKey: (repo: RepoType) => string,
- pageSize: number,
-) {
+import { Dict, Paging } from '../../../types/types';
+import { REPOSITORY_PAGE_SIZE } from './constants';
+
+type RepoTypes =
+ | AzureRepository
+ | BitbucketRepository
+ | BitbucketCloudRepository
+ | GithubRepository
+ | GitlabProject;
+type RepoCollectionTypes = Dict<RepoTypes[]> | RepoTypes[];
+
+export function useProjectCreate<
+ RepoType extends RepoTypes,
+ RepoCollectionType extends RepoCollectionTypes,
+ GroupType,
+>(almKey: AlmKeys, dopSettings: DopSetting[], getKey: (repo: RepoType) => string) {
const [isInitialized, setIsInitialized] = useState(false);
const [selectedDopSetting, setSelectedDopSetting] = useState<DopSetting>();
const [isLoadingOrganizations, setIsLoadingOrganizations] = useState(true);
- const [organizations, setOrganizations] = useState<GroupType[]>([]);
+ const [organizations, setOrganizations] = useState<GroupType[]>();
const [selectedOrganization, setSelectedOrganization] = useState<GroupType>();
const [isLoadingRepositories, setIsLoadingRepositories] = useState<boolean>(false);
const [isLoadingMoreRepositories, setIsLoadingMoreRepositories] = useState<boolean>(false);
- const [repositories, setRepositories] = useState<RepoType[]>([]);
+ const [repositories, setRepositories] = useState<RepoCollectionType>();
const [selectedRepository, setSelectedRepository] = useState<RepoType>();
const [showPersonalAccessTokenForm, setShowPersonalAccessTokenForm] = useState<boolean>(true);
const [resetPersonalAccessToken, setResetPersonalAccessToken] = useState<boolean>(false);
const [searchQuery, setSearchQuery] = useState<string>('');
const [projectsPaging, setProjectsPaging] = useState<Paging>({
pageIndex: 1,
- pageSize,
+ pageSize: REPOSITORY_PAGE_SIZE,
total: 0,
});
@@ -54,6 +69,26 @@ export function useProjectCreate<RepoType, GroupType>(
const isMonorepoSetup = location.query?.mono === 'true';
const hasDopSettings = useMemo(() => Boolean(dopSettings?.length), [dopSettings]);
+ const almInstances = useMemo(
+ () =>
+ dopSettings?.map((dopSetting) => ({
+ alm: dopSetting.type,
+ key: dopSetting.key,
+ url: dopSetting.url,
+ })) ?? [],
+ [dopSettings],
+ );
+
+ const selectedAlmInstance = useMemo(
+ () =>
+ selectedDopSetting && {
+ alm: selectedDopSetting.type,
+ key: selectedDopSetting.key,
+ url: selectedDopSetting.url,
+ },
+ [selectedDopSetting],
+ );
+
const cleanUrl = useCallback(() => {
delete location.query.resetPat;
router.replace(location);
@@ -70,7 +105,7 @@ export function useProjectCreate<RepoType, GroupType>(
setSelectedDopSetting(setting);
setShowPersonalAccessTokenForm(true);
setOrganizations([]);
- setRepositories([]);
+ setRepositories(undefined);
setSearchQuery('');
}, []);
@@ -93,7 +128,16 @@ export function useProjectCreate<RepoType, GroupType>(
const handleSelectRepository = useCallback(
(repositoryKey: string) => {
- setSelectedRepository(repositories.find((repo) => getKey(repo) === repositoryKey));
+ if (Array.isArray(repositories)) {
+ const repos = repositories as RepoType[];
+ setSelectedRepository(repos.find((repo) => getKey(repo) === repositoryKey));
+ } else {
+ const repos = repositories as Dict<RepoType[]>;
+ const selected = Object.values(repos)
+ .flat()
+ .find((repo) => getKey(repo) === repositoryKey);
+ setSelectedRepository(selected);
+ }
},
[getKey, repositories, setSelectedRepository],
);
@@ -124,6 +168,7 @@ export function useProjectCreate<RepoType, GroupType>(
}, [almKey, dopSettings, hasDopSettings, location, selectedDopSetting, setSelectedDopSetting]);
return {
+ almInstances,
handlePersonalAccessTokenCreated,
handleSelectRepository,
hasDopSettings,
@@ -148,6 +193,7 @@ export function useProjectCreate<RepoType, GroupType>(
setIsLoadingOrganizations,
setProjectsPaging,
setOrganizations,
+ selectedAlmInstance,
selectedOrganization,
setRepositories,
setResetPersonalAccessToken,