Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync settings after App installation #178

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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