From 63d6bca609be80e12ff797f22d46681b439ac5ab Mon Sep 17 00:00:00 2001 From: John Firebaugh Date: Tue, 3 Jul 2018 14:05:19 -0700 Subject: [build] Show diff to prior size --- scripts/check_binary_size.js | 81 ++++++++++++++++++++++++++++++++++++-------- 1 file changed, 67 insertions(+), 14 deletions(-) diff --git a/scripts/check_binary_size.js b/scripts/check_binary_size.js index 70ddbc7e0e..2b86436f92 100755 --- a/scripts/check_binary_size.js +++ b/scripts/check_binary_size.js @@ -4,6 +4,7 @@ const jwt = require('jsonwebtoken'); const github = require('@octokit/rest')(); const prettyBytes = require('pretty-bytes'); const fs = require('fs'); +const {execSync} = require('child_process'); const SIZE_CHECK_APP_ID = 14028; const SIZE_CHECK_APP_INSTALLATION_ID = 229425; @@ -11,6 +12,7 @@ const SIZE_CHECK_APP_INSTALLATION_ID = 229425; const file = process.argv[2]; const label = process.argv[3]; const {size} = fs.statSync(file); +const name = `Size - ${label}`; process.on('unhandledRejection', error => { console.log(error); @@ -27,21 +29,72 @@ const payload = { const token = jwt.sign(payload, key, {algorithm: 'RS256'}); github.authenticate({type: 'app', token}); -github.apps.createInstallationToken({installation_id: SIZE_CHECK_APP_INSTALLATION_ID}) - .then(({data}) => { - github.authenticate({type: 'token', token: data.token}); - return github.checks.create({ +function getPriorSize() { + const pr = process.env['CIRCLE_PULL_REQUEST']; + if (!pr) { + console.log('No pull request available.'); + return Promise.resolve(null); + } + + const number = +pr.match(/\/(\d+)\/?$/)[1]; + console.log(`Pull request ${number}.`); + + return github.pullRequests.get({ + owner: 'mapbox', + repo: 'mapbox-gl-native', + number + }).then(({data}) => { + const head = process.env['CIRCLE_SHA1']; + const base = data.base.sha; + console.log(`Pull request target is ${base}.`); + + const mergeBase = execSync(`git merge-base ${base} ${head}`).toString().trim(); + console.log(`Merge base is ${mergeBase}.`); + + return github.checks.listForRef({ owner: 'mapbox', repo: 'mapbox-gl-native', - name: `Size - ${label}`, - head_branch: process.env['CIRCLE_BRANCH'], - head_sha: process.env['CIRCLE_SHA1'], - status: 'completed', - conclusion: 'success', - completed_at: new Date().toISOString(), - output: { - title: prettyBytes(size), - summary: `\`${file}\` is ${size} bytes (${prettyBytes(size)})` - } + ref: mergeBase }); + }).then(({data}) => { + const run = data.check_runs.find(run => run.name === name); + if (!run) { + console.log('No matching check found.'); + return Promise.resolve(null); + } + const prior = +run.output.summary.match(/`.*` is (\d+) bytes/)[1]; + console.log(`Prior size was ${prettyBytes(prior)}.`); + return prior; + }); +} + +github.apps.createInstallationToken({installation_id: SIZE_CHECK_APP_INSTALLATION_ID}) + .then(({data}) => { + github.authenticate({type: 'token', token: data.token}); + getPriorSize().then(prior => { + const title = (() => { + if (prior) { + const change = size - prior; + const percent = (change / prior) * 100; + return `${change >= 0 ? '+' : ''}${prettyBytes(change)} ${percent.toFixed(3)}% (${prettyBytes(size)})`; + } else { + return prettyBytes(size); + } + })(); + + return github.checks.create({ + owner: 'mapbox', + repo: 'mapbox-gl-native', + name: `Size - ${label}`, + head_branch: process.env['CIRCLE_BRANCH'], + head_sha: process.env['CIRCLE_SHA1'], + status: 'completed', + conclusion: 'success', + completed_at: new Date().toISOString(), + output: { + title, + summary: `\`${file}\` is ${size} bytes (${prettyBytes(size)})` + } + }); + }) }); -- cgit v1.2.1