From 41cec438353a25fdf6ae6e2dcf54a33e252b6c06 Mon Sep 17 00:00:00 2001 From: Lilly Holden Date: Fri, 15 Jul 2022 15:41:10 +0100 Subject: [PATCH] Scaffolder Gitlab MR - added optional assignee Signed-off-by: Lilly Holden --- plugins/scaffolder-backend/api-report.md | 1 + .../publish/gitlabMergeRequest.test.ts | 133 ++++++++++++++++++ .../builtin/publish/gitlabMergeRequest.ts | 22 +++ 3 files changed, 156 insertions(+) diff --git a/plugins/scaffolder-backend/api-report.md b/plugins/scaffolder-backend/api-report.md index bcdcfd7546..69777631b7 100644 --- a/plugins/scaffolder-backend/api-report.md +++ b/plugins/scaffolder-backend/api-report.md @@ -406,6 +406,7 @@ export const createPublishGitlabMergeRequestAction: (options: { token?: string | undefined; projectid?: string | undefined; removeSourceBranch?: boolean | undefined; + assignee?: string | 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 987e360617..486e5b4ac1 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 @@ -53,6 +53,15 @@ const mockGitlabClient = { }, Users: { current: jest.fn(), + username: jest.fn(async (user: string) => { + if (user !== 'John Smith') throw new Error('user does not exist'); + else + return [ + { + id: 123, + }, + ]; + }), }, }; @@ -204,4 +213,128 @@ describe('createGitLabMergeRequest', () => { ); }); }); + + describe('createGitLabMergeRequestWithAssignee', () => { + it('assignee is set correcly when a valid assignee username is passed in options', async () => { + const input = { + repoUrl: 'gitlab.com?repo=repo&owner=owner', + title: 'Create my new MR', + branchName: 'new-mr', + description: 'This is an important change', + removeSourceBranch: false, + draft: true, + targetPath: 'Subdirectory', + assignee: 'John Smith', + }; + 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( + 'owner/repo', + 'new-mr', + 'main', + 'Create my new MR', + { + description: 'This is an important change', + removeSourceBranch: false, + assigneeId: 123, + }, + ); + }); + + it('assignee is not set when a valid assignee username is not passed in options', async () => { + const input = { + repoUrl: 'gitlab.com?repo=repo&owner=owner', + title: 'Create my new MR', + branchName: 'new-mr', + description: 'This is an important change', + removeSourceBranch: false, + draft: true, + targetPath: 'Subdirectory', + assingnee: 'John Doe', + }; + 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( + 'owner/repo', + 'new-mr', + 'main', + 'Create my new MR', + { + description: 'This is an important change', + removeSourceBranch: false, + assigneeId: undefined, + }, + ); + }); + }); + describe('createGitLabMergeRequestWithoutAssignee', () => { + it('merge request is successfully created without an assignee when assignee username is not passed in options', async () => { + const input = { + repoUrl: 'gitlab.com?repo=repo&owner=owner', + title: 'Create my new MR', + branchName: 'new-mr', + description: 'This is an important change', + removeSourceBranch: false, + draft: true, + targetPath: 'Subdirectory', + }; + 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( + 'owner/repo', + 'new-mr', + 'main', + 'Create my new MR', + { + description: 'This is an important change', + removeSourceBranch: false, + }, + ); + }); + }); }); 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..4dfeb9c2b4 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; + assignee?: string; }>({ id: 'publish:gitlab:merge-request', schema: { @@ -92,6 +93,11 @@ export const createPublishGitlabMergeRequestAction = (options: { description: 'Option to delete source branch once the MR has been merged. Default: false', }, + assignee: { + title: 'Merge Request Assignee', + type: 'string', + description: 'User this merge request will be assigned to', + }, }, }, output: { @@ -146,6 +152,21 @@ export const createPublishGitlabMergeRequestAction = (options: { [tokenType]: token, }); + const assignee = ctx.input.assignee; + + let assigneeId = undefined; + + if (assignee !== undefined) { + try { + const assigneeUser = await api.Users.username(assignee); + assigneeId = assigneeUser[0].id; + } catch (e) { + throw new InputError( + `Failed to find gitlab user id for ${assignee}: ${e}`, + ); + } + } + const targetPath = resolveSafeChildPath( ctx.workspacePath, ctx.input.targetPath, @@ -199,6 +220,7 @@ export const createPublishGitlabMergeRequestAction = (options: { removeSourceBranch: ctx.input.removeSourceBranch ? ctx.input.removeSourceBranch : false, + assigneeId: assigneeId, }, ).then((mergeRequest: { web_url: string }) => { return mergeRequest.web_url;