From 08a037f5f9d7e0c7c599398ad433233c9854ea2d Mon Sep 17 00:00:00 2001 From: Gregor Date: Fri, 31 May 2019 22:24:20 -0700 Subject: [PATCH] refactor: lib/common/reset-repositories.js --- lib/common/reset-repositories.js | 53 ++++++++++++++++++ lib/handle-installation.js | 92 +++++++++----------------------- 2 files changed, 77 insertions(+), 68 deletions(-) create mode 100644 lib/common/reset-repositories.js diff --git a/lib/common/reset-repositories.js b/lib/common/reset-repositories.js new file mode 100644 index 000000000..088a33e12 --- /dev/null +++ b/lib/common/reset-repositories.js @@ -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 + } + } +} diff --git a/lib/handle-installation.js b/lib/handle-installation.js index 8ad767359..b4885b914 100644 --- a/lib/handle-installation.js +++ b/lib/handle-installation.js @@ -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 @@ -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) + }) }