From 973fd50d7355d15eaf59bc077a99329943cd2e34 Mon Sep 17 00:00:00 2001 From: Andrew Clark Date: Mon, 1 Feb 2021 10:30:50 -0600 Subject: [PATCH] Add automatic retry to download script (#20704) If build job is still pending, the script will continously poll until it reaches the retry limit. I've set the limit at 10 minutes, since our CI pipeline almost always finishes before that. --- scripts/release/get-build-id-for-commit.js | 57 ++++++++++++++-------- 1 file changed, 36 insertions(+), 21 deletions(-) diff --git a/scripts/release/get-build-id-for-commit.js b/scripts/release/get-build-id-for-commit.js index 88350e99d280d..fca6d0280d3a3 100644 --- a/scripts/release/get-build-id-for-commit.js +++ b/scripts/release/get-build-id-for-commit.js @@ -2,33 +2,48 @@ const fetch = require('node-fetch'); +const POLLING_INTERVAL = 5 * 1000; // 5 seconds +const RETRY_TIMEOUT = 10 * 60 * 1000; // 10 minutes + +function wait(ms) { + return new Promise(resolve => { + setTimeout(() => resolve(), ms); + }); +} + async function getBuildIdForCommit(sha) { - let ciBuildId = null; - const statusesResponse = await fetch( - `https://api.github.com/repos/facebook/react/commits/${sha}/status` - ); + const retryLimit = Date.now() + RETRY_TIMEOUT; + retry: while (true) { + const statusesResponse = await fetch( + `https://api.github.com/repos/facebook/react/commits/${sha}/status` + ); - if (!statusesResponse.ok) { - throw Error('Could not find commit for: ' + sha); - } + if (!statusesResponse.ok) { + throw Error('Could not find commit for: ' + sha); + } - const {statuses, state} = await statusesResponse.json(); - if (state === 'failure') { - throw new Error(`Base commit is broken: ${sha}`); - } - for (let i = 0; i < statuses.length; i++) { - const status = statuses[i]; - if (status.context === `ci/circleci: process_artifacts_combined`) { - if (status.state === 'success') { - ciBuildId = /\/facebook\/react\/([0-9]+)/.exec(status.target_url)[1]; - break; - } - if (status.state === 'pending') { - throw new Error(`Build job for base commit is still pending: ${sha}`); + const {statuses, state} = await statusesResponse.json(); + if (state === 'failure') { + throw new Error(`Base commit is broken: ${sha}`); + } + for (let i = 0; i < statuses.length; i++) { + const status = statuses[i]; + if (status.context === `ci/circleci: process_artifacts_combined`) { + if (status.state === 'success') { + return /\/facebook\/react\/([0-9]+)/.exec(status.target_url)[1]; + } + if (status.state === 'failure') { + throw new Error(`Build job for commit failed: ${sha}`); + } + if (Date.now() < retryLimit) { + await wait(POLLING_INTERVAL); + continue retry; + } + throw new Error('Exceeded retry limit. Build job is still pending.'); } } + throw new Error('Could not find build for commit: ' + sha); } - return ciBuildId; } module.exports = getBuildIdForCommit;