Skip to content

Commit dbf03e0

Browse files
committed
refactor(ui): Extract function to get defaults for plugin options
The function will be reused for other plugin types. Signed-off-by: Martin Nonnenmacher <[email protected]>
1 parent b184213 commit dbf03e0

File tree

1 file changed

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

1 file changed

+26
-22
lines changed

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

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,29 @@ function mergePluginConfigs(
313313
return merged;
314314
}
315315

316+
function getPluginDefaultValues(plugins: PreconfiguredPluginDescriptor[]) {
317+
return plugins.reduce(
318+
(acc, plugin) => {
319+
const options: Record<string, string> = {};
320+
const secrets: Record<string, string> = {};
321+
322+
plugin.options?.forEach((option) => {
323+
if (option.defaultValue !== undefined) {
324+
if (option.type === 'SECRET') {
325+
secrets[option.name] = String(option.defaultValue);
326+
} else {
327+
options[option.name] = String(option.defaultValue);
328+
}
329+
}
330+
});
331+
332+
acc[plugin.id] = { options: options, secrets: secrets };
333+
return acc;
334+
},
335+
{} as Record<string, PluginConfig>
336+
);
337+
}
338+
316339
/**
317340
* Get the default values for the create run form. The form can be provided with a previously run
318341
* ORT run, in which case the values from it are used as defaults. Otherwise uses base defaults.
@@ -367,26 +390,7 @@ export function defaultValues(
367390
})
368391
: false;
369392

370-
const advisorDefaultConfig = advisorPlugins.reduce(
371-
(acc, plugin) => {
372-
const options: Record<string, string> = {};
373-
const secrets: Record<string, string> = {};
374-
375-
plugin.options?.forEach((option) => {
376-
if (option.defaultValue !== undefined) {
377-
if (option.type === 'SECRET') {
378-
secrets[option.name] = String(option.defaultValue);
379-
} else {
380-
options[option.name] = String(option.defaultValue);
381-
}
382-
}
383-
});
384-
385-
acc[plugin.id] = { options: options, secrets: secrets };
386-
return acc;
387-
},
388-
{} as Record<string, PluginConfig>
389-
);
393+
const advisorPluginDefaultValues = getPluginDefaultValues(advisorPlugins);
390394

391395
// Default values for the form: edit only these, not the defaultValues object.
392396
const baseDefaults = {
@@ -430,7 +434,7 @@ export function defaultValues(
430434
enabled: true,
431435
skipExcluded: true,
432436
advisors: ['OSV', 'VulnerableCode'],
433-
config: advisorDefaultConfig,
437+
config: advisorPluginDefaultValues,
434438
},
435439
scanner: {
436440
enabled: true,
@@ -496,7 +500,7 @@ export function defaultValues(
496500
baseDefaults.jobConfigs.advisor.advisors,
497501
config: mergePluginConfigs(
498502
ortRun?.jobConfigs?.advisor?.config,
499-
advisorDefaultConfig
503+
advisorPluginDefaultValues
500504
),
501505
},
502506
scanner: {

0 commit comments

Comments
 (0)