[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
Orta committed
Feb 11, 2018 at 14:43 UTC
c7ce0091dcb2036e19df980247c9a4388ad75741
3 files changed
+68
-15
dangerfile.js
+60
-13
@@ -7,11 +7,30 @@
7
8
'use strict';
9
10
+// Hi, if this is your first time editing/reading a Dangerfile, here's a summary:
11
+// It's a JS runtime which helps you provide continuous feedback inside GitHub.
12
+//
13
+// You can see the docs here: http://danger.systems/js/
14
+//
15
+// If you want to test changes Danger, I'd recommend checking out an existing PR
16
+// and then running the `danger pr` command.
17
+//
18
+// You'll need a GitHub token, you can re-use this one:
19
+//
20
+// e622517d9f1136ea8900 07c6373666312cdfaa69
21
+//
22
+// (Just remove the space)
23
+//
24
+// So, for example:
25
+//
26
+// `DANGER_GITHUB_API_TOKEN=[ENV_ABOVE] yarn danger pr https://github.com/facebook/react/pull/11865
27
+
28
const {markdown, danger} = require('danger');
29
const fetch = require('node-fetch');
30
31
const {generateResultsArray} = require('./scripts/rollup/stats');
32
const {readFileSync} = require('fs');
33
+const {exec} = require('child_process');
34
35
const currentBuildResults = JSON.parse(
36
readFileSync('./scripts/rollup/results.json')
@@ -54,13 +73,40 @@ function setBoldness(row, isBold) {
73
}
74
}
75
57
-// Grab the results.json before we ran CI via the GH API
58
-// const baseMerge = danger.github.pr.base.sha
59
-const parentOfOldestCommit = danger.git.commits[0].parents[0];
60
-const commitURL = sha =>
61
- `http://react.zpao.com/builds/master/_commits/${sha}/results.json`;
76
+/**
77
+ * Gets the commit that represents the merge between the current branch
78
+ * and master.
79
+ */
80
+function getMergeBase() {
81
+ return git('merge-base HEAD origin/master');
82
+}
83
63
-fetch(commitURL(parentOfOldestCommit)).then(async response => {
84
+/**
85
+ * Gets the commit that represents the merge between the current branch
86
+ * and master.
87
+ */
88
+function git(args) {
89
+ return new Promise(res => {
90
+ exec('git ' + args, (err, stdout, stderr) => {
91
+ if (err) {
92
+ throw err;
93
+ } else {
94
+ res(stdout.trim());
95
+ }
96
+ });
97
+ });
98
+}
99
+
100
+(async function() {
101
+ // Use git locally to grab the commit which represents the place
102
+ // where the branches differ
103
+ const mergeBaseCommit = await getMergeBase();
104
+ const commitURL = sha =>
105
+ `http://react.zpao.com/builds/master/_commits/${sha}/results.json`;
106
+ const response = await fetch(commitURL(mergeBaseCommit));
107
+
108
+ // Take the JSON of the build response and
109
+ // make an array comparing the results for printing
110
const previousBuildResults = await response.json();
111
const results = generateResultsArray(
112
currentBuildResults,
@@ -74,6 +120,7 @@ fetch(commitURL(parentOfOldestCommit)).then(async response => {
120
Math.abs(r.prevFileSizeChange) > percentToWarrentShowing ||
121
Math.abs(r.prevGzipSizeChange) > percentToWarrentShowing
122
)
123
+
124
.map(r => r.packageName);
125
126
if (packagesToShow.length) {
@@ -152,16 +199,16 @@ fetch(commitURL(parentOfOldestCommit)).then(async response => {
199
}
200
201
const summary = `
155
-<details>
156
-<summary>Details of bundled changes.</summary>
202
+ <details>
203
+ <summary>Details of bundled changes.</summary>
204
158
-<p>Comparing: ${parentOfOldestCommit}...${danger.github.pr.head.sha}</p>
205
+ <p>Comparing: ${mergeBaseCommit}...${danger.github.pr.head.sha}</p>
206
207
161
-${allTables.join('\n')}
208
+ ${allTables.join('\n')}
209
163
-</details>
164
-`;
210
+ </details>
211
+ `;
212
markdown(summary);
213
}
167
-});
214
+})();
scripts/circleci/test_entry_point.sh
+1
-1
@@ -25,7 +25,7 @@ if [ $((2 % CIRCLE_NODE_TOTAL)) -eq "$CIRCLE_NODE_INDEX" ]; then
25
COMMANDS_TO_RUN+=('./scripts/circleci/build.sh')
26
COMMANDS_TO_RUN+=('yarn test-build --maxWorkers=2')
27
COMMANDS_TO_RUN+=('yarn test-build-prod --maxWorkers=2')
28
-# COMMANDS_TO_RUN+=('node ./scripts/tasks/danger')
28
+ COMMANDS_TO_RUN+=('node ./scripts/tasks/danger')
29
COMMANDS_TO_RUN+=('./scripts/circleci/upload_build.sh')
30
fi
31
scripts/rollup/stats.js
+7
-1
@@ -21,7 +21,13 @@ function saveResults() {
21
}
22
23
function percentChange(prev, current) {
24
- return Math.floor((current - prev) / prev * 100);
24
+ const change = Math.floor((current - prev) / prev * 100);
25
+ // When a new package is created
26
+ if (isFinite(change)) {
27
+ return change;
28
+ } else {
29
+ return 100;
30
+ }
31
}
32
33
function percentChangeString(change) {