11import {
22 AuditReport ,
33 CategoryConfig ,
4+ CategoryRef ,
45 Issue ,
5- PluginReport ,
66} from '@code-pushup/models' ;
77import { CommitData } from './git' ;
88import {
@@ -24,15 +24,24 @@ import {
2424 detailsTableHeaders ,
2525 formatDuration ,
2626 formatReportScore ,
27+ getAuditByRef ,
28+ getGroupWithAudits ,
29+ getPluginNameFromSlug ,
2730 getRoundScoreMarker ,
2831 getSeverityIcon ,
2932 getSquaredScoreMarker ,
3033 pluginMetaTableHeaders ,
3134 reportHeadlineText ,
3235 reportMetaTableHeaders ,
3336 reportOverviewTableHeaders ,
37+ sortAudits ,
38+ sortCategoryAudits ,
3439} from './report' ;
35- import { EnrichedScoredAuditGroup , ScoredReport } from './scoring' ;
40+ import {
41+ EnrichedScoredAuditGroupWithAudits ,
42+ ScoredReport ,
43+ WeighedAuditReport ,
44+ } from './scoring' ;
3645import { slugify } from './transformation' ;
3746
3847export function reportToMd (
@@ -87,14 +96,37 @@ function reportToCategoriesSection(report: ScoredReport): string {
8796 ) } Score: ${ style ( formatReportScore ( category . score ) ) } `;
8897 const categoryDocs = getDocsAndDescription ( category ) ;
8998
90- const refs = category . refs . reduce ( ( acc , ref ) => {
91- if ( ref . type === 'group' ) {
92- acc += groupRefItemToCategorySection ( ref . slug , ref . plugin , plugins ) ;
93- } else {
94- acc += auditRefItemToCategorySection ( ref . slug , ref . plugin , plugins ) ;
95- }
96- return acc ;
97- } , '' ) ;
99+ const auditsAndGroups = category . refs . reduce (
100+ (
101+ acc : {
102+ audits : WeighedAuditReport [ ] ;
103+ groups : EnrichedScoredAuditGroupWithAudits [ ] ;
104+ } ,
105+ ref : CategoryRef ,
106+ ) => ( {
107+ ...acc ,
108+ ...( ref . type === 'group'
109+ ? {
110+ groups : [
111+ ...acc . groups ,
112+ getGroupWithAudits ( ref . slug , ref . plugin , plugins ) ,
113+ ] ,
114+ }
115+ : {
116+ audits : [ ...acc . audits , getAuditByRef ( ref , plugins ) ] ,
117+ } ) ,
118+ } ) ,
119+ { groups : [ ] , audits : [ ] } ,
120+ ) ;
121+
122+ const audits = auditsAndGroups . audits
123+ . sort ( sortCategoryAudits )
124+ . map ( audit => auditItemToCategorySection ( audit , plugins ) )
125+ . join ( NEW_LINE ) ;
126+
127+ const groups = auditsAndGroups . groups
128+ . map ( group => groupItemToCategorySection ( group , plugins ) )
129+ . join ( '' ) ;
98130
99131 return (
100132 acc +
@@ -105,73 +137,43 @@ function reportToCategoriesSection(report: ScoredReport): string {
105137 categoryDocs +
106138 categoryScore +
107139 NEW_LINE +
140+ groups +
108141 NEW_LINE +
109- refs
142+ audits
110143 ) ;
111144 } , '' ) ;
112145
113146 return h2 ( '🏷 Categories' ) + NEW_LINE + categoryDetails ;
114147}
115148
116- function auditRefItemToCategorySection (
117- refSlug : string ,
118- refPlugin : string ,
149+ function auditItemToCategorySection (
150+ audit : WeighedAuditReport ,
119151 plugins : ScoredReport [ 'plugins' ] ,
120152) : string {
121- const plugin = plugins . find ( ( { slug } ) => slug === refPlugin ) as PluginReport ;
122- const pluginAudit = plugin ?. audits . find ( ( { slug } ) => slug === refSlug ) ;
123-
124- if ( ! pluginAudit ) {
125- throwIsNotPresentError ( `Audit ${ refSlug } ` , plugin ?. slug ) ;
126- }
127-
153+ const pluginTitle = getPluginNameFromSlug ( audit . plugin , plugins ) ;
128154 const auditTitle = link (
129- `#${ slugify ( pluginAudit . title ) } -${ slugify ( plugin . title ) } ` ,
130- pluginAudit ?. title ,
155+ `#${ slugify ( audit . title ) } -${ slugify ( pluginTitle ) } ` ,
156+ audit ?. title ,
131157 ) ;
132-
133- return (
134- li (
135- `${ getSquaredScoreMarker ( pluginAudit . score ) } ${ auditTitle } (_${
136- plugin . title
137- } _) - ${ getAuditResult ( pluginAudit ) } `,
138- ) + NEW_LINE
158+ return li (
159+ `${ getSquaredScoreMarker (
160+ audit . score ,
161+ ) } ${ auditTitle } (_${ pluginTitle } _) - ${ getAuditResult ( audit ) } `,
139162 ) ;
140163}
141164
142- function groupRefItemToCategorySection (
143- refSlug : string ,
144- refPlugin : string ,
165+ function groupItemToCategorySection (
166+ group : EnrichedScoredAuditGroupWithAudits ,
145167 plugins : ScoredReport [ 'plugins' ] ,
146168) : string {
147- const plugin = plugins . find ( ( { slug } ) => slug === refPlugin ) as PluginReport ;
148- const group = plugin ?. groups ?. find (
149- ( { slug } ) => slug === refSlug ,
150- ) as EnrichedScoredAuditGroup ;
169+ const pluginTitle = getPluginNameFromSlug ( group . plugin , plugins ) ;
151170 const groupScore = Number ( formatReportScore ( group ?. score || 0 ) ) ;
152-
153- if ( ! group ) {
154- throwIsNotPresentError ( `Group ${ refSlug } ` , plugin ?. slug ) ;
155- }
156-
157171 const groupTitle = li (
158- `${ getRoundScoreMarker ( groupScore ) } ${ group . title } (_${ plugin . title } _)` ,
172+ `${ getRoundScoreMarker ( groupScore ) } ${ group . title } (_${ pluginTitle } _)` ,
159173 ) ;
160- const foundAudits = group . refs . reduce < AuditReport [ ] > ( ( acc , ref ) => {
161- const audit = plugin ?. audits . find (
162- ( { slug : auditSlugInPluginAudits } ) =>
163- auditSlugInPluginAudits === ref . slug ,
164- ) ;
165- if ( audit ) {
166- return [ ...acc , audit ] ;
167- }
168-
169- return acc ;
170- } , [ ] ) ;
171-
172- const groupAudits = foundAudits . reduce ( ( acc , audit ) => {
174+ const groupAudits = group . audits . reduce ( ( acc , audit ) => {
173175 const auditTitle = link (
174- `#${ slugify ( audit . title ) } -${ slugify ( plugin . title ) } ` ,
176+ `#${ slugify ( audit . title ) } -${ slugify ( pluginTitle ) } ` ,
175177 audit ?. title ,
176178 ) ;
177179 acc += ` ${ li (
@@ -187,9 +189,12 @@ function groupRefItemToCategorySection(
187189}
188190
189191function reportToAuditsSection ( report : ScoredReport ) : string {
190- const auditsData = report . plugins . reduce ( ( acc , plugin ) => {
191- const audits = plugin . audits . reduce ( ( acc , audit ) => {
192- const auditTitle = `${ audit . title } (${ plugin . title } )` ;
192+ const auditsSection = report . plugins . reduce ( ( acc , plugin ) => {
193+ const auditsData = plugin . audits . sort ( sortAudits ) . reduce ( ( acc , audit ) => {
194+ const auditTitle = `${ audit . title } (${ getPluginNameFromSlug (
195+ audit . plugin ,
196+ report . plugins ,
197+ ) } )`;
193198 const detailsTitle = `${ getSquaredScoreMarker (
194199 audit . score ,
195200 ) } ${ getAuditResult ( audit , true ) } (score: ${ formatReportScore (
@@ -243,11 +248,10 @@ function reportToAuditsSection(report: ScoredReport): string {
243248
244249 return acc ;
245250 } , '' ) ;
246-
247- return acc + audits ;
251+ return acc + auditsData ;
248252 } , '' ) ;
249253
250- return h2 ( '🛡️ Audits' ) + NEW_LINE + NEW_LINE + auditsData ;
254+ return h2 ( '🛡️ Audits' ) + NEW_LINE + NEW_LINE + auditsSection ;
251255}
252256
253257function reportToAboutSection (
@@ -325,7 +329,3 @@ function getAuditResult(audit: AuditReport, isHtml = false): string {
325329 ? `<b>${ displayValue || value } </b>`
326330 : style ( String ( displayValue || value ) ) ;
327331}
328-
329- function throwIsNotPresentError ( itemName : string , presentPlace : string ) : never {
330- throw new Error ( `${ itemName } is not present in ${ presentPlace } ` ) ;
331- }
0 commit comments