diff --git a/cloudbuild.yml b/cloudbuild.yml index e12ce055881..98ec8462ed8 100644 --- a/cloudbuild.yml +++ b/cloudbuild.yml @@ -11,6 +11,10 @@ steps: id: 'diff' args: ['diff'] waitFor: ['yarn'] + env: + - 'COMMIT_SHA=$COMMIT_SHA' + - 'BRANCH_NAME=$BRANCH_NAME' + - 'REPO_NAME=$REPO_NAME' # Core. - name: 'gcr.io/cloud-builders/gcloud' diff --git a/scripts/diff.js b/scripts/diff.js index fde9e6f6897..77c57b41142 100755 --- a/scripts/diff.js +++ b/scripts/diff.js @@ -15,6 +15,7 @@ // ============================================================================= const {exec} = require('./test-util'); +const shell = require('shelljs'); const {readdirSync, statSync, writeFileSync} = require('fs'); const {join} = require('path'); const fs = require('fs'); @@ -30,9 +31,45 @@ const dirs = readdirSync('.').filter(f => { return f !== 'node_modules' && f !== '.git' && statSync(f).isDirectory(); }); -exec( - `git clone --depth=1 --single-branch ` + - `https://github.com/tensorflow/tfjs ${CLONE_PATH}`); +let commitSha = process.env['COMMIT_SHA']; +let branchName = process.env['BRANCH_NAME']; +// If commit sha or branch name are null we are running this locally and are in +// a git repository. +if (commitSha == null) { + commitSha = exec(`git rev-parse HEAD`).stdout.trim(); +} +if (branchName == null) { + branchName = exec(`git rev-parse --abbrev-ref HEAD`).stdout.trim(); +} +console.log('commitSha: ', commitSha); +console.log('branchName: ', branchName); + +// We cannot do --depth=1 here because we need to check out an old merge base. +// We cannot do --single-branch here because we need multiple branches. +exec(`git clone https://github.com/tensorflow/tfjs ${CLONE_PATH}`); + +console.log(); // Break up the console for readability. + +shell.cd(CLONE_PATH); + +// If we cannot check out the commit then this PR is coming from a fork. +const res = shell.exec(`git checkout ${commitSha}`, {silent: true}); +const isPullRequestFromFork = res.code !== 0; + +// Only checkout the merge base if the pull requests comes from a +// tensorflow/tfjs branch. Otherwise clone master and diff against master. +if (!isPullRequestFromFork) { + console.log('PR is coming from tensorflow/tfjs. Finding the merge base...'); + exec(`git checkout ${branchName}`); + const mergeBase = exec(`git merge-base master ${branchName}`).stdout.trim(); + exec(`git fetch origin ${mergeBase}`); + exec(`git checkout ${mergeBase}`); + console.log('mergeBase: ', mergeBase); +} else { + console.log('PR is coming from a fork. Diffing against master.'); +} +shell.cd('..'); +console.log(); // Break up the console for readability. let triggerAllBuilds = false; let whitelistDiffOutput = []; @@ -45,11 +82,11 @@ filesWhitelistToTriggerBuild.forEach(fileToTriggerBuild => { } }); -// Break up the console for readability. -console.log(); +console.log(); // Break up the console for readability. let triggeredBuilds = []; dirs.forEach(dir => { + shell.rm('-f', `${dir}/diff`); const diffOutput = diff(`${dir}/`); if (diffOutput !== '') { console.log(`${dir} has modified files.`); @@ -65,16 +102,17 @@ dirs.forEach(dir => { } }); -// Break up the console for readability. -console.log(); +console.log(); // Break up the console for readability. -// Filter the triggered builds to log by whether a cloudbuild.yml file exists -// for that directory. +// Filter the triggered builds to log by whether a cloudbuild.yml file +// exists for that directory. triggeredBuilds = triggeredBuilds.filter( triggeredBuild => fs.existsSync(triggeredBuild + '/cloudbuild.yml')); console.log('Triggering builds for ', triggeredBuilds.join(', ')); function diff(fileOrDirName) { - const diffCmd = `diff -rq ${CLONE_PATH}/${fileOrDirName} ./${fileOrDirName}`; + const diffCmd = `diff -rq ` + + `${CLONE_PATH}/${fileOrDirName} ` + + `${fileOrDirName}`; return exec(diffCmd, {silent: true}, true).stdout.trim(); }