1414// limitations under the License.
1515// =============================================================================
1616
17- const { exec} = require ( './test-util' ) ;
17+ const { exec, constructDependencyGraph, computeAffectedPackages} =
18+ require ( './test-util' ) ;
1819const shell = require ( 'shelljs' ) ;
1920const { readdirSync, statSync, writeFileSync} = require ( 'fs' ) ;
2021const { join} = require ( 'path' ) ;
2122const fs = require ( 'fs' ) ;
2223
2324const filesWhitelistToTriggerBuild = [
2425 'cloudbuild.yml' , 'package.json' , 'tsconfig.json' , 'tslint.json' ,
25- 'scripts/diff .js' , 'scripts/run-build.sh'
26+ 'scripts/find-affected-packages .js' , 'scripts/run-build.sh'
2627] ;
2728
2829const CLONE_PATH = 'clone' ;
@@ -86,7 +87,7 @@ console.log(); // Break up the console for readability.
8687
8788let triggeredBuilds = [ ] ;
8889dirs . forEach ( dir => {
89- shell . rm ( '-f' , `${ dir } /diff ` ) ;
90+ shell . rm ( '-f' , `${ dir } /run-ci ` ) ;
9091 const diffOutput = diff ( `${ dir } /` ) ;
9192 if ( diffOutput !== '' ) {
9293 console . log ( `${ dir } has modified files.` ) ;
@@ -97,13 +98,31 @@ dirs.forEach(dir => {
9798 const shouldDiff = diffOutput !== '' || triggerAllBuilds ;
9899 if ( shouldDiff ) {
99100 const diffContents = whitelistDiffOutput . join ( '\n' ) + '\n' + diffOutput ;
100- writeFileSync ( join ( dir , 'diff ' ) , diffContents ) ;
101+ writeFileSync ( join ( dir , 'run-ci ' ) , diffContents ) ;
101102 triggeredBuilds . push ( dir ) ;
102103 }
103104} ) ;
104105
105106console . log ( ) ; // Break up the console for readability.
106107
108+ // Only add affected packages if not triggering all builds.
109+ if ( ! triggerAllBuilds ) {
110+ console . log ( 'Computing affected packages.' ) ;
111+ const affectedBuilds = new Set ( ) ;
112+ const dependencyGraph =
113+ constructDependencyGraph ( 'scripts/package_dependencies.json' ) ;
114+ triggeredBuilds . forEach ( triggeredBuild => {
115+ const affectedPackages =
116+ computeAffectedPackages ( dependencyGraph , triggeredBuild ) ;
117+ affectedPackages . forEach ( package => {
118+ writeFileSync ( join ( package , 'run-ci' ) ) ;
119+ affectedBuilds . add ( package ) ;
120+ } ) ;
121+ } ) ;
122+
123+ triggeredBuilds . push ( Array . from ( affectedBuilds ) ) ;
124+ }
125+
107126// Filter the triggered builds to log by whether a cloudbuild.yml file
108127// exists for that directory.
109128triggeredBuilds = triggeredBuilds . filter (
0 commit comments