Merge pull request #29319 from jboeijenga/feat/mr-creation-feedback

feat: Improve Error Handling for Existing Branches in GitLab Actions
This commit is contained in:
Ben Lambert
2025-04-15 10:31:40 +02:00
committed by GitHub
2 changed files with 37 additions and 12 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend-module-gitlab': minor
---
Added additional feedback in case branch is already created
@@ -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 () => {