diff --git a/packages/utils/src/lib/reports/generate-md-reports-diff-utils.ts b/packages/utils/src/lib/reports/generate-md-reports-diff-utils.ts index 00a36f1cb..512e433fb 100644 --- a/packages/utils/src/lib/reports/generate-md-reports-diff-utils.ts +++ b/packages/utils/src/lib/reports/generate-md-reports-diff-utils.ts @@ -152,3 +152,31 @@ function countDiffOutcomes( unchanged: outcomes.filter(outcome => outcome === 'unchanged').length, }; } + +export function compareDiffsBy( + 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( + (acc, { scores }) => acc + Math.abs(scores.diff), + 0, + ); +} + +function sumConfigChanges({ + added, + removed, +}: { + added: unknown[]; + removed: unknown[]; +}): number { + return added.length + removed.length; +} diff --git a/packages/utils/src/lib/reports/generate-md-reports-diff.ts b/packages/utils/src/lib/reports/generate-md-reports-diff.ts index bf0943daa..bd3b718e5 100644 --- a/packages/utils/src/lib/reports/generate-md-reports-diff.ts +++ b/packages/utils/src/lib/reports/generate-md-reports-diff.ts @@ -10,6 +10,7 @@ import { HIERARCHY } from '../text-formats'; import { toArray } from '../transform'; import { changesToDiffOutcomes, + compareDiffsBy, createGroupsOrAuditsDetails, formatPortalLink, formatTitle, @@ -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', );