Skip to content

Commit

Permalink
Fixed pipeline env selector (#1128)
Browse files Browse the repository at this point in the history
* Fixed pipeline env selector

* Removed console log
  • Loading branch information
satr authored Nov 13, 2024
1 parent a16a978 commit f152515
Showing 1 changed file with 60 additions and 37 deletions.
97 changes: 60 additions & 37 deletions src/components/create-job-form/pipeline-form-build-branches.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,31 @@ export function PipelineFormBuildBranches({
const [branchFullName, setBranchFullName] = useState('');
const [toEnvironment, setToEnvironment] = useState('');
const branches = useGetApplicationBranches(appName);
const [filteredBranches, setFilteredBranches] = useState([]);

const handleOnTextChange = ({
target: { value },
}: ChangeEvent<HTMLInputElement>) => {
setBranch(value);
setBranchFullName(value);
setToEnvironment('');
const cleanRegex = (val: string): string => {
switch (val) {
case '*':
return '.+';
case '':
return '^$';
}
return val;
};
const values = Array.from(
new Set(
Object.entries(branches)
.filter(([key]) => new RegExp(cleanRegex(key)).test(value))
.flatMap(([, commits]) => commits)
)
);
setFilteredBranches(values);
};
const handleChange = ({
target: { value },
Expand All @@ -57,7 +76,7 @@ export function PipelineFormBuildBranches({
appName,
pipelineParametersBuild: { branch, toEnvironment },
};
let jobName = '';
let jobName: string;
if (pipelineName === 'build-deploy') {
jobName = (await triggerBuildDeploy(body).unwrap()).name;
} else {
Expand Down Expand Up @@ -122,42 +141,43 @@ export function PipelineFormBuildBranches({
</fieldset>
</>
)}
{selectedBranch && branches[selectedBranch]?.length > 1 && (
<div className="grid grid--gap-small input">
<Typography
group="input"
variant="text"
token={{ color: 'currentColor' }}
>
Environment (optional)
</Typography>
<NativeSelect
id="ToEnvironmentSelect"
label=""
name="toEnvironment"
onChange={(e) => setToEnvironment(e.target.value)}
value={toEnvironment}
>
<option value="">
All environments build from the branch{' '}
{branch ?? selectedBranch}
</option>
{branches[selectedBranch]?.map((envName) => (
<option key={envName} value={envName}>
{envName}
{!isAnyValidRegex(branch) &&
branch &&
filteredBranches?.length > 0 && (
<div className="grid grid--gap-small input">
<Typography
group="input"
variant="text"
token={{ color: 'currentColor' }}
>
Environment (optional)
</Typography>
<NativeSelect
id="ToEnvironmentSelect"
label=""
name="toEnvironment"
onChange={(e) => setToEnvironment(e.target.value)}
value={toEnvironment}
>
<option value="">
All environments build from the branch {branch}
</option>
))}
</NativeSelect>
</div>
)}
{pipelineName === 'build-deploy' && branches && selectedBranch && (
<TargetEnvs
targetEnvs={
toEnvironment ? [toEnvironment] : branches[selectedBranch]
}
branch={branch}
/>
)}
{filteredBranches?.map((envName) => (
<option key={envName} value={envName}>
{envName}
</option>
))}
</NativeSelect>
</div>
)}
{pipelineName === 'build-deploy' &&
branch &&
!isAnyValidRegex(branch) && (
<TargetEnvs
targetEnvs={toEnvironment ? [toEnvironment] : filteredBranches}
branch={branch}
/>
)}
</div>

<div className="o-action-bar">
Expand All @@ -172,7 +192,10 @@ export function PipelineFormBuildBranches({
</Alert>
)}
<div>
<Button disabled={!isValidBranchName(branch)} type="submit">
<Button
disabled={!isValidBranchName(branch) || isAnyValidRegex(branch)}
type="submit"
>
Create job
</Button>
</div>
Expand Down

0 comments on commit f152515

Please sign in to comment.