11const path = require ( 'path' )
22const { Eta } = require ( 'eta' )
3+ const jsonPointer = require ( 'json-pointer' )
4+ const lodashSet = require ( 'lodash.set' )
35const commetMessageTemplate = require ( './commentmessage' )
46const errorTemplate = require ( './error' )
7+ const ConfigManager = require ( './configManager' )
58const Glob = require ( './glob' )
69const NopCommand = require ( './nopcommand' )
710const MergeDeep = require ( './mergeDeep' )
@@ -298,11 +301,6 @@ ${this.results.reduce((x, y) => {
298301
299302 async updateRepos ( repo ) {
300303 this . subOrgConfigs = this . subOrgConfigs || await this . getSubOrgConfigs ( )
301- let repoConfig = this . config . repository
302- if ( repoConfig ) {
303- repoConfig = Object . assign ( repoConfig , { name : repo . repo , org : repo . owner } )
304- }
305-
306304 const subOrgConfig = this . getSubOrgConfig ( repo . repo )
307305
308306 // If suborg config has been updated then only restrict to the repos for that suborg
@@ -313,6 +311,14 @@ ${this.results.reduce((x, y) => {
313311
314312 this . log . debug ( `Process normally... Not a SubOrg config change or SubOrg config was changed and this repo is part of it. ${ JSON . stringify ( repo ) } suborg config ${ JSON . stringify ( this . subOrgConfigMap ) } ` )
315313
314+ let repoConfig = this . config . repository
315+
316+ // Overlay with repo information
317+ if ( repoConfig ) {
318+ repoConfig = Object . assign ( repoConfig , { name : repo . repo , org : repo . owner } )
319+ }
320+
321+ // Overlay with suborg
316322 if ( subOrgConfig ) {
317323 let suborgRepoConfig = subOrgConfig . repository
318324 if ( suborgRepoConfig ) {
@@ -321,19 +327,26 @@ ${this.results.reduce((x, y) => {
321327 }
322328 }
323329
324- // Overlay repo config
330+ // Overlay with centralized repo config
325331 // RepoConfigs should be preloaded but checking anyway
326332 const overrideRepoConfig = this . repoConfigs [ `${ repo . repo } .yml` ] ?. repository || this . repoConfigs [ `${ repo . repo } .yaml` ] ?. repository
327333 if ( overrideRepoConfig ) {
328334 repoConfig = this . mergeDeep . mergeDeep ( { } , repoConfig , overrideRepoConfig )
329335 }
336+
337+ // Overlay with decentralized repo config
338+ const unsafeRepoOverrideConfig = ( await this . getUnsafeRepoConfig ( repo ) ) ?. repository
339+ if ( unsafeRepoOverrideConfig ) {
340+ repoConfig = this . mergeDeep . mergeDeep ( { } , repoConfig , unsafeRepoOverrideConfig )
341+ }
342+
330343 const { shouldContinue, nopCommands} = await new Archive ( this . nop , this . github , repo , repoConfig , this . log ) . sync ( )
331344 if ( nopCommands ) this . appendToResults ( nopCommands )
332345 if ( shouldContinue ) {
333346 if ( repoConfig ) {
334347 try {
335348 this . log . debug ( `found a matching repoconfig for this repo ${ JSON . stringify ( repoConfig ) } ` )
336- const childPlugins = this . childPluginsList ( repo )
349+ const childPlugins = await this . childPluginsList ( repo )
337350 const RepoPlugin = Settings . PLUGINS . repository
338351 return new RepoPlugin ( this . nop , this . github , repo , repoConfig , this . installation_id , this . log , this . errors ) . sync ( ) . then ( res => {
339352 this . appendToResults ( res )
@@ -356,7 +369,7 @@ ${this.results.reduce((x, y) => {
356369 }
357370 } else {
358371 this . log . debug ( `Didnt find any a matching repoconfig for this repo ${ JSON . stringify ( repo ) } in ${ JSON . stringify ( this . repoConfigs ) } ` )
359- const childPlugins = this . childPluginsList ( repo )
372+ const childPlugins = await this . childPluginsList ( repo )
360373 return Promise . all ( childPlugins . map ( ( [ Plugin , config ] ) => {
361374 return new Plugin ( this . nop , this . github , repo , config , this . log , this . errors ) . sync ( ) . then ( res => {
362375 this . appendToResults ( res )
@@ -379,26 +392,59 @@ ${this.results.reduce((x, y) => {
379392 for ( const k of Object . keys ( this . subOrgConfigs ) ) {
380393 const repoPattern = new Glob ( k )
381394 if ( repoName . search ( repoPattern ) >= 0 ) {
382- return this . subOrgConfigs [ k ]
395+ const subOrgConfig = this . subOrgConfigs [ k ]
396+ // Coerce 'repositories' to 'repository'
397+ subOrgConfig . repository = subOrgConfig . repositories
398+ delete subOrgConfig . repositories
399+ return subOrgConfig
383400 }
384401 }
385402 }
386403 return undefined
387404 }
388405
406+ async getUnsafeRepoConfig ( repo ) {
407+ const repoConfig = await ConfigManager . loadYaml ( this . github , {
408+ ...repo ,
409+ path : '.github/settings.yml'
410+ } )
411+
412+ const { unsafeFields } = this . config
413+
414+ const result = { }
415+ for ( const unsafeField of unsafeFields ) {
416+ let value
417+ try {
418+ value = jsonPointer . get ( repoConfig , unsafeField )
419+ } catch { }
420+
421+ if ( value !== undefined ) {
422+ lodashSet ( result , jsonPointer . parse ( unsafeField ) , value )
423+ }
424+ }
425+
426+ return result
427+ }
428+
389429 // Remove Org specific configs from the repo config
390430 returnRepoSpecificConfigs ( config ) {
391431 const newConfig = Object . assign ( { } , config ) // clone
392432 delete newConfig . rulesets
433+
434+ // Coerce 'repositories' to 'repository'
435+ newConfig . repository = newConfig . repositories
436+ delete newConfig . repositories
437+
393438 return newConfig
394439 }
395440
396- childPluginsList ( repo ) {
441+ async childPluginsList ( repo ) {
397442 const repoName = repo . repo
398443 const subOrgOverrideConfig = this . getSubOrgConfig ( repoName )
399- this . log . debug ( `suborg config for ${ repoName } is ${ JSON . stringify ( subOrgOverrideConfig ) } ` )
444+ this . log . debug ( `suborg config for ${ repoName } is ${ JSON . stringify ( subOrgOverrideConfig ) } ` )
400445 const repoOverrideConfig = this . getRepoOverrideConfig ( repoName )
401- const overrideConfig = this . mergeDeep . mergeDeep ( { } , this . returnRepoSpecificConfigs ( this . config ) , subOrgOverrideConfig , repoOverrideConfig )
446+ const unsafeRepoOverrideConfig = await this . getUnsafeRepoConfig ( repo )
447+ const overrideConfig = this . mergeDeep . mergeDeep ( { } , this . returnRepoSpecificConfigs ( this . config ) , subOrgOverrideConfig , repoOverrideConfig , unsafeRepoOverrideConfig )
402448
403449 this . log . debug ( `consolidated config is ${ JSON . stringify ( overrideConfig ) } ` )
404450
0 commit comments