Add config and tests for new gitlab actions update and delete

Signed-off-by: Aisha Saini <asaini1@jaguarlandrover.com>
Signed-off-by: asaini1 <asaini1@jaguarlandrover.com>
This commit is contained in:
Aisha Saini
2022-05-27 15:21:26 +01:00
committed by asaini1
parent 08a27a58ad
commit b4149fc0a3
4 changed files with 184 additions and 1 deletions
+1 -1
View File
@@ -2,4 +2,4 @@
'@backstage/plugin-scaffolder-backend': minor
---
Update GitLab Merge Request Action to allow source branch to be deleted
Update GitLab Merge Request Action to allow source branch to be deleted & configure additional gitlab actions: update and delete
+1
View File
@@ -346,6 +346,7 @@ export const createPublishGitlabMergeRequestAction: (options: {
token?: string | undefined;
projectid?: string | undefined;
removeSourceBranch?: boolean | undefined;
gitlabAction?: 'update' | 'create' | 'delete' | undefined;
}>;
// @public
@@ -79,6 +79,20 @@ jest.mock('@gitbeaker/node', () => ({
},
}));
jest.mock('globby', () =>
jest.fn(async (_: any) => {
return ['foo/bar5'];
}),
);
jest.mock('fs-extra', () => {
return {
readFile: jest.fn(async (_: any) => {
return Buffer.from('some content');
}),
};
});
describe('createGitLabMergeRequest', () => {
let instance: TemplateAction<any>;
@@ -216,4 +230,165 @@ describe('createGitLabMergeRequest', () => {
);
});
});
describe('createGitLabMergeRequestWithoutGitlabAction', () => {
it('gitlabAction is create by default when not passed in options', async () => {
const input = {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
title: 'Create my new MR',
branchName: 'new-mr',
description: 'This MR is really good',
draft: true,
};
mockFs({
[workspacePath]: {
source: { 'foo.txt': 'Hello there!' },
irrelevant: { 'bar.txt': 'Nothing to see here' },
},
});
const ctx = {
createTemporaryDirectory: jest.fn(),
output: jest.fn(),
logger: getRootLogger(),
logStream: new Writable(),
input,
workspacePath,
};
await instance.handler(ctx);
expect(mockGitlabClient.Commits.create).toHaveBeenCalledWith(
undefined,
'new-mr',
'Create my new MR',
[
{
action: 'create',
filePath: 'foo/bar5',
content: 'some content',
},
],
);
});
});
describe('createGitLabMergeRequestWithGitlabAction', () => {
it('gitlabAction is create when create is passed in options', async () => {
const input = {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
title: 'Create my new MR',
branchName: 'new-mr',
description: 'MR description',
gitlabAction: 'create',
draft: true,
};
mockFs({
[workspacePath]: {
source: { 'foo.txt': 'Hello there!' },
irrelevant: { 'bar.txt': 'Nothing to see here' },
},
});
const ctx = {
createTemporaryDirectory: jest.fn(),
output: jest.fn(),
logger: getRootLogger(),
logStream: new Writable(),
input,
workspacePath,
};
await instance.handler(ctx);
expect(mockGitlabClient.Commits.create).toHaveBeenCalledWith(
undefined,
'new-mr',
'Create my new MR',
[
{
action: 'create',
filePath: 'foo/bar5',
content: 'some content',
},
],
);
});
it('gitlabAction is update when update is passed in options', async () => {
const input = {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
title: 'Create my new MR',
branchName: 'new-mr',
description: 'MR description',
gitlabAction: 'update',
draft: true,
};
mockFs({
[workspacePath]: {
source: { 'foo.txt': 'Hello there!' },
irrelevant: { 'bar.txt': 'Nothing to see here' },
},
});
const ctx = {
createTemporaryDirectory: jest.fn(),
output: jest.fn(),
logger: getRootLogger(),
logStream: new Writable(),
input,
workspacePath,
};
await instance.handler(ctx);
expect(mockGitlabClient.Commits.create).toHaveBeenCalledWith(
undefined,
'new-mr',
'Create my new MR',
[
{
action: 'update',
filePath: 'foo/bar5',
content: 'some content',
},
],
);
});
it('gitlabAction is delete when delete is passed in options', async () => {
const input = {
repoUrl: 'gitlab.com?repo=repo&owner=owner',
title: 'Create my new MR',
branchName: 'new-mr',
description: 'other MR description',
gitlabAction: 'delete',
draft: true,
};
mockFs({
[workspacePath]: {
source: { 'foo.txt': 'Hello there!' },
irrelevant: { 'bar.txt': 'Nothing to see here' },
},
});
const ctx = {
createTemporaryDirectory: jest.fn(),
output: jest.fn(),
logger: getRootLogger(),
logStream: new Writable(),
input,
workspacePath,
};
await instance.handler(ctx);
expect(mockGitlabClient.Commits.create).toHaveBeenCalledWith(
undefined,
'new-mr',
'Create my new MR',
[
{
action: 'delete',
filePath: 'foo/bar5',
},
],
);
});
});
});
@@ -43,6 +43,7 @@ export const createPublishGitlabMergeRequestAction = (options: {
/** @deprecated Use projectPath instead */
projectid?: string;
removeSourceBranch?: boolean;
gitlabAction?: 'create' | 'update' | 'delete';
}>({
id: 'publish:gitlab:merge-request',
schema: {
@@ -92,6 +93,12 @@ export const createPublishGitlabMergeRequestAction = (options: {
description:
'Option to delete source branch once the MR has been merged. Default: false',
},
gitlabAction: {
title: 'GitLab Action',
type: 'string',
description:
'Option to configure gitlab action. Options are: create (by default), update and delete',
},
},
},
output: {