Skip to content

Commit

Permalink
feat: check PR contribution
Browse files Browse the repository at this point in the history
* Adding forbidden pattern Step check
* Adding Attributes Step check
  • Loading branch information
benjaminParisel committed Oct 12, 2023
1 parent 3035e3b commit 41e8f0f
Show file tree
Hide file tree
Showing 23 changed files with 23,857 additions and 0 deletions.
4 changes: 4 additions & 0 deletions packages/pr-files-checker/.eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
dist/
lib/
node_modules/
jest.config.js
55 changes: 55 additions & 0 deletions packages/pr-files-checker/.eslintrc.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
{
"plugins": ["jest", "@typescript-eslint"],
"extends": ["plugin:github/recommended"],
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": 9,
"sourceType": "module",
"project": "./tsconfig.json"
},
"rules": {
"i18n-text/no-en": "off",
"eslint-comments/no-use": "off",
"import/no-namespace": "off",
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/explicit-member-accessibility": ["error", {"accessibility": "no-public"}],
"@typescript-eslint/no-require-imports": "error",
"@typescript-eslint/array-type": "error",
"@typescript-eslint/await-thenable": "error",
"@typescript-eslint/ban-ts-comment": "error",
"camelcase": "off",
"@typescript-eslint/consistent-type-assertions": "error",
"@typescript-eslint/explicit-function-return-type": ["error", {"allowExpressions": true}],
"@typescript-eslint/func-call-spacing": ["error", "never"],
"@typescript-eslint/no-array-constructor": "error",
"@typescript-eslint/no-empty-interface": "error",
"@typescript-eslint/no-explicit-any": "error",
"@typescript-eslint/no-extraneous-class": "error",
"@typescript-eslint/no-for-in-array": "error",
"@typescript-eslint/no-inferrable-types": "error",
"@typescript-eslint/no-misused-new": "error",
"@typescript-eslint/no-namespace": "error",
"@typescript-eslint/no-non-null-assertion": "warn",
"@typescript-eslint/no-unnecessary-qualifier": "error",
"@typescript-eslint/no-unnecessary-type-assertion": "error",
"@typescript-eslint/no-useless-constructor": "error",
"@typescript-eslint/no-var-requires": "error",
"@typescript-eslint/prefer-for-of": "warn",
"@typescript-eslint/prefer-function-type": "warn",
"@typescript-eslint/prefer-includes": "error",
"@typescript-eslint/prefer-string-starts-ends-with": "error",
"@typescript-eslint/promise-function-async": "error",
"@typescript-eslint/require-array-sort-compare": "error",
"@typescript-eslint/restrict-plus-operands": "error",
"semi": "off",
"@typescript-eslint/semi": ["error", "never"],
"@typescript-eslint/type-annotation-spacing": "error",
"@typescript-eslint/unbound-method": "error"
},
"env": {
"node": true,
"es6": true,
"jest/globals": true
}
}
1 change: 1 addition & 0 deletions packages/pr-files-checker/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lib/
1 change: 1 addition & 0 deletions packages/pr-files-checker/.nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
16
44 changes: 44 additions & 0 deletions packages/pr-files-checker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
# GitHub PR diff checker

This action checks the diff in a PR, and fails if one or more of the set criteria isn't met.

# Using this action

You need to add this in a file in `.github/workflows` and set appropriate options.

```
name: Check PR content
on: [pull_request]
jobs:
check_pr:
runs-on: ubuntu-latest
name: Check for forbidden string
steps:
- name: Scan forbidden string
uses: bonitasoft/actions/packages/pr-diff-checker@TAG_VERSION
with:
github-token: ${{ secrets.GITHUB_TOKEN }}
diffDoesNotContain: '["http://documentation.mydomain","link:"]'
extensionsToCheck: '[".adoc"]'
```

An example is also provided in .github/workflows/ in this repository.


## Development

**Node version**: see the [.nvmrc](.nvmrc) file

As for all JavaScript actions, the `dist` folder must be committed.

So when changing the JS code or when updating the production dependencies (that are bundled in the final distribution),
please regenerate the content of the `dist` folder.
* Run `npm ci && npm run package`
* Commit the dist folder


## License

This is a modification and inspiration from [JJ/github-pr-contains-action](https://github.com/JJ/github-pr-contains-action/) and is released under the MIT license.
54 changes: 54 additions & 0 deletions packages/pr-files-checker/__tests__/AttributesCheckingStep.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import { describe, test, jest, expect } from "@jest/globals";

jest.mock("@actions/github", () => ({
context: {
repo: {
owner: "your_owner",
repo: "your_repo",
},
sha: "your_sha",
},
}));

function isExtensionAllowed(
filePath: string,
allowedExtensions: string[]
): boolean {
// Get the last occurrence of "." in the filePath
const lastDotIndex = filePath.lastIndexOf(".");

// If there's no "." in the filePath or it's the last character, return false
if (lastDotIndex === -1 || lastDotIndex === filePath.length - 1) {
return false;
}

// Get the file extension from the filePath
const fileExtension = filePath.slice(lastDotIndex + 1).toLowerCase();

// Check if the file extension is in the allowedExtensions array
if (allowedExtensions.includes(fileExtension)) {
return true;
}

return false;
}

describe("test gh_pr_files_checker", function () {
test("test gh-pr-files-checker.AttributesCheckingStep.validate", function (done) {
//TODO: Write test

const files = [
"packages/pr-files-checker/__tests__/test-files/attributes-checking/modules/attributes-checking-1.adoc",
".packages/pr-files-checker/__tests__/test-files/attributes-checking/attributes-checking-2.adoc",
"packages/pr-files-checker/__tests__/test-files/attributes-checking/attributes-checking-3.adoc",
];
const attributes = ["name", "description"];
const extensions = ["adoc"];

let a = files.filter(
(file) =>
isExtensionAllowed(file, extensions) && file.includes("/modules/")
);
console.log("a", a);
});
});
Empty file.
38 changes: 38 additions & 0 deletions packages/pr-files-checker/__tests__/fixtures/modules/mock-gh-pr.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.prUpdateAdocFile = void 0;
exports.prUpdateAdocFile = [{
chunks: [{
changes: [
{
type: 'add',
add: true,
ln: 1,
content: '+Update action title'
}
]
}],
deletions: 1,
additions: 1,
from: '.github/workflows/pr-checker.yml',
to: '.github/workflows/pr-checker.yml',
index: ['633d498..a60f60d', '100644']
},
{
chunks: [{
changes: [
{
type: 'add',
add: true,
ln: 6,
content: '+For example, if I try to use http://documentation.mydomain, the action will be failed on PR.'
}
]
}],
deletions: 0,
additions: 2,
from: 'examples/example.adoc',
to: 'examples/example.adoc',
index: ['a692222..0de4de5', '100644']
}
];
41 changes: 41 additions & 0 deletions packages/pr-files-checker/__tests__/fixtures/modules/mock-gh-pr.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
export const prUpdateAdocFile: any = [
{
chunks: [
{
changes: [
{
type: "add",
add: true,
ln: 1,
content: "+Update action title",
},
],
},
],
deletions: 1,
additions: 1,
from: ".github/workflows/pr-checker.yml",
to: ".github/workflows/pr-checker.yml",
index: ["633d498..a60f60d", "100644"],
},
{
chunks: [
{
changes: [
{
type: "add",
add: true,
ln: 6,
content:
"+For example, if I try to use http://documentation.mydomain, the action will be failed on PR.",
},
],
},
],
deletions: 0,
additions: 2,
from: "examples/example.adoc",
to: "examples/example.adoc",
index: ["a692222..0de4de5", "100644"],
},
];
22 changes: 22 additions & 0 deletions packages/pr-files-checker/action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: 'PR files contribution guideline checker'
description: 'Checks files updated on PR are following the contribution guideline for documentation.'
inputs:
github-token:
description: 'Github token'
required: true
files-to-check:
description: 'Comma-separated list of file extension to check in the modified files. (ex: adoc, md, txt)`'
required: false
default: 'adoc'
attributes-to-check:
description: 'Comma-separated list of attributes to check in the modified files. (ex: :description:, :alias:), keep empty to skip this check.`'
default: ''
required: false
forbidden-pattern-to-check:
description: 'Comma-separated list of forbidden pattern to check in the modified files. (ex: http://documentation.mydomain,link:), keep empty to skip this check.'
default: ''
required: false

runs:
using: 'node20'
main: 'dist/index.js'
Loading

0 comments on commit 41e8f0f

Please sign in to comment.