|
| 1 | +'use strict' |
| 2 | + |
| 3 | +const github = require('../lib/github') |
| 4 | +const logger = require('./logger') |
| 5 | +const context = require('./context') |
| 6 | +const gitURLParse = require('git-url-parse') |
| 7 | + |
| 8 | +const debug = logger('wiby:closepr') |
| 9 | + |
| 10 | +module.exports = async ({ dependents }) => { |
| 11 | + const closedPrs = [] |
| 12 | + for (const { repository: url, pullRequest } of dependents) { |
| 13 | + if (pullRequest) { |
| 14 | + const dependentPkgInfo = gitURLParse(url) |
| 15 | + const parentBranchName = await context.getParentBranchName() |
| 16 | + const branch = await context.getTestingBranchName(parentBranchName) |
| 17 | + const resp = await github.getChecks(dependentPkgInfo.owner, dependentPkgInfo.name, branch) |
| 18 | + const closedPR = await closeDependencyPR(dependentPkgInfo.owner, dependentPkgInfo.name, branch, resp.data.check_runs) |
| 19 | + if (closedPR) { |
| 20 | + closedPrs.push(closedPR) |
| 21 | + } |
| 22 | + } |
| 23 | + } |
| 24 | + return closedPrs |
| 25 | +} |
| 26 | + |
| 27 | +const closeDependencyPR = module.exports.closeDependencyPR = async function closeDependencyPR (owner, repo, branch, checkRuns) { |
| 28 | + if (!checkRuns) { |
| 29 | + return |
| 30 | + } |
| 31 | + // TODO, in reality multiple checks could exist and they may not all have passed |
| 32 | + const prsToClose = checkRuns.reduce((acc, check) => { |
| 33 | + if (check.status === 'completed' && |
| 34 | + check.conclusion === 'success' && |
| 35 | + check.pull_requests && |
| 36 | + check.pull_requests.length !== 0) { |
| 37 | + check.pull_requests.forEach((pr) => { |
| 38 | + if (pr.head.ref === branch) { |
| 39 | + acc.push({ |
| 40 | + number: pr.number |
| 41 | + }) |
| 42 | + } |
| 43 | + }) |
| 44 | + } |
| 45 | + return acc |
| 46 | + }, []) |
| 47 | + debug(`Dependent module: ${JSON.stringify(prsToClose, null, 2)}`) |
| 48 | + return await Promise.all(prsToClose.map((pr) => github.closePR(owner, repo, pr.number))) |
| 49 | +} |
0 commit comments