Skip to content

Commit d18b2a7

Browse files
committed
Sync settings after App installation
1 parent e293c44 commit d18b2a7

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

index.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const yaml = require('js-yaml')
12
const mergeArrayByName = require('./lib/mergeArrayByName')
23

34
module.exports = (robot, _, Settings = require('./lib/settings')) => {
@@ -6,6 +7,42 @@ module.exports = (robot, _, Settings = require('./lib/settings')) => {
67
return Settings.sync(context.github, repo, config)
78
}
89

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+
946
robot.on('push', async context => {
1047
const { payload } = context
1148
const { repository } = payload

0 commit comments

Comments
 (0)