From cb817bf2ad064be7fb21ae004caccc247a3dc570 Mon Sep 17 00:00:00 2001 From: Ritik Kapoor Date: Mon, 18 Dec 2023 08:42:38 +0000 Subject: [PATCH] [CODE-1159] UI to disable public repo creation with a flag - GITNESS_PUBLIC_RESOURCE_CREATION_ENABLED (#895) --- .../ImportForm/ImportForm.tsx | 66 +------ .../NewRepoModalButton/NewRepoModalButton.tsx | 126 +++++++------ .../GeneralSettingsContent.tsx | 176 ++++++++++-------- web/src/utils/GitUtils.ts | 1 - 4 files changed, 171 insertions(+), 198 deletions(-) diff --git a/web/src/components/NewRepoModalButton/ImportForm/ImportForm.tsx b/web/src/components/NewRepoModalButton/ImportForm/ImportForm.tsx index ae747316a..f9d90299f 100644 --- a/web/src/components/NewRepoModalButton/ImportForm/ImportForm.tsx +++ b/web/src/components/NewRepoModalButton/ImportForm/ImportForm.tsx @@ -18,20 +18,11 @@ import React, { useState } from 'react' import { Intent } from '@blueprintjs/core' import * as yup from 'yup' import { Color } from '@harnessio/design-system' -import { Button, Container, Layout, FlexExpander, Formik, FormikForm, FormInput, Text } from '@harnessio/uicore' +import { Button, Layout, FlexExpander, Formik, FormikForm, FormInput, Text } from '@harnessio/uicore' import { Icon } from '@harnessio/icons' -import { FontVariation } from '@harnessio/design-system' import { useStrings } from 'framework/strings' import { REGEX_VALID_REPO_NAME } from 'utils/Utils' -import { - ImportFormData, - RepoVisibility, - GitProviders, - getProviders, - getOrgLabel, - getOrgPlaceholder -} from 'utils/GitUtils' -import Private from '../../../icons/private.svg' +import { ImportFormData, GitProviders, getProviders, getOrgLabel, getOrgPlaceholder } from 'utils/GitUtils' import css from '../NewRepoModalButton.module.scss' interface ImportFormProps { @@ -56,8 +47,7 @@ const ImportForm = (props: ImportFormProps) => { username: '', password: '', name: '', - description: '', - isPublic: RepoVisibility.PRIVATE + description: '' } const validationSchemaStepOne = yup.object().shape({ @@ -244,56 +234,6 @@ const ImportForm = (props: ImportFormProps) => { }} /> -
- - - - - - - - {getString('public')} - - {getString('createRepoModal.publicLabel')} - - - - - - ), - value: RepoVisibility.PUBLIC - }, - { - label: ( - - - - - - {/* */} - - - {getString('private')} - - {getString('createRepoModal.privateLabel')} - - - - - - ), - value: RepoVisibility.PRIVATE - } - ]} - /> - - = ({ const ModalComponent: React.FC = () => { const { getString } = useStrings() const [branchName, setBranchName] = useState(DEFAULT_BRANCH_NAME) + const [enablePublicRepo, setEnablePublicRepo] = useState(false) const { showError } = useToaster() const { mutate: createRepo, loading: submitLoading } = useMutate({ @@ -141,13 +143,31 @@ export const NewRepoModalButton: React.FC = ({ loading: licenseLoading, error: licenseError } = useGet({ path: '/api/v1/resources/license' }) - const loading = submitLoading || gitIgnoreLoading || licenseLoading || importRepoLoading || submitImportLoading + const { + data: systemConfig, + loading: systemConfigLoading, + error: systemConfigError + } = useGet({ path: 'api/v1/system/config' }) + + const loading = + submitLoading || + gitIgnoreLoading || + licenseLoading || + importRepoLoading || + submitImportLoading || + systemConfigLoading useEffect(() => { - if (gitIgnoreError || licenseError) { - showError(getErrorMessage(gitIgnoreError || licenseError), 0) + if (gitIgnoreError || licenseError || systemConfigError) { + showError(getErrorMessage(gitIgnoreError || licenseError || systemConfigError), 0) } - }, [gitIgnoreError, licenseError, showError]) + }, [gitIgnoreError, licenseError, systemConfigError, showError]) + + useEffect(() => { + if (systemConfig) { + setEnablePublicRepo(systemConfig.public_resource_creation_enabled) + } + }, [systemConfig]) const handleSubmit = (formData: RepoFormData) => { try { const payload: OpenapiCreateRepositoryRequest = { @@ -309,54 +329,56 @@ export const NewRepoModalButton: React.FC = ({ {getString('createRepoModal.branch')} -
- - - - - - - {getString('public')} - - {getString('createRepoModal.publicLabel')} - - - - - - ), - value: RepoVisibility.PUBLIC - }, - { - label: ( - - - - - - {/* */} - - - {getString('private')} - - {getString('createRepoModal.privateLabel')} - - - - - - ), - value: RepoVisibility.PRIVATE - } - ]} - /> - + +
+ + + + + + + {getString('public')} + + {getString('createRepoModal.publicLabel')} + + + + + + ), + value: RepoVisibility.PUBLIC + }, + { + label: ( + + + + + + {/* */} + + + {getString('private')} + + {getString('createRepoModal.privateLabel')} + + + + + + ), + value: RepoVisibility.PRIVATE + } + ]} + /> + +

{ const currRepoVisibility = repoMetadata?.is_public === true ? RepoVisibility.PUBLIC : RepoVisibility.PRIVATE const [repoVis, setRepoVis] = useState(currRepoVisibility) + const [enablePublicRepo, setEnablePublicRepo] = useState(false) const { mutate } = useMutate({ verb: 'PATCH', path: `/api/v1/repos/${repoMetadata?.path}/+/` @@ -87,6 +89,14 @@ const GeneralSettingsContent = (props: GeneralSettingsProps) => { }, [space] ) + const { data: systemConfig } = useGet({ path: 'api/v1/system/config' }) + + useEffect(() => { + if (systemConfig) { + setEnablePublicRepo(systemConfig.public_resource_creation_enabled) + } + }, [systemConfig]) + const ModalComponent: React.FC = () => { return ( {
- - - - - {getString('repoVisibility')} - - - - { - setRepoVis((evt.target as HTMLInputElement).value as RepoVisibility) - }} - className={css.radioContainer} - items={[ - { - label: ( - - - - - - {getString('public')} - - {getString('createRepoModal.publicLabel')} - - - - - - ), + + + + + + {getString('repoVisibility')} + + + + { + setRepoVis((evt.target as HTMLInputElement).value as RepoVisibility) + }} + className={css.radioContainer} + items={[ + { + label: ( + + + + + + {getString('public')} + + {getString('createRepoModal.publicLabel')} + + + + + + ), - value: RepoVisibility.PUBLIC - }, - { - label: ( - - - - - - - - {getString('private')} - - {getString('createRepoModal.privateLabel')} - - - - - - ), - value: RepoVisibility.PRIVATE - } - ]} - /> -
- - {repoVis !== currRepoVisibility ? ( -