Skip to content

Commit

Permalink
Is Membership feature
Browse files Browse the repository at this point in the history
  • Loading branch information
gagoar committed Aug 19, 2021
1 parent c75a689 commit b6d7ca6
Show file tree
Hide file tree
Showing 11 changed files with 334 additions and 50 deletions.
1 change: 1 addition & 0 deletions __mocks__/rules/rule1.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@
"customMessage": "This is a custom message for a rule",
"users": ["eeny", "meeny", "miny", "moe"],
"action": "comment",
"isMemberOf": ["counting_out_game"],
"includes": ["*.ts"]
}
35 changes: 29 additions & 6 deletions __tests__/index.HttpFailure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { env } from '../src/environment';
import { Event, HttpErrors } from '../src/util/constants';
import * as actions from '@actions/core';
import { Main, mockedInput } from './util/helpers';
import event from '../__mocks__/event.json';
import getCommentsResponse from '../__mocks__/scenarios/get_comments.json';
import { mockCompareCommits } from './util/mockGitHubRequest';
import { mockCompareCommits, mockRequest } from './util/mockGitHubRequest';


jest.mock('@actions/core');
jest.mock('../src/environment', () => {
Expand All @@ -19,7 +21,16 @@ jest.mock('../src/environment', () => {
},
};
});
jest.mock('@actions/github', () => {
const workflowEvent = jest.requireActual('../__mocks__/event.json') as Event;

return {
context: {
actor: workflowEvent.repository.name,
repo: { owner: workflowEvent.repository.owner.login },
},
};
});
describe('use-herald', () => {
const getInput = actions.getInput as jest.Mock<any>;
const setFailed = actions.setFailed as jest.Mock<any>;
Expand All @@ -35,7 +46,7 @@ describe('use-herald', () => {
// eslint-disable-next-line @typescript-eslint/no-var-requires
} = require(`../${env.GITHUB_EVENT_PATH}`) as Event;

const getGithubMock = () =>
const getCompareCommitsMock = () =>
mockCompareCommits({
login,
name,
Expand All @@ -49,13 +60,23 @@ describe('use-herald', () => {
});

const prIssue = 2;
const github = getGithubMock();
const compareCommitsMock = getCompareCommitsMock();
const membershipMock = mockRequest(
'get',
`/orgs/${event.repository.owner.login}/teams/counting_out_game/memberships/${event.repository.name}`,
200,
{
role: 'maintainer',
state: 'active',
url: `https://api.github.com/teams/1/memberships/${event.repository.owner.login}`,
}
);

github
compareCommitsMock
.get(`/repos/${login}/${name}/issues/${prIssue}/comments?page=1&per_page=100`)
.reply(200, getCommentsResponse);

github
compareCommitsMock
.post(`/repos/${login}/${name}/issues/2/comments`)
.replyWithError({ message: 'Resource not accessible by integration', code: HttpErrors.RESOURCE_NOT_ACCESSIBLE });

Expand All @@ -73,6 +94,8 @@ describe('use-herald', () => {
],
]
`);
expect(github.isDone()).toBe(true);
expect(compareCommitsMock.isDone()).toBe(true);
expect(membershipMock.isDone()).toBe(true);
});

});
89 changes: 75 additions & 14 deletions __tests__/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,38 +2,52 @@
import { Props } from '../src';
import { Event } from '../src/util/constants';
import * as actions from '@actions/core';
import { env } from '../src/environment';
import * as comment from '../src/comment';
import { mockCompareCommits } from './util/mockGitHubRequest';
import { mockCompareCommits, mockRequest, MockResponse } from './util/mockGitHubRequest';
import getCompareCommitsResponse from '../__mocks__/scenarios/get_compare_commits.json';
import { Main, mockedInput } from './util/helpers';
import event from '../__mocks__/event.json';

jest.mock('@actions/core');
jest.mock('../src/comment');

jest.mock('../src/environment', () => {
const { env } = jest.requireActual('../src/environment');

const GITHUB_EVENT_PATH = '__mocks__/event.json';

return {
env: {
...env,
GITHUB_EVENT_PATH: '__mocks__/event.json',
GITHUB_EVENT_PATH,
GITHUB_EVENT_NAME: 'pull_request',
},
};
});

const event = require(`../${env.GITHUB_EVENT_PATH}`) as Event;
jest.mock('@actions/github', () => {
const workflowEvent = jest.requireActual('../__mocks__/event.json') as Event;

return {
context: {
actor: workflowEvent.repository.name,
repo: { owner: workflowEvent.repository.owner.login },
},
};
});

const handleComment = comment.handleComment as jest.Mock<any>;
const setOutput = actions.setOutput as jest.Mock<any>;
const setFailed = actions.setFailed as jest.Mock<any>;
const getInput = actions.getInput as jest.Mock<any>;

const getGithubMock = () =>
const getCompareCommitsMock = (response?: MockResponse) =>
mockCompareCommits({
login: event.repository.owner.login,
name: event.repository.name,
base: event.pull_request.base.sha,
head: event.pull_request.head.sha,
response,
});

describe('use-herald-action', () => {
Expand All @@ -55,13 +69,33 @@ describe('use-herald-action', () => {
expect(setFailed).toHaveBeenCalled();
});

it('should fail if the compareCommits response does not have files', async () => {
const changedRulesDirectory = { ...mockedInput, [Props.rulesLocation]: '__mocks__/required_rules/*.json' };
getInput.mockImplementation((key: Partial<keyof typeof changedRulesDirectory>) => {
return changedRulesDirectory[key];
});

const compareCommitsMock = getCompareCommitsMock({ getCompareCommitsResponse, files: null });

const { main } = require('../src') as Main;

await main();

expect(setFailed.mock.calls[0]).toMatchInlineSnapshot(`
Array [
"There were no files returned from f95f852bd8fca8fcc58a9a2d6c842781e32a215e base and ec26c3e57ca3a959ca5aad62de7213c562f8c821 head",
]
`);
expect(setOutput).not.toHaveBeenCalled();
expect(compareCommitsMock.isDone()).toBe(true);
});
it('should fail if rules with errorLevel set to "error" does not match', async () => {
const changedRulesDirectory = { ...mockedInput, [Props.rulesLocation]: '__mocks__/required_rules/*.json' };
getInput.mockImplementation((key: Partial<keyof typeof changedRulesDirectory>) => {
return changedRulesDirectory[key];
});

const github = getGithubMock();
const compareCommitsMock = getCompareCommitsMock();

const { main } = require('../src') as Main;

Expand All @@ -73,23 +107,35 @@ describe('use-herald-action', () => {
]
`);
expect(setOutput).not.toHaveBeenCalled();
expect(github.isDone()).toBe(true);
expect(compareCommitsMock.isDone()).toBe(true);
});

it('should run normally (with dryRun: true)', async () => {
getInput.mockImplementation((key: Partial<keyof typeof mockedInput>) => {
return mockedInput[key];
});

const github = getGithubMock();
const compareCommitsMock = getCompareCommitsMock();

const membershipMock = mockRequest(
'get',
`/orgs/${event.repository.owner.login}/teams/counting_out_game/memberships/${event.repository.name}`,
200,
{
role: 'maintainer',
state: 'active',
url: `https://api.github.com/teams/1/memberships/${event.repository.owner.login}`,
}
);

const { main } = require('../src') as Main;

await main();

expect(setFailed).not.toHaveBeenCalled();
expect(setOutput).toHaveBeenCalled();
expect(github.isDone()).toBe(true);
expect(compareCommitsMock.isDone()).toBe(true);
expect(membershipMock.isDone()).toBe(true);
});

it('should run the entire action', async () => {
Expand All @@ -98,7 +144,18 @@ describe('use-herald-action', () => {
return input[key];
});

const github = getGithubMock();
const compareCommitsMock = getCompareCommitsMock();

const membershipMock = mockRequest(
'get',
`/orgs/${event.repository.owner.login}/teams/counting_out_game/memberships/${event.repository.name}`,
200,
{
role: 'maintainer',
state: 'active',
url: `https://api.github.com/teams/1/memberships/${event.repository.owner.login}`,
}
);

const { main } = require('../src') as Main;

Expand All @@ -119,9 +176,12 @@ describe('use-herald-action', () => {
"*.ts",
],
"includesInPatch": Array [],
"isMemberOf": Array [
"counting_out_game",
],
"matched": true,
"name": "rule1.json",
"path": "${env.GITHUB_WORKSPACE}/__mocks__/rules/rule1.json",
"path": "/Users/gfrigerio/base/use-herald/__mocks__/rules/rule1.json",
"teams": Array [],
"users": Array [
"eeny",
Expand All @@ -135,7 +195,8 @@ describe('use-herald-action', () => {
]
`);

expect(github.isDone()).toBe(true);
expect(compareCommitsMock.isDone()).toBe(true);
expect(membershipMock.isDone()).toBe(true);
});

it('should run the entire action (no rules found)', async () => {
Expand All @@ -149,14 +210,14 @@ describe('use-herald-action', () => {
return input[key];
});

const github = getGithubMock();
const compareCommitsMock = getCompareCommitsMock();
const { main } = require('../src') as Main;

await main();

expect(handleComment).not.toHaveBeenCalled();
expect(setFailed.mock.calls).toMatchInlineSnapshot('Array []');
expect(setOutput.mock.calls).toMatchSnapshot();
expect(github.isDone()).toBe(true);
expect(compareCommitsMock.isDone()).toBe(true);
});
});
Loading

0 comments on commit b6d7ca6

Please sign in to comment.