Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
Signed-off-by: Peter Zhu <[email protected]>
  • Loading branch information
peterzhuamazon committed Oct 25, 2024
1 parent 0d47735 commit 8904227
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 39 deletions.
63 changes: 34 additions & 29 deletions src/call/add-issue-to-github-project-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// : Ex: `opensearch-project/206` which is the OpenSearch Roadmap Project
// Requirements : ADDITIONAL_RESOURCE_CONTEXT=true

import crypto from 'crypto';
import * as crypto from 'crypto';
import { Probot } from 'probot';
import { Resource } from '../service/resource/resource';
import { validateResourceConfig } from '../utility/verification/verify-resource';
Expand Down Expand Up @@ -45,49 +45,54 @@ export default async function addIssueToGitHubProjectV2(
context: any,
resource: Resource,
{ labels, projects }: AddIssueToGitHubProjectV2Params,
): Promise<string | Map<string, string>> {
if (!(await validateResourceConfig(app, context, resource))) return 'none';
if (!(await validateProjects(app, resource, projects))) return 'none';
): Promise<void | Map<string, [string, string]>> {
if (!(await validateResourceConfig(app, context, resource))) return;
if (!(await validateProjects(app, resource, projects))) return;

// Verify triggered label
const label = context.payload.label.name.trim();
if (!labels.includes(label)) {
app.log.error(`"${label}" is not defined in call paramter "labels": ${labels}.`);
return 'none';
return;
}

const orgName = context.payload.organization.login;
const repoName = context.payload.repository.name;
const issueNumber = context.payload.issue.number;
const issueNodeId = context.payload.issue.node_id;
const itemIdMap = new Map<string, string>();
const itemIdMap = new Map<string, [string, string]>();

// Add to project
await Promise.all(
projects.map(async (project) => {
app.log.info(`Attempt to add ${orgName}/${repoName}/${issueNumber} to project ${project}`);
const mutationId = await crypto.randomBytes(20).toString('hex');
const projectSplit = project.split('/');
const projectNodeId = resource.organizations.get(projectSplit[0])?.projects.get(Number(projectSplit[1]))?.nodeId;
const addToProjectMutation = `
mutation {
addProjectV2ItemById(input: {
clientMutationId: "${mutationId}",
contentId: "${issueNodeId}",
projectId: "${projectNodeId}",
}) {
item {
id
try {
await Promise.all(
projects.map(async (project) => {
app.log.info(`Attempt to add ${orgName}/${repoName}/${issueNumber} to project ${project}`);
const mutationId = await crypto.randomBytes(20).toString('hex');
const projectSplit = project.split('/');
const projectNodeId = resource.organizations.get(projectSplit[0])?.projects.get(Number(projectSplit[1]))?.nodeId;
const addToProjectMutation = `
mutation {
addProjectV2ItemById(input: {
clientMutationId: "${mutationId}",
contentId: "${issueNodeId}",
projectId: "${projectNodeId}",
}) {
item {
id
}
}
}
}
`;
const responseAddToProject = await context.octokit.graphql(addToProjectMutation);
app.log.info(responseAddToProject);
const itemId = responseAddToProject.addProjectV2ItemById.item.id;
itemIdMap.set(project, itemId);
}),
);
`;
const responseAddToProject = await context.octokit.graphql(addToProjectMutation);
app.log.info(responseAddToProject);
const itemId = responseAddToProject.addProjectV2ItemById.item.id;
itemIdMap.set(project, [itemId, label]);
}),
);
} catch (e) {
app.log.error(`ERROR: ${e}`);
return;
}

return itemIdMap;
}
63 changes: 53 additions & 10 deletions test/call/add-issue-to-github-project-v2.test.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,16 @@
//import * as crypto from 'crypto';
import addIssueToGitHubProjectV2, { AddIssueToGitHubProjectV2Params } from '../../src/call/add-issue-to-github-project-v2';
import { validateProjects } from '../../src/call/add-issue-to-github-project-v2';
import { Probot, Logger } from 'probot';

// Mock mutationId return
jest.mock('crypto', () => ({
randomBytes: jest.fn(() => {
return {
toString: jest.fn().mockReturnValue('mutation-id'),
};
}),
}));

describe('addIssueToGitHubProjectV2Functions', () => {
let app: Probot;
let context: any;
Expand Down Expand Up @@ -44,12 +52,6 @@ describe('addIssueToGitHubProjectV2Functions', () => {
labels: ['Meta', 'RFC'],
projects: ['test-org/222'],
};

jest.mock('crypto', () => ({
randomBytes: jest.fn().mockReturnValue({
toString: jest.fn().mockReturnValue('mocked-mutation-id'),
}),
}));
});

afterEach(() => {
Expand All @@ -71,12 +73,53 @@ describe('addIssueToGitHubProjectV2Functions', () => {
describe('addIssueToGitHubProjectV2', () => {
it('should print error if context label does not match the ones in resource config', async () => {
context.payload.label.name = 'enhancement';

const result = await addIssueToGitHubProjectV2(app, context, resource, params);

expect(app.log.error).toHaveBeenCalledWith('"enhancement" is not defined in call paramter "labels": Meta,RFC.');
expect(result).toBe('none');
expect(result).toBe(undefined);
expect(context.octokit.graphql).not.toHaveBeenCalled();
});

it('should add context issue to project when conditions are met', async () => {
const graphQLResponse = {
addProjectV2ItemById: { item: { id: 'new-item-id' } },
};

context.octokit.graphql.mockResolvedValue(graphQLResponse);

const result = await addIssueToGitHubProjectV2(app, context, resource, params);

/* prettier-ignore-start */
const graphQLCallStack = `
mutation {
addProjectV2ItemById(input: {
clientMutationId: "mutation-id",
contentId: "issue-111-nodeid",
projectId: "project-222-nodeid",
}) {
item {
id
}
}
}
`;
/* prettier-ignore-end */

expect(context.octokit.graphql).toHaveBeenCalledWith(graphQLCallStack);
expect(JSON.stringify((result as Map<string, [string, string]>).get('test-org/222'))).toBe('["new-item-id","Meta"]');
expect(app.log.info).toHaveBeenCalledWith(graphQLResponse);
});

it('should print log error when GraphQL call fails', async () => {
context.octokit.graphql.mockRejectedValue(new Error('GraphQL request failed'));

const result = await addIssueToGitHubProjectV2(app, context, resource, params)

expect(context.octokit.graphql).rejects.toThrow('GraphQL request failed')
expect(context.octokit.graphql).toHaveBeenCalled();
expect(result).toBe(undefined);
expect(app.log.error).toHaveBeenCalledWith('ERROR: Error: GraphQL request failed');
});
});
});

0 comments on commit 8904227

Please sign in to comment.