From 9b909ea9d81c6bcecee94bb717235e0563cc8852 Mon Sep 17 00:00:00 2001 From: Tak Tran Date: Thu, 18 Oct 2018 17:19:16 +0100 Subject: [PATCH] =?UTF-8?q?Delete=20existing=20review=20app=20if=20it=20al?= =?UTF-8?q?ready=20exists=20before=20creating=20=20=F0=9F=90=BF=20v2.10.3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tasks/review-app.js | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) diff --git a/tasks/review-app.js b/tasks/review-app.js index 7a8d48c8..983b4974 100644 --- a/tasks/review-app.js +++ b/tasks/review-app.js @@ -20,6 +20,7 @@ const REVIEW_APP_STATUSES = { }; const getReviewAppUrl = reviewAppId => `https://api.heroku.com/review-apps/${reviewAppId}`; +const getPipelineReviewAppsUrl = pipelineId => `https://api.heroku.com/pipelines/${pipelineId}/review-apps`; const getAppUrl = appId => `https://api.heroku.com/apps/${appId}`; const getGithubArchiveUrl = ({ repoName, branch }) => `https://api.github.com/repos/Financial-Times/${repoName}/tarball/${branch}`; @@ -121,6 +122,25 @@ const getAppName = async (appId) => { }); }; +const deleteGitBranchReviewApp = ({ pipelineId, branch, headers }) => { + const getReviewAppId = (pipelineId) => fetch(getPipelineReviewAppsUrl(pipelineId), { + headers + }) + .then(throwIfNotOk) + .then(res => res.json()) + .then((reviewApps = []) => + reviewApps.find( + ({ branch: reviewAppBranch }) => branch === reviewAppBranch) + ) + .then(({ id }) => id); + const deleteReviewApp = (reviewAppId) => fetch(getReviewAppUrl(reviewAppId), { + headers, + method: 'delete' + }).then(throwIfNotOk); + + return getReviewAppId(pipelineId).then(deleteReviewApp); +}; + async function task (app, options) { const { repoName, branch, commit, githubToken } = options; @@ -134,12 +154,22 @@ async function task (app, options) { version: commit } }; - - return fetch(REVIEW_APPS_URL, { + const createReviewApp = () => fetch(REVIEW_APPS_URL, { headers, method: 'post', body: JSON.stringify(body) - }) + }); + + return createReviewApp() + .then(res => { + const { status } = res; + if (status === 409) { + console.error(`Review app already created for ${branch} branch. Deleting existing review app first.`); // eslint-disable-line no-console + return deleteGitBranchReviewApp({ pipelineId, branch, headers }) + .then(createReviewApp); + } + return res; + }) .then(throwIfNotOk) .then(res => res.json()) .then(waitTillReviewAppCreated)