1
+ const yaml = require ( 'js-yaml' )
1
2
const mergeArrayByName = require ( './lib/mergeArrayByName' )
2
3
3
4
module . exports = ( robot , _ , Settings = require ( './lib/settings' ) ) => {
@@ -6,6 +7,42 @@ module.exports = (robot, _, Settings = require('./lib/settings')) => {
6
7
return Settings . sync ( context . github , repo , config )
7
8
}
8
9
10
+ robot . on ( 'installation' , async context => {
11
+ const { github, payload } = context
12
+ const { action, repositories, installation } = payload
13
+ const { account } = installation
14
+ const { login : repositoryOwner } = account
15
+
16
+ if ( action === 'deleted' ) {
17
+ robot . log . debug ( 'Integration deleted, returning...' )
18
+ return
19
+ }
20
+
21
+ await Promise . all ( repositories . map ( async ( repository ) => {
22
+ const { name : repositoryName } = repository
23
+
24
+ const repo = {
25
+ owner : repositoryOwner ,
26
+ repo : repositoryName
27
+ }
28
+
29
+ var res
30
+ try {
31
+ res = await github . repos . getContents ( Object . assign ( repo , { path : Settings . FILE_NAME } ) )
32
+ } catch ( error ) {
33
+ if ( error . status !== 404 ) {
34
+ robot . log . warn ( `Unknown error ${ error . status } occurred when fetching '${ Settings . FILE_NAME } ' in '${ repositoryOwner } /${ repositoryName } ', returning...` )
35
+ } else {
36
+ robot . log . debug ( `File '${ Settings . FILE_NAME } ' not found in '${ repositoryOwner } /${ repositoryName } ', returning...` )
37
+ }
38
+ return
39
+ }
40
+
41
+ const config = yaml . safeLoad ( Buffer . from ( res . data . content , 'base64' ) . toString ( ) ) || { }
42
+ return Settings . sync ( context . github , repo , config )
43
+ } ) )
44
+ } )
45
+
9
46
robot . on ( 'push' , async context => {
10
47
const { payload } = context
11
48
const { repository } = payload
0 commit comments