Skip to content

Commit

Permalink
refactor: lib/common/reset-repositories.js
Browse files Browse the repository at this point in the history
  • Loading branch information
gr2m committed Jun 1, 2019
1 parent d6603bb commit 08a037f
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 68 deletions.
53 changes: 53 additions & 0 deletions lib/common/reset-repositories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
module.exports = resetRepositories

const getPlan = require('../get-plan')
const handlePullRequestChange = require('../handle-pull-request-change')

async function resetRepositories ({ app, context, account, repositories }, repo) {
const repositoryNames = repositories.map(repository => repository.name)
const owner = account.login
const plan = await getPlan(app, account)

const promises = repositoryNames.map(async repo => {
const pullRequests = await getPullRequests({ context, owner, repo })
await Promise.all(pullRequests.map(pullRequest => {
const event = toEvent({ context, plan, owner, repo, pullRequest })
return handlePullRequestChange(null, event)
}))
})

await Promise.all(promises)
}

async function getPullRequests ({ context, owner, repo }) {
const options = context.github.pulls.list.endpoint.merge({
owner,
repo,
state: 'open',
sort: 'updated',
direction: 'desc',
per_page: 100
})

return context.github.paginate(options)
}

function toEvent ({ context, plan, owner, repo, pullRequest }) {
return {
event: 'pull_request',
plan,
github: context.github,
log: context.log,
repo (options) {
return Object.assign({
owner: owner,
repo
}, options)
},
payload: {
action: 'opened',
pull_request: pullRequest,
repository: pullRequest.base.repo
}
}
}
92 changes: 24 additions & 68 deletions lib/handle-installation.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,10 @@ module.exports = handleInstallation
const pluralize = require('pluralize')

const getConfig = require('./app-config')
const getPlan = require('./get-plan')
const handlePullRequestChange = require('./handle-pull-request-change')
const resetRepositories = require('./common/reset-repositories')

// On install or accepted permissions, update all PRs for installs
// On uninstall, just log
//
//
async function handleInstallation (app, context) {
const { action, repositories, repositories_added: added, repositories_removed: removed, installation: { account, repository_selection: selection } } = context.payload

Expand All @@ -27,83 +24,42 @@ async function handleInstallation (app, context) {
return
}

if (action === 'created') {
log.info(`🤗 ${account.type} ${account.login} installed on ${pluralize('repository', repositories.length, true)}`)
const repositoryNames = repositories.map(repository => repository.name)
const plan = await getPlan(app, account)
await Promise.all(repositoryNames.map(reset.bind(null, {
plan,
context,
owner: account.login
})))
if (action === 'removed') {
log.info(`➖ ${account.type} ${account.login} removed ${pluralize('repository', removed.length, true)}`)
return
}

if (action === 'new_permissions_accepted') {
log.info(`👌 ${account.type} ${account.login} accepted new permissions`)

const options = context.github.apps.listRepos.endpoint.merge({
per_page: 100
})
if (action === 'created') {
log.info(`🤗 ${account.type} ${account.login} installed on ${pluralize('repository', repositories.length, true)}`)

const repositories = await context.github.paginate(options)
const repositoryNames = repositories.map(repository => repository.name)
const plan = await getPlan(app, account)
await Promise.all(repositoryNames.map(reset.bind(null, {
plan,
return resetRepositories({
app,
context,
owner: account.login
})))
return
account,
repositories
})
}

if (action === 'added') {
log.info(`➕ ${account.type} ${account.login} added ${pluralize('repository', added.length, true)}`)

const repositoryNames = added.map(repository => repository.name)
const plan = await getPlan(app, account)

await Promise.all(repositoryNames.map(reset.bind(null, {
plan,
return resetRepositories({
app,
context,
owner: account.login
})))
return
account,
repositories: added
})
}

log.info(`➖ ${account.type} ${account.login} removed ${pluralize('repository', removed.length, true)}`)
}
// action === new_permissions_accepted
log.info(`👌 ${account.type} ${account.login} accepted new permissions`)

async function reset ({ plan, owner, context }, repo) {
const options = context.github.pulls.list.endpoint.merge({
owner,
repo,
state: 'open',
sort: 'updated',
direction: 'desc',
per_page: 100
})
const options = context.github.apps.listRepos.endpoint.merge({ per_page: 100 })

const pullRequests = await context.github.paginate(options)
await Promise.all(
pullRequests.map(pullRequest => {
return handlePullRequestChange(null, {
plan,
github: context.github,
log: context.log,
repo (options) {
return Object.assign({
owner: owner,
repo
}, options)
},
event: 'pull_request',
payload: {
action: 'opened',
pull_request: pullRequest,
repository: pullRequest.base.repo
}
})
})
)
return resetRepositories({
app,
context,
account,
repositories: await context.github.paginate(options)
})
}

0 comments on commit 08a037f

Please sign in to comment.