1515// =============================================================================
1616
1717const { exec} = require ( './test-util' ) ;
18+ const shell = require ( 'shelljs' ) ;
1819const { readdirSync, statSync, writeFileSync} = require ( 'fs' ) ;
1920const { join} = require ( 'path' ) ;
2021const fs = require ( 'fs' ) ;
@@ -30,9 +31,45 @@ const dirs = readdirSync('.').filter(f => {
3031 return f !== 'node_modules' && f !== '.git' && statSync ( f ) . isDirectory ( ) ;
3132} ) ;
3233
33- exec (
34- `git clone --depth=1 --single-branch ` +
35- `https://github.com/tensorflow/tfjs ${ CLONE_PATH } ` ) ;
34+ let commitSha = process . env [ 'COMMIT_SHA' ] ;
35+ let branchName = process . env [ 'BRANCH_NAME' ] ;
36+ // If commit sha or branch name are null we are running this locally and are in
37+ // a git repository.
38+ if ( commitSha == null ) {
39+ commitSha = exec ( `git rev-parse HEAD` ) . stdout . trim ( ) ;
40+ }
41+ if ( branchName == null ) {
42+ branchName = exec ( `git rev-parse --abbrev-ref HEAD` ) . stdout . trim ( ) ;
43+ }
44+ console . log ( 'commitSha: ' , commitSha ) ;
45+ console . log ( 'branchName: ' , branchName ) ;
46+
47+ // We cannot do --depth=1 here because we need to check out an old merge base.
48+ // We cannot do --single-branch here because we need multiple branches.
49+ exec ( `git clone https://github.com/tensorflow/tfjs ${ CLONE_PATH } ` ) ;
50+
51+ console . log ( ) ; // Break up the console for readability.
52+
53+ shell . cd ( CLONE_PATH ) ;
54+
55+ // If we cannot check out the commit then this PR is coming from a fork.
56+ const res = shell . exec ( `git checkout ${ commitSha } ` , { silent : true } ) ;
57+ const isPullRequestFromFork = res . code !== 0 ;
58+
59+ // Only checkout the merge base if the pull requests comes from a
60+ // tensorflow/tfjs branch. Otherwise clone master and diff against master.
61+ if ( ! isPullRequestFromFork ) {
62+ console . log ( 'PR is coming from tensorflow/tfjs. Finding the merge base...' ) ;
63+ exec ( `git checkout ${ branchName } ` ) ;
64+ const mergeBase = exec ( `git merge-base master ${ branchName } ` ) . stdout . trim ( ) ;
65+ exec ( `git fetch origin ${ mergeBase } ` ) ;
66+ exec ( `git checkout ${ mergeBase } ` ) ;
67+ console . log ( 'mergeBase: ' , mergeBase ) ;
68+ } else {
69+ console . log ( 'PR is coming from a fork. Diffing against master.' ) ;
70+ }
71+ shell . cd ( '..' ) ;
72+ console . log ( ) ; // Break up the console for readability.
3673
3774let triggerAllBuilds = false ;
3875let whitelistDiffOutput = [ ] ;
@@ -45,11 +82,11 @@ filesWhitelistToTriggerBuild.forEach(fileToTriggerBuild => {
4582 }
4683} ) ;
4784
48- // Break up the console for readability.
49- console . log ( ) ;
85+ console . log ( ) ; // Break up the console for readability.
5086
5187let triggeredBuilds = [ ] ;
5288dirs . forEach ( dir => {
89+ shell . rm ( '-f' , `${ dir } /diff` ) ;
5390 const diffOutput = diff ( `${ dir } /` ) ;
5491 if ( diffOutput !== '' ) {
5592 console . log ( `${ dir } has modified files.` ) ;
@@ -65,16 +102,17 @@ dirs.forEach(dir => {
65102 }
66103} ) ;
67104
68- // Break up the console for readability.
69- console . log ( ) ;
105+ console . log ( ) ; // Break up the console for readability.
70106
71- // Filter the triggered builds to log by whether a cloudbuild.yml file exists
72- // for that directory.
107+ // Filter the triggered builds to log by whether a cloudbuild.yml file
108+ // exists for that directory.
73109triggeredBuilds = triggeredBuilds . filter (
74110 triggeredBuild => fs . existsSync ( triggeredBuild + '/cloudbuild.yml' ) ) ;
75111console . log ( 'Triggering builds for ' , triggeredBuilds . join ( ', ' ) ) ;
76112
77113function diff ( fileOrDirName ) {
78- const diffCmd = `diff -rq ${ CLONE_PATH } /${ fileOrDirName } ./${ fileOrDirName } ` ;
114+ const diffCmd = `diff -rq ` +
115+ `${ CLONE_PATH } /${ fileOrDirName } ` +
116+ `${ fileOrDirName } ` ;
79117 return exec ( diffCmd , { silent : true } , true ) . stdout . trim ( ) ;
80118}
0 commit comments