Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle skipped audits and groups #911

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 64 additions & 5 deletions packages/cli/src/lib/implementation/filter.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,54 @@
import type { CoreConfig } from '@code-pushup/models';
import type {
CategoryConfig,
CoreConfig,
PluginConfig,
} from '@code-pushup/models';
import { filterItemRefsBy } from '@code-pushup/utils';
import type { FilterOptions, Filterables } from './filter.model.js';
import {
handleConflictingOptions,
isValidCategoryRef,
validateFilterOption,
validateFilteredCategories,
validateFinalState,
} from './validate-filter-options.utils.js';

// eslint-disable-next-line max-lines-per-function
export function filterMiddleware<T extends FilterOptions>(
originalProcessArgs: T,
): T {
const {
plugins,
categories,
categories: rcCategories,
plugins: rcPlugins,
skipCategories = [],
onlyCategories = [],
skipPlugins = [],
onlyPlugins = [],
verbose = false,
} = originalProcessArgs;

const plugins = processPlugins(rcPlugins);
const categories = filterSkippedCategories(rcCategories, plugins);

if (rcCategories && categories) {
validateFilteredCategories(rcCategories, categories, {
onlyCategories,
skipCategories,
verbose,
});
}

if (
skipCategories.length === 0 &&
onlyCategories.length === 0 &&
skipPlugins.length === 0 &&
onlyPlugins.length === 0
) {
return originalProcessArgs;
return {
...originalProcessArgs,
...(categories && { categories }),
plugins,
};
}

handleConflictingOptions('categories', onlyCategories, skipCategories);
Expand All @@ -52,7 +74,7 @@ export function filterMiddleware<T extends FilterOptions>(

validateFinalState(
{ categories: finalCategories, plugins: filteredPlugins },
{ categories, plugins },
{ categories: rcCategories, plugins: rcPlugins },
);

return {
Expand Down Expand Up @@ -141,3 +163,40 @@ function filterPluginsFromCategories({
);
return plugins.filter(plugin => validPluginSlugs.has(plugin.slug));
}

function filterSkippedItems<T extends { isSkipped?: boolean }>(
items: T[] | undefined,
): Omit<T, 'isSkipped'>[] {
return (items ?? [])
.filter(({ isSkipped }) => isSkipped !== true)
.map(({ isSkipped, ...props }) => props);
}

export function processPlugins(plugins: PluginConfig[]): PluginConfig[] {
return plugins.map((plugin: PluginConfig) => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The name processPlugins isn't very self-explanatory:

Suggested change
export function processPlugins(plugins: PluginConfig[]): PluginConfig[] {
export function filterSkippedInPlugins(plugins: PluginConfig[]): PluginConfig[] {

const filteredAudits = filterSkippedItems(plugin.audits);
return {
...plugin,
...(plugin.groups && {
groups: filterItemRefsBy(filterSkippedItems(plugin.groups), ref =>
filteredAudits.some(({ slug }) => slug === ref.slug),
),
}),
audits: filteredAudits,
};
});
}

export function filterSkippedCategories(
categories: CoreConfig['categories'],
plugins: CoreConfig['plugins'],
): CoreConfig['categories'] {
return categories
?.map(category => {
const validRefs = category.refs.filter(ref =>
isValidCategoryRef(ref, plugins),
);
return validRefs.length > 0 ? { ...category, refs: validRefs } : null;
})
.filter((category): category is CategoryConfig => category != null);
}
Loading
Loading