Scaffolder Gitlab MR - added optional assignee

Signed-off-by: Lilly Holden <lholden@adaptavist.com>
This commit is contained in:
Lilly Holden
2022-07-15 15:41:10 +01:00
parent a3b9b7f012
commit 41cec43835
3 changed files with 156 additions and 0 deletions
+1
View File
@@ -406,6 +406,7 @@ export const createPublishGitlabMergeRequestAction: (options: {
token?: string | undefined;
projectid?: string | undefined;
removeSourceBranch?: boolean | undefined;
assignee?: string | undefined;
}>;
// @public
@@ -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,
},
);
});
});
});
@@ -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;