Skip to content

Commit

Permalink
Define pre-commit hooks.
Browse files Browse the repository at this point in the history
The pre-commit framework allows users to run MegaLinter locally as a Git
pre-commit hook. While it does have built-in support for Node.js hooks,
it doesn't support monorepos, because the package.json must be in the
root directory. While it does have built-in support for Docker hooks,
the user experience of overriding arguments to mega-linter-runner is
nicer than that for the Docker image. Hence, hand-roll system hooks
instead, and leverage npx to execute (and, if necessary, download)
mega-linter-runner.
  • Loading branch information
Kurt-von-Laven committed Apr 26, 2022
1 parent d43b202 commit 2bfcdaa
Show file tree
Hide file tree
Showing 3 changed files with 84 additions and 0 deletions.
66 changes: 66 additions & 0 deletions .pre-commit-hooks.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
- id: megalinter
name: Run MegaLinter on files modified relative to DEFAULT_BRANCH
entry: npx --
language: system
pass_filenames: false
args:
- mega-linter-runner
- --fix
- --env
- "'APPLY_FIXES=all'"
- --env
- "'FAIL_IF_UPDATED_SOURCES=true'"
- --env
- "'LOG_LEVEL=warning'"
- --env
- "'VALIDATE_ALL_CODEBASE=false'"
- --env
- "'DISABLE_LINTERS=COPYPASTE_JSCPD'"
stages:
- commit
description: >
See https://megalinter.github.io/latest/mega-linter-runner/#usage and
https://megalinter.github.io/latest/configuration/ if you wish to override
the default arguments. mega-linter-runner is specified as an argument so
that you may override the version (e.g., [email protected]). Depends
on npx, which ships with npm 7+, and Docker. Unlike most pre-commit hooks,
MegaLinter itself computes the files to run on. Runs very slowly when the
pertinent Docker image isn't already cached (c.f.,
https://github.com/marketplace/actions/docker-cache/). If you encounter
permission errors, try running Docker in rootless mode (c.f.,
https://github.com/marketplace/actions/rootless-docker/). Linter results are
logged to the report directory, so you should make sure to list it in your
.gitignore and wipe it between runs. Skip jscpd since duplicate detection
fundamentally requires scanning the entire repository and hence can't be
performed incrementally.
- id: megalinter-all
name: Run MegaLinter on all files
entry: npx --
language: system
pass_filenames: false
args:
- mega-linter-runner
- --fix
- --env
- "'APPLY_FIXES=all'"
- --env
- "'FAIL_IF_UPDATED_SOURCES=true'"
- --env
- "'LOG_LEVEL=warning'"
- --env
- "'VALIDATE_ALL_CODEBASE=true'"
stages:
- push
description: >
See https://megalinter.github.io/latest/mega-linter-runner/#usage and
https://megalinter.github.io/latest/configuration/ if you wish to override
the default arguments. mega-linter-runner is specified as an argument so
that you may override the version (e.g., [email protected]). Depends
on npx, which ships with npm 7+, and Docker. Runs very slowly when the
pertinent Docker image isn't already cached (c.f.,
https://github.com/marketplace/actions/docker-cache/). If you encounter
permission errors, try running Docker in rootless mode (c.f.,
https://github.com/marketplace/actions/rootless-docker/). Linter results are
logged to the report directory, so you should make sure to list it in your
.gitignore and wipe it between runs.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
Note: Can be used with `megalinter/megalinter@beta` in your GitHub Action mega-linter.yml file, or with `megalinter/megalinter:beta` docker image

- Add gherkin-lint in dotnet flavor ([#1435](https://github.com/megalinter/megalinter/issues/1435))
- Define pre-commit hooks ([#569](https://github.com/megalinter/megalinter/issues/569)).

- Linter versions upgrades
- [luacheck](https://luacheck.readthedocs.io) from 0.26.0 to **0.26.1** on 2022-04-24
Expand Down
17 changes: 17 additions & 0 deletions mega-linter-runner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,23 @@ Example:
npx mega-linter-runner -r beta -e 'ENABLE=MARKDOWN,YAML' -e 'SHOW_ELAPSED_TIME=true'
```

### Pre-commit hook

You can run mega-linter-runner as a [pre-commit](https://pre-commit.com/) hook

Sample `.pre-commit-config.yaml`:

```yaml
repos:
- repo: https://github.com/megalinter/megalinter
rev: v5.12.0 # Git tag specifying the hook, not mega-linter-runner, version
hooks:
- id: megalinter # Faster, less thorough, runs pre-commit by default
- id: megalinter-all # Slower, more thorough, runs pre-push by default
```
See [`.pre-commit-hooks.yaml`](../.pre-commit-hooks.yaml) for more details.

## Usage

```shell
Expand Down

0 comments on commit 2bfcdaa

Please sign in to comment.