Merge pull request #24022 from parmar-abhinav/master-feature

Add examples for gitlab:projectDeployToken:create scaffolder action & improved related tests
This commit is contained in:
Patrik Oldsberg
2024-04-13 15:57:09 +02:00
committed by GitHub
4 changed files with 295 additions and 0 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend-module-gitlab': patch
---
Add examples for `gitlab:projectDeployToken:create` scaffolder action & improve related tests
@@ -0,0 +1,179 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { createGitlabProjectDeployTokenAction } from './createGitlabProjectDeployTokenAction';
import { createMockActionContext } from '@backstage/plugin-scaffolder-node-test-utils';
import { ScmIntegrations } from '@backstage/integration';
import { ConfigReader } from '@backstage/config';
import yaml from 'yaml';
import { examples } from './createGitlabProjectDeployTokenAction.examples';
const mockGitlabClient = {
ProjectDeployTokens: {
add: jest.fn(),
},
};
jest.mock('@gitbeaker/node', () => ({
Gitlab: class {
constructor() {
return mockGitlabClient;
}
},
}));
describe('gitlab:create-deploy-token', () => {
const config = new ConfigReader({
integrations: {
gitlab: [
{
host: 'gitlab.com',
token: 'tokenlols',
apiBaseUrl: 'https://api.gitlab.com',
},
{
host: 'hosted.gitlab.com',
apiBaseUrl: 'https://api.hosted.gitlab.com',
},
],
},
});
const integrations = ScmIntegrations.fromConfig(config);
const action = createGitlabProjectDeployTokenAction({ integrations });
const mockContext = createMockActionContext({
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '123',
name: 'tokenname',
username: 'tokenuser',
scopes: ['read_repository'],
},
});
beforeEach(() => {
jest.resetAllMocks();
});
it(`Should ${examples[0].description}`, async () => {
mockGitlabClient.ProjectDeployTokens.add.mockResolvedValue({
token: 'TOKEN',
username: 'User',
});
await action.handler({
...mockContext,
input: yaml.parse(examples[0].example).steps[0].input,
});
expect(mockGitlabClient.ProjectDeployTokens.add).toHaveBeenCalledWith(
'456',
'tokenname',
undefined,
{ username: undefined },
);
expect(mockContext.output).toHaveBeenCalledWith('deploy_token', 'TOKEN');
expect(mockContext.output).toHaveBeenCalledWith('user', 'User');
});
it(`Should ${examples[1].description}`, async () => {
mockGitlabClient.ProjectDeployTokens.add.mockResolvedValue({
token: 'TOKEN',
username: 'User',
});
await action.handler({
...mockContext,
input: yaml.parse(examples[1].example).steps[0].input,
});
expect(mockGitlabClient.ProjectDeployTokens.add).toHaveBeenCalledWith(
'789',
'tokenname',
['read_registry', 'write_repository'],
{ username: undefined },
);
expect(mockContext.output).toHaveBeenCalledWith('deploy_token', 'TOKEN');
expect(mockContext.output).toHaveBeenCalledWith('user', 'User');
});
it(`Should ${examples[2].description}`, async () => {
mockGitlabClient.ProjectDeployTokens.add.mockResolvedValue({
token: 'TOKEN',
username: 'User',
});
await action.handler({
...mockContext,
input: yaml.parse(examples[2].example).steps[0].input,
});
expect(mockGitlabClient.ProjectDeployTokens.add).toHaveBeenCalledWith(
'101112',
'my-custom-token',
undefined,
{ username: undefined },
);
expect(mockContext.output).toHaveBeenCalledWith('deploy_token', 'TOKEN');
expect(mockContext.output).toHaveBeenCalledWith('user', 'User');
});
it(`Should ${examples[3].description}`, async () => {
mockGitlabClient.ProjectDeployTokens.add.mockResolvedValue({
token: 'TOKEN',
username: 'User',
});
await action.handler({
...mockContext,
input: yaml.parse(examples[3].example).steps[0].input,
});
expect(mockGitlabClient.ProjectDeployTokens.add).toHaveBeenCalledWith(
42,
'tokenname',
undefined,
{ username: undefined },
);
expect(mockContext.output).toHaveBeenCalledWith('deploy_token', 'TOKEN');
expect(mockContext.output).toHaveBeenCalledWith('user', 'User');
});
it(`Should ${examples[4].description}`, async () => {
mockGitlabClient.ProjectDeployTokens.add.mockResolvedValue({
token: 'TOKEN',
username: 'User',
});
await action.handler({
...mockContext,
input: yaml.parse(examples[4].example).steps[0].input,
});
expect(mockGitlabClient.ProjectDeployTokens.add).toHaveBeenCalledWith(
42,
'tokenname',
undefined,
{ username: 'tokenuser' },
);
expect(mockContext.output).toHaveBeenCalledWith('deploy_token', 'TOKEN');
expect(mockContext.output).toHaveBeenCalledWith('user', 'User');
});
});
@@ -0,0 +1,109 @@
/*
* Copyright 2023 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import { TemplateExample } from '@backstage/plugin-scaffolder-node';
import yaml from 'yaml';
export const examples: TemplateExample[] = [
{
description: 'Create a GitLab project deploy token with minimal options.',
example: yaml.stringify({
steps: [
{
id: 'createDeployToken',
action: 'gitlab:projectDeployToken:create',
name: 'Create GitLab Project Deploy Token',
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '456',
name: 'tokenname',
},
},
],
}),
},
{
description: 'Create a GitLab project deploy token with custom scopes.',
example: yaml.stringify({
steps: [
{
id: 'createDeployToken',
action: 'gitlab:projectDeployToken:create',
name: 'Create GitLab Project Deploy Token',
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '789',
name: 'tokenname',
scopes: ['read_registry', 'write_repository'],
},
},
],
}),
},
{
description: 'Create a GitLab project deploy token with a specified name.',
example: yaml.stringify({
steps: [
{
id: 'createDeployToken',
action: 'gitlab:projectDeployToken:create',
name: 'Create GitLab Project Deploy Token',
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '101112',
name: 'my-custom-token',
},
},
],
}),
},
{
description:
'Create a GitLab project deploy token with a numeric project ID.',
example: yaml.stringify({
steps: [
{
id: 'createDeployToken',
action: 'gitlab:projectDeployToken:create',
name: 'Create GitLab Project Deploy Token',
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: 42,
name: 'tokenname',
},
},
],
}),
},
{
description: 'Create a GitLab project deploy token with a custom username',
example: yaml.stringify({
steps: [
{
id: 'createDeployToken',
action: 'gitlab:projectDeployToken:create',
name: 'Create GitLab Project Deploy Token',
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: 42,
name: 'tokenname',
username: 'tokenuser',
},
},
],
}),
},
];
@@ -22,6 +22,7 @@ import commonGitlabConfig from '../commonGitlabConfig';
import { getToken } from '../util';
import { InputError } from '@backstage/errors';
import { z } from 'zod';
import { examples } from './createGitlabProjectDeployTokenAction.examples';
/**
* Creates a `gitlab:projectDeployToken:create` Scaffolder action.
@@ -35,6 +36,7 @@ export const createGitlabProjectDeployTokenAction = (options: {
const { integrations } = options;
return createTemplateAction({
id: 'gitlab:projectDeployToken:create',
examples,
schema: {
input: commonGitlabConfig.merge(
z.object({