mirror of
https://github.com/strapi/strapi.git
synced 2025-12-05 11:32:13 +00:00
Add tests, complete readme
This commit is contained in:
parent
9f5e114484
commit
e8e7a7e424
21
.github/actions/check-pr-status/README.md
vendored
21
.github/actions/check-pr-status/README.md
vendored
@ -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
|
||||
|
||||
103
.github/actions/check-pr-status/__tests__/action.test.js
vendored
Normal file
103
.github/actions/check-pr-status/__tests__/action.test.js
vendored
Normal 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
21
.github/actions/check-pr-status/index.js
vendored
21
.github/actions/check-pr-status/index.js
vendored
@ -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;
|
||||
}
|
||||
|
||||
3
.github/actions/check-pr-status/package.json
vendored
3
.github/actions/check-pr-status/package.json
vendored
@ -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",
|
||||
|
||||
@ -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'],
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user