- 
                Notifications
    You must be signed in to change notification settings 
- Fork 49.7k
Adds Danger and a rule showing build size differences #11865
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Merged
      
      
    
  
     Merged
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            7 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      abfb144
              
                Adds danger_js with an initial rule for warning about large PRs
              
              
                 2d84a5c
              
                [WIP] Get the before and after for the build results
              
              
                orta 15ada00
              
                [Dev] More work on the Dangerfile
              
              
                orta a0d9b7a
              
                [Danger] Split the reports into sections based on their package
              
              
                orta 56379cb
              
                Remove the --extract-errors on the circle build
              
              
                orta 8d9e9cb
              
                [Danger] Improve the lookup for previous -> current build to also inc…
              
              
                orta 284a53d
              
                Fix rebase
              
              
                gaearon File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,151 @@ | ||
| /** | ||
| * Copyright (c) 2013-present, Facebook, Inc. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|  | ||
| 'use strict'; | ||
|  | ||
| const {markdown, danger} = require('danger'); | ||
| const fetch = require('node-fetch'); | ||
|  | ||
| const {generateResultsArray} = require('./scripts/rollup/stats'); | ||
| const {readFileSync} = require('fs'); | ||
|  | ||
| const currentBuildResults = JSON.parse( | ||
| readFileSync('./scripts/rollup/results.json') | ||
| ); | ||
|  | ||
| /** | ||
| * Generates a Markdown table | ||
| * @param {string[]} headers | ||
| * @param {string[][]} body | ||
| */ | ||
| function generateMDTable(headers, body) { | ||
| const tableHeaders = [ | ||
| headers.join(' | '), | ||
| headers.map(() => ' --- ').join(' | '), | ||
| ]; | ||
|  | ||
| const tablebody = body.map(r => r.join(' | ')); | ||
| return tableHeaders.join('\n') + '\n' + tablebody.join('\n'); | ||
| } | ||
|  | ||
| /** | ||
| * Generates a user-readable string from a percentage change | ||
| * @param {string[]} headers | ||
| */ | ||
| function emojiPercent(change) { | ||
| if (change > 0) { | ||
| return `:small_red_triangle:+${change}%`; | ||
| } else if (change <= 0) { | ||
| return `${change}%`; | ||
| } | ||
| } | ||
|  | ||
| // 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`; | ||
|  | ||
| fetch(commitURL(parentOfOldestCommit)).then(async response => { | ||
| const previousBuildResults = await response.json(); | ||
| const results = generateResultsArray( | ||
| currentBuildResults, | ||
| previousBuildResults | ||
| ); | ||
|  | ||
| const percentToWarrentShowing = 1; | ||
| const packagesToShow = results | ||
| .filter( | ||
| r => | ||
| Math.abs(r.prevFileSizeChange) > percentToWarrentShowing || | ||
| Math.abs(r.prevGzipSizeChange) > percentToWarrentShowing | ||
| ) | ||
| .map(r => r.packageName); | ||
|  | ||
| if (packagesToShow.length) { | ||
| let allTables = []; | ||
|  | ||
| // Highlight React and React DOM changes inline | ||
| // e.g. react: `react.production.min.js`: -3%, `react.development.js`: +4% | ||
|  | ||
| if (packagesToShow.includes('react')) { | ||
| const reactProd = results.find( | ||
| r => r.bundleType === 'UMD_PROD' && r.packageName === 'react' | ||
| ); | ||
| if ( | ||
| reactProd.prevFileSizeChange !== 0 || | ||
| reactProd.prevGzipSizeChange !== 0 | ||
| ) { | ||
| const changeSize = emojiPercent(reactProd.prevFileSizeChange); | ||
| const changeGzip = emojiPercent(reactProd.prevGzipSizeChange); | ||
| markdown(`React: size: ${changeSize}, gzip: ${changeGzip}`); | ||
| } | ||
| } | ||
|  | ||
| if (packagesToShow.includes('react-dom')) { | ||
| const reactDOMProd = results.find( | ||
| r => r.bundleType === 'UMD_PROD' && r.packageName === 'react-dom' | ||
| ); | ||
| if ( | ||
| reactDOMProd.prevFileSizeChange !== 0 || | ||
| reactDOMProd.prevGzipSizeChange !== 0 | ||
| ) { | ||
| const changeSize = emojiPercent(reactDOMProd.prevFileSizeChange); | ||
| const changeGzip = emojiPercent(reactDOMProd.prevGzipSizeChange); | ||
| markdown(`ReactDOM: size: ${changeSize}, gzip: ${changeGzip}`); | ||
| } | ||
| } | ||
|  | ||
| // Show a hidden summary table for all diffs | ||
|  | ||
| // eslint-disable-next-line no-var | ||
| for (var name of new Set(packagesToShow)) { | ||
| const thisBundleResults = results.filter(r => r.packageName === name); | ||
| const changedFiles = thisBundleResults.filter( | ||
| r => r.prevGzipSizeChange !== 0 || r.prevGzipSizeChange !== 0 | ||
| ); | ||
|  | ||
| const mdHeaders = [ | ||
| 'File', | ||
| 'Filesize Diff', | ||
| 'Gzip Diff', | ||
| 'Prev Size', | ||
| 'Current Size', | ||
| 'Prev Gzip', | ||
| 'Current Gzip', | ||
| 'ENV', | ||
| ]; | ||
|  | ||
| const mdRows = changedFiles.map(r => [ | ||
| r.filename, | ||
| emojiPercent(r.prevFileSizeChange), | ||
| emojiPercent(r.prevGzipSizeChange), | ||
| r.prevSize, | ||
| r.prevFileSize, | ||
| r.prevGzip, | ||
| r.prevGzipSize, | ||
| r.bundleType, | ||
| ]); | ||
|  | ||
| allTables.push(`\n## ${name}`); | ||
| allTables.push(generateMDTable(mdHeaders, mdRows)); | ||
| } | ||
|  | ||
| const summary = ` | ||
| <details> | ||
| <summary>Details of bundled changes.</summary> | ||
|  | ||
| <p>Comparing: ${parentOfOldestCommit}...${danger.github.pr.head.sha}</p> | ||
|  | ||
|  | ||
| ${allTables.join('\n')} | ||
|  | ||
| </details> | ||
| `; | ||
| markdown(summary); | ||
| } | ||
| }); | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
|  | @@ -400,4 +400,4 @@ | |
| "gzip": 525 | ||
| } | ||
| ] | ||
| } | ||
| } | ||
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| /** | ||
| * Copyright (c) 2013-present, Facebook, Inc. | ||
| * | ||
| * This source code is licensed under the MIT license found in the | ||
| * LICENSE file in the root directory of this source tree. | ||
| */ | ||
|  | ||
| 'use strict'; | ||
|  | ||
| const path = require('path'); | ||
| const spawn = require('child_process').spawn; | ||
|  | ||
| const extension = process.platform === 'win32' ? '.cmd' : ''; | ||
|  | ||
| // This came from React Native's circle.yml | ||
| const token = 'e622517d9f1136ea8900' + '07c6373666312cdfaa69'; | ||
| spawn(path.join('node_modules', '.bin', 'danger-ci' + extension), [], { | ||
| // Allow colors to pass through | ||
| stdio: 'inherit', | ||
| env: { | ||
| ...process.env, | ||
| DANGER_GITHUB_API_TOKEN: token, | ||
| }, | ||
| }).on('close', function(code) { | ||
| if (code !== 0) { | ||
| console.error('Danger failed'); | ||
| } else { | ||
| console.log('Danger passed'); | ||
| } | ||
|  | ||
| process.exit(code); | ||
| }); | 
      
      Oops, something went wrong.
        
    
  
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this file should not be checked in. It should be gitignored.
Locally, instead of comparing to it during the run, we should locate a special cache folder (like
babel-loaderuses for caching). Inside that folder, we should have{sha}.jsonfor every build done locally (that hasn't been purged from the cache yet).When we build with a clean git state, we can copy the JSON as
{sha.json}into the cache. On every build (clean or not) we should also look up{merge-base-sha}.jsonin local cache. If it exists, that's what we compare against. Otherwise, we don't print the stats locally.Would that make sense?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(It's not a blocker though if we can get the remote results to be right at least)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, it should be gitignored 👍
I'm about to head into a meeting but I'll gonna take a look into why the results I'm seeing locally are different in general, maybe a node_modules introduced by Danger changes the results?