Skip to content

Commit c3cdb68

Browse files
committed
Clone and change context to use built-in probot functionality
1 parent d18b2a7 commit c3cdb68

File tree

1 file changed

+47
-23
lines changed

1 file changed

+47
-23
lines changed

index.js

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

43
module.exports = (robot, _, Settings = require('./lib/settings')) => {
@@ -7,39 +6,64 @@ module.exports = (robot, _, Settings = require('./lib/settings')) => {
76
return Settings.sync(context.github, repo, config)
87
}
98

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
1352
const { account } = installation
1453
const { login: repositoryOwner } = account
1554

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...')
1857
return
1958
}
2059

2160
await Promise.all(repositories.map(async (repository) => {
2261
const { name: repositoryName } = repository
2362

24-
const repo = {
63+
return triggerRepositoryUpdate(context, {
2564
owner: repositoryOwner,
2665
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+
})
4367
}))
4468
})
4569

0 commit comments

Comments
 (0)