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

Commit

Permalink
Revert "Get app name if there is a conflict in the review app. 🐿 v2.1…
Browse files Browse the repository at this point in the history
…0.3"

This reverts commit cb414ee.

Oops!
  • Loading branch information
taktran committed Nov 7, 2018
1 parent cb414ee commit f8c50a7
Showing 1 changed file with 23 additions and 52 deletions.
75 changes: 23 additions & 52 deletions tasks/review-app.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ const getReviewAppUrl = reviewAppId => `https://api.heroku.com/review-apps/${rev
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}`;
const getBuildsUrl = appId => `https://api.heroku.com/apps/${appId}/builds`;

function herokuHeaders ({ useReviewAppApi } = {}) {
const defaultHeaders = useReviewAppApi
Expand Down Expand Up @@ -123,40 +122,23 @@ const getAppName = async (appId) => {
});
};

const findCreatedReviewApp = ({ pipelineId, branch }) => {
return fetch(getPipelineReviewAppsUrl(pipelineId))
const deleteGitBranchReviewApp = ({ pipelineId, branch, headers }) => {
const getReviewAppId = (pipelineId) => fetch(getPipelineReviewAppsUrl(pipelineId), {
headers
})
.then(throwIfNotOk)
.then(res => res.json())
.then((reviewApps = []) =>
reviewApps.find(({ branch: reviewAppBranch }) => reviewAppBranch === branch));
};

const getBuilds = (data) => {
const { app: { id } } = data;
return fetch(getBuildsUrl(id));
};

const waitForReviewAppBuild = (commit) => async (reviewApp) => {
const checkForBuildAppId = getBuilds(reviewApp)
.then(builds => {
return builds.find(({ source_blob: { version } }) =>
version === commit);
})
.then(build => {
if (!build) {
throw new Error (`Build for commit ${commit} not found`);
}
return build;
})
.then(({ app: { id } }) => id);
reviewApps.find(
({ branch: reviewAppBranch }) => branch === reviewAppBranch)
)
.then(({ id }) => id);
const deleteReviewApp = (reviewAppId) => fetch(getReviewAppUrl(reviewAppId), {
headers,
method: 'delete'
}).then(throwIfNotOk);

return pRetry(checkForBuildAppId, {
factor: RETRY_EXP_BACK_OFF_FACTOR,
retries: NUM_RETRIES,
minTimeout: RETRY_INTERVAL,
onFailedAttempt: (err) => {
const { attemptNumber, message } = err;
console.error(`${attemptNumber}/${NUM_RETRIES}: ${message}`); // eslint-disable-line no-console
}
});
return getReviewAppId(pipelineId).then(deleteReviewApp);
};

async function task (app, options) {
Expand All @@ -182,27 +164,16 @@ async function task (app, options) {
.then(res => {
const { status } = res;
if (status === 409) {
return findCreatedReviewApp({
pipelineId,
branch
})
.then(reviewApp => {
if (!reviewApp) {
throw new Error(`No review app found for pipeline ${pipelineId}, branch ${branch}`);
}

return reviewApp;
})
.then(waitTillReviewAppCreated)
.then(waitForReviewAppBuild(commit))
.then(getAppName);
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 Promise.resolve(res)
.then(throwIfNotOk)
.then(res => res.json())
.then(waitTillReviewAppCreated)
.then(getAppName);
return res;
})
.then(throwIfNotOk)
.then(res => res.json())
.then(waitTillReviewAppCreated)
.then(getAppName)
.then(appName => {
console.log(appName); // eslint-disable-line no-console
});
Expand Down

0 comments on commit f8c50a7

Please sign in to comment.