|
| 1 | +/* |
| 2 | + * Copyright OpenSearch Contributors |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * |
| 5 | + * The OpenSearch Contributors require contributions made to |
| 6 | + * this file be licensed under the Apache-2.0 license or a |
| 7 | + * compatible open source license. |
| 8 | + */ |
| 9 | + |
| 10 | +import addIssueToGitHubProjectV2, { AddIssueToGitHubProjectV2Params } from '../../src/call/add-issue-to-github-project-v2'; |
| 11 | +import { validateProjects } from '../../src/call/add-issue-to-github-project-v2'; |
| 12 | +import { Probot, Logger } from 'probot'; |
| 13 | + |
| 14 | +// Mock mutationId return |
| 15 | +jest.mock('crypto', () => ({ |
| 16 | + randomBytes: jest.fn(() => { |
| 17 | + return { |
| 18 | + toString: jest.fn().mockReturnValue('mutation-id'), |
| 19 | + }; |
| 20 | + }), |
| 21 | +})); |
| 22 | + |
| 23 | +describe('addIssueToGitHubProjectV2Functions', () => { |
| 24 | + let app: Probot; |
| 25 | + let context: any; |
| 26 | + let resource: any; |
| 27 | + let params: AddIssueToGitHubProjectV2Params; |
| 28 | + |
| 29 | + beforeEach(() => { |
| 30 | + app = new Probot({ appId: 1, secret: 'test', privateKey: 'test' }); |
| 31 | + app.log = { |
| 32 | + info: jest.fn(), |
| 33 | + error: jest.fn(), |
| 34 | + } as unknown as Logger; |
| 35 | + |
| 36 | + context = { |
| 37 | + payload: { |
| 38 | + label: { name: 'Meta' }, |
| 39 | + organization: { login: 'test-org' }, |
| 40 | + repository: { name: 'test-repo' }, |
| 41 | + issue: { number: 111, node_id: 'issue-111-nodeid' }, |
| 42 | + }, |
| 43 | + octokit: { |
| 44 | + graphql: jest.fn(), |
| 45 | + }, |
| 46 | + }; |
| 47 | + |
| 48 | + resource = { |
| 49 | + organizations: new Map([ |
| 50 | + [ |
| 51 | + 'test-org', |
| 52 | + { |
| 53 | + projects: new Map([[222, { nodeId: 'project-222-nodeid' }]]), |
| 54 | + repositories: new Map([['test-repo', 'repo object']]), |
| 55 | + }, |
| 56 | + ], |
| 57 | + ]), |
| 58 | + }; |
| 59 | + |
| 60 | + params = { |
| 61 | + labels: ['Meta', 'RFC'], |
| 62 | + projects: ['test-org/222'], |
| 63 | + }; |
| 64 | + }); |
| 65 | + |
| 66 | + afterEach(() => { |
| 67 | + jest.clearAllMocks(); |
| 68 | + }); |
| 69 | + |
| 70 | + describe('validateProjects', () => { |
| 71 | + it('should print error if validateProjects returns false', async () => { |
| 72 | + resource.organizations.get('test-org').projects.delete(222); |
| 73 | + |
| 74 | + const result = await validateProjects(app, resource, params.projects); |
| 75 | + |
| 76 | + expect(app.log.error).toHaveBeenCalledWith('Project 222 in organization test-org is not defined in resource config!'); |
| 77 | + expect(result).toBe(false); |
| 78 | + expect(context.octokit.graphql).not.toHaveBeenCalled(); |
| 79 | + }); |
| 80 | + }); |
| 81 | + |
| 82 | + describe('addIssueToGitHubProjectV2', () => { |
| 83 | + it('should print error if context label does not match the ones in resource config', async () => { |
| 84 | + context.payload.label.name = 'enhancement'; |
| 85 | + |
| 86 | + const result = await addIssueToGitHubProjectV2(app, context, resource, params); |
| 87 | + |
| 88 | + expect(app.log.error).toHaveBeenCalledWith('"enhancement" is not defined in call paramter "labels": Meta,RFC.'); |
| 89 | + expect(result).toBe(null); |
| 90 | + expect(context.octokit.graphql).not.toHaveBeenCalled(); |
| 91 | + }); |
| 92 | + |
| 93 | + it('should add context issue to project when conditions are met', async () => { |
| 94 | + const graphQLResponse = { |
| 95 | + addProjectV2ItemById: { item: { id: 'new-item-id' } }, |
| 96 | + }; |
| 97 | + |
| 98 | + context.octokit.graphql.mockResolvedValue(graphQLResponse); |
| 99 | + |
| 100 | + const result = await addIssueToGitHubProjectV2(app, context, resource, params); |
| 101 | + |
| 102 | + /* prettier-ignore-start */ |
| 103 | + const graphQLCallStack = ` |
| 104 | + mutation { |
| 105 | + addProjectV2ItemById(input: { |
| 106 | + clientMutationId: "mutation-id", |
| 107 | + contentId: "issue-111-nodeid", |
| 108 | + projectId: "project-222-nodeid", |
| 109 | + }) { |
| 110 | + item { |
| 111 | + id |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | + `; |
| 116 | + /* prettier-ignore-end */ |
| 117 | + |
| 118 | + expect(context.octokit.graphql).toHaveBeenCalledWith(graphQLCallStack); |
| 119 | + expect(JSON.stringify((result as Map<string, [string, string]>).get('test-org/222'))).toBe('["new-item-id","Meta"]'); |
| 120 | + expect(app.log.info).toHaveBeenCalledWith(graphQLResponse); |
| 121 | + }); |
| 122 | + |
| 123 | + it('should print log error when GraphQL call fails', async () => { |
| 124 | + context.octokit.graphql.mockRejectedValue(new Error('GraphQL request failed')); |
| 125 | + |
| 126 | + const result = await addIssueToGitHubProjectV2(app, context, resource, params); |
| 127 | + |
| 128 | + expect(context.octokit.graphql).rejects.toThrow('GraphQL request failed'); |
| 129 | + expect(context.octokit.graphql).toHaveBeenCalled(); |
| 130 | + expect(result).toBe(null); |
| 131 | + expect(app.log.error).toHaveBeenCalledWith('ERROR: Error: GraphQL request failed'); |
| 132 | + }); |
| 133 | + }); |
| 134 | +}); |
0 commit comments