Skip to content
This repository has been archived by the owner on Mar 22, 2024. It is now read-only.

Commit

Permalink
Delete existing review app if it already exists before creating 🐿 v2.…
Browse files Browse the repository at this point in the history
…10.3
  • Loading branch information
taktran committed Oct 18, 2018
1 parent ae7580a commit 9b909ea
Showing 1 changed file with 33 additions and 3 deletions.
36 changes: 33 additions & 3 deletions tasks/review-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;

Expand Down Expand Up @@ -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;

Expand All @@ -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)
Expand Down

0 comments on commit 9b909ea

Please sign in to comment.