From f8c50a75e2a32ce0184806b9f7b2c854db1566df Mon Sep 17 00:00:00 2001 From: Tak Tran Date: Wed, 7 Nov 2018 12:31:37 +0000 Subject: [PATCH] =?UTF-8?q?Revert=20"Get=20app=20name=20if=20there=20is=20?= =?UTF-8?q?a=20conflict=20in=20the=20review=20app.=20=F0=9F=90=BF=20v2.10.?= =?UTF-8?q?3"?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit This reverts commit cb414eefdf4565718519ddd617a2dacefc1a072d. Oops! --- tasks/review-app.js | 75 ++++++++++++++------------------------------- 1 file changed, 23 insertions(+), 52 deletions(-) diff --git a/tasks/review-app.js b/tasks/review-app.js index 7c1cc108..983b4974 100644 --- a/tasks/review-app.js +++ b/tasks/review-app.js @@ -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 @@ -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) { @@ -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 });