From 7a39edd3f9ddeefd07737ee429aad56c5c7a2cc4 Mon Sep 17 00:00:00 2001 From: Arthur Gavlyukovskiy Date: Tue, 9 Jan 2024 12:01:29 +0100 Subject: [PATCH] Remove nested try-catch Signed-off-by: Arthur Gavlyukovskiy --- .../src/actions/gitlabRepoPush.ts | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabRepoPush.ts b/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabRepoPush.ts index 59f51b263c..330d0f6f08 100644 --- a/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabRepoPush.ts +++ b/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabRepoPush.ts @@ -149,22 +149,27 @@ export const createGitlabRepoPushAction = (options: { execute_filemode: file.executable, })); + let branchExists = false; try { await api.Branches.show(repoID, branchName); - } catch (e) { + branchExists = true; + } catch (e: any) { if (e.response?.statusCode !== 404) { throw new InputError( `Failed to check status of branch '${branchName}'. Please make sure that branch already exists or Backstage has permissions to create one. ${e}`, ); } + } + + if (!branchExists) { // create a branch using the default branch as ref try { const projects = await api.Projects.show(repoID); const { default_branch: defaultBranch } = projects; await api.Branches.create(repoID, branchName, String(defaultBranch)); - } catch (e2) { + } catch (e) { throw new InputError( - `The branch '${branchName}' was not found and creation failed with error. Please make sure that branch already exists or Backstage has permissions to create one. ${e2}`, + `The branch '${branchName}' was not found and creation failed with error. Please make sure that branch already exists or Backstage has permissions to create one. ${e}`, ); } }