123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230 |
- /*
- * SonarQube
- * Copyright (C) 2009-2024 SonarSource SA
- * mailto:info AT sonarsource DOT com
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of the GNU Lesser General Public
- * License as published by the Free Software Foundation; either
- * version 3 of the License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * Lesser General Public License for more details.
- *
- * You should have received a copy of the GNU Lesser General Public License
- * along with this program; if not, write to the Free Software Foundation,
- * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
- */
- /* eslint-disable react/no-unused-prop-types */
-
- import { Link, Spinner } from '@sonarsource/echoes-react';
- import { DarkLabel, FlagMessage, InputSelect, LightPrimary, Title } from 'design-system';
- import React, { useContext, useEffect, useState } from 'react';
- import { FormattedMessage } from 'react-intl';
- import { AvailableFeaturesContext } from '../../../../app/components/available-features/AvailableFeaturesContext';
- import { translate } from '../../../../helpers/l10n';
- import { LabelValueSelectOption } from '../../../../helpers/search';
- import { queryToSearch } from '../../../../helpers/urls';
- import { GithubOrganization, GithubRepository } from '../../../../types/alm-integration';
- import { AlmKeys, AlmSettingsInstance } from '../../../../types/alm-settings';
- import { Feature } from '../../../../types/features';
- import { Paging } from '../../../../types/types';
- import AlmSettingsInstanceDropdown from '../components/AlmSettingsInstanceDropdown';
- import RepositoryList from '../components/RepositoryList';
- import { CreateProjectModes } from '../types';
-
- interface GitHubProjectCreateRendererProps {
- canAdmin: boolean;
- error: boolean;
- loadingBindings: boolean;
- loadingOrganizations: boolean;
- loadingRepositories: boolean;
- onImportRepository: (key: string[]) => void;
- onLoadMore: () => void;
- onSearch: (q: string) => void;
- onSelectOrganization: (key: string) => void;
- organizations: GithubOrganization[];
- repositories?: GithubRepository[];
- repositoryPaging: Paging;
- searchQuery: string;
- selectedOrganization?: GithubOrganization;
- almInstances: AlmSettingsInstance[];
- selectedAlmInstance?: AlmSettingsInstance;
- onSelectedAlmInstanceChange: (instance: AlmSettingsInstance) => void;
- }
-
- function orgToOption({ key, name }: GithubOrganization) {
- return { value: key, label: name };
- }
-
- export default function GitHubProjectCreateRenderer(
- props: Readonly<GitHubProjectCreateRendererProps>,
- ) {
- const isMonorepoSupported = useContext(AvailableFeaturesContext).includes(
- Feature.MonoRepositoryPullRequestDecoration,
- );
-
- const {
- canAdmin,
- error,
- loadingBindings,
- loadingOrganizations,
- organizations,
- selectedOrganization,
- almInstances,
- selectedAlmInstance,
- repositories,
- } = props;
- const [selected, setSelected] = useState<Set<string>>(new Set());
-
- useEffect(() => {
- const selectedKeys = Array.from(selected).filter((key) =>
- repositories?.find((r) => r.key === key),
- );
- setSelected(new Set(selectedKeys));
- // We want to update only when `repositories` changes.
- // If we subscribe to `selected` changes we will enter an infinite loop.
- // eslint-disable-next-line react-hooks/exhaustive-deps
- }, [repositories]);
-
- if (loadingBindings) {
- return <Spinner />;
- }
-
- const handleCheck = (key: string) => {
- setSelected((prev) => new Set(prev.delete(key) ? prev : prev.add(key)));
- };
-
- const handleCheckAll = () => {
- setSelected(
- new Set(repositories?.filter((r) => r.sqProjectKey === undefined).map((r) => r.key) ?? []),
- );
- };
-
- const handleImport = () => {
- props.onImportRepository(Array.from(selected));
- };
-
- const handleUncheckAll = () => {
- setSelected(new Set());
- };
-
- return (
- <>
- <header className="sw-mb-10">
- <Title className="sw-mb-4">{translate('onboarding.create_project.github.title')}</Title>
- <LightPrimary className="sw-body-sm">
- {isMonorepoSupported ? (
- <FormattedMessage
- id="onboarding.create_project.github.subtitle.with_monorepo"
- values={{
- monorepoSetupLink: (
- <Link
- to={{
- pathname: '/projects/create',
- search: queryToSearch({
- mode: CreateProjectModes.GitHub,
- mono: true,
- }),
- }}
- >
- <FormattedMessage id="onboarding.create_project.subtitle_monorepo_setup_link" />
- </Link>
- ),
- }}
- />
- ) : (
- <FormattedMessage id="onboarding.create_project.github.subtitle" />
- )}
- </LightPrimary>
- </header>
-
- <AlmSettingsInstanceDropdown
- almKey={AlmKeys.GitHub}
- almInstances={almInstances}
- selectedAlmInstance={selectedAlmInstance}
- onChangeConfig={props.onSelectedAlmInstanceChange}
- />
-
- {error && selectedAlmInstance && (
- <FlagMessage variant="warning" className="sw-my-2">
- <span>
- {canAdmin ? (
- <FormattedMessage
- id="onboarding.create_project.github.warning.message_admin"
- defaultMessage={translate('onboarding.create_project.github.warning.message_admin')}
- values={{
- link: (
- <Link to="/admin/settings?category=almintegration">
- {translate('onboarding.create_project.github.warning.message_admin.link')}
- </Link>
- ),
- }}
- />
- ) : (
- translate('onboarding.create_project.github.warning.message')
- )}
- </span>
- </FlagMessage>
- )}
-
- <Spinner isLoading={loadingOrganizations && !error}>
- {!error && (
- <div className="sw-flex sw-flex-col">
- <DarkLabel htmlFor="github-choose-organization" className="sw-mb-2">
- {translate('onboarding.create_project.github.choose_organization')}
- </DarkLabel>
- {organizations.length > 0 ? (
- <InputSelect
- className="sw-w-7/12 sw-mb-9"
- size="full"
- isSearchable
- inputId="github-choose-organization"
- options={organizations.map(orgToOption)}
- onChange={({ value }: LabelValueSelectOption) => props.onSelectOrganization(value)}
- value={selectedOrganization ? orgToOption(selectedOrganization) : null}
- />
- ) : (
- !loadingOrganizations && (
- <FlagMessage variant="error" className="sw-mb-2">
- <span>
- {canAdmin ? (
- <FormattedMessage
- id="onboarding.create_project.github.no_orgs_admin"
- defaultMessage={translate('onboarding.create_project.github.no_orgs_admin')}
- values={{
- link: (
- <Link to="/admin/settings?category=almintegration">
- {translate(
- 'onboarding.create_project.github.warning.message_admin.link',
- )}
- </Link>
- ),
- }}
- />
- ) : (
- translate('onboarding.create_project.github.no_orgs')
- )}
- </span>
- </FlagMessage>
- )
- )}
- </div>
- )}
- {selectedOrganization && (
- <RepositoryList
- {...props}
- almKey={AlmKeys.GitHub}
- checkAll={handleCheckAll}
- onCheck={handleCheck}
- onImport={handleImport}
- selected={selected}
- uncheckAll={handleUncheckAll}
- />
- )}
- </Spinner>
- </>
- );
- }
|