Skip to content

Commit

Permalink
feat(ui): Add mustRunAfter to analyzer configuration
Browse files Browse the repository at this point in the history
Add the config parameter and the corresponding UI to select the 
package managers that should be run preceding this.

Signed-off-by: Jyrki Keisala <[email protected]>
  • Loading branch information
Etsija committed Jan 15, 2025
1 parent 1d2eac5 commit d69f427
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import { PlusIcon, TrashIcon } from 'lucide-react';
import { useFieldArray, UseFormReturn } from 'react-hook-form';

import { MultiSelectField } from '@/components/form/multi-select-field';
import {
Accordion,
AccordionContent,
Expand Down Expand Up @@ -261,7 +262,7 @@ const FieldWithOptions = ({ form, pmIndex, pmName }: FieldWithOptionsProps) => {
))}
<Button
size='sm'
className='mt-2'
className='mb-4 mt-2'
variant='outline'
type='button'
onClick={() => {
Expand All @@ -271,6 +272,13 @@ const FieldWithOptions = ({ form, pmIndex, pmName }: FieldWithOptionsProps) => {
Add option
<PlusIcon className='ml-1 h-4 w-4' />
</Button>
<MultiSelectField
form={form}
name={`jobConfigs.analyzer.packageManagers.${pmName}.mustRunAfter`}
label='Must run after'
description='A list of package manager names that this package manager must run after. For example, this can be used, if another package manager generates files that this package manager requires to run correctly.'
options={packageManagers.filter((pm) => pm.id !== pmName)}
/>
<Separator className='my-2' />
</AccordionContent>
</AccordionItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@ const keyValueSchema = z.object({

const packageManagerOptionsSchema = z.object({
enabled: z.boolean(),
mustRunAfter: z
.array(
z
.string()
// Only accept valid package manager IDs to the string array in the form.
.refine((pkgMgr): pkgMgr is PackageManagerId =>
packageManagers.some((pm) => pm.id === pkgMgr)
)
)
.optional(),
options: z.array(keyValueSchema).optional(),
});

Expand Down Expand Up @@ -237,17 +247,21 @@ export function defaultValues(
if (ortRun) {
return {
enabled:
ortRun?.jobConfigs.analyzer?.enabledPackageManagers?.includes(
ortRun.jobConfigs.analyzer?.enabledPackageManagers?.includes(
packageManagerId
) || false,
mustRunAfter:
(ortRun.jobConfigs.analyzer?.packageManagerOptions?.[packageManagerId]
?.mustRunAfter as PackageManagerId[]) || [],
options: convertMapToArray(
ortRun?.jobConfigs.analyzer?.packageManagerOptions?.[packageManagerId]
ortRun.jobConfigs.analyzer?.packageManagerOptions?.[packageManagerId]
?.options || {}
),
};
}
return {
enabled: enabledByDefault,
mustRunAfter: [],
options: [],
};
};
Expand Down Expand Up @@ -537,12 +551,27 @@ export function formValuesToPayload(
packageManagers: typeof values.jobConfigs.analyzer.packageManagers
) => {
const options = Object.entries(packageManagers)
.filter(([, pm]) => pm.enabled && pm.options && pm.options.length > 0)
.map(([pmId, pm]) => ({
[pmId]: {
options: convertArrayToMap(pm.options || []),
},
}))
.filter(
// Skip package managers that are not enabled or have no extra options set.
([, pm]) =>
pm.enabled &&
((pm.options && pm.options.length > 0) ||
(pm.mustRunAfter && pm.mustRunAfter.length > 0))
)
.map(([pmId, pm]) => {
// Build the filtered options object, including only non-empty properties.
const filteredOptions = {
...(pm.mustRunAfter?.length ? { mustRunAfter: pm.mustRunAfter } : {}),
...(pm.options?.length
? { options: convertArrayToMap(pm.options) }
: {}),
};
// Return the object only if it has valid options.
return Object.keys(filteredOptions).length > 0
? { [pmId]: filteredOptions }
: {};
})
// Combine all package manager objects into a single result.
.reduce((acc, pm) => ({ ...acc, ...pm }), {});
// If no options are set, return undefined.
return Object.keys(options).length > 0 ? options : undefined;
Expand Down

0 comments on commit d69f427

Please sign in to comment.