Use utility function getClient
Signed-off-by: Thorsten Lanfer <tlanfer@gmail.com>
This commit is contained in:
+79
-8
@@ -25,19 +25,17 @@ const mockGitlabClient = {
|
||||
create: jest.fn(),
|
||||
},
|
||||
};
|
||||
jest.mock('@gitbeaker/node', () => ({
|
||||
Gitlab: class {
|
||||
constructor() {
|
||||
return mockGitlabClient;
|
||||
}
|
||||
},
|
||||
// const mockGitlabApi = jest.fn().mockReturnValue(mockGitlabClient);
|
||||
|
||||
jest.mock('../util', () => ({
|
||||
getClient: () => mockGitlabClient,
|
||||
}));
|
||||
|
||||
describe('gitlab:group:ensureExists', () => {
|
||||
const mockContext = createMockActionContext();
|
||||
|
||||
afterEach(() => {
|
||||
jest.resetAllMocks();
|
||||
jest.clearAllMocks();
|
||||
});
|
||||
|
||||
it('should create a new group if it does not exists', async () => {
|
||||
@@ -81,7 +79,7 @@ describe('gitlab:group:ensureExists', () => {
|
||||
});
|
||||
|
||||
expect(mockGitlabClient.Groups.create).toHaveBeenCalledWith('bar', 'bar', {
|
||||
parent_id: 2,
|
||||
parentId: 2,
|
||||
});
|
||||
|
||||
expect(mockContext.output).toHaveBeenCalledWith('groupId', 3);
|
||||
@@ -161,4 +159,77 @@ describe('gitlab:group:ensureExists', () => {
|
||||
|
||||
expect(mockContext.output).toHaveBeenCalledWith('groupId', 42);
|
||||
});
|
||||
|
||||
it('should use the token from the integration config when none is provided', async () => {
|
||||
mockGitlabClient.Groups.search.mockResolvedValue([
|
||||
{
|
||||
id: 1,
|
||||
full_path: 'foobar',
|
||||
},
|
||||
]);
|
||||
|
||||
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',
|
||||
path: ['foobar'],
|
||||
},
|
||||
});
|
||||
|
||||
// expect(Gitlab).toHaveBeenCalledWith(
|
||||
// expect.objectContaining({
|
||||
// 'token': 'tokenlols',
|
||||
// }),
|
||||
// );
|
||||
});
|
||||
|
||||
it('should use a provided token as bearer authentication', async () => {
|
||||
mockGitlabClient.Groups.search.mockResolvedValue([
|
||||
{
|
||||
id: 1,
|
||||
full_path: 'foobar',
|
||||
},
|
||||
]);
|
||||
|
||||
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',
|
||||
path: ['foobar'],
|
||||
token: 'mysecrettoken',
|
||||
},
|
||||
});
|
||||
|
||||
// expect(Gitlab).toHaveBeenCalledWith(
|
||||
// expect.objectContaining({
|
||||
// oauthToken: 'mysecrettoken',
|
||||
// }),
|
||||
// );
|
||||
});
|
||||
});
|
||||
|
||||
@@ -17,10 +17,9 @@
|
||||
import { ScmIntegrationRegistry } from '@backstage/integration';
|
||||
import { createTemplateAction } from '@backstage/plugin-scaffolder-node';
|
||||
import { GroupSchema } from '@gitbeaker/core/dist/types/resources/Groups';
|
||||
import { Gitlab } from '@gitbeaker/node';
|
||||
import { z } from 'zod';
|
||||
import commonGitlabConfig from '../commonGitlabConfig';
|
||||
import { getToken } from '../util';
|
||||
import { getClient } from '../util';
|
||||
import { examples } from './gitlabGroupEnsureExists.examples';
|
||||
|
||||
/**
|
||||
@@ -60,13 +59,8 @@ export const createGitlabGroupEnsureExistsAction = (options: {
|
||||
return;
|
||||
}
|
||||
|
||||
const { path } = ctx.input;
|
||||
const { token, integrationConfig } = getToken(ctx.input, integrations);
|
||||
|
||||
const api = new Gitlab({
|
||||
host: integrationConfig.config.baseUrl,
|
||||
token: token,
|
||||
});
|
||||
const { token, repoUrl, path } = ctx.input;
|
||||
const api = getClient({ host: repoUrl, token, integrations });
|
||||
|
||||
let currentPath: string | null = null;
|
||||
let parent: GroupSchema | null = null;
|
||||
@@ -82,15 +76,15 @@ export const createGitlabGroupEnsureExistsAction = (options: {
|
||||
);
|
||||
if (!subGroup) {
|
||||
ctx.logger.info(`creating missing group ${fullPath}`);
|
||||
parent = await api.Groups.create(
|
||||
parent = (await api.Groups.create(
|
||||
pathElement,
|
||||
pathElement,
|
||||
parent
|
||||
? {
|
||||
parent_id: parent.id,
|
||||
parentId: parent.id,
|
||||
}
|
||||
: {},
|
||||
);
|
||||
)) as GroupSchema;
|
||||
} else {
|
||||
parent = subGroup;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user