Skip to content

Commit 40ae53c

Browse files
ortarhagigi
authored andcommitted
[Danger] Use the PR's mergebase for a branch in the dangerfile (facebook#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
1 parent e4e944c commit 40ae53c

File tree

3 files changed

+68
-15
lines changed

3 files changed

+68
-15
lines changed

dangerfile.js

Lines changed: 60 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,30 @@
77

88
'use strict';
99

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+
1028
const {markdown, danger} = require('danger');
1129
const fetch = require('node-fetch');
1230

1331
const {generateResultsArray} = require('./scripts/rollup/stats');
1432
const {readFileSync} = require('fs');
33+
const {exec} = require('child_process');
1534

1635
const currentBuildResults = JSON.parse(
1736
readFileSync('./scripts/rollup/results.json')
@@ -54,13 +73,40 @@ function setBoldness(row, isBold) {
5473
}
5574
}
5675

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+
}
6283

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
64110
const previousBuildResults = await response.json();
65111
const results = generateResultsArray(
66112
currentBuildResults,
@@ -74,6 +120,7 @@ fetch(commitURL(parentOfOldestCommit)).then(async response => {
74120
Math.abs(r.prevFileSizeChange) > percentToWarrentShowing ||
75121
Math.abs(r.prevGzipSizeChange) > percentToWarrentShowing
76122
)
123+
77124
.map(r => r.packageName);
78125

79126
if (packagesToShow.length) {
@@ -152,16 +199,16 @@ fetch(commitURL(parentOfOldestCommit)).then(async response => {
152199
}
153200

154201
const summary = `
155-
<details>
156-
<summary>Details of bundled changes.</summary>
202+
<details>
203+
<summary>Details of bundled changes.</summary>
157204
158-
<p>Comparing: ${parentOfOldestCommit}...${danger.github.pr.head.sha}</p>
205+
<p>Comparing: ${mergeBaseCommit}...${danger.github.pr.head.sha}</p>
159206
160207
161-
${allTables.join('\n')}
208+
${allTables.join('\n')}
162209
163-
</details>
164-
`;
210+
</details>
211+
`;
165212
markdown(summary);
166213
}
167-
});
214+
})();

scripts/circleci/test_entry_point.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ if [ $((2 % CIRCLE_NODE_TOTAL)) -eq "$CIRCLE_NODE_INDEX" ]; then
2525
COMMANDS_TO_RUN+=('./scripts/circleci/build.sh')
2626
COMMANDS_TO_RUN+=('yarn test-build --maxWorkers=2')
2727
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')
2929
COMMANDS_TO_RUN+=('./scripts/circleci/upload_build.sh')
3030
fi
3131

scripts/rollup/stats.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,13 @@ function saveResults() {
2121
}
2222

2323
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+
}
2531
}
2632

2733
function percentChangeString(change) {

0 commit comments

Comments
 (0)