Add tests, complete readme

This commit is contained in:
Alexandre Bodin 2022-02-18 11:44:08 +01:00
parent 9f5e114484
commit e8e7a7e424
6 changed files with 144 additions and 10 deletions

View File

@ -2,6 +2,8 @@
This action checks a PR labels, milestone and status to validate it is ready for merging into master.
> ❗️ When making changes to this code, make sure to run the build before committing. See [Development](#development) to know more.
## Conditions
1. The PR should not have the following labels:
@ -12,3 +14,22 @@ This action checks a PR labels, milestone and status to validate it is ready for
2. The PR should have one and only one `source: *` label.
3. The PR should have one and only one `issue-type: *` label.
4. The PR must have a milestone defined.
## Contributing
### Requirements
- The code is compatible with Node 14+
### Dependencies
- Run `yarn` to install the dependencies
### Development
In order for the action to run on github all the code needs to be bundled and committed because github actions do not manage dependencies for us. [Github reference documentation](https://docs.github.com/en/actions/creating-actions/creating-a-javascript-action#commit-tag-and-push-your-action-to-github).
### Commands
- `yarn build`: Build the code the must be committed
- `yarn watch`: Build in watch mode

View File

@ -0,0 +1,103 @@
const action = require('../index');
jest.mock('@actions/github');
jest.mock('@actions/core');
const github = require('@actions/github');
const core = require('@actions/core');
test.each(action.BLOCKING_LABELS)('Test blocking labels %s', async label => {
github.context = {
payload: {
pull_request: {
labels: [{ name: label }],
},
},
};
const setFailed = jest.spyOn(core, 'setFailed');
await action();
expect(setFailed).toHaveBeenCalled();
console.log(setFailed.mock.calls[0][0]);
expect(setFailed.mock.calls[0][0]).toContain(`The PR has been labelled with a blocking label`);
setFailed.mockRestore();
});
test('Test missing source label', async () => {
github.context = {
payload: {
pull_request: {
labels: [{ name: 'issue-type: enhancement' }],
},
},
};
const setFailed = jest.spyOn(core, 'setFailed');
await action();
expect(setFailed).toHaveBeenCalled();
expect(setFailed.mock.calls[0][0]).toBe(`The PR must have one and only one 'source:' label.`);
setFailed.mockRestore();
});
test('Test too many source label', async () => {
github.context = {
payload: {
pull_request: {
labels: [{ name: 'source: a' }, { name: 'source: b' }, { name: 'issue-type: enhancement' }],
},
},
};
const setFailed = jest.spyOn(core, 'setFailed');
await action();
expect(setFailed).toHaveBeenCalled();
expect(setFailed.mock.calls[0][0]).toBe(`The PR must have one and only one 'source:' label.`);
setFailed.mockRestore();
});
test('Test missing issue-type label', async () => {
github.context = {
payload: {
pull_request: {
labels: [{ name: 'source: core' }],
},
},
};
const setFailed = jest.spyOn(core, 'setFailed');
await action();
expect(setFailed).toHaveBeenCalled();
expect(setFailed.mock.calls[0][0]).toBe(`The PR must have one and only one 'issue-type:' label.`);
setFailed.mockRestore();
});
test('Test too many issue-type label', async () => {
github.context = {
payload: {
pull_request: {
labels: [{ name: 'issue-type: a' }, { name: 'issue-type: b' }, { name: 'source: core' }],
},
},
};
const setFailed = jest.spyOn(core, 'setFailed');
await action();
expect(setFailed).toHaveBeenCalled();
expect(setFailed.mock.calls[0][0]).toBe(`The PR must have one and only one 'issue-type:' label.`);
setFailed.mockRestore();
});

File diff suppressed because one or more lines are too long

View File

@ -1,16 +1,19 @@
const core = require('@actions/core');
const { context } = require('@actions/github');
const github = require('@actions/github');
const BLOCKING_LABELS = [`flag: 💥 Breaking change`, `flag: don't merge`];
async function main() {
try {
const labels = context.payload.pull_request?.labels;
const labels = github.context.payload.pull_request?.labels ?? [];
const hasBlockingLabel = labels.some(label => BLOCKING_LABELS.includes(label.name));
if (hasBlockingLabel) {
const blockingLabels = labels.filter(label => BLOCKING_LABELS.includes(label.name));
if (blockingLabels.length > 0) {
core.setFailed(
`The PR has been labelled with a blocking label (${BLOCKING_LABELS.join(', ')}).`
`The PR has been labelled with a blocking label (${blockingLabels
.map(label => label.name)
.join(', ')}).`
);
return;
@ -41,4 +44,10 @@ async function main() {
}
}
main();
main.BLOCKING_LABELS = BLOCKING_LABELS;
if (require.main === module) {
main();
} else {
module.exports = main;
}

View File

@ -5,7 +5,8 @@
"license": "MIT",
"private": true,
"scripts": {
"build": "NODE_ENV=production ncc build index.js -o dist --minify"
"build": "NODE_ENV=production ncc build index.js -o dist --minify",
"watch": "NODE_ENV=production ncc build index.js -w -o dist --minify"
},
"devDependencies": {
"@actions/core": "1.6.0",

View File

@ -4,5 +4,5 @@ const baseConfig = require('./jest.base-config');
module.exports = {
...baseConfig,
projects: ['<rootDir>/packages/**/jest.config.js'],
projects: ['<rootDir>/.github', '<rootDir>/packages/**/jest.config.js'],
};