Skip to content

Commit

Permalink
Clone and change context to use built-in probot functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
mvegter committed Dec 8, 2019
1 parent d18b2a7 commit c3cdb68
Showing 1 changed file with 47 additions and 23 deletions.
70 changes: 47 additions & 23 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
const yaml = require('js-yaml')
const mergeArrayByName = require('./lib/mergeArrayByName')

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

robot.on('installation', async context => {
const { github, payload } = context
const { action, repositories, installation } = payload
async function triggerRepositoryUpdate (_context, { owner, repo }) {
/* Clone context without reference */
const context = Object.assign(Object.create(Object.getPrototypeOf(_context)), _context)

/* Change context to target repository */
const { repository } = context.payload
context.payload.repository = Object.assign(repository || {}, {
owner: {
login: owner
},
name: repo
})

return syncSettings(context, { owner, repo })
}

robot.on([
'installation.created',
'installation.new_permissions_accepted'
], async context => {
const { payload } = context
const { repositories, installation } = payload
const { account } = installation
const { login: repositoryOwner } = account

if (!repositories) {
robot.log.debug('No new repositories found in the installation event, returning...')
return
}

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

return triggerRepositoryUpdate(context, {
owner: repositoryOwner,
repo: repositoryName
})
}))
})

robot.on('installation_repositories.added', async context => {
const { payload } = context
const { repositories_added: repositories, installation } = payload
const { account } = installation
const { login: repositoryOwner } = account

if (action === 'deleted') {
robot.log.debug('Integration deleted, returning...')
if (!repositories) {
robot.log.debug('No new repositories found in the installation event, returning...')
return
}

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

const repo = {
return triggerRepositoryUpdate(context, {
owner: repositoryOwner,
repo: repositoryName
}

var res
try {
res = await github.repos.getContents(Object.assign(repo, { path: Settings.FILE_NAME }))
} catch (error) {
if (error.status !== 404) {
robot.log.warn(`Unknown error ${error.status} occurred when fetching '${Settings.FILE_NAME}' in '${repositoryOwner}/${repositoryName}', returning...`)
} else {
robot.log.debug(`File '${Settings.FILE_NAME}' not found in '${repositoryOwner}/${repositoryName}', returning...`)
}
return
}

const config = yaml.safeLoad(Buffer.from(res.data.content, 'base64').toString()) || {}
return Settings.sync(context.github, repo, config)
})
}))
})

Expand Down

0 comments on commit c3cdb68

Please sign in to comment.