Skip to content

Commit 9b729f7

Browse files
committed
refactor(ui): Extract function to convert plugin payload
Extract the function that was used for the advisor plugins to make it reusable for other plugin types. Signed-off-by: Martin Nonnenmacher <[email protected]>
1 parent dbf03e0 commit 9b729f7

File tree

1 file changed

+59
-57
lines changed
  • ui/src/routes/organizations/$orgId/products/$productId/repositories/$repoId/_repo-layout/create-run

1 file changed

+59
-57
lines changed

ui/src/routes/organizations/$orgId/products/$productId/repositories/$repoId/_repo-layout/create-run/-create-run-utils.ts

Lines changed: 59 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,64 @@ export function defaultValues(
556556
: baseDefaults;
557557
}
558558

559+
/**
560+
* Convert the plugin config from form values to the payload format expected by the back-end. Configuration for plugins
561+
* which are not enabled is not included in the payload.
562+
*/
563+
function createPluginPayload(
564+
config: Record<string, unknown> | undefined,
565+
enabledPlugins: string[]
566+
): { [key: string]: PluginConfig } | undefined {
567+
if (!config) return undefined;
568+
569+
const filtered = Object.fromEntries(
570+
Object.entries(config)
571+
.filter(([key]) => enabledPlugins.includes(key))
572+
.map(([key, value]) => {
573+
if (value && typeof value === 'object') {
574+
const pluginConfig = value as Record<string, unknown>;
575+
const convertedConfig: PluginConfig = {
576+
options: {},
577+
secrets: {},
578+
};
579+
580+
if (
581+
pluginConfig.options &&
582+
typeof pluginConfig.options === 'object'
583+
) {
584+
convertedConfig.options = Object.fromEntries(
585+
Object.entries(pluginConfig.options as Record<string, unknown>)
586+
.filter(
587+
([, optValue]) => optValue !== undefined && optValue !== null
588+
)
589+
.map(([optKey, optValue]) => [optKey, String(optValue)])
590+
);
591+
}
592+
593+
if (
594+
pluginConfig.secrets &&
595+
typeof pluginConfig.secrets === 'object'
596+
) {
597+
convertedConfig.secrets = Object.fromEntries(
598+
Object.entries(pluginConfig.secrets as Record<string, unknown>)
599+
.filter(
600+
([, secValue]) => secValue !== undefined && secValue !== null
601+
)
602+
.map(([secKey, secValue]) => [secKey, String(secValue)])
603+
);
604+
}
605+
606+
return [key, convertedConfig];
607+
}
608+
return [key, value];
609+
})
610+
);
611+
612+
return Object.keys(filtered).length > 0
613+
? (filtered as { [key: string]: PluginConfig })
614+
: undefined;
615+
}
616+
559617
/**
560618
* Due to API schema and requirements for the form schema, the form values can't be directly passed
561619
* to the API. This function converts form values to correct payload to create an ORT run.
@@ -661,67 +719,11 @@ export function formValuesToPayload(
661719
// Advisor configuration
662720
//
663721

664-
function createAdvisorPluginConfig(
665-
config: Record<string, unknown> | undefined,
666-
advisors: string[]
667-
): { [key: string]: PluginConfig } | undefined {
668-
if (!config) return undefined;
669-
670-
const filtered = Object.fromEntries(
671-
Object.entries(config)
672-
.filter(([key]) => advisors.includes(key))
673-
.map(([key, value]) => {
674-
if (value && typeof value === 'object') {
675-
const pluginConfig = value as Record<string, unknown>;
676-
const convertedConfig: PluginConfig = {
677-
options: {},
678-
secrets: {},
679-
};
680-
681-
if (
682-
pluginConfig.options &&
683-
typeof pluginConfig.options === 'object'
684-
) {
685-
convertedConfig.options = Object.fromEntries(
686-
Object.entries(pluginConfig.options as Record<string, unknown>)
687-
.filter(
688-
([, optValue]) =>
689-
optValue !== undefined && optValue !== null
690-
)
691-
.map(([optKey, optValue]) => [optKey, String(optValue)])
692-
);
693-
}
694-
695-
if (
696-
pluginConfig.secrets &&
697-
typeof pluginConfig.secrets === 'object'
698-
) {
699-
convertedConfig.secrets = Object.fromEntries(
700-
Object.entries(pluginConfig.secrets as Record<string, unknown>)
701-
.filter(
702-
([, secValue]) =>
703-
secValue !== undefined && secValue !== null
704-
)
705-
.map(([secKey, secValue]) => [secKey, String(secValue)])
706-
);
707-
}
708-
709-
return [key, convertedConfig];
710-
}
711-
return [key, value];
712-
})
713-
);
714-
715-
return Object.keys(filtered).length > 0
716-
? (filtered as { [key: string]: PluginConfig })
717-
: undefined;
718-
}
719-
720722
const advisorConfig = values.jobConfigs.advisor.enabled
721723
? {
722724
skipExcluded: values.jobConfigs.advisor.skipExcluded,
723725
advisors: values.jobConfigs.advisor.advisors,
724-
config: createAdvisorPluginConfig(
726+
config: createPluginPayload(
725727
values.jobConfigs.advisor.config,
726728
values.jobConfigs.advisor.advisors
727729
),

0 commit comments

Comments
 (0)