From 00498e48c967c236fde52a1a17ee05958501b755 Mon Sep 17 00:00:00 2001 From: Jasper Boeijenga Date: Thu, 20 Mar 2025 15:52:32 +0100 Subject: [PATCH 1/2] Added more feedback in case branch is already created Signed-off-by: Jasper Boeijenga --- .../src/actions/gitlabMergeRequest.ts | 44 ++++++++++++++----- 1 file changed, 32 insertions(+), 12 deletions(-) diff --git a/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabMergeRequest.ts b/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabMergeRequest.ts index 89ae1bddf2..49b7b501d4 100644 --- a/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabMergeRequest.ts +++ b/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabMergeRequest.ts @@ -414,20 +414,39 @@ which uses additional API calls in order to detect whether to 'create', 'update' execute_filemode: file.executable, })); - let createBranch: boolean; - if (actions.length) { - createBranch = true; - } else { - try { - await api.Branches.show(repoID, branchName); - createBranch = false; - ctx.logger.info( - `Using existing branch ${branchName} without modification.`, - ); - } catch (e) { - createBranch = true; + let createBranch = actions.length > 0; + + try { + const branch = await api.Branches.show(repoID, branchName); + if (createBranch) { + const mergeRequests = await api.MergeRequests.all({ + projectId: repoID, + source_branch: branchName, + }); + + if (mergeRequests.length > 0) { + // If an open MR exists, include the MR link in the error message + throw new InputError( + `The branch creation failed because the branch already exists at: ${branch.web_url}. Additionally, there is a Merge Request for this branch: ${mergeRequests[0].web_url}`, + ); + } else { + // If no open MR, just notify about the existing branch + throw new InputError( + `The branch creation failed because the branch already exists at: ${branch.web_url}.`, + ); + } } + + ctx.logger.info( + `Using existing branch ${branchName} without modification.`, + ); + } catch (e) { + if (e instanceof InputError) { + throw e; + } + createBranch = true; } + if (createBranch) { try { await api.Branches.create(repoID, branchName, String(targetBranch)); @@ -439,6 +458,7 @@ which uses additional API calls in order to detect whether to 'create', 'update' ); } } + await ctx.checkpoint({ key: `commit.to.${repoID}.${branchName}`, fn: async () => { From d86ce219a6602197ca99d944778815f685eb75e8 Mon Sep 17 00:00:00 2001 From: Jasper Boeijenga Date: Thu, 20 Mar 2025 15:55:16 +0100 Subject: [PATCH 2/2] Added changeset Signed-off-by: Jasper Boeijenga --- .changeset/gold-bugs-rhyme.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/gold-bugs-rhyme.md diff --git a/.changeset/gold-bugs-rhyme.md b/.changeset/gold-bugs-rhyme.md new file mode 100644 index 0000000000..e636e3f62f --- /dev/null +++ b/.changeset/gold-bugs-rhyme.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend-module-gitlab': minor +--- + +Added additional feedback in case branch is already created