From eeff5046aecf935064a406d3ac08ae3dfc687ac2 Mon Sep 17 00:00:00 2001 From: Lilly Holden Date: Thu, 16 Jun 2022 12:32:52 +0100 Subject: [PATCH 1/5] Updated publish:gitlab:merge-request action to allow commit updates and deletes Signed-off-by: Lilly Holden --- .changeset/thick-readers-invite.md | 5 +++++ .../actions/builtin/publish/gitlabMergeRequest.ts | 10 +++++++++- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100644 .changeset/thick-readers-invite.md diff --git a/.changeset/thick-readers-invite.md b/.changeset/thick-readers-invite.md new file mode 100644 index 0000000000..3da406ff05 --- /dev/null +++ b/.changeset/thick-readers-invite.md @@ -0,0 +1,5 @@ +--- +'@backstage/plugin-scaffolder-backend': patch +--- + +Updated publish:gitlab:merge-request action to allow commit updates and deletes 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..17026cc895 100644 --- a/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.ts +++ b/plugins/scaffolder-backend/src/scaffolder/actions/builtin/publish/gitlabMergeRequest.ts @@ -40,6 +40,7 @@ export const createPublishGitlabMergeRequestAction = (options: { branchName: string; targetPath: string; token?: string; + commitAction?: 'create' | 'delete' | 'update'; /** @deprecated Use projectPath instead */ projectid?: string; }>({ @@ -85,6 +86,13 @@ export const createPublishGitlabMergeRequestAction = (options: { type: 'string', description: 'The token to use for authorization to GitLab', }, + commitAction: { + title: 'Commit action', + type: 'string', + enum: ['create', 'update', 'delete'], + description: + 'The action to be used for git commit. Defaults to create.', + }, }, }, output: { @@ -148,7 +156,7 @@ export const createPublishGitlabMergeRequestAction = (options: { }); const actions: Types.CommitAction[] = fileContents.map(file => ({ - action: 'create', + action: ctx.input.commitAction ?? 'create', filePath: path.posix.join(ctx.input.targetPath, file.path), encoding: 'base64', content: file.content.toString('base64'), From a44e5955e043bf2f75ea8f18c120f781b888218b Mon Sep 17 00:00:00 2001 From: Lilly Holden Date: Mon, 20 Jun 2022 13:57:11 +0100 Subject: [PATCH 2/5] Updated API Report Signed-off-by: Lilly Holden --- 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..b5950cd868 100644 --- a/plugins/scaffolder-backend/api-report.md +++ b/plugins/scaffolder-backend/api-report.md @@ -344,6 +344,7 @@ export const createPublishGitlabMergeRequestAction: (options: { branchName: string; targetPath: string; token?: string | undefined; + commitAction?: 'update' | 'delete' | 'create' | undefined; projectid?: string | undefined; }>; From 84f29c180b773c6c3531c18d7fd75174ab7957fd Mon Sep 17 00:00:00 2001 From: Lilly Holden Date: Wed, 6 Jul 2022 10:51:15 +0100 Subject: [PATCH 3/5] Added tests Signed-off-by: Lilly Holden --- .../publish/gitlabMergeRequest.test.ts | 174 ++++++++++++++++++ 1 file changed, 174 insertions(+) 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 987e360617..9bbdfa6c3f 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 @@ -204,4 +204,178 @@ describe('createGitLabMergeRequest', () => { ); }); }); + + describe('createGitLabMergeRequestWithoutCommitAction', () => { + it('default commitAction is create', 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, + targetPath: 'source', + }; + 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( + 'owner/repo', + 'new-mr', + 'Create my new MR', + [ + { + action: 'create', + filePath: 'source/foo.txt', + content: 'SGVsbG8gdGhlcmUh', + encoding: 'base64', + execute_filemode: false, + }, + ], + ); + }); + }); + + describe('createGitLabMergeRequestWithCommitAction', () => { + it('commitAction 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', + commitAction: 'create', + draft: true, + targetPath: 'source', + }; + 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( + 'owner/repo', + 'new-mr', + 'Create my new MR', + [ + { + action: 'create', + filePath: 'source/foo.txt', + content: 'SGVsbG8gdGhlcmUh', + encoding: 'base64', + execute_filemode: false, + }, + ], + ); + }); + + it('commitAction 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', + commitAction: 'update', + draft: true, + targetPath: 'source', + }; + 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( + 'owner/repo', + 'new-mr', + 'Create my new MR', + [ + { + action: 'update', + filePath: 'source/foo.txt', + content: 'SGVsbG8gdGhlcmUh', + encoding: 'base64', + execute_filemode: false, + }, + ], + ); + }); + + it('commitAction 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', + commitAction: 'delete', + draft: true, + targetPath: 'source', + }; + 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( + 'owner/repo', + 'new-mr', + 'Create my new MR', + [ + { + action: 'delete', + filePath: 'source/foo.txt', + content: 'SGVsbG8gdGhlcmUh', + encoding: 'base64', + execute_filemode: false, + }, + ], + ); + }); + }); }); From af75a454bdabff8891f85d4a0f7f7a48a2fd626d Mon Sep 17 00:00:00 2001 From: Lilly Holden Date: Thu, 14 Jul 2022 15:28:47 +0100 Subject: [PATCH 4/5] Updated API report Signed-off-by: Lilly Holden --- plugins/scaffolder-backend/api-report.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/scaffolder-backend/api-report.md b/plugins/scaffolder-backend/api-report.md index 30f77aac01..2667f97277 100644 --- a/plugins/scaffolder-backend/api-report.md +++ b/plugins/scaffolder-backend/api-report.md @@ -401,7 +401,7 @@ export const createPublishGitlabMergeRequestAction: (options: { branchName: string; targetPath: string; token?: string | undefined; - commitAction?: 'update' | 'delete' | 'create' | undefined; + commitAction?: 'update' | 'create' | 'delete' | undefined; projectid?: string | undefined; removeSourceBranch?: boolean | undefined; }>; From 183b30ff8a25199d8e473e6337e03bfe8c932570 Mon Sep 17 00:00:00 2001 From: Lilly Holden Date: Mon, 18 Jul 2022 14:21:11 +0100 Subject: [PATCH 5/5] updated changeset Signed-off-by: Lilly Holden --- .changeset/thick-readers-invite.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.changeset/thick-readers-invite.md b/.changeset/thick-readers-invite.md index 3da406ff05..689ae7eb0c 100644 --- a/.changeset/thick-readers-invite.md +++ b/.changeset/thick-readers-invite.md @@ -1,5 +1,5 @@ --- -'@backstage/plugin-scaffolder-backend': patch +'@backstage/plugin-scaffolder-backend': minor --- -Updated publish:gitlab:merge-request action to allow commit updates and deletes +Updated `publish:gitlab:merge-request` action to allow commit updates and deletes