feat: allow the oauth token to be used in the gitlab:projectVariableAction:create action

Signed-off-by: ElaineDeMattosSilvaB <elaine.de-mattos-silva-bezerra@deutschebahn.com>
This commit is contained in:
ElaineDeMattosSilvaB
2024-07-03 19:47:37 +02:00
parent 9e525886d0
commit fad1b903a8
3 changed files with 49 additions and 44 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/plugin-scaffolder-backend-module-gitlab': patch
---
Allow the `createGitlabProjectVariableAction` to use oauth tokens
@@ -26,7 +26,7 @@ const mockGitlabClient = {
create: jest.fn(),
},
};
jest.mock('@gitbeaker/node', () => ({
jest.mock('@gitbeaker/rest', () => ({
Gitlab: class {
constructor() {
return mockGitlabClient;
@@ -41,7 +41,7 @@ describe('gitlab:projectVariableAction: create examples', () => {
{
host: 'gitlab.com',
token: 'tokenlols',
apiBaseUrl: 'https://api.gitlab.com',
apiBaseUrl: 'https://api.gitlab.com/api/v4',
},
{
host: 'hosted.gitlab.com',
@@ -79,17 +79,18 @@ describe('gitlab:projectVariableAction: create examples', () => {
expect(mockGitlabClient.ProjectVariables.create).toHaveBeenCalledWith(
'123',
'MY_VARIABLE',
'my_value',
{
key: 'MY_VARIABLE',
value: 'my_value',
variable_type: 'env_var',
environment_scope: '*',
variableType: 'env_var', // Correctly using variableType
environmentScope: '*',
masked: false,
protected: false,
raw: false,
},
);
});
it(`Should ${examples[1].description}`, async () => {
mockGitlabClient.ProjectVariables.create.mockResolvedValue({
token: 'TOKEN',
@@ -102,14 +103,14 @@ describe('gitlab:projectVariableAction: create examples', () => {
expect(mockGitlabClient.ProjectVariables.create).toHaveBeenCalledWith(
'123',
'MY_VARIABLE',
'my-file-content',
{
key: 'MY_VARIABLE',
value: 'my-file-content',
variableType: 'file', // Correctly using variableType
protected: false,
masked: false,
raw: false,
environment_scope: '*',
variable_type: 'file',
environmentScope: '*',
},
);
});
@@ -126,13 +127,13 @@ describe('gitlab:projectVariableAction: create examples', () => {
expect(mockGitlabClient.ProjectVariables.create).toHaveBeenCalledWith(
'456',
'MY_VARIABLE',
'my_value',
{
key: 'MY_VARIABLE',
value: 'my_value',
masked: false,
raw: false,
environment_scope: '*',
variable_type: 'env_var',
environmentScope: '*',
variableType: 'env_var', // Correctly using variableType
protected: true,
},
);
@@ -150,13 +151,13 @@ describe('gitlab:projectVariableAction: create examples', () => {
expect(mockGitlabClient.ProjectVariables.create).toHaveBeenCalledWith(
'789',
'DB_PASSWORD',
'password123',
{
key: 'DB_PASSWORD',
value: 'password123',
protected: false,
raw: false,
environment_scope: '*',
variable_type: 'env_var',
environmentScope: '*',
variableType: 'env_var', // Correctly using variableType
masked: true,
},
);
@@ -174,12 +175,12 @@ describe('gitlab:projectVariableAction: create examples', () => {
expect(mockGitlabClient.ProjectVariables.create).toHaveBeenCalledWith(
'123',
'MY_VARIABLE',
'my_value',
{
key: 'MY_VARIABLE',
value: 'my_value',
protected: false,
environment_scope: '*',
variable_type: 'env_var',
environmentScope: '*',
variableType: 'env_var', // Correctly using variableType
masked: false,
raw: true,
},
@@ -198,14 +199,14 @@ describe('gitlab:projectVariableAction: create examples', () => {
expect(mockGitlabClient.ProjectVariables.create).toHaveBeenCalledWith(
'123',
'MY_VARIABLE',
'my_value',
{
key: 'MY_VARIABLE',
value: 'my_value',
protected: false,
variable_type: 'env_var',
variableType: 'env_var', // Correctly using variableType
masked: false,
raw: false,
environment_scope: 'production',
environmentScope: 'production',
},
);
});
@@ -222,14 +223,14 @@ describe('gitlab:projectVariableAction: create examples', () => {
expect(mockGitlabClient.ProjectVariables.create).toHaveBeenCalledWith(
'123',
'MY_VARIABLE',
'my_value',
{
key: 'MY_VARIABLE',
value: 'my_value',
protected: false,
variable_type: 'env_var',
variableType: 'env_var', // Correctly using variableType
masked: false,
raw: false,
environment_scope: '*',
environmentScope: '*',
},
);
});
@@ -16,10 +16,10 @@
import { ScmIntegrationRegistry } from '@backstage/integration';
import { createTemplateAction } from '@backstage/plugin-scaffolder-node';
import { Gitlab } from '@gitbeaker/node';
import { VariableType } from '@gitbeaker/rest';
import { z } from 'zod';
import commonGitlabConfig from '../commonGitlabConfig';
import { getToken } from '../util';
import { getClient, parseRepoUrl } from '../util';
import { examples } from './gitlabProjectVariableCreate.examples';
/**
@@ -72,6 +72,7 @@ export const createGitlabProjectVariableAction = (options: {
},
async handler(ctx) {
const {
repoUrl,
projectId,
key,
value,
@@ -80,21 +81,19 @@ export const createGitlabProjectVariableAction = (options: {
masked = false,
raw = false,
environmentScope = '*',
token,
} = ctx.input;
const { token, integrationConfig } = getToken(ctx.input, integrations);
const api = new Gitlab({
host: integrationConfig.config.baseUrl,
token: token,
});
await api.ProjectVariables.create(projectId, {
key: key,
value: value,
variable_type: variableType,
const { host } = parseRepoUrl(repoUrl, integrations);
const api = getClient({ host, integrations, token });
await api.ProjectVariables.create(projectId, key, value, {
variableType: variableType as VariableType,
protected: variableProtected,
masked: masked,
raw: raw,
environment_scope: environmentScope,
masked,
raw,
environmentScope,
});
},
});