1
1
import {
2
2
AuditReport ,
3
3
CategoryConfig ,
4
+ CategoryRef ,
4
5
Issue ,
5
- PluginReport ,
6
6
} from '@code-pushup/models' ;
7
7
import { CommitData } from './git' ;
8
8
import {
@@ -24,15 +24,24 @@ import {
24
24
detailsTableHeaders ,
25
25
formatDuration ,
26
26
formatReportScore ,
27
+ getAuditByRef ,
28
+ getGroupWithAudits ,
29
+ getPluginNameFromSlug ,
27
30
getRoundScoreMarker ,
28
31
getSeverityIcon ,
29
32
getSquaredScoreMarker ,
30
33
pluginMetaTableHeaders ,
31
34
reportHeadlineText ,
32
35
reportMetaTableHeaders ,
33
36
reportOverviewTableHeaders ,
37
+ sortAudits ,
38
+ sortCategoryAudits ,
34
39
} from './report' ;
35
- import { EnrichedScoredAuditGroup , ScoredReport } from './scoring' ;
40
+ import {
41
+ EnrichedScoredAuditGroupWithAudits ,
42
+ ScoredReport ,
43
+ WeighedAuditReport ,
44
+ } from './scoring' ;
36
45
import { slugify } from './transformation' ;
37
46
38
47
export function reportToMd (
@@ -87,14 +96,37 @@ function reportToCategoriesSection(report: ScoredReport): string {
87
96
) } Score: ${ style ( formatReportScore ( category . score ) ) } `;
88
97
const categoryDocs = getDocsAndDescription ( category ) ;
89
98
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 ( '' ) ;
98
130
99
131
return (
100
132
acc +
@@ -105,73 +137,43 @@ function reportToCategoriesSection(report: ScoredReport): string {
105
137
categoryDocs +
106
138
categoryScore +
107
139
NEW_LINE +
140
+ groups +
108
141
NEW_LINE +
109
- refs
142
+ audits
110
143
) ;
111
144
} , '' ) ;
112
145
113
146
return h2 ( '🏷 Categories' ) + NEW_LINE + categoryDetails ;
114
147
}
115
148
116
- function auditRefItemToCategorySection (
117
- refSlug : string ,
118
- refPlugin : string ,
149
+ function auditItemToCategorySection (
150
+ audit : WeighedAuditReport ,
119
151
plugins : ScoredReport [ 'plugins' ] ,
120
152
) : 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 ) ;
128
154
const auditTitle = link (
129
- `#${ slugify ( pluginAudit . title ) } -${ slugify ( plugin . title ) } ` ,
130
- pluginAudit ?. title ,
155
+ `#${ slugify ( audit . title ) } -${ slugify ( pluginTitle ) } ` ,
156
+ audit ?. title ,
131
157
) ;
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 ) } `,
139
162
) ;
140
163
}
141
164
142
- function groupRefItemToCategorySection (
143
- refSlug : string ,
144
- refPlugin : string ,
165
+ function groupItemToCategorySection (
166
+ group : EnrichedScoredAuditGroupWithAudits ,
145
167
plugins : ScoredReport [ 'plugins' ] ,
146
168
) : 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 ) ;
151
170
const groupScore = Number ( formatReportScore ( group ?. score || 0 ) ) ;
152
-
153
- if ( ! group ) {
154
- throwIsNotPresentError ( `Group ${ refSlug } ` , plugin ?. slug ) ;
155
- }
156
-
157
171
const groupTitle = li (
158
- `${ getRoundScoreMarker ( groupScore ) } ${ group . title } (_${ plugin . title } _)` ,
172
+ `${ getRoundScoreMarker ( groupScore ) } ${ group . title } (_${ pluginTitle } _)` ,
159
173
) ;
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 ) => {
173
175
const auditTitle = link (
174
- `#${ slugify ( audit . title ) } -${ slugify ( plugin . title ) } ` ,
176
+ `#${ slugify ( audit . title ) } -${ slugify ( pluginTitle ) } ` ,
175
177
audit ?. title ,
176
178
) ;
177
179
acc += ` ${ li (
@@ -187,9 +189,12 @@ function groupRefItemToCategorySection(
187
189
}
188
190
189
191
function 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
+ ) } )`;
193
198
const detailsTitle = `${ getSquaredScoreMarker (
194
199
audit . score ,
195
200
) } ${ getAuditResult ( audit , true ) } (score: ${ formatReportScore (
@@ -243,11 +248,10 @@ function reportToAuditsSection(report: ScoredReport): string {
243
248
244
249
return acc ;
245
250
} , '' ) ;
246
-
247
- return acc + audits ;
251
+ return acc + auditsData ;
248
252
} , '' ) ;
249
253
250
- return h2 ( '🛡️ Audits' ) + NEW_LINE + NEW_LINE + auditsData ;
254
+ return h2 ( '🛡️ Audits' ) + NEW_LINE + NEW_LINE + auditsSection ;
251
255
}
252
256
253
257
function reportToAboutSection (
@@ -325,7 +329,3 @@ function getAuditResult(audit: AuditReport, isHtml = false): string {
325
329
? `<b>${ displayValue || value } </b>`
326
330
: style ( String ( displayValue || value ) ) ;
327
331
}
328
-
329
- function throwIsNotPresentError ( itemName : string , presentPlace : string ) : never {
330
- throw new Error ( `${ itemName } is not present in ${ presentPlace } ` ) ;
331
- }
0 commit comments