Add support for dry run for gitlab:group:ensureExists action.

Signed-off-by: Andreas Berger <andreas@berger-ecommerce.com>
This commit is contained in:
Andreas Berger
2023-05-11 12:43:21 +02:00
parent 667c490cc1
commit 30e6edd7f6
3 changed files with 43 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend-module-gitlab': patch
---
Add support for dry run for `gitlab:group:ensureExists` action.
@@ -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);
});
});
@@ -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<GroupSchema>; // recast since the return type for search is wrong in the gitbeaker typings
)) as unknown as Array<GroupSchema>; // recast since the return type for search is wrong in the gitbeaker typings
const subGroup = result.find(
searchPathElem => searchPathElem.full_path === fullPath,
);