1
- const yaml = require ( 'js-yaml' )
2
1
const mergeArrayByName = require ( './lib/mergeArrayByName' )
3
2
4
3
module . exports = ( robot , _ , Settings = require ( './lib/settings' ) ) => {
@@ -7,39 +6,64 @@ module.exports = (robot, _, Settings = require('./lib/settings')) => {
7
6
return Settings . sync ( context . github , repo , config )
8
7
}
9
8
10
- robot . on ( 'installation' , async context => {
11
- const { github, payload } = context
12
- const { action, repositories, installation } = payload
9
+ async function triggerRepositoryUpdate ( _context , { owner, repo } ) {
10
+ /* Clone context without reference */
11
+ const context = Object . assign ( Object . create ( Object . getPrototypeOf ( _context ) ) , _context )
12
+
13
+ /* Change context to target repository */
14
+ const { repository } = context . payload
15
+ context . payload . repository = Object . assign ( repository || { } , {
16
+ owner : {
17
+ login : owner
18
+ } ,
19
+ name : repo
20
+ } )
21
+
22
+ return syncSettings ( context , { owner, repo } )
23
+ }
24
+
25
+ robot . on ( [
26
+ 'installation.created' ,
27
+ 'installation.new_permissions_accepted'
28
+ ] , async context => {
29
+ const { payload } = context
30
+ const { repositories, installation } = payload
31
+ const { account } = installation
32
+ const { login : repositoryOwner } = account
33
+
34
+ if ( ! repositories ) {
35
+ robot . log . debug ( 'No new repositories found in the installation event, returning...' )
36
+ return
37
+ }
38
+
39
+ await Promise . all ( repositories . map ( async ( repository ) => {
40
+ const { name : repositoryName } = repository
41
+
42
+ return triggerRepositoryUpdate ( context , {
43
+ owner : repositoryOwner ,
44
+ repo : repositoryName
45
+ } )
46
+ } ) )
47
+ } )
48
+
49
+ robot . on ( 'installation_repositories.added' , async context => {
50
+ const { payload } = context
51
+ const { repositories_added : repositories , installation } = payload
13
52
const { account } = installation
14
53
const { login : repositoryOwner } = account
15
54
16
- if ( action === 'deleted' ) {
17
- robot . log . debug ( 'Integration deleted , returning...' )
55
+ if ( ! repositories ) {
56
+ robot . log . debug ( 'No new repositories found in the installation event , returning...' )
18
57
return
19
58
}
20
59
21
60
await Promise . all ( repositories . map ( async ( repository ) => {
22
61
const { name : repositoryName } = repository
23
62
24
- const repo = {
63
+ return triggerRepositoryUpdate ( context , {
25
64
owner : repositoryOwner ,
26
65
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 )
66
+ } )
43
67
} ) )
44
68
} )
45
69
0 commit comments