From 4baf8a4ece338045f3a2ad919242661919cc5b14 Mon Sep 17 00:00:00 2001 From: Aisha Saini Date: Tue, 3 May 2022 15:40:24 +0100 Subject: [PATCH 1/8] Update GitLab MR Action to allow source branch to be deleted on merge Signed-off-by: Aisha Saini Signed-off-by: asaini1 --- .changeset/old-onions-hear.md | 5 +++++ .../actions/builtin/publish/gitlabMergeRequest.ts | 12 +++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 .changeset/old-onions-hear.md diff --git a/.changeset/old-onions-hear.md b/.changeset/old-onions-hear.md new file mode 100644 index 0000000000..82dcc09190 --- /dev/null +++ b/.changeset/old-onions-hear.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend': minor +--- + +Update GitLab Merge Request Action to allow source branch to be deleted diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.ts index f717a7a0c6..fe94f5ae53 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.ts @@ -42,6 +42,7 @@ export const createPublishGitlabMergeRequestAction = (options: { token?: string; /** @deprecated Use projectPath instead */ projectid?: string; + removeSourceBranch?: boolean; }>({ id: 'publish:gitlab:merge-request', schema: { @@ -85,6 +86,12 @@ export const createPublishGitlabMergeRequestAction = (options: { type: 'string', description: 'The token to use for authorization to GitLab', }, + removeSourceBranch: { + title: 'Delete source branch', + type: 'boolean', + description: + 'Option to delete source branch once the MR has been merged. Default: false', + }, }, }, output: { @@ -187,7 +194,10 @@ export const createPublishGitlabMergeRequestAction = (options: { destinationBranch, String(defaultBranch), ctx.input.title, - { description: ctx.input.description }, + { + description: ctx.input.description, + removeSourceBranch: ctx.input.removeSourceBranch, + }, ).then((mergeRequest: { web_url: string }) => { return mergeRequest.web_url; }); From 5b99da51d9d0b090938e6224384ac2096680f83c Mon Sep 17 00:00:00 2001 From: Aisha Saini Date: Wed, 18 May 2022 08:43:42 +0100 Subject: [PATCH 2/8] Add api-report.md file Signed-off-by: Aisha Saini Signed-off-by: asaini1 --- plugins/scaffolder-backend/api-report.md | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/scaffolder-backend/api-report.md b/plugins/scaffolder-backend/api-report.md index 707b8e62ba..b0f831fc8c 100644 --- a/plugins/scaffolder-backend/api-report.md +++ b/plugins/scaffolder-backend/api-report.md @@ -345,6 +345,7 @@ export const createPublishGitlabMergeRequestAction: (options: { targetPath: string; token?: string | undefined; projectid?: string | undefined; + removeSourceBranch?: boolean | undefined; }>; // @public From f560f836ca57429505092834f55a8a38ce48f44d Mon Sep 17 00:00:00 2001 From: Aisha Saini Date: Wed, 18 May 2022 09:56:48 +0100 Subject: [PATCH 3/8] Update removeSourceBranch option to cater for when not set Signed-off-by: Aisha Saini Signed-off-by: asaini1 --- .../scaffolder/actions/builtin/publish/gitlabMergeRequest.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.ts index fe94f5ae53..b1d5b35fa3 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.ts @@ -196,7 +196,9 @@ export const createPublishGitlabMergeRequestAction = (options: { ctx.input.title, { description: ctx.input.description, - removeSourceBranch: ctx.input.removeSourceBranch, + removeSourceBranch: ctx.input.removeSourceBranch + ? ctx.input.removeSourceBranch + : false, }, ).then((mergeRequest: { web_url: string }) => { return mergeRequest.web_url; From 08a27a58ad4110b3f18cf4f932072be3dae5e983 Mon Sep 17 00:00:00 2001 From: Aisha Saini Date: Tue, 24 May 2022 10:56:10 +0100 Subject: [PATCH 4/8] Add gitlab MR test file Signed-off-by: Aisha Saini Signed-off-by: asaini1 --- .../publish/gitlabMergeRequest.test.ts | 219 ++++++++++++++++++ 1 file changed, 219 insertions(+) create mode 100644 plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.test.ts diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.test.ts new file mode 100644 index 0000000000..cf2bc57514 --- /dev/null +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.test.ts @@ -0,0 +1,219 @@ +/* + * Copyright 2022 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. + */ + +// * 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 { getRootLogger } from '@backstage/backend-common'; +import { ConfigReader } from '@backstage/config'; +import { ScmIntegrations } from '@backstage/integration'; +import mockFs from 'mock-fs'; +import os from 'os'; +import { resolve as resolvePath } from 'path'; +import { Writable } from 'stream'; +import { TemplateAction } from '../../types'; +import { createPublishGitlabMergeRequestAction } from './gitlabMergeRequest'; + +const root = os.platform() === 'win32' ? 'C:\\root' : '/root'; +const workspacePath = resolvePath(root, 'my-workspace'); + +const mockGitlabClient = { + Namespaces: { + show: jest.fn(), + }, + Branches: { + create: jest.fn(), + }, + Commits: { + create: jest.fn(), + }, + MergeRequests: { + create: jest.fn(async (_: any) => { + return { + default_branch: 'main', + }; + }), + }, + Projects: { + create: jest.fn(), + show: jest.fn(async (_: any) => { + return { + default_branch: 'main', + }; + }), + }, + Users: { + current: jest.fn(), + }, +}; + +jest.mock('@gitbeaker/node', () => ({ + Gitlab: class { + constructor() { + return mockGitlabClient; + } + }, +})); + +describe('createGitLabMergeRequest', () => { + let instance: TemplateAction; + + beforeEach(() => { + const config = new ConfigReader({ + integrations: { + gitlab: [ + { + host: 'gitlab.com', + token: 'token', + apiBaseUrl: 'https://api.gitlab.com', + }, + { + host: 'hosted.gitlab.com', + apiBaseUrl: 'https://api.hosted.gitlab.com', + }, + ], + }, + }); + + const integrations = ScmIntegrations.fromConfig(config); + instance = createPublishGitlabMergeRequestAction({ integrations }); + }); + + afterEach(() => { + mockFs.restore(); + }); + + describe('createGitLabMergeRequestWithoutRemoveBranch', () => { + it('removeSourceBranch is false 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.MergeRequests.create).toHaveBeenCalledWith( + undefined, + 'new-mr', + 'main', + 'Create my new MR', + { description: 'This MR is really good', removeSourceBranch: false }, + ); + }); + }); + + describe('createGitLabMergeRequestWithRemoveBranch', () => { + it('removeSourceBranch is true when true 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', + removeSourceBranch: true, + 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.MergeRequests.create).toHaveBeenCalledWith( + undefined, + 'new-mr', + 'main', + 'Create my new MR', + { description: 'MR description', removeSourceBranch: true }, + ); + }); + + it('removeSourceBranch is false when false 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', + removeSourceBranch: false, + 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.MergeRequests.create).toHaveBeenCalledWith( + undefined, + 'new-mr', + 'main', + 'Create my new MR', + { + description: 'other MR description', + removeSourceBranch: false, + }, + ); + }); + }); +}); From b4149fc0a361ac80d655a2eae164a8dbbe0989d1 Mon Sep 17 00:00:00 2001 From: Aisha Saini Date: Fri, 27 May 2022 15:21:26 +0100 Subject: [PATCH 5/8] Add config and tests for new gitlab actions update and delete Signed-off-by: Aisha Saini Signed-off-by: asaini1 --- .changeset/old-onions-hear.md | 2 +- plugins/scaffolder-backend/api-report.md | 1 + .../publish/gitlabMergeRequest.test.ts | 175 ++++++++++++++++++ .../builtin/publish/gitlabMergeRequest.ts | 7 + 4 files changed, 184 insertions(+), 1 deletion(-) diff --git a/.changeset/old-onions-hear.md b/.changeset/old-onions-hear.md index 82dcc09190..febf2c2bf6 100644 --- a/.changeset/old-onions-hear.md +++ b/.changeset/old-onions-hear.md @@ -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 diff --git a/plugins/scaffolder-backend/api-report.md b/plugins/scaffolder-backend/api-report.md index b0f831fc8c..742293da30 100644 --- a/plugins/scaffolder-backend/api-report.md +++ b/plugins/scaffolder-backend/api-report.md @@ -346,6 +346,7 @@ export const createPublishGitlabMergeRequestAction: (options: { token?: string | undefined; projectid?: string | undefined; removeSourceBranch?: boolean | undefined; + gitlabAction?: 'update' | 'create' | 'delete' | undefined; }>; // @public diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.test.ts index cf2bc57514..889b8b862c 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.test.ts @@ -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; @@ -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', + }, + ], + ); + }); + }); }); diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.ts index b1d5b35fa3..7e0949454b 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.ts @@ -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: { From c8e8a164b7744d6d4a990f21ea89863d2a5f9ce1 Mon Sep 17 00:00:00 2001 From: Aisha Saini Date: Wed, 15 Jun 2022 15:57:41 +0100 Subject: [PATCH 6/8] Remove gitlab action changes from PR Signed-off-by: Aisha Saini Signed-off-by: asaini1 --- .changeset/old-onions-hear.md | 2 +- plugins/scaffolder-backend/api-report.md | 1 - .../actions/builtin/publish/gitlabMergeRequest.ts | 7 ------- 3 files changed, 1 insertion(+), 9 deletions(-) diff --git a/.changeset/old-onions-hear.md b/.changeset/old-onions-hear.md index febf2c2bf6..82dcc09190 100644 --- a/.changeset/old-onions-hear.md +++ b/.changeset/old-onions-hear.md @@ -2,4 +2,4 @@ '@backstage/plugin-scaffolder-backend': minor --- -Update GitLab Merge Request Action to allow source branch to be deleted & configure additional gitlab actions: update and delete +Update GitLab Merge Request Action to allow source branch to be deleted diff --git a/plugins/scaffolder-backend/api-report.md b/plugins/scaffolder-backend/api-report.md index 742293da30..b0f831fc8c 100644 --- a/plugins/scaffolder-backend/api-report.md +++ b/plugins/scaffolder-backend/api-report.md @@ -346,7 +346,6 @@ export const createPublishGitlabMergeRequestAction: (options: { token?: string | undefined; projectid?: string | undefined; removeSourceBranch?: boolean | undefined; - gitlabAction?: 'update' | 'create' | 'delete' | undefined; }>; // @public diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.ts index 7e0949454b..b1d5b35fa3 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.ts @@ -43,7 +43,6 @@ export const createPublishGitlabMergeRequestAction = (options: { /** @deprecated Use projectPath instead */ projectid?: string; removeSourceBranch?: boolean; - gitlabAction?: 'create' | 'update' | 'delete'; }>({ id: 'publish:gitlab:merge-request', schema: { @@ -93,12 +92,6 @@ 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: { From 3c09270d2deffa67106cbe2dab584d50907aa3a4 Mon Sep 17 00:00:00 2001 From: asaini1 Date: Fri, 24 Jun 2022 11:49:41 +0100 Subject: [PATCH 7/8] Update tests Signed-off-by: Aisha Saini Signed-off-by: asaini1 --- .../publish/gitlabMergeRequest.test.ts | 184 +----------------- 1 file changed, 6 insertions(+), 178 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.test.ts index 889b8b862c..7fe01921d1 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.test.ts @@ -79,20 +79,6 @@ 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; @@ -129,6 +115,7 @@ describe('createGitLabMergeRequest', () => { branchName: 'new-mr', description: 'This MR is really good', draft: true, + targetPath: 'Subdirectory', }; mockFs({ [workspacePath]: { @@ -147,7 +134,7 @@ describe('createGitLabMergeRequest', () => { await instance.handler(ctx); expect(mockGitlabClient.MergeRequests.create).toHaveBeenCalledWith( - undefined, + 'owner/repo', 'new-mr', 'main', 'Create my new MR', @@ -165,6 +152,7 @@ describe('createGitLabMergeRequest', () => { description: 'MR description', removeSourceBranch: true, draft: true, + targetPath: 'Subdirectory', }; mockFs({ [workspacePath]: { @@ -184,7 +172,7 @@ describe('createGitLabMergeRequest', () => { await instance.handler(ctx); expect(mockGitlabClient.MergeRequests.create).toHaveBeenCalledWith( - undefined, + 'owner/repo', 'new-mr', 'main', 'Create my new MR', @@ -200,6 +188,7 @@ describe('createGitLabMergeRequest', () => { description: 'other MR description', removeSourceBranch: false, draft: true, + targetPath: 'Subdirectory', }; mockFs({ [workspacePath]: { @@ -219,7 +208,7 @@ describe('createGitLabMergeRequest', () => { await instance.handler(ctx); expect(mockGitlabClient.MergeRequests.create).toHaveBeenCalledWith( - undefined, + 'owner/repo', 'new-mr', 'main', 'Create my new MR', @@ -230,165 +219,4 @@ 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', - }, - ], - ); - }); - }); }); From eded24fc8c2bb1a65dd849740427ea22925b2dac Mon Sep 17 00:00:00 2001 From: asaini1 Date: Mon, 27 Jun 2022 14:01:53 +0100 Subject: [PATCH 8/8] Remove duplicate license Signed-off-by: asaini1 --- .../builtin/publish/gitlabMergeRequest.test.ts | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.test.ts b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.test.ts index 7fe01921d1..987e360617 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.test.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.test.ts @@ -13,21 +13,6 @@ * See the License for the specific language governing permissions and * limitations under the License. */ - -// * 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 { getRootLogger } from '@backstage/backend-common'; import { ConfigReader } from '@backstage/config'; import { ScmIntegrations } from '@backstage/integration';