Commit e6ecd254 authored by Ash McKenzie's avatar Ash McKenzie

Merge branch '220807-consider-replacing-overcommit-with-another-git-hooks-manager' into 'master'

Add a default Lefthook configuration

See merge request gitlab-org/gitlab!36347
parents e5bc562b 5acb0662
......@@ -56,6 +56,7 @@ eslint-report.html
/doc/code/*
/dump.rdb
/jsconfig.json
/lefthook-local.yml
/log/*.log*
/node_modules
/nohup.out
......
......@@ -37,7 +37,8 @@ def build_message(commit, message, more_info: true)
end
def squash_mr?
gitlab_danger.ci? ? gitlab.mr_json['squash'] : false
# Locally, we assume the MR is set to be squashed so that the user only sees warnings instead of errors.
gitlab_danger.ci? ? gitlab.mr_json['squash'] : true
end
def wip_mr?
......@@ -112,7 +113,7 @@ def lint_commits(commits)
if multi_line_commit_linter && multi_line_commit_linter.failed?
warn_or_fail_commits(multi_line_commit_linter)
else
elsif gitlab_danger.ci? # We don't have access to the MR title locally
title_linter = lint_mr_title(gitlab.mr_json['title'])
if title_linter.failed?
warn_or_fail_commits(title_linter)
......
......@@ -15,25 +15,35 @@ settings automatically by default. If your editor/IDE does not automatically sup
we suggest investigating to see if a plugin exists. For instance here is the
[plugin for vim](https://github.com/editorconfig/editorconfig-vim).
## Pre-commit static analysis
## Pre-push static analysis
You should install [`overcommit`](https://github.com/sds/overcommit) to automatically check for
static analysis offenses before committing locally.
We strongly recommend installing
[Lefthook](https://github.com/Arkweid/lefthook) to automatically check for
static analysis offenses before pushing your changes.
After installing `overcommit`, run the following in your GitLab source directory:
```shell
make -C tooling/overcommit
# Make sure to uninstall Overcommit first
overcommit --uninstall
# Install lefthook
gem install lefthook && lefthook install -f
```
Then before a commit is created, `overcommit` automatically checks for RuboCop (and other checks)
offenses on every modified file.
Before you push your changes, Lefthook will then automatically run Danger checks, as well
as RuboCop, ES Lint, HAML Lint, and SCSS Lint for the changed files.
This saves you time as you don't have to wait for the same errors to be detected by CI/CD.
`overcommit` relies on a pre-commit hook to prevent commits that violate its ruleset. To override
this behavior, pass the `OVERCOMMIT_DISABLE` environment variable. For example,
`OVERCOMMIT_DISABLE=1 git rebase master` to rebase while disabling the Git hook.
Lefthook relies on a pre-push hook to prevent commits that violate its ruleset.
If you wish to override this behavior, pass the environment variable `LEFTHOOK=0`.
That is, `LEFTHOOK=0 git push`.
You can also:
- Define [local configuration](https://github.com/Arkweid/lefthook/blob/master/docs/full_guide.md#local-config).
- Skip [checks per tag on the fly](https://github.com/Arkweid/lefthook/blob/master/docs/full_guide.md#skip-some-tags-on-the-fly).
## Ruby, Rails, RSpec
......
......@@ -716,7 +716,7 @@ Sample configuration for `overcommit` is available in the
file for the [`gitlab`](https://gitlab.com/gitlab-org/gitlab) project.
To set up `overcommit` for documentation linting, see
[Pre-commit static analysis](../contributing/style_guides.md#pre-commit-static-analysis).
[Pre-commit static analysis](../contributing/style_guides.md#pre-push-static-analysis).
#### Disable Vale tests
......
pre-push:
parallel: true
commands:
danger:
run: bundle exec danger dry_run
eslint:
tags: frontend style
files: git diff --name-only $(git merge-base origin/master HEAD)..HEAD
glob: "*.{js,vue}"
run: yarn eslint --cache --max-warnings 0 --report-unused-disable-directives {files}
haml-lint:
tags: view haml style
files: git diff --name-only $(git merge-base origin/master HEAD)..HEAD
glob: "*.html.haml"
run: bundle exec haml-lint --config .haml-lint.yml {files}
scss-lint:
tags: stylesheet css style
files: git diff --name-only $(git merge-base origin/master HEAD)..HEAD
glob: "*.scss.css"
exclude: "app/assets/stylesheets/pages/emojis.scss"
run: bundle exec scss-lint --config .scss-lint.yml {files}
rubocop:
tags: backend style
files: git diff --name-only $(git merge-base origin/master HEAD)..HEAD
glob: "*.rb"
run: bundle exec rubocop --parallel --force-exclusion {files}
......@@ -168,7 +168,7 @@ module Gitlab
%r{\A(\.gitlab-ci\.yml\z|\.gitlab\/ci)} => :engineering_productivity,
%r{\A\.codeclimate\.yml\z} => :engineering_productivity,
%r{\A\.overcommit\.yml\.example\z} => :engineering_productivity,
%r{\Alefthook.yml\z} => :engineering_productivity,
%r{\A\.editorconfig\z} => :engineering_productivity,
%r{Dangerfile\z} => :engineering_productivity,
%r{\A(ee/)?(danger/|lib/gitlab/danger/)} => :engineering_productivity,
......
......@@ -278,9 +278,9 @@ RSpec.describe Gitlab::Danger::Helper do
'scripts/foo' | [:engineering_productivity]
'lib/gitlab/danger/foo' | [:engineering_productivity]
'ee/lib/gitlab/danger/foo' | [:engineering_productivity]
'.overcommit.yml.example' | [:engineering_productivity]
'lefthook.yml' | [:engineering_productivity]
'.editorconfig' | [:engineering_productivity]
'tooling/overcommit/foo' | [:engineering_productivity]
'tooling/bin/find_foss_tests' | [:engineering_productivity]
'.codeclimate.yml' | [:engineering_productivity]
'.gitlab/CODEOWNERS' | [:engineering_productivity]
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment