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 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 () => {