From b24b0a2d30408bf7e280d13d94b95cd3c369e98d Mon Sep 17 00:00:00 2001 From: Elaine De-Mattos-Silva-Bezerra Date: Tue, 5 Dec 2023 22:24:11 +0100 Subject: [PATCH] feat: added initial tests Signed-off-by: Elaine Mattos --- .../sample-templates/template.yaml | 38 +++++- .../src/util.test.ts | 115 ++++++++++++++---- .../src/util.ts | 7 +- 3 files changed, 129 insertions(+), 31 deletions(-) diff --git a/plugins/scaffolder-backend-module-gitlab/sample-templates/template.yaml b/plugins/scaffolder-backend-module-gitlab/sample-templates/template.yaml index 9078c33483..8ae328a8d2 100644 --- a/plugins/scaffolder-backend-module-gitlab/sample-templates/template.yaml +++ b/plugins/scaffolder-backend-module-gitlab/sample-templates/template.yaml @@ -20,6 +20,31 @@ spec: ui:autofocus: true ui:options: rows: 5 + projectId: + title: Project id + type: number + weight: + title: Issue weight + type: number + title: + title: Issue title + type: string + description: + title: Issue description + type: string + issueType: + title: Type + type: string + ui:help: 'issue, incident, test_case' + createdAt: + title: Creation date + type: string + format: date-time + dueDate: + title: Due date + type: string + format: date-time + - title: Choose a location required: - repoUrl @@ -39,16 +64,17 @@ spec: input: repoUrl: ${{ parameters.repoUrl }} # git.tech.rz.db.de?owner=devex-core&repo=test-repo token: ${{ secrets.USER_OAUTH_TOKEN }} - projectId: 52723821 - title: Test Issue + projectId: ${{ parameters.projectId }} # 52723821 + title: ${{ parameters.title }} + weight: ${{ parameters.weight }} assignees: - 11013327 - description: This is the description of the issue + description: ${{ parameters.description }} confidential: true - createdAt: 2022-09-27 18:00:00.000 - dueDate: 2024-09-28 12:00:00.000 - epicId: 1456 + createdAt: ${{ parameters.createdAt }} # "2023-03-11T03:45:40Z" + dueDate: ${{ parameters.dueDate }} # "2024-03-11T03:45:40Z" labels: test-label1,test-label2 + issueType: ${{ parameters.issueType }} output: links: - title: Link to new issue diff --git a/plugins/scaffolder-backend-module-gitlab/src/util.test.ts b/plugins/scaffolder-backend-module-gitlab/src/util.test.ts index 0da094fb32..c8307a6bd5 100644 --- a/plugins/scaffolder-backend-module-gitlab/src/util.test.ts +++ b/plugins/scaffolder-backend-module-gitlab/src/util.test.ts @@ -13,24 +13,17 @@ * See the License for the specific language governing permissions and * limitations under the License. */ -// import { InputError } from '@backstage/errors'; -import { - // parseRepoHost, - // getToken, - // convertDate, - getTopLevelParentGroup, -} from './util'; + +import * as util from './util'; import { Gitlab, GroupSchema } from '@gitbeaker/rest'; -// import { ScmIntegrations } from '@backstage/integration'; -// import { ConfigReader } from '@backstage/config'; // Mock the Gitlab client and its methods jest.mock('@gitbeaker/rest', () => { - const mockShow = jest.fn(); return { Gitlab: jest.fn().mockImplementation(() => ({ Groups: { - show: mockShow, + show: jest.fn(), + search: jest.fn(), }, })), }; @@ -51,13 +44,84 @@ describe('getTopLevelParentGroup', () => { ], }; - it('should return top-level parent group when parent_id is present', async () => { - const mockGroupId = 123; - const mockParentGroupId = 456; + it('should return the top-level parent group', async () => { + // Instance with token + const mockGitlabClient = new Gitlab({ + host: config.gitlab[0].host, + token: config.gitlab[0].token!, + }); + // Mocked nested groups + const mockGroups: GroupSchema[] = [ + { + id: 789, + parent_id: 0, + path: '', + description: '', + visibility: '', + share_with_group_lock: false, + require_two_factor_authentication: false, + two_factor_grace_period: 0, + project_creation_level: '', + subgroup_creation_level: '', + lfs_enabled: false, + default_branch_protection: 0, + request_access_enabled: false, + created_at: '', + avatar_url: '', + full_name: '', + full_path: '', + web_url: '', + name: '', + }, + { + id: 456, + parent_id: 789, + path: '', + description: '', + visibility: '', + share_with_group_lock: false, + require_two_factor_authentication: false, + two_factor_grace_period: 0, + project_creation_level: '', + subgroup_creation_level: '', + lfs_enabled: false, + default_branch_protection: 0, + request_access_enabled: false, + created_at: '', + avatar_url: '', + full_name: '', + full_path: '', + web_url: '', + name: '', + }, + { + id: 123, + parent_id: 456, + path: '', + description: '', + visibility: '', + share_with_group_lock: false, + require_two_factor_authentication: false, + two_factor_grace_period: 0, + project_creation_level: '', + subgroup_creation_level: '', + lfs_enabled: false, + default_branch_protection: 0, + request_access_enabled: false, + created_at: '', + avatar_url: '', + full_name: '', + full_path: '', + web_url: '', + name: '', + }, + ]; + + // Top level group const mockTopParentGroup: GroupSchema = { - id: mockGroupId, - parent_id: mockParentGroupId, + id: 789, + parent_id: 0, path: '', description: '', visibility: '', @@ -77,16 +141,21 @@ describe('getTopLevelParentGroup', () => { name: '', }; - // Instance with token - const mockGitlabClient = new Gitlab({ - host: config.gitlab[0].host, - token: config.gitlab[0].token!, - }); + const showSpy = jest.spyOn(mockGitlabClient.Groups, 'show'); - const action = getTopLevelParentGroup(mockGitlabClient, mockGroupId); + // Mock implementation of Groups.show + showSpy.mockImplementation( + async (groupId: string | number): Promise => { + const id = + typeof groupId === 'number' ? groupId : parseInt(groupId, 10); + const mockGroup = mockGroups.find(group => group.id === id) || null; + return mockGroup as GroupSchema; + }, + ); - const result = await action; // Await the result + const action = util.getTopLevelParentGroup(mockGitlabClient, 123); + const result = await action; expect(result).toEqual(mockTopParentGroup); }); }); diff --git a/plugins/scaffolder-backend-module-gitlab/src/util.ts b/plugins/scaffolder-backend-module-gitlab/src/util.ts index c967e1cee9..d100dfd8ef 100644 --- a/plugins/scaffolder-backend-module-gitlab/src/util.ts +++ b/plugins/scaffolder-backend-module-gitlab/src/util.ts @@ -22,6 +22,7 @@ import { import { z } from 'zod'; import commonGitlabConfig from './commonGitlabConfig'; import { Gitlab, GroupSchema } from '@gitbeaker/rest'; +import * as util from './util'; export const parseRepoHost = (repoUrl: string): string => { let parsed; @@ -143,9 +144,11 @@ export async function getTopLevelParentGroup( try { const topParentGroup = await client.Groups.show(groupId); if (topParentGroup.parent_id) { - return getTopLevelParentGroup(client, topParentGroup.parent_id as number); + return util.getTopLevelParentGroup( + client, + topParentGroup.parent_id as number, + ); } - return topParentGroup as GroupSchema; } catch (error: any) { throw new InputError(