Skip to content

Commit

Permalink
feat(utils): sort projects in report-diff.md by most changed
Browse files Browse the repository at this point in the history
  • Loading branch information
matejchalk committed Aug 16, 2024
1 parent 6c5ba40 commit 40d2476
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 9 deletions.
28 changes: 28 additions & 0 deletions packages/utils/src/lib/reports/generate-md-reports-diff-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,31 @@ function countDiffOutcomes(
unchanged: outcomes.filter(outcome => outcome === 'unchanged').length,
};
}

export function compareDiffsBy<T extends 'categories' | 'groups' | 'audits'>(
type: T,
a: ReportsDiff,
b: ReportsDiff,
): number {
return (
sumScoreChanges(b[type].changed) - sumScoreChanges(a[type].changed) ||
sumConfigChanges(b[type]) - sumConfigChanges(a[type])
);
}

function sumScoreChanges(changes: Change[]): number {
return changes.reduce<number>(
(acc, { scores }) => acc + Math.abs(scores.diff),
0,
);
}

function sumConfigChanges({
added,
removed,
}: {
added: unknown[];
removed: unknown[];
}): number {
return added.length + removed.length;
}
26 changes: 17 additions & 9 deletions packages/utils/src/lib/reports/generate-md-reports-diff.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { HIERARCHY } from '../text-formats';
import { toArray } from '../transform';
import {
changesToDiffOutcomes,
compareDiffsBy,
createGroupsOrAuditsDetails,
formatPortalLink,
formatTitle,
Expand Down Expand Up @@ -53,15 +54,22 @@ export type ProjectDiffWithOutcome = ProjectDiff & {
export function generateMdReportsDiffForMonorepo(
projects: ProjectDiff[],
): string {
// TODO: sort projects (most changed, alphabetical)
const projectsWithOutcomes = projects.map(
(project): ProjectDiffWithOutcome => ({
...project,
outcome: mergeDiffOutcomes(
changesToDiffOutcomes(getDiffChanges(project.diff)),
),
}),
);
const projectsWithOutcomes = projects
.map(
(project): ProjectDiffWithOutcome => ({
...project,
outcome: mergeDiffOutcomes(
changesToDiffOutcomes(getDiffChanges(project.diff)),
),
}),
)
.sort(
(a, b) =>
compareDiffsBy('categories', a.diff, b.diff) ||
compareDiffsBy('groups', a.diff, b.diff) ||
compareDiffsBy('audits', a.diff, b.diff) ||
a.name.localeCompare(b.name),
);
const unchanged = projectsWithOutcomes.filter(
({ outcome }) => outcome === 'unchanged',
);
Expand Down

0 comments on commit 40d2476

Please sign in to comment.