Skip to content

Commit

Permalink
feat(utils): add sorting of audit issues for report md
Browse files Browse the repository at this point in the history
Closes #313

#313
  • Loading branch information
MishaSeredenkoPushBased committed Dec 11, 2023
1 parent 8047ef7 commit b1c5dd7
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
3 changes: 2 additions & 1 deletion packages/utils/src/lib/report-to-md.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import {
reportHeadlineText,
reportMetaTableHeaders,
reportOverviewTableHeaders,
sortAuditIssues,
sortAudits,
sortCategoryAudits,
} from './report';
Expand Down Expand Up @@ -216,7 +217,7 @@ function reportToAuditsSection(report: ScoredReport): string {

const detailsTableData = [
detailsTableHeaders,
...audit.details.issues.map((issue: Issue) => {
...audit.details.issues.sort(sortAuditIssues).map((issue: Issue) => {
const severity = `${getSeverityIcon(issue.severity)} <i>${
issue.severity
}</i>`;
Expand Down
20 changes: 20 additions & 0 deletions packages/utils/src/lib/report.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
CategoryRef,
IssueSeverity as CliIssueSeverity,
Format,
Issue,
PersistConfig,
Report,
reportSchema,
Expand Down Expand Up @@ -278,3 +279,22 @@ export function getPluginNameFromSlug(
plugins.find(({ slug: pluginSlug }) => pluginSlug === slug)?.title || slug
);
}

export function sortAuditIssues(a: Issue, b: Issue): number {
if (a.severity !== b.severity) {
return -compareIssueSeverity(a.severity, b.severity);
}

if (a.source?.file !== b.source?.file) {
return a.source?.file.localeCompare(b.source?.file || '') || 0;
}

if (a.source?.position?.startLine !== b.source?.position?.startLine) {
return (
(a.source?.position?.startLine || 0) -
(b.source?.position?.startLine || 0)
);
}

return 0;
}
42 changes: 41 additions & 1 deletion packages/utils/src/lib/report.unit.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import { vol } from 'memfs';
import { afterEach, describe, expect, it, vi } from 'vitest';
import { CategoryRef, IssueSeverity, PluginReport } from '@code-pushup/models';
import {
CategoryRef,
Issue,
IssueSeverity,
PluginReport,
} from '@code-pushup/models';
import { MEMFS_VOLUME, report } from '@code-pushup/models/testing';
import {
calcDuration,
compareIssueSeverity,
countWeightedRefs,
getPluginNameFromSlug,
loadReport,
sortAuditIssues,
sortAudits,
sortCategoryAudits,
} from './report';
Expand Down Expand Up @@ -223,3 +229,37 @@ describe('getPluginNameFromSlug', () => {
expect(getPluginNameFromSlug('plugin-b', plugins)).toBe('Plugin B');
});
});

describe('sortAuditIssues', () => {
it('should sort issues by severity and source file', () => {
const mockIssues = [
{ severity: 'warning', source: { file: 'b' } },
{ severity: 'error', source: { file: 'c' } },
{ severity: 'error', source: { file: 'a' } },
{ severity: 'info', source: { file: 'b' } },
] as Issue[];
const sortedIssues = [...mockIssues].sort(sortAuditIssues);
expect(sortedIssues).toEqual([
{ severity: 'error', source: { file: 'a' } },
{ severity: 'error', source: { file: 'c' } },
{ severity: 'warning', source: { file: 'b' } },
{ severity: 'info', source: { file: 'b' } },
]);
});

it('should sort issues by source file and source start line', () => {
const mockIssues = [
{ severity: 'info', source: { file: 'b', position: { startLine: 2 } } },
{ severity: 'info', source: { file: 'c', position: { startLine: 1 } } },
{ severity: 'info', source: { file: 'a', position: { startLine: 2 } } },
{ severity: 'info', source: { file: 'b', position: { startLine: 1 } } },
] as Issue[];
const sortedIssues = [...mockIssues].sort(sortAuditIssues);
expect(sortedIssues).toEqual([
{ severity: 'info', source: { file: 'a', position: { startLine: 2 } } },
{ severity: 'info', source: { file: 'b', position: { startLine: 1 } } },
{ severity: 'info', source: { file: 'b', position: { startLine: 2 } } },
{ severity: 'info', source: { file: 'c', position: { startLine: 1 } } },
]);
});
});

0 comments on commit b1c5dd7

Please sign in to comment.