@@ -3,6 +3,7 @@ const { log, getPRNumber } = require('./utils');
33const db = require ( './db' ) ;
44const gh = require ( './github' ) ;
55const printDeltaTable = require ( './delta-table' ) ;
6+ const { sumSizesOf, ZERO_SIZE } = require ( './delta' ) ;
67
78const REPO = 'Automattic/wp-calypso' ;
89const WATERMARK = 'c52822' ;
@@ -35,6 +36,49 @@ function groupByArea(deltas) {
3536 } ) ;
3637}
3738
39+ function totalDeltasForArea ( areaDelta , delta ) {
40+ if ( ! areaDelta ) {
41+ return { ...ZERO_SIZE } ;
42+ }
43+
44+ // Produce an array of arrays:
45+ // [ [ chunks in use in first commit ] , [ chunks in use in second commit ] ]
46+ // The items will be unique inside each array.
47+ const chunksInUse = [ 'firstChunks' , 'secondChunks' ]
48+ . map ( chunkType => areaDelta . reduce (
49+ ( acc , group ) => {
50+ for ( const chunk of group [ chunkType ] ) {
51+ acc . add ( chunk ) ;
52+ }
53+ return acc ;
54+ } ,
55+ new Set ( )
56+ ) )
57+ . map ( set => [ ...set ] ) ;
58+
59+ // Produce an array of size objects, representing the sum of all the chunks for each commit:
60+ // [ { stat_size: 0, parsed_size: 0, gzip_size: 0 }, { stat_size: 0, parsed_size: 0, gzip_size: 0 } ]
61+ // The first object is for the first commit, and the second object for the second commit.
62+ const chunkSizes = [ 'firstSizes' , 'secondSizes' ]
63+ . map ( ( property , index ) => chunksInUse [ index ] . reduce (
64+ ( acc , chunkName ) => {
65+ const chunk = delta . chunks . find ( chunk => chunk . name === chunkName ) || { } ;
66+ acc = sumSizesOf ( acc , chunk [ property ] ) ;
67+ return acc ;
68+ } ,
69+ { ...ZERO_SIZE }
70+ ) ) ;
71+
72+ // Produce a single object with the delta between first and second commit:
73+ // { stat_size: 0, parsed_size: 0, gzip_size: 0 }
74+ let deltaSizes = { } ;
75+ for ( const sizeType in chunkSizes [ 0 ] ) {
76+ deltaSizes [ sizeType ] = chunkSizes [ 1 ] [ sizeType ] - chunkSizes [ 0 ] [ sizeType ] ;
77+ }
78+
79+ return deltaSizes ;
80+ }
81+
3882const AREAS = [
3983 {
4084 id : 'runtime' ,
@@ -90,8 +134,11 @@ function watermarkString(watermark) {
90134 return `icfy-watermark: ${ watermark } ` ;
91135}
92136
93- async function statsMessage ( push ) {
94- const delta = await db . getPushDelta ( push . ancestor , push . sha , { extractManifestGroup : true } ) ;
137+ async function getDelta ( push ) {
138+ return await db . getPushDelta ( push . ancestor , push . sha , { extractManifestGroup : true } ) ;
139+ }
140+
141+ function statsMessage ( delta ) {
95142 const byArea = groupByArea ( delta . groups ) ;
96143
97144 const message = [ ] ;
@@ -112,7 +159,7 @@ async function statsMessage(push) {
112159 continue ;
113160 }
114161
115- const bytesDelta = _ . reduce ( areaDelta , ( sum , delta ) => sum + delta . deltaSizes . gzip_size , 0 ) ;
162+ const bytesDelta = totalDeltasForArea ( areaDelta , delta ) . gzip_size || 0 ;
116163 const changedBytes = Math . abs ( bytesDelta ) ;
117164 const suffix = bytesDelta < 0 ? 'removed 📉' : 'added 📈' ;
118165
@@ -187,7 +234,7 @@ module.exports = async function commentOnGithub(sha) {
187234
188235 const [ firstComment , ...otherComments ] = await getOurPRCommentIDs ( REPO , prNumber ) ;
189236
190- const message = await statsMessage ( push ) ;
237+ const message = statsMessage ( await getDelta ( push ) ) ;
191238
192239 if ( ! firstComment ) {
193240 log ( 'Posting first comment on PR' , prNumber ) ;
0 commit comments