@@ -6,91 +6,22 @@ const Glob = require('./lib/glob')
6
6
const ConfigManager = require ( './lib/configManager' )
7
7
const NopCommand = require ( './lib/nopcommand' )
8
8
const env = require ( './lib/env' )
9
+ const { setupRoutes } = require ( './lib/routes' )
10
+ const { initCache } = require ( './lib/installationCache' )
11
+ const { hubSyncHandler } = require ( './lib/hubSyncHandler' )
9
12
10
13
let deploymentConfig
11
14
12
15
module . exports = ( robot , { getRouter } , Settings = require ( './lib/settings' ) ) => {
13
16
let appSlug = 'safe-settings'
14
- async function syncAllSettings ( nop , context , repo = context . repo ( ) , ref ) {
15
- try {
16
- deploymentConfig = await loadYamlFileSystem ( )
17
- robot . log . debug ( `deploymentConfig is ${ JSON . stringify ( deploymentConfig ) } ` )
18
- const configManager = new ConfigManager ( context , ref )
19
- const runtimeConfig = await configManager . loadGlobalSettingsYaml ( )
20
- const config = Object . assign ( { } , deploymentConfig , runtimeConfig )
21
- robot . log . debug ( `config for ref ${ ref } is ${ JSON . stringify ( config ) } ` )
22
- if ( ref ) {
23
- return Settings . syncAll ( nop , context , repo , config , ref )
24
- } else {
25
- return Settings . syncAll ( nop , context , repo , config )
26
- }
27
- } catch ( e ) {
28
- if ( nop ) {
29
- let filename = env . SETTINGS_FILE_PATH
30
- if ( ! deploymentConfig ) {
31
- filename = env . DEPLOYMENT_CONFIG_FILE_PATH
32
- deploymentConfig = { }
33
- }
34
- const nopcommand = new NopCommand ( filename , repo , null , e , 'ERROR' )
35
- robot . log . error ( `NOPCOMMAND ${ JSON . stringify ( nopcommand ) } ` )
36
- Settings . handleError ( nop , context , repo , deploymentConfig , ref , nopcommand )
37
- } else {
38
- throw e
39
- }
40
- }
41
- }
42
17
43
- async function syncSubOrgSettings ( nop , context , suborg , repo = context . repo ( ) , ref ) {
44
- try {
45
- deploymentConfig = await loadYamlFileSystem ( )
46
- robot . log . debug ( `deploymentConfig is ${ JSON . stringify ( deploymentConfig ) } ` )
47
- const configManager = new ConfigManager ( context , ref )
48
- const runtimeConfig = await configManager . loadGlobalSettingsYaml ( )
49
- const config = Object . assign ( { } , deploymentConfig , runtimeConfig )
50
- robot . log . debug ( `config for ref ${ ref } is ${ JSON . stringify ( config ) } ` )
51
- return Settings . syncSubOrgs ( nop , context , suborg , repo , config , ref )
52
- } catch ( e ) {
53
- if ( nop ) {
54
- let filename = env . SETTINGS_FILE_PATH
55
- if ( ! deploymentConfig ) {
56
- filename = env . DEPLOYMENT_CONFIG_FILE_PATH
57
- deploymentConfig = { }
58
- }
59
- const nopcommand = new NopCommand ( filename , repo , null , e , 'ERROR' )
60
- robot . log . error ( `NOPCOMMAND ${ JSON . stringify ( nopcommand ) } ` )
61
- Settings . handleError ( nop , context , repo , deploymentConfig , ref , nopcommand )
62
- } else {
63
- throw e
64
- }
65
- }
66
- }
18
+ // Initialize all routes (static UI + API) via centralized module
19
+ setupRoutes ( robot , getRouter )
67
20
68
- async function syncSettings ( nop , context , repo = context . repo ( ) , ref ) {
69
- try {
70
- deploymentConfig = await loadYamlFileSystem ( )
71
- robot . log . debug ( `deploymentConfig is ${ JSON . stringify ( deploymentConfig ) } ` )
72
- const configManager = new ConfigManager ( context , ref )
73
- const runtimeConfig = await configManager . loadGlobalSettingsYaml ( )
74
- const config = Object . assign ( { } , deploymentConfig , runtimeConfig )
75
- robot . log . debug ( `config for ref ${ ref } is ${ JSON . stringify ( config ) } ` )
76
- return Settings . sync ( nop , context , repo , config , ref )
77
- } catch ( e ) {
78
- if ( nop ) {
79
- let filename = env . SETTINGS_FILE_PATH
80
- if ( ! deploymentConfig ) {
81
- filename = env . DEPLOYMENT_CONFIG_FILE_PATH
82
- deploymentConfig = { }
83
- }
84
- const nopcommand = new NopCommand ( filename , repo , null , e , 'ERROR' )
85
- robot . log . error ( `NOPCOMMAND ${ JSON . stringify ( nopcommand ) } ` )
86
- Settings . handleError ( nop , context , repo , deploymentConfig , ref , nopcommand )
87
- } else {
88
- throw e
89
- }
90
- }
91
- }
21
+ // Initialize installation cache (env-controlled prefetch)
22
+ initCache ( robot )
92
23
93
- async function renameSync ( nop , context , repo = context . repo ( ) , rename , ref ) {
24
+ async function renameSync ( nop , context , repo = context . repo ( ) , rename , ref ) {
94
25
try {
95
26
deploymentConfig = await loadYamlFileSystem ( )
96
27
robot . log . debug ( `deploymentConfig is ${ JSON . stringify ( deploymentConfig ) } ` )
@@ -115,13 +46,14 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
115
46
}
116
47
}
117
48
}
49
+
118
50
/**
119
51
* Loads the deployment config file from file system
120
52
* Do this once when the app starts and then return the cached value
121
53
*
122
54
* @return The parsed YAML file
123
55
*/
124
- async function loadYamlFileSystem ( ) {
56
+ async function loadYamlFileSystem ( ) {
125
57
if ( deploymentConfig === undefined ) {
126
58
const deploymentConfigPath = env . DEPLOYMENT_CONFIG_FILE_PATH
127
59
if ( fs . existsSync ( deploymentConfigPath ) ) {
@@ -133,7 +65,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
133
65
return deploymentConfig
134
66
}
135
67
136
- function getAllChangedSubOrgConfigs ( payload ) {
68
+ function getAllChangedSubOrgConfigs ( payload ) {
137
69
const pattern = Settings . SUB_ORG_PATTERN
138
70
139
71
const getMatchingFiles = ( commits , type ) =>
@@ -150,7 +82,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
150
82
} ) )
151
83
}
152
84
153
- function getAllChangedRepoConfigs ( payload , owner ) {
85
+ function getAllChangedRepoConfigs ( payload , owner ) {
154
86
const pattern = Settings . REPO_PATTERN
155
87
156
88
const getMatchingFiles = ( commits , type ) =>
@@ -167,7 +99,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
167
99
} ) )
168
100
}
169
101
170
- function getChangedRepoConfigName ( files , owner ) {
102
+ function getChangedRepoConfigName ( files , owner ) {
171
103
const pattern = Settings . REPO_PATTERN
172
104
173
105
const modifiedFiles = files . filter ( ( s ) => pattern . test ( s ) )
@@ -178,7 +110,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
178
110
} ) )
179
111
}
180
112
181
- function getChangedSubOrgConfigName ( files ) {
113
+ function getChangedSubOrgConfigName ( files ) {
182
114
const pattern = Settings . SUB_ORG_PATTERN
183
115
184
116
const modifiedFiles = files . filter ( ( s ) => pattern . test ( s ) )
@@ -188,7 +120,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
188
120
path : modifiedFile
189
121
} ) )
190
122
}
191
- async function createCheckRun ( context , pull_request , head_sha , head_branch ) {
123
+ async function createCheckRun ( context , pull_request , head_sha , head_branch ) {
192
124
const { payload } = context
193
125
// robot.log.debug(`Check suite was requested! for ${context.repo()} ${pull_request.number} ${head_sha} ${head_branch}`)
194
126
const res = await context . octokit . checks . create ( {
@@ -200,7 +132,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
200
132
robot . log . debug ( JSON . stringify ( res , null ) )
201
133
}
202
134
203
- async function info ( ) {
135
+ async function info ( ) {
204
136
const github = await robot . auth ( )
205
137
const installations = await github . paginate (
206
138
github . apps . listInstallations . endpoint . merge ( { per_page : 100 } )
@@ -215,7 +147,7 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
215
147
}
216
148
}
217
149
218
- async function syncInstallation ( nop = false ) {
150
+ async function syncInstallation ( nop = false ) {
219
151
robot . log . trace ( 'Fetching installations' )
220
152
const github = await robot . auth ( )
221
153
@@ -521,6 +453,19 @@ module.exports = (robot, { getRouter }, Settings = require('./lib/settings')) =>
521
453
return createCheckRun ( context , pull_request , payload . pull_request . head . sha , payload . pull_request . head . ref )
522
454
} )
523
455
456
+ /**
457
+ * @description Handle pull_request.closed events to support hub synchronization
458
+ * @param {Object } context - The context object provided by Probot
459
+ */
460
+ robot . on ( 'pull_request.closed' , async context => {
461
+ try {
462
+ await hubSyncHandler ( robot , context )
463
+ } catch ( err ) {
464
+ robot . log . error ( `pull_request.closed handler failed: ${ err && err . message ? err . message : err } ` )
465
+ }
466
+ return null
467
+ } )
468
+
524
469
robot . on ( [ 'check_suite.rerequested' ] , async context => {
525
470
robot . log . debug ( 'Check suite was rerequested!' )
526
471
return createCheckRun ( context )
0 commit comments