@@ -6,17 +6,17 @@ import os from 'os';
6
6
import path from 'path' ;
7
7
import { Readable } from 'stream' ;
8
8
import { finished } from 'stream/promises' ;
9
+ import simpleGit from 'simple-git' ;
9
10
import chalk from 'chalk' ;
10
11
import fse from 'fs-extra' ;
11
12
import difference from 'lodash/difference' ;
12
13
import minimist from 'minimist' ;
13
14
import pixelmatch from 'pixelmatch' ;
14
15
import { PNG } from 'pngjs' ;
15
- import { remark } from 'remark' ;
16
- import remarkGfm from 'remark-gfm' ;
17
- import remarkHtml from 'remark-html' ;
18
16
import sharp from 'sharp' ;
19
17
18
+ import markdown2Html from './convert' ;
19
+
20
20
const ROOT_DIR = process . cwd ( ) ;
21
21
const ALI_OSS_BUCKET = 'antd-visual-diff' ;
22
22
@@ -119,36 +119,100 @@ async function downloadBaseSnapshots(ref: string, targetDir: string) {
119
119
} ) ;
120
120
}
121
121
122
+ interface IImageDesc {
123
+ src : string ;
124
+ alt : string ;
125
+ }
126
+
127
+ function getMdImageTag ( desc : IImageDesc , extraCaption ?: boolean ) {
128
+ const { src, alt } = desc ;
129
+ if ( ! extraCaption || ! alt ) {
130
+ // in md2html report, we use `@microflash/rehype-figure` to generate a figure
131
+ return `` ;
132
+ }
133
+ // show caption with image in github markdown comment
134
+ return ` ${ alt } ` ;
135
+ }
136
+
122
137
interface IBadCase {
123
138
type : 'removed' | 'changed' ;
124
139
filename : string ;
140
+ /**
141
+ * compare target file
142
+ */
143
+ targetFilename ?: string ;
125
144
/**
126
145
* 0 - 1
127
146
*/
128
147
weight : number ;
129
148
}
130
149
131
- function md2Html ( md : string ) {
132
- return remark ( ) . use ( remarkGfm ) . use ( remarkHtml ) . processSync ( md ) . toString ( ) ;
133
- }
150
+ const git = simpleGit ( ) ;
134
151
135
- function parseArgs ( ) {
152
+ async function parseArgs ( ) {
136
153
// parse args from -- --pr-id=123 --base_ref=feature
137
154
const argv = minimist ( process . argv . slice ( 2 ) ) ;
138
155
const prId = argv [ 'pr-id' ] ;
139
156
assert ( prId , 'Missing --pr-id' ) ;
140
157
const baseRef = argv [ 'base-ref' ] ;
141
158
assert ( baseRef , 'Missing --base-ref' ) ;
159
+
160
+ const { latest } = await git . log ( ) ;
161
+
142
162
return {
143
163
prId,
144
164
baseRef,
165
+ currentRef : latest ?. hash . slice ( 0 , 8 ) || '' ,
145
166
} ;
146
167
}
147
168
169
+ function generateLineReport (
170
+ badCase : IBadCase ,
171
+ publicPath : string ,
172
+ currentRef : string ,
173
+ extraCaption ?: boolean ,
174
+ ) {
175
+ const { filename, type, targetFilename } = badCase ;
176
+
177
+ let lineHTMLReport = '' ;
178
+ if ( type === 'changed' ) {
179
+ lineHTMLReport += '| ' ;
180
+ lineHTMLReport += [
181
+ // add ref as query to avoid github cache image object
182
+ getMdImageTag ( {
183
+ src : `${ publicPath } /images/base/${ filename } ?ref=${ currentRef } ` ,
184
+ alt : targetFilename || '' ,
185
+ } , extraCaption ) ,
186
+ getMdImageTag ( {
187
+ src : `${ publicPath } /images/current/${ filename } ?ref=${ currentRef } ` ,
188
+ alt : filename ,
189
+ } , extraCaption ) ,
190
+ getMdImageTag ( {
191
+ src : `${ publicPath } /images/diff/${ filename } ?ref=${ currentRef } ` ,
192
+ alt : '' ,
193
+ } , extraCaption ) ,
194
+ ] . join ( ' | ' ) ;
195
+ lineHTMLReport += ' |\n' ;
196
+ } else if ( type === 'removed' ) {
197
+ lineHTMLReport += '| ' ;
198
+ lineHTMLReport += [
199
+ getMdImageTag ( {
200
+ src : `${ publicPath } /images/base/${ filename } ?ref=${ currentRef } ` ,
201
+ alt : targetFilename || '' ,
202
+ } , extraCaption ) ,
203
+ `⛔️⛔️⛔️ Missing ⛔️⛔️⛔️` ,
204
+ `🚨🚨🚨 Removed 🚨🚨🚨` ,
205
+ ] . join ( ' | ' ) ;
206
+ lineHTMLReport += ' |\n' ;
207
+ }
208
+ return lineHTMLReport ;
209
+ }
210
+
148
211
function generateReport (
149
212
badCases : IBadCase [ ] ,
150
213
targetBranch : string ,
151
214
targetRef : string ,
215
+ currentRef : string ,
152
216
prId : string ,
153
217
) : [ string , string ] {
154
218
const reportDirname = path . basename ( REPORT_DIR ) ;
@@ -174,15 +238,15 @@ function generateReport(
174
238
'<img src="https://github.com/ant-design/ant-design/assets/507615/2d1a77dc-dbc6-4b0f-9cbc-19a43d3c29cd" width="300" />' ,
175
239
] . join ( '\n' ) ;
176
240
177
- return [ mdStr , md2Html ( mdStr ) ] ;
241
+ return [ mdStr , markdown2Html ( mdStr ) ] ;
178
242
}
179
243
180
244
let reportMdStr = `
181
245
${ commonHeader }
182
246
${ fullReport }
183
247
184
- | Image name | Expected | Actual | Diff |
185
- | --- | --- | --- | --- |
248
+ | Expected (Branch ${ targetBranch } ) | Actual (Current PR) | Diff |
249
+ | --- | --- | --- |
186
250
` . trim ( ) ;
187
251
188
252
reportMdStr += '\n' ;
@@ -192,44 +256,33 @@ ${fullReport}
192
256
let diffCount = 0 ;
193
257
194
258
for ( const badCase of badCases ) {
195
- const { filename, type } = badCase ;
196
- let lineReportMdStr = '' ;
197
- if ( type === 'changed' ) {
198
- lineReportMdStr += '| ' ;
199
- lineReportMdStr += [
200
- `\`${ badCase . filename } \`` ,
201
- `` ,
202
- `` ,
203
- `` ,
204
- ] . join ( ' | ' ) ;
205
- lineReportMdStr += ' |\n' ;
206
- } else if ( type === 'removed' ) {
207
- lineReportMdStr += '| ' ;
208
- lineReportMdStr += [
209
- `\`${ badCase . filename } \`` ,
210
- `` ,
211
- `⛔️⛔️⛔️ Missing ⛔️⛔️⛔️` ,
212
- `🚨🚨🚨 Removed 🚨🚨🚨` ,
213
- ] . join ( ' | ' ) ;
214
- lineReportMdStr += ' |\n' ;
215
- }
216
-
217
259
diffCount += 1 ;
218
260
if ( diffCount <= 10 ) {
219
- reportMdStr += lineReportMdStr ;
261
+ // 将图片下方增加文件名
262
+ reportMdStr += generateLineReport (
263
+ badCase ,
264
+ publicPath ,
265
+ currentRef ,
266
+ true ,
267
+ ) ;
220
268
}
221
269
222
- fullVersionMd += lineReportMdStr ;
270
+ fullVersionMd += generateLineReport (
271
+ badCase ,
272
+ publicPath ,
273
+ currentRef ,
274
+ false ,
275
+ ) ;
223
276
}
224
277
225
278
reportMdStr += addonFullReportDesc ;
226
279
227
280
// convert fullVersionMd to html
228
- return [ reportMdStr , md2Html ( fullVersionMd ) ] ;
281
+ return [ reportMdStr , markdown2Html ( fullVersionMd ) ] ;
229
282
}
230
283
231
284
async function boot ( ) {
232
- const { prId, baseRef : targetBranch = 'master' } = parseArgs ( ) ;
285
+ const { prId, baseRef : targetBranch = 'master' , currentRef } = await parseArgs ( ) ;
233
286
234
287
const baseImgSourceDir = path . resolve ( ROOT_DIR , `./imageSnapshots-${ targetBranch } ` ) ;
235
288
@@ -324,6 +377,7 @@ async function boot() {
324
377
badCases . push ( {
325
378
type : 'changed' ,
326
379
filename : compareImgName ,
380
+ targetFilename : baseImgName ,
327
381
weight : mismatchedPxPercent ,
328
382
} ) ;
329
383
} else {
@@ -340,6 +394,7 @@ async function boot() {
340
394
badCases ,
341
395
targetBranch ,
342
396
targetCommitSha ,
397
+ currentRef ,
343
398
prId ,
344
399
) ;
345
400
await fse . writeFile ( path . join ( REPORT_DIR , './report.md' ) , reportMdStr ) ;
0 commit comments