From d15355c9b8f22d29324ae20be52dd69334572a41 Mon Sep 17 00:00:00 2001 From: Peter Macdonald Date: Wed, 16 Apr 2025 12:58:02 +0200 Subject: [PATCH] feat: Update gitlab repo push error handling slightly when the commit action is not create Signed-off-by: Peter Macdonald --- .changeset/bumpy-showers-design.md | 5 ++ .../src/actions/gitlabRepoPush.test.ts | 71 +++++++++++++++++++ .../src/actions/gitlabRepoPush.ts | 7 ++ 3 files changed, 83 insertions(+) create mode 100644 .changeset/bumpy-showers-design.md diff --git a/.changeset/bumpy-showers-design.md b/.changeset/bumpy-showers-design.md new file mode 100644 index 0000000000..2db5006944 --- /dev/null +++ b/.changeset/bumpy-showers-design.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend-module-gitlab': patch +--- + +If the commit action is not `create` log a more appropriate error message to the end user advising that the files they're trying to modify might not exist diff --git a/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabRepoPush.test.ts b/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabRepoPush.test.ts index 65cf2244d4..5644e0b952 100644 --- a/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabRepoPush.test.ts +++ b/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabRepoPush.test.ts @@ -376,4 +376,75 @@ describe('createGitLabCommit', () => { ); }); }); + + describe('error handling', () => { + it('throws appropriate error for create action when commit fails', async () => { + const input = { + repoUrl: 'gitlab.com?repo=repo&owner=owner', + commitMessage: 'Create my new commit', + branchName: 'some-branch', + commitAction: 'create', + }; + mockDir.setContent({ + [workspacePath]: { + 'foo.txt': 'Hello there!', + }, + }); + + const ctx = createMockActionContext({ input, workspacePath }); + mockGitlabClient.Commits.create.mockRejectedValue( + new Error('Commit failed'), + ); + + await expect(instance.handler(ctx)).rejects.toThrow( + 'Committing the changes to some-branch failed. Please check that none of the files created by the template already exists. Error: Commit failed', + ); + }); + + it('throws appropriate error for update action when commit fails', async () => { + const input = { + repoUrl: 'gitlab.com?repo=repo&owner=owner', + commitMessage: 'Update my commit', + branchName: 'some-branch', + commitAction: 'update', + }; + mockDir.setContent({ + [workspacePath]: { + 'foo.txt': 'Hello there!', + }, + }); + + const ctx = createMockActionContext({ input, workspacePath }); + mockGitlabClient.Commits.create.mockRejectedValue( + new Error('Commit failed'), + ); + + await expect(instance.handler(ctx)).rejects.toThrow( + "Modifying the files in some-branch failed. Please verify that all files you're trying to modify exist in the repository. Error: Commit failed", + ); + }); + + it('throws appropriate error for delete action when commit fails', async () => { + const input = { + repoUrl: 'gitlab.com?repo=repo&owner=owner', + commitMessage: 'Delete my commit', + branchName: 'some-branch', + commitAction: 'delete', + }; + mockDir.setContent({ + [workspacePath]: { + 'foo.txt': 'Hello there!', + }, + }); + + const ctx = createMockActionContext({ input, workspacePath }); + mockGitlabClient.Commits.create.mockRejectedValue( + new Error('Commit failed'), + ); + + await expect(instance.handler(ctx)).rejects.toThrow( + "Modifying the files in some-branch failed. Please verify that all files you're trying to modify exist in the repository. Error: Commit failed", + ); + }); + }); }); diff --git a/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabRepoPush.ts b/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabRepoPush.ts index 666e236cda..8b6f5cca50 100644 --- a/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabRepoPush.ts +++ b/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabRepoPush.ts @@ -203,6 +203,13 @@ export const createGitlabRepoPushAction = (options: { ctx.output('projectPath', repoID); ctx.output('commitHash', commitId); } catch (e) { + if (commitAction !== 'create') { + throw new InputError( + `Modifying the files in ${branchName} failed. Please verify that all files you're trying to modify exist in the repository. ${getErrorMessage( + e, + )}`, + ); + } throw new InputError( `Committing the changes to ${branchName} failed. Please check that none of the files created by the template already exists. ${getErrorMessage( e,