diff --git a/src/components/CreatePipeline.tsx b/src/components/CreatePipeline.tsx index 71dad6bd41586ddc870257543339d638693029a9..e1c674979e176faafb6cb64ae48429fb11b86157 100644 --- a/src/components/CreatePipeline.tsx +++ b/src/components/CreatePipeline.tsx @@ -1,6 +1,6 @@ 'use client'; -import { Dispatch, SetStateAction, useState } from 'react'; +import { ChangeEvent, Dispatch, SetStateAction, useState } from 'react'; import { GenState, TestState, getEnabledElements, getValidSelectedClusters } from '@/lib/generation'; import { AppRouterInstance } from 'next/dist/shared/lib/app-router-context.shared-runtime'; @@ -13,6 +13,7 @@ import RemoteBranchSelector from './RemoteBranchSelector'; import Tab from '@mui/material/Tab'; import Tabs from '@mui/material/Tabs'; import TestImagesTabContent from './TestImagesTabContent'; +import TextField from '@mui/material/TextField'; import config from '@/lib/config'; import { useAuth } from 'react-oidc-context'; @@ -56,6 +57,7 @@ type Pipeline = { // FIXME: can we do something like typeof(useRouter()) ? function createPipelineAction(generations: GenState, clusters: TestState, ref: string, refApiBranch: string, postinstallBranch: string | null, + envVersion: string, token: string | undefined, router: AppRouterInstance, setLoading: Dispatch<SetStateAction<boolean>>) { @@ -70,6 +72,7 @@ function createPipelineAction(generations: GenState, clusters: TestState, // that doesn't match, to avoid this hack. { key: 'ENVIRONMENTS', value: 'none'}, { key: 'REFREPO_BRANCH', value: refApiBranch}, + { key: 'ENV_VERSION', value: envVersion}, { key: 'ENVIRONMENTS_LIST', value: getEnabledElements(generations).join(',')}, { key: 'CLUSTERS', value: getValidSelectedClusters(generations, clusters).join(',')}, ] @@ -94,18 +97,33 @@ function createPipelineAction(generations: GenState, clusters: TestState, }); } +function createCurrentVersion() { + const pad = (n: number): string => n < 10 ? `0${n}` : `${n}`; + const currentDate = new Date(); + const month = pad(currentDate.getMonth() + 1); + const date = pad(currentDate.getDate()); + const hour = pad(currentDate.getHours()); + return `${currentDate.getFullYear()}${month}${date}${hour}`; +} + +function validVersion(date: string): boolean { + return date.match(/\d{10}/) !== null; +} + function CreatePipelineButton({ generations, clusters, branch, refApiBranch, postinstallBranch, + envVersion, }: { branch: string | null, refApiBranch: string | null, postinstallBranch: string | null, generations: GenState, clusters: TestState, + envVersion: string, }) { const enabledEnvs = getEnabledElements(generations); const auth = useAuth(); @@ -116,13 +134,15 @@ function CreatePipelineButton({ component="label" role={undefined} variant="contained" - disabled={enabledEnvs.length === 0 || branch === null || refApiBranch === null || loading} + disabled={enabledEnvs.length === 0 || branch === null || refApiBranch === null || loading || !validVersion(envVersion)} color="success" onClick={() => { if (branch === null || refApiBranch === null) { return; } - createPipelineAction(generations, clusters, branch, refApiBranch, postinstallBranch, auth.user?.access_token, router, setLoading) + createPipelineAction(generations, clusters, branch, refApiBranch, + postinstallBranch, envVersion, + auth.user?.access_token, router, setLoading); }} sx={{ ml: 'auto' }} > @@ -153,6 +173,8 @@ export default function CreatePipeline() { const [branch, setBranch] = useState<string | null>(null); const [refApiBranch, setRefApiBranch] = useState<string | null>(null); const [postinstallBranch, setPostinstallBranch] = useState<string | null>(null); + const [envVersion, setEnvVersion] = useState<string>(createCurrentVersion()); + const validEnvVersion = validVersion(envVersion); return ( <Container maxWidth="xl"> @@ -166,6 +188,13 @@ export default function CreatePipeline() { useDefault /> </Box> + <TextField + value={envVersion} + error={!validEnvVersion} + helperText={validEnvVersion ? null : 'Version must match YYYYMMDDHH'} + onChange={(ev: ChangeEvent<HTMLInputElement>) => setEnvVersion(ev.target.value)} + label="Environment version" + /> <Box sx={{ borderBottom: 1, borderColor: 'divider', mb: 2 }}> <Tabs value={value} onChange={handleChange}> <Tab label="Generation" /> @@ -176,6 +205,7 @@ export default function CreatePipeline() { postinstallBranch={postinstallBranch} generations={generations} clusters={clusters} + envVersion={envVersion} /> </Tabs> </Box>