mirror of https://github.com/harness/drone.git
feat: [CDE-255]: Use Query Params to populate create gitspace URL , repoType and branch (#2593)
* feat: [CDE-255]: UI lint fix * feat: [CDE-255]: Use Query Params to populate create gitspace URL , repoType and branchpull/3545/head
parent
7af28da479
commit
219c2142f4
|
@ -27,6 +27,8 @@ import type { OpenapiCreateGitspaceRequest } from 'cde-gitness/services'
|
|||
import { BranchInput } from 'cde-gitness/components/BranchInput/BranchInput'
|
||||
import { useRepoLookupForGitspace } from 'services/cde'
|
||||
import { useGetCDEAPIParams } from 'cde-gitness/hooks/useGetCDEAPIParams'
|
||||
import type { RepoQueryParams } from 'cde-gitness/pages/GitspaceCreate/CDECreateGitspace'
|
||||
import { useQueryParams } from 'hooks/useQueryParams'
|
||||
import { getRepoIdFromURL, getRepoNameFromURL, isValidUrl } from './CDEAnyGitImport.utils'
|
||||
import css from './CDEAnyGitImport.module.scss'
|
||||
|
||||
|
@ -37,6 +39,7 @@ enum RepoCheckStatus {
|
|||
|
||||
export const CDEAnyGitImport = () => {
|
||||
const { getString } = useStrings()
|
||||
const repoQueryParams = useQueryParams<RepoQueryParams>()
|
||||
const { setValues, setFieldError, values } = useFormikContext<OpenapiCreateGitspaceRequest>()
|
||||
const { accountIdentifier = '', orgIdentifier = '', projectIdentifier = '' } = useGetCDEAPIParams()
|
||||
|
||||
|
@ -54,8 +57,14 @@ export const CDEAnyGitImport = () => {
|
|||
}
|
||||
}, [values?.code_repo_type])
|
||||
|
||||
useEffect(() => {
|
||||
if (values.code_repo_url === repoQueryParams.codeRepoURL && repoQueryParams.codeRepoURL) {
|
||||
onChange(repoQueryParams.codeRepoURL as string, Boolean(repoQueryParams.branch))
|
||||
}
|
||||
}, [values.code_repo_url, repoQueryParams.codeRepoURL])
|
||||
|
||||
const onChange = useCallback(
|
||||
debounce(async (url: string) => {
|
||||
debounce(async (url: string, skipBranchUpdate?: boolean) => {
|
||||
let errorMessage = ''
|
||||
try {
|
||||
if (isValidUrl(url)) {
|
||||
|
@ -81,11 +90,12 @@ export const CDEAnyGitImport = () => {
|
|||
setFieldError('code_repo_url', errorMessage)
|
||||
}, 500)
|
||||
} else {
|
||||
const branchValue = skipBranchUpdate ? {} : { branch: response.branch }
|
||||
setValues((prvValues: any) => {
|
||||
return {
|
||||
...prvValues,
|
||||
code_repo_url: response.url,
|
||||
branch: response.branch,
|
||||
...branchValue,
|
||||
identifier: getRepoIdFromURL(response.url),
|
||||
name: getRepoNameFromURL(response.url),
|
||||
code_repo_type: values?.code_repo_type
|
||||
|
|
|
@ -31,6 +31,8 @@ import noRepo from 'cde-gitness/assests/noRepo.svg?url'
|
|||
import { RepoCreationType } from 'utils/GitUtils'
|
||||
import gitnessRepoLogo from 'cde-gitness/assests/gitness.svg?url'
|
||||
import { EnumGitspaceCodeRepoType } from 'cde-gitness/constants'
|
||||
import { useQueryParams } from 'hooks/useQueryParams'
|
||||
import type { RepoQueryParams } from 'cde-gitness/pages/GitspaceCreate/CDECreateGitspace'
|
||||
import { GitspaceSelect } from '../GitspaceSelect/GitspaceSelect'
|
||||
import css from './GitnessRepoImportForm.module.scss'
|
||||
|
||||
|
@ -141,6 +143,17 @@ export const GitnessRepoImportForm = ({ isCDE }: { isCDE?: boolean }) => {
|
|||
const repoListOptions = repositories || []
|
||||
const hideInitialMenu = Boolean(repoSearch) || Boolean(repositories)
|
||||
|
||||
const repoQueryParams = useQueryParams<RepoQueryParams>()
|
||||
|
||||
useEffect(() => {
|
||||
if (isCDE) {
|
||||
const repoData = repoListOptions?.find(repo => repo.git_url === repoQueryParams.codeRepoURL)
|
||||
if (!repoQueryParams.branch && repoData) {
|
||||
formik.setFieldValue('branch', repoData?.default_branch)
|
||||
}
|
||||
}
|
||||
}, [repoListOptions, isCDE])
|
||||
|
||||
const formik = useFormikContext<any>()
|
||||
|
||||
const { values } = formik
|
||||
|
|
|
@ -38,6 +38,7 @@ import { GitnessRepoImportForm } from 'cde-gitness/components/GitnessRepoImportF
|
|||
import { EnumGitspaceCodeRepoType, StandaloneIDEType } from 'cde-gitness/constants'
|
||||
import { CDESSHSelect } from 'cde-gitness/components/CDESSHSelect/CDESSHSelect'
|
||||
import { useQueryParams } from 'hooks/useQueryParams'
|
||||
import { getRepoIdFromURL, getRepoNameFromURL } from 'cde-gitness/components/CDEAnyGitImport/CDEAnyGitImport.utils'
|
||||
import { gitnessFormInitialValues } from './GitspaceCreate.constants'
|
||||
import { validateGitnessForm } from './GitspaceCreate.utils'
|
||||
import css from './GitspaceCreate.module.scss'
|
||||
|
@ -48,6 +49,14 @@ interface SCMType {
|
|||
icon: string
|
||||
}
|
||||
|
||||
export interface RepoQueryParams {
|
||||
name?: string
|
||||
identifier?: string
|
||||
branch?: string
|
||||
codeRepoURL?: string
|
||||
codeRepoType?: EnumGitspaceCodeRepoType
|
||||
}
|
||||
|
||||
export const CDECreateGitspace = () => {
|
||||
const { getString } = useStrings()
|
||||
const { routes, currentUserProfileURL, hooks, currentUser } = useAppContext()
|
||||
|
@ -57,9 +66,9 @@ export const CDECreateGitspace = () => {
|
|||
const { accountIdentifier = '', orgIdentifier = '', projectIdentifier = '' } = useGetCDEAPIParams()
|
||||
const { showSuccess, showError } = useToaster()
|
||||
const { mutate } = useCreateGitspace({ accountIdentifier, orgIdentifier, projectIdentifier })
|
||||
const { idpRepoURL = '' } = useQueryParams<{ idpRepoURL?: string }>()
|
||||
const repoQueryParams = useQueryParams<RepoQueryParams>()
|
||||
|
||||
const [repoURLviaQueryParam, setrepoURLviaQueryParam] = useState('')
|
||||
const [repoURLviaQueryParam, setrepoURLviaQueryParam] = useState<RepoQueryParams>({ ...repoQueryParams })
|
||||
|
||||
const scmOptions: SCMType[] = [
|
||||
{ name: 'Harness Code', value: EnumGitspaceCodeRepoType.HARNESS_CODE, icon: harnessCode },
|
||||
|
@ -74,15 +83,36 @@ export const CDECreateGitspace = () => {
|
|||
})
|
||||
|
||||
useEffect(() => {
|
||||
if (idpRepoURL !== repoURLviaQueryParam) {
|
||||
setrepoURLviaQueryParam(idpRepoURL)
|
||||
const { codeRepoURL, codeRepoType, branch: queryParamBranch } = repoQueryParams
|
||||
if (codeRepoURL !== repoURLviaQueryParam.codeRepoURL && codeRepoType !== repoURLviaQueryParam.codeRepoType) {
|
||||
setrepoURLviaQueryParam(prv => {
|
||||
return {
|
||||
...prv,
|
||||
name: getRepoNameFromURL(codeRepoURL),
|
||||
identifier: getRepoIdFromURL(codeRepoURL),
|
||||
branch: queryParamBranch,
|
||||
codeRepoURL,
|
||||
codeRepoType
|
||||
}
|
||||
})
|
||||
}
|
||||
}, [idpRepoURL])
|
||||
}, [repoQueryParams])
|
||||
|
||||
const oauthSCMsListTypes =
|
||||
OauthSCMs?.data?.userSourceCodeManagerResponseDTOList?.map((item: { type: string }) => item.type.toLowerCase()) ||
|
||||
[]
|
||||
|
||||
const includeQueryParams =
|
||||
repoQueryParams?.codeRepoURL && repoQueryParams?.codeRepoType
|
||||
? {
|
||||
code_repo_url: repoQueryParams.codeRepoURL,
|
||||
branch: repoQueryParams.branch,
|
||||
identifier: getRepoIdFromURL(repoQueryParams.codeRepoURL),
|
||||
name: getRepoNameFromURL(repoQueryParams.codeRepoURL),
|
||||
code_repo_type: repoQueryParams.codeRepoType
|
||||
}
|
||||
: {}
|
||||
|
||||
return (
|
||||
<Formik
|
||||
onSubmit={async data => {
|
||||
|
@ -108,8 +138,8 @@ export const CDECreateGitspace = () => {
|
|||
}}
|
||||
initialValues={{
|
||||
...gitnessFormInitialValues,
|
||||
code_repo_url: idpRepoURL,
|
||||
code_repo_type: EnumGitspaceCodeRepoType.HARNESS_CODE
|
||||
code_repo_type: EnumGitspaceCodeRepoType.HARNESS_CODE,
|
||||
...includeQueryParams
|
||||
}}
|
||||
validationSchema={validateGitnessForm(getString)}
|
||||
formName="importRepoForm"
|
||||
|
|
Loading…
Reference in New Issue