@@ -13,13 +13,15 @@ let deploymentConfig
1313module . exports = ( robot , { getRouter } , Settings = require ( './lib/settings' ) ) => {
1414 let appName = 'safe-settings'
1515 let appSlug = 'safe-settings'
16+
17+ // All repos _could_ be affected, so sync everything
1618 async function syncAllSettings ( nop , context , repo = context . repo ( ) , ref ) {
1719 try {
18- deploymentConfig = await loadYamlFileSystem ( )
19- robot . log . debug ( `deploymentConfig is ${ JSON . stringify ( deploymentConfig ) } ` )
20+ robot . log . info ( 'Synchronizing all settings' )
21+
2022 const configManager = new ConfigManager ( context , ref )
21- const runtimeConfig = await configManager . loadGlobalSettingsYaml ( )
22- const config = Object . assign ( { } , deploymentConfig , runtimeConfig )
23+ const config = await configManager . loadGlobalSettingsYaml ( )
24+
2325 robot . log . debug ( `config for ref ${ ref } is ${ JSON . stringify ( config ) } ` )
2426 if ( ref ) {
2527 return Settings . syncAll ( nop , context , repo , config , ref )
@@ -42,13 +44,14 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
4244 }
4345 }
4446
47+ // Only the suborg has been modified, so only sync that
4548 async function syncSubOrgSettings ( nop , context , suborg , repo = context . repo ( ) , ref ) {
4649 try {
47- deploymentConfig = await loadYamlFileSystem ( )
48- robot . log . debug ( `deploymentConfig is ${ JSON . stringify ( deploymentConfig ) } ` )
50+ robot . log . info ( `Synchronizing settings for suborg: ${ suborg } ` )
51+
4952 const configManager = new ConfigManager ( context , ref )
50- const runtimeConfig = await configManager . loadGlobalSettingsYaml ( )
51- const config = Object . assign ( { } , deploymentConfig , runtimeConfig )
53+ const config = await configManager . loadGlobalSettingsYaml ( )
54+
5255 robot . log . debug ( `config for ref ${ ref } is ${ JSON . stringify ( config ) } ` )
5356 return Settings . syncSubOrgs ( nop , context , suborg , repo , config , ref )
5457 } catch ( e ) {
@@ -67,13 +70,14 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
6770 }
6871 }
6972
73+ // Only the repository has been modified, so only sync that
7074 async function syncSettings ( nop , context , repo = context . repo ( ) , ref ) {
7175 try {
72- deploymentConfig = await loadYamlFileSystem ( )
73- robot . log . debug ( `deploymentConfig is ${ JSON . stringify ( deploymentConfig ) } ` )
76+ robot . log . info ( `Synchronizing settings for repo: ${ repo } ` )
77+
7478 const configManager = new ConfigManager ( context , ref )
75- const runtimeConfig = await configManager . loadGlobalSettingsYaml ( )
76- const config = Object . assign ( { } , deploymentConfig , runtimeConfig )
79+ const config = await configManager . loadGlobalSettingsYaml ( )
80+
7781 robot . log . debug ( `config for ref ${ ref } is ${ JSON . stringify ( config ) } ` )
7882 return Settings . sync ( nop , context , repo , config , ref )
7983 } catch ( e ) {
@@ -258,47 +262,57 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
258262 return null
259263 }
260264
265+ // On any push
261266 robot . on ( 'push' , async context => {
262267 const { payload } = context
263268 const { repository } = payload
264269
265- const adminRepo = repository . name === env . ADMIN_REPO
266- if ( ! adminRepo ) {
267- return
268- }
269-
270270 const defaultBranch = payload . ref === 'refs/heads/' + repository . default_branch
271271 if ( ! defaultBranch ) {
272272 robot . log . debug ( 'Not working on the default branch, returning...' )
273273 return
274274 }
275275
276- const settingsModified = payload . commits . find ( commit => {
277- return commit . added . includes ( Settings . FILE_NAME ) ||
278- commit . modified . includes ( Settings . FILE_NAME )
279- } )
280- if ( settingsModified ) {
281- robot . log . debug ( `Changes in '${ Settings . FILE_NAME } ' detected, doing a full synch...` )
282- return syncAllSettings ( false , context )
283- }
276+ const adminRepo = repository . name === env . ADMIN_REPO
277+ if ( adminRepo ) {
278+ const settingsModified = payload . commits . find ( commit => {
279+ return commit . added . includes ( Settings . FILE_NAME ) ||
280+ commit . modified . includes ( Settings . FILE_NAME )
281+ } )
282+ if ( settingsModified ) {
283+ robot . log . debug ( `Changes in '${ Settings . FILE_NAME } ' detected, doing a full sync...` )
284+ return syncAllSettings ( false , context )
285+ }
284286
285- const repoChanges = getAllChangedRepoConfigs ( payload , context . repo ( ) . owner )
286- if ( repoChanges . length > 0 ) {
287- return Promise . all ( repoChanges . map ( repo => {
288- return syncSettings ( false , context , repo )
289- } ) )
290- }
287+ const repoChanges = getAllChangedRepoConfigs ( payload , context . repo ( ) . owner )
288+ if ( repoChanges . length > 0 ) {
289+ return Promise . all ( repoChanges . map ( repo => {
290+ return syncSettings ( false , context , repo )
291+ } ) )
292+ }
291293
292- const changes = getAllChangedSubOrgConfigs ( payload )
293- if ( changes . length ) {
294- return Promise . all ( changes . map ( suborg => {
295- return syncSubOrgSettings ( false , context , suborg )
296- } ) )
297- }
294+ const changes = getAllChangedSubOrgConfigs ( payload )
295+ if ( changes . length ) {
296+ return Promise . all ( changes . map ( suborg => {
297+ return syncSubOrgSettings ( false , context , suborg )
298+ } ) )
299+ }
298300
299- robot . log . debug ( `No changes in '${ Settings . FILE_NAME } ' detected, returning...` )
301+ robot . log . debug ( `No changes in '${ Settings . FILE_NAME } ' detected, returning...` )
302+ } else {
303+ const settingsModified = payload . commits . find ( commit => {
304+ return commit . added . includes ( '.github/settings.yml' ) ||
305+ commit . modified . includes ( '.github/settings.yml' )
306+ } )
307+ if ( settingsModified ) {
308+ robot . log . debug ( `Changes in '.github/settings.yml' detected, doing a sync for ${ repository . name } ...` )
309+ return syncSettings ( false , context )
310+ }
311+ }
300312 } )
301313
314+ // Invoke syncSettings for events that could cause drift
315+
302316 robot . on ( 'create' , async context => {
303317 const { payload } = context
304318 const { sender } = payload
@@ -394,6 +408,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
394408 return syncSettings ( false , context )
395409 } )
396410
411+ // Renames need some extra handling
397412 robot . on ( 'repository.renamed' , async context => {
398413 if ( env . BLOCK_REPO_RENAME_BY_HUMAN !== 'true' ) {
399414 robot . log . debug ( `"env.BLOCK_REPO_RENAME_BY_HUMAN" is 'false' by default. Repo rename is not managed by Safe-settings. Continue with the default behavior.` )
@@ -474,7 +489,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
474489 }
475490 } )
476491
477-
492+ // Validate settings when PR checks are needed
478493 robot . on ( 'check_suite.requested' , async context => {
479494 const { payload } = context
480495 const { repository } = payload
@@ -503,6 +518,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
503518 return createCheckRun ( context , pull_request , headSha , headBranch )
504519 } )
505520
521+ // Validate settings when PR checks are needed
506522 robot . on ( 'pull_request.opened' , async context => {
507523 robot . log . debug ( 'Pull_request opened !' )
508524 const { payload } = context
@@ -522,6 +538,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
522538 return createCheckRun ( context , pull_request , payload . pull_request . head . sha , payload . pull_request . head . ref )
523539 } )
524540
541+ // Validate settings when PR checks are needed
525542 robot . on ( 'pull_request.reopened' , async context => {
526543 robot . log . debug ( 'Pull_request REopened !' )
527544 const { payload } = context
@@ -543,16 +560,13 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
543560 return createCheckRun ( context , pull_request , payload . pull_request . head . sha , payload . pull_request . head . ref )
544561 } )
545562
563+ // Validate settings when PR checks are needed
546564 robot . on ( [ 'check_suite.rerequested' ] , async context => {
547565 robot . log . debug ( 'Check suite was rerequested!' )
548566 return createCheckRun ( context )
549567 } )
550568
551- robot . on ( [ 'check_suite.rerequested' ] , async context => {
552- robot . log . debug ( 'Check suite was rerequested!' )
553- return createCheckRun ( context )
554- } )
555-
569+ // Validate settings when PR checks are needed
556570 robot . on ( [ 'check_run.created' ] , async context => {
557571 robot . log . debug ( 'Check run was created!' )
558572 const { payload } = context
0 commit comments