From 6c180284587f881fb5d2fe278997cfb4f6d13457 Mon Sep 17 00:00:00 2001 From: Tejas Kumar Date: Wed, 19 May 2021 10:46:55 +0200 Subject: [PATCH] Refactor to remove recursion Signed-off-by: Tejas Kumar --- .../src/scaffolder/stages/publish/helpers.ts | 29 ++++++++----------- 1 file changed, 12 insertions(+), 17 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts b/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts index f1ad9617ce..6874efe06a 100644 --- a/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts +++ b/plugins/scaffolder-backend/src/scaffolder/stages/publish/helpers.ts @@ -80,10 +80,9 @@ export const enableBranchProtectionOnDefaultRepoBranch = async ({ repoName, client, owner, - isRetry = false, }: BranchProtectionOptions): Promise => { - try { - await client.repos.updateBranchProtection({ + const tryOnce = () => { + return client.repos.updateBranchProtection({ mediaType: { /** * 👇 we need this preview because allowing a custom @@ -102,21 +101,17 @@ export const enableBranchProtectionOnDefaultRepoBranch = async ({ enforce_admins: true, required_pull_request_reviews: { required_approving_review_count: 1 }, }); + }; + + try { + await tryOnce(); } catch (e) { - if (!isRetry && e.message.includes('Branch not found')) { - // GitHub has eventual consistency. Fail silently, wait, and try again. - await new Promise(resolve => setTimeout(resolve, 600)); - - await enableBranchProtectionOnDefaultRepoBranch({ - repoName, - client, - owner, - isRetry: true, - }); - - return; + if (!e.message.includes('Branch not found')) { + throw e; } - - throw e; } + + // GitHub has eventual consistency. Fail silently, wait, and try again. + await new Promise(resolve => setTimeout(resolve, 600)); + await tryOnce(); };