diff --git a/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabGroupEnsureExists.examples.test.ts b/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabGroupEnsureExists.examples.test.ts index b9377fd97b..202b397d19 100644 --- a/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabGroupEnsureExists.examples.test.ts +++ b/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabGroupEnsureExists.examples.test.ts @@ -27,7 +27,8 @@ const mockGitlabClient = { create: jest.fn(), }, }; -jest.mock('@gitbeaker/node', () => ({ + +jest.mock('@gitbeaker/rest', () => ({ Gitlab: class { constructor() { return mockGitlabClient; @@ -38,8 +39,8 @@ jest.mock('@gitbeaker/node', () => ({ describe('gitlab:group:ensureExists', () => { const mockContext = createMockActionContext(); - afterEach(() => { - jest.resetAllMocks(); + beforeEach(() => { + jest.clearAllMocks(); }); it(`Should ${examples[0].description}`, async () => { @@ -55,7 +56,7 @@ describe('gitlab:group:ensureExists', () => { { host: 'gitlab.com', token: 'tokenlols', - apiBaseUrl: 'https://api.gitlab.com', + apiBaseUrl: 'https://gitlab.com/api/v4', }, ], }, @@ -114,7 +115,7 @@ describe('gitlab:group:ensureExists', () => { 'group2', 'group2', { - parent_id: 1, + parentId: 1, }, ); @@ -161,7 +162,7 @@ describe('gitlab:group:ensureExists', () => { 'group3', 'group3', { - parent_id: 2, + parentId: 2, }, ); diff --git a/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabGroupEnsureExists.test.ts b/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabGroupEnsureExists.test.ts index 67476494bb..b425a4fac8 100644 --- a/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabGroupEnsureExists.test.ts +++ b/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabGroupEnsureExists.test.ts @@ -25,19 +25,37 @@ const mockGitlabClient = { create: jest.fn(), }, }; -// const mockGitlabApi = jest.fn().mockReturnValue(mockGitlabClient); -jest.mock('../util', () => ({ - getClient: () => mockGitlabClient, +jest.mock('@gitbeaker/rest', () => ({ + Gitlab: class { + constructor() { + return mockGitlabClient; + } + }, })); describe('gitlab:group:ensureExists', () => { - const mockContext = createMockActionContext(); - - afterEach(() => { + beforeEach(() => { jest.clearAllMocks(); }); + const config = new ConfigReader({ + integrations: { + gitlab: [ + { + host: 'gitlab.com', + token: 'tokenlols', + apiBaseUrl: 'https://gitlab.com/api/v4', + }, + ], + }, + }); + const integrations = ScmIntegrations.fromConfig(config); + + const action = createGitlabGroupEnsureExistsAction({ integrations }); + + const mockContext = createMockActionContext(); + it('should create a new group if it does not exists', async () => { mockGitlabClient.Groups.search.mockResolvedValue([ { @@ -55,25 +73,10 @@ describe('gitlab:group:ensureExists', () => { full_path: 'foo/bar', }); - 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, input: { - repoUrl: 'gitlab.com', + repoUrl: 'gitlab.com?repo=repo&owner=owner', path: ['foo', 'bar'], }, }); @@ -101,25 +104,10 @@ describe('gitlab:group:ensureExists', () => { }, ]); - 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, input: { - repoUrl: 'gitlab.com', + repoUrl: 'gitlab.com?repo=repo&owner=owner', path: ['foo', 'bar'], }, }); @@ -130,26 +118,11 @@ describe('gitlab:group:ensureExists', () => { }); 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', + repoUrl: 'gitlab.com?repo=repo&owner=owner', path: ['foo', 'bar'], }, }); @@ -168,23 +141,10 @@ describe('gitlab:group:ensureExists', () => { }, ]); - 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, input: { - repoUrl: 'gitlab.com', + repoUrl: 'gitlab.com?repo=repo&owner=owner', path: ['foobar'], }, }); @@ -204,23 +164,10 @@ describe('gitlab:group:ensureExists', () => { }, ]); - 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, input: { - repoUrl: 'gitlab.com', + repoUrl: 'gitlab.com?repo=repo&owner=owner', path: ['foobar'], token: 'mysecrettoken', }, diff --git a/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabGroupEnsureExists.ts b/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabGroupEnsureExists.ts index 31e1d027c2..0a270fc167 100644 --- a/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabGroupEnsureExists.ts +++ b/plugins/scaffolder-backend-module-gitlab/src/actions/gitlabGroupEnsureExists.ts @@ -19,7 +19,7 @@ import { createTemplateAction } from '@backstage/plugin-scaffolder-node'; import { GroupSchema } from '@gitbeaker/core/dist/types/resources/Groups'; import { z } from 'zod'; import commonGitlabConfig from '../commonGitlabConfig'; -import { getClient } from '../util'; +import { getClient, parseRepoUrl } from '../util'; import { examples } from './gitlabGroupEnsureExists.examples'; /** @@ -60,7 +60,10 @@ export const createGitlabGroupEnsureExistsAction = (options: { } const { token, repoUrl, path } = ctx.input; - const api = getClient({ host: repoUrl, token, integrations }); + + const { host } = parseRepoUrl(repoUrl, integrations); + + const api = getClient({ host, integrations, token }); let currentPath: string | null = null; let parent: GroupSchema | null = null;