diff --git a/src/backends/github/API.js b/src/backends/github/API.js index aad6cea9..ee927621 100644 --- a/src/backends/github/API.js +++ b/src/backends/github/API.js @@ -1,6 +1,7 @@ import LocalForage from "localforage"; import { Base64 } from "js-base64"; import _ from "lodash"; +import { filterPromises } from "../../lib/promiseHelper"; import AssetProxy from "../../valueObjects/AssetProxy"; import { SIMPLE, EDITORIAL_WORKFLOW, status } from "../../constants/publishModes"; import { APIError, EditorialWorkflowError } from "../../valueObjects/errors"; @@ -177,6 +178,15 @@ export default class API { listUnpublishedBranches() { console.log("%c Checking for Unpublished entries", "line-height: 30px;text-align: center;font-weight: bold"); // eslint-disable-line return this.request(`${ this.repoURL }/git/refs/heads/cms`) + .then(branches => filterPromises(branches, (branch) => { + const branchName = branch.ref.substring("/refs/heads/".length - 1); + + // Get PRs with a `head` of `branchName`. Note that this is a + // substring match, so we need to check that the `head.ref` of + // at least one of the returned objects matches `branchName`. + return this.request(`${ this.repoURL }/pulls?head=${ branchName }&state=open`) + .then(prs => prs.some(pr => pr.head.ref === branchName)); + })) .catch((error) => { console.log("%c No Unpublished entries", "line-height: 30px;text-align: center;font-weight: bold"); // eslint-disable-line throw error; diff --git a/src/lib/promiseHelper.js b/src/lib/promiseHelper.js new file mode 100644 index 00000000..b8724569 --- /dev/null +++ b/src/lib/promiseHelper.js @@ -0,0 +1,3 @@ +export const filterPromises = (arr, filter) => + Promise.all(arr.map(entry => filter(entry))) + .then(bits => arr.filter(entry => bits.shift()));