Add examples for scaffolder action & improve related tests

Signed-off-by: JeevaRamanathan <jeevaramanathan.m@infosys.com>
This commit is contained in:
JeevaRamanathan
2024-04-27 22:22:06 +05:30
parent d26e5a5d45
commit 18f736ffc5
4 changed files with 403 additions and 1 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend-module-gitlab': minor
---
Add examples for `gitlab:projectVariable:create` scaffolder action & improve related tests
@@ -0,0 +1,236 @@
/*
* Copyright 2021 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 { createGitlabProjectVariableAction } from './createGitlabProjectVariableAction';
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 './createGitlabProjectVariableAction.examples';
const mockGitlabClient = {
ProjectVariables: {
create: jest.fn(),
},
};
jest.mock('@gitbeaker/node', () => ({
Gitlab: class {
constructor() {
return mockGitlabClient;
}
},
}));
describe('gitlab:projectVariableAction: create examples', () => {
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 = createGitlabProjectVariableAction({ integrations });
const mockContext = createMockActionContext({
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '123',
key: 'MY_VARIABLE',
value: 'my_value',
variableType: 'env_var',
},
});
beforeEach(() => {
jest.resetAllMocks();
});
it(`Should ${examples[0].description}`, async () => {
mockGitlabClient.ProjectVariables.create.mockResolvedValue({
token: 'TOKEN',
});
await action.handler({
...mockContext,
input: yaml.parse(examples[0].example).steps[0].input,
});
expect(mockGitlabClient.ProjectVariables.create).toHaveBeenCalledWith(
'123',
{
key: 'MY_VARIABLE',
value: 'my_value',
variable_type: 'env_var',
environment_scope: '*',
masked: false,
protected: false,
raw: false,
},
);
});
it(`Should ${examples[1].description}`, async () => {
mockGitlabClient.ProjectVariables.create.mockResolvedValue({
token: 'TOKEN',
});
await action.handler({
...mockContext,
input: yaml.parse(examples[1].example).steps[0].input,
});
expect(mockGitlabClient.ProjectVariables.create).toHaveBeenCalledWith(
'123',
{
key: 'MY_VARIABLE',
value: 'my-file-content',
protected: false,
masked: false,
raw: false,
environment_scope: '*',
variable_type: 'file',
},
);
});
it(`Should ${examples[2].description}`, async () => {
mockGitlabClient.ProjectVariables.create.mockResolvedValue({
token: 'TOKEN',
});
await action.handler({
...mockContext,
input: yaml.parse(examples[2].example).steps[0].input,
});
expect(mockGitlabClient.ProjectVariables.create).toHaveBeenCalledWith(
'456',
{
key: 'MY_VARIABLE',
value: 'my_value',
masked: false,
raw: false,
environment_scope: '*',
variable_type: 'env_var',
protected: true,
},
);
});
it(`Should ${examples[3].description}`, async () => {
mockGitlabClient.ProjectVariables.create.mockResolvedValue({
token: 'TOKEN',
});
await action.handler({
...mockContext,
input: yaml.parse(examples[3].example).steps[0].input,
});
expect(mockGitlabClient.ProjectVariables.create).toHaveBeenCalledWith(
'789',
{
key: 'DB_PASSWORD',
value: 'password123',
protected: false,
raw: false,
environment_scope: '*',
variable_type: 'env_var',
masked: true,
},
);
});
it(`Should ${examples[4].description}`, async () => {
mockGitlabClient.ProjectVariables.create.mockResolvedValue({
token: 'TOKEN',
});
await action.handler({
...mockContext,
input: yaml.parse(examples[4].example).steps[0].input,
});
expect(mockGitlabClient.ProjectVariables.create).toHaveBeenCalledWith(
'123',
{
key: 'MY_VARIABLE',
value: 'my_value',
protected: false,
environment_scope: '*',
variable_type: 'env_var',
masked: false,
raw: true,
},
);
});
it(`Should ${examples[5].description}`, async () => {
mockGitlabClient.ProjectVariables.create.mockResolvedValue({
token: 'TOKEN',
});
await action.handler({
...mockContext,
input: yaml.parse(examples[5].example).steps[0].input,
});
expect(mockGitlabClient.ProjectVariables.create).toHaveBeenCalledWith(
'123',
{
key: 'MY_VARIABLE',
value: 'my_value',
protected: false,
variable_type: 'env_var',
masked: false,
raw: false,
environment_scope: 'production',
},
);
});
it(`Should ${examples[6].description}`, async () => {
mockGitlabClient.ProjectVariables.create.mockResolvedValue({
token: 'TOKEN',
});
await action.handler({
...mockContext,
input: yaml.parse(examples[6].example).steps[0].input,
});
expect(mockGitlabClient.ProjectVariables.create).toHaveBeenCalledWith(
'123',
{
key: 'MY_VARIABLE',
value: 'my_value',
protected: false,
variable_type: 'env_var',
masked: false,
raw: false,
environment_scope: '*',
},
);
});
});
@@ -0,0 +1,160 @@
/*
* 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: 'Creating a GitLab project variable of type env_var',
example: yaml.stringify({
steps: [
{
id: 'createVariable',
action: 'gitlab:createGitlabProjectVariableAction',
name: 'Create GitLab Project Variable',
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '123',
key: 'MY_VARIABLE',
value: 'my_value',
variableType: 'env_var',
},
},
],
}),
},
{
description: 'Creating a GitLab project variable of type file',
example: yaml.stringify({
steps: [
{
id: 'createVariable',
action: 'gitlab:createGitlabProjectVariableAction',
name: 'Create GitLab Project Variable',
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '123',
key: 'MY_VARIABLE',
value: 'my-file-content',
variableType: 'file',
},
},
],
}),
},
{
description: 'Create a GitLab project variable that is protected.',
example: yaml.stringify({
steps: [
{
id: 'createVariable',
action: 'gitlab:createGitlabProjectVariableAction',
name: 'Create GitLab Project Variable',
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '456',
key: 'MY_VARIABLE',
value: 'my_value',
variableType: 'env_var',
variableProtected: true,
},
},
],
}),
},
{
description: 'Create a GitLab project variable with masked flag as true',
example: yaml.stringify({
steps: [
{
id: 'createVariable',
action: 'gitlab:createGitlabProjectVariableAction',
name: 'Create GitLab Project Variable',
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '789',
key: 'DB_PASSWORD',
value: 'password123',
variableType: 'env_var',
masked: true,
},
},
],
}),
},
{
description: 'Create a GitLab project variable that is expandable.',
example: yaml.stringify({
steps: [
{
id: 'createVariable',
action: 'gitlab:projectVariable:create',
name: 'Create GitLab Project Variable',
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '123',
key: 'MY_VARIABLE',
value: 'my_value',
variableType: 'env_var',
raw: true,
},
},
],
}),
},
{
description:
'Create a GitLab project variable with a specific environment scope.',
example: yaml.stringify({
steps: [
{
id: 'createVariable',
action: 'gitlab:projectVariable:create',
name: 'Create GitLab Project Variable',
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '123',
key: 'MY_VARIABLE',
value: 'my_value',
variableType: 'env_var',
environmentScope: 'production',
},
},
],
}),
},
{
description:
'Create a GitLab project variable with a wildcard environment scope.',
example: yaml.stringify({
steps: [
{
id: 'createVariable',
action: 'gitlab:projectVariable:create',
name: 'Create GitLab Project Variable',
input: {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
projectId: '123',
key: 'MY_VARIABLE',
value: 'my_value',
variableType: 'env_var',
environmentScope: '*',
},
},
],
}),
},
];
@@ -20,6 +20,7 @@ import { Gitlab } from '@gitbeaker/node';
import { getToken } from '../util';
import commonGitlabConfig from '../commonGitlabConfig';
import { z } from 'zod';
import { examples } from './createGitlabProjectVariableAction.examples';
/**
* Creates a `gitlab:projectVariable:create` Scaffolder action.
@@ -33,6 +34,7 @@ export const createGitlabProjectVariableAction = (options: {
const { integrations } = options;
return createTemplateAction({
id: 'gitlab:projectVariable:create',
examples,
schema: {
input: commonGitlabConfig.merge(
z.object({
@@ -85,7 +87,6 @@ export const createGitlabProjectVariableAction = (options: {
host: integrationConfig.config.baseUrl,
token: token,
});
await api.ProjectVariables.create(projectId, {
key: key,
value: value,