From 30e6edd7f6a556e7648da4213f56503af74158bf Mon Sep 17 00:00:00 2001 From: Andreas Berger Date: Thu, 11 May 2023 12:43:21 +0200 Subject: [PATCH] Add support for dry run for `gitlab:group:ensureExists` action. Signed-off-by: Andreas Berger --- .changeset/five-turtles-play.md | 5 +++ ...reateGitlabGroupEnsureExistsAction.test.ts | 31 +++++++++++++++++++ .../createGitlabGroupEnsureExistsAction.ts | 8 ++++- 3 files changed, 43 insertions(+), 1 deletion(-) create mode 100644 .changeset/five-turtles-play.md diff --git a/.changeset/five-turtles-play.md b/.changeset/five-turtles-play.md new file mode 100644 index 0000000000..447cd60b3f --- /dev/null +++ b/.changeset/five-turtles-play.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend-module-gitlab': patch +--- + +Add support for dry run for `gitlab:group:ensureExists` action. diff --git a/plugins/scaffolder-backend-module-gitlab/src/actions/createGitlabGroupEnsureExistsAction.test.ts b/plugins/scaffolder-backend-module-gitlab/src/actions/createGitlabGroupEnsureExistsAction.test.ts index 6fb119413f..3c67477ed5 100644 --- a/plugins/scaffolder-backend-module-gitlab/src/actions/createGitlabGroupEnsureExistsAction.test.ts +++ b/plugins/scaffolder-backend-module-gitlab/src/actions/createGitlabGroupEnsureExistsAction.test.ts @@ -137,4 +137,35 @@ describe('gitlab:group:ensureExists', () => { expect(mockContext.output).toHaveBeenCalledWith('groupId', 42); }); + + it('should not call API on dryRun', async () => { + const config = new ConfigReader({ + integrations: { + gitlab: [ + { + host: 'gitlab.com', + token: 'tokenlols', + apiBaseUrl: 'https://api.gitlab.com', + }, + ], + }, + }); + const integrations = ScmIntegrations.fromConfig(config); + + const action = createGitlabGroupEnsureExistsAction({ integrations }); + + await action.handler({ + ...mockContext, + isDryRun: true, + input: { + repoUrl: 'gitlab.com', + path: ['foo', 'bar'], + }, + }); + + expect(mockGitlabClient.Groups.search).not.toHaveBeenCalled(); + expect(mockGitlabClient.Groups.create).not.toHaveBeenCalled(); + + expect(mockContext.output).toHaveBeenCalledWith('groupId', 42); + }); }); diff --git a/plugins/scaffolder-backend-module-gitlab/src/actions/createGitlabGroupEnsureExistsAction.ts b/plugins/scaffolder-backend-module-gitlab/src/actions/createGitlabGroupEnsureExistsAction.ts index 698d402697..644af12b5d 100644 --- a/plugins/scaffolder-backend-module-gitlab/src/actions/createGitlabGroupEnsureExistsAction.ts +++ b/plugins/scaffolder-backend-module-gitlab/src/actions/createGitlabGroupEnsureExistsAction.ts @@ -35,6 +35,7 @@ export const createGitlabGroupEnsureExistsAction = (options: { return createTemplateAction({ id: 'gitlab:group:ensureExists', description: 'Ensures a Gitlab group exists', + supportsDryRun: true, schema: { input: commonGitlabConfig.and( z.object({ @@ -52,6 +53,11 @@ export const createGitlabGroupEnsureExistsAction = (options: { }), }, async handler(ctx) { + if (ctx.isDryRun) { + ctx.output('groupId', 42); + return; + } + const { path } = ctx.input; const { token, integrationConfig } = getToken(ctx.input, integrations); @@ -66,7 +72,7 @@ export const createGitlabGroupEnsureExistsAction = (options: { const fullPath = `${currentPath}/${pathElement}`; const result = (await api.Groups.search( fullPath, - )) as any as Array; // recast since the return type for search is wrong in the gitbeaker typings + )) as unknown as Array; // recast since the return type for search is wrong in the gitbeaker typings const subGroup = result.find( searchPathElem => searchPathElem.full_path === fullPath, );