Skip to content

Commit

Permalink
Sync settings after App installation repository-settings#178
Browse files Browse the repository at this point in the history
  • Loading branch information
Pamela Sarkisyan authored Jul 29, 2021
1 parent 2236030 commit 16f5e83
Showing 1 changed file with 61 additions and 0 deletions.
61 changes: 61 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,67 @@ module.exports = (robot, _, Settings = require('./lib/settings')) => {
return Settings.sync(context.octokit, 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
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

0 comments on commit 16f5e83

Please sign in to comment.