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+
1028const { markdown, danger} = require ( 'danger' ) ;
1129const fetch = require ( 'node-fetch' ) ;
1230
1331const { generateResultsArray} = require ( './scripts/rollup/stats' ) ;
1432const { readFileSync} = require ( 'fs' ) ;
33+ const { exec} = require ( 'child_process' ) ;
1534
1635const 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+ } ) ( ) ;
0 commit comments