From c7ce0091dcb2036e19df980247c9a4388ad75741 Mon Sep 17 00:00:00 2001 From: Orta Date: Sun, 11 Feb 2018 14:43:29 -0500 Subject: [PATCH] [Danger] Use the PR's mergebase for a branch in the dangerfile (#12049) * [Danger] Use the PR's mergebase for a branch in the dangerfile instead of the root commit's parent. * [Danger] Get the full history to find the merge base --- dangerfile.js | 73 +++++++++++++++++++++++----- scripts/circleci/test_entry_point.sh | 2 +- scripts/rollup/stats.js | 8 ++- 3 files changed, 68 insertions(+), 15 deletions(-) diff --git a/dangerfile.js b/dangerfile.js index 2db280e0f7..97824c08ed 100644 --- a/dangerfile.js +++ b/dangerfile.js @@ -7,11 +7,30 @@ 'use strict'; +// Hi, if this is your first time editing/reading a Dangerfile, here's a summary: +// It's a JS runtime which helps you provide continuous feedback inside GitHub. +// +// You can see the docs here: http://danger.systems/js/ +// +// If you want to test changes Danger, I'd recommend checking out an existing PR +// and then running the `danger pr` command. +// +// You'll need a GitHub token, you can re-use this one: +// +// e622517d9f1136ea8900 07c6373666312cdfaa69 +// +// (Just remove the space) +// +// So, for example: +// +// `DANGER_GITHUB_API_TOKEN=[ENV_ABOVE] yarn danger pr https://github.com/facebook/react/pull/11865 + const {markdown, danger} = require('danger'); const fetch = require('node-fetch'); const {generateResultsArray} = require('./scripts/rollup/stats'); const {readFileSync} = require('fs'); +const {exec} = require('child_process'); const currentBuildResults = JSON.parse( readFileSync('./scripts/rollup/results.json') @@ -54,13 +73,40 @@ function setBoldness(row, isBold) { } } -// Grab the results.json before we ran CI via the GH API -// const baseMerge = danger.github.pr.base.sha -const parentOfOldestCommit = danger.git.commits[0].parents[0]; -const commitURL = sha => - `http://react.zpao.com/builds/master/_commits/${sha}/results.json`; +/** + * Gets the commit that represents the merge between the current branch + * and master. + */ +function getMergeBase() { + return git('merge-base HEAD origin/master'); +} -fetch(commitURL(parentOfOldestCommit)).then(async response => { +/** + * Gets the commit that represents the merge between the current branch + * and master. + */ +function git(args) { + return new Promise(res => { + exec('git ' + args, (err, stdout, stderr) => { + if (err) { + throw err; + } else { + res(stdout.trim()); + } + }); + }); +} + +(async function() { + // Use git locally to grab the commit which represents the place + // where the branches differ + const mergeBaseCommit = await getMergeBase(); + const commitURL = sha => + `http://react.zpao.com/builds/master/_commits/${sha}/results.json`; + const response = await fetch(commitURL(mergeBaseCommit)); + + // Take the JSON of the build response and + // make an array comparing the results for printing const previousBuildResults = await response.json(); const results = generateResultsArray( currentBuildResults, @@ -74,6 +120,7 @@ fetch(commitURL(parentOfOldestCommit)).then(async response => { Math.abs(r.prevFileSizeChange) > percentToWarrentShowing || Math.abs(r.prevGzipSizeChange) > percentToWarrentShowing ) + .map(r => r.packageName); if (packagesToShow.length) { @@ -152,16 +199,16 @@ fetch(commitURL(parentOfOldestCommit)).then(async response => { } const summary = ` -
-Details of bundled changes. +
+ Details of bundled changes. -

Comparing: ${parentOfOldestCommit}...${danger.github.pr.head.sha}

+

Comparing: ${mergeBaseCommit}...${danger.github.pr.head.sha}

-${allTables.join('\n')} + ${allTables.join('\n')} -
-`; +
+ `; markdown(summary); } -}); +})(); diff --git a/scripts/circleci/test_entry_point.sh b/scripts/circleci/test_entry_point.sh index e35fa15fb0..249b09142c 100755 --- a/scripts/circleci/test_entry_point.sh +++ b/scripts/circleci/test_entry_point.sh @@ -25,7 +25,7 @@ if [ $((2 % CIRCLE_NODE_TOTAL)) -eq "$CIRCLE_NODE_INDEX" ]; then COMMANDS_TO_RUN+=('./scripts/circleci/build.sh') COMMANDS_TO_RUN+=('yarn test-build --maxWorkers=2') COMMANDS_TO_RUN+=('yarn test-build-prod --maxWorkers=2') -# COMMANDS_TO_RUN+=('node ./scripts/tasks/danger') + COMMANDS_TO_RUN+=('node ./scripts/tasks/danger') COMMANDS_TO_RUN+=('./scripts/circleci/upload_build.sh') fi diff --git a/scripts/rollup/stats.js b/scripts/rollup/stats.js index b12ab810e9..125e5e6717 100644 --- a/scripts/rollup/stats.js +++ b/scripts/rollup/stats.js @@ -21,7 +21,13 @@ function saveResults() { } function percentChange(prev, current) { - return Math.floor((current - prev) / prev * 100); + const change = Math.floor((current - prev) / prev * 100); + // When a new package is created + if (isFinite(change)) { + return change; + } else { + return 100; + } } function percentChangeString(change) {