Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,67 @@ module.exports = (robot, _, Settings = require('./lib/settings')) => {
return Settings.sync(context.github, repo, config)
}

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
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems that this is very similar to the installation event. Have you considered doing something like repositories = payload.repositories | payload.repositories_added and just combine the two events.

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('push', async context => {
const { payload } = context
const { repository } = payload
Expand Down