Skip to content

Commit

Permalink
Switch from using the pre_commit gem to overcommit
Browse files Browse the repository at this point in the history
`pre_commit` was starting to show its age:
  - It uses the now defunct `scss-lint` package. See
    jish/pre-commit#278 and
    thewca#3649.
  - It bundles an older version of JsHint that does not have support
    for ES{7,8,9}.

I've configured `overcommit` to behave similarly to `pre_commit`. Here's
a list of the specific things I've configured and tested:

    + css linting using stylelint (this brings us one step closer to
      getting off of `ruby-sass`:
      thewca#3649)
    + illegal char check (such as <<<)
    + rubocop
    + jshint (with support for ES9!)
  • Loading branch information
jfly committed Jul 31, 2019
1 parent 8207c69 commit cf1b6f4
Show file tree
Hide file tree
Showing 15 changed files with 120 additions and 76 deletions.
31 changes: 31 additions & 0 deletions .git-hooks/pre_commit/illegal_strings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

module Overcommit::Hook::PreCommit
class IllegalStrings < Base
def run
errors = []

illegal_strs = {
("<" * 3) => "conflict marker",
"\t" => "tab",
"\r" => "carriage return",
"\uFEFF" => "byte order marker (BOM)",
("WCA " + "id") => "We prefer 'WCA ID', see https://github.com/thewca/worldcubeassociation.org/issues/268",
}
applicable_files.each do |file|
File.foreach(file).with_index do |line, line_num|
illegal_strs.each do |illegal_str, description|
index = line.index illegal_str
if index
errors << "#{file}:#{line_num+1}:#{index+1} Found illegal string: #{description}"
end
end
end
end

return :fail, errors.join("\n") if errors.any?

:pass
end
end
end
2 changes: 1 addition & 1 deletion .jshintrc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
"esnext": true,
"esversion": 8,
"asi": true
}
53 changes: 53 additions & 0 deletions .overcommit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# Use this file to configure the Overcommit hooks you wish to use. This will
# extend the default configuration defined in:
# https://github.com/sds/overcommit/blob/master/config/default.yml
#
# At the topmost level of this YAML file is a key representing type of hook
# being run (e.g. pre-commit, commit-msg, etc.). Within each type you can
# customize each hook, such as whether to only run it on certain files (via
# `include`), whether to only display output if it fails (via `quiet`), etc.
#
# For a complete list of hooks, see:
# https://github.com/sds/overcommit/tree/master/lib/overcommit/hook
#
# For a complete list of options that you can use to customize hooks, see:
# https://github.com/sds/overcommit#configuration
#
# Uncomment the following lines to make the configuration take effect.

PreCommit:
ALL:
exclude:
- 'regulations/**/*'
- 'webroot/**/*'
- 'WcaOnRails/vendor/**/*'
- 'WcaOnRails/tmp/**/*'
- 'WcaOnRails/db/structure.sql'
- 'WcaOnRails/node_modules/**/*'
- 'chef/**/*'
- '.gitmodules'
- '**/*.pdf'
- '**/*.jpg'
- '**/*.png'
- '**/*.gif'
- '**/*.xls'
- '**/*.ico'
- '**/*.jar'
- '**/*.svg'

RuboCop:
enabled: true
on_warn: fail # Treat all warnings as failures

IllegalStrings:
enabled: true

JsHint:
enabled: true
required_executable: 'WcaOnRails/bin/yarn'
flags: ['run', '-s', 'jshint', '--verbose']

Stylelint:
enabled: true
required_executable: 'WcaOnRails/bin/yarn'
flags: ['run', '-s', 'stylelint', '-f', 'compact']
15 changes: 0 additions & 15 deletions .pre_commit.ignore

This file was deleted.

6 changes: 3 additions & 3 deletions WcaOnRails/.rubocop.yml → .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ AllCops:
TargetRubyVersion: 2.3
DisplayCopNames: true
Exclude:
- 'node_modules/**/*'
- 'bin/**/*'
- 'vendor/**/*'
- 'WcaOnRails/node_modules/**/*'
- 'WcaOnRails/bin/**/*'
- 'WcaOnRails/vendor/**/*'

Bundler/OrderedGems:
Enabled: false
Expand Down
10 changes: 5 additions & 5 deletions WcaOnRails/.rubocop_todo.yml → .rubocop_todo.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@
# SupportedStyles: snake_case, camelCase
Naming/MethodName:
Exclude:
- 'app/controllers/users_controller.rb'
- 'app/models/light_result.rb'
- 'app/models/event.rb'
- 'app/models/round_type.rb'
- 'db/migrate/20160109070723_convert_process_links_format_to_markdown.rb'
- 'WcaOnRails/app/controllers/users_controller.rb'
- 'WcaOnRails/app/models/light_result.rb'
- 'WcaOnRails/app/models/event.rb'
- 'WcaOnRails/app/models/round_type.rb'
- 'WcaOnRails/db/migrate/20160109070723_convert_process_links_format_to_markdown.rb'

# Offense count: 16
# Configuration parameters: EnforcedStyle, SupportedStyles.
Expand Down
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ before_script:
# and our https://github.com/thewca/worldcubeassociation.org/issues/3476
- DISABLE_SPRING=1 bundle exec rake assets:precompile
script:
- bundle exec pre-commit run git
- bundle exec overcommit --sign && bundle exec overcommit --sign pre-commit && (cd .. && BUNDLE_GEMFILE=WcaOnRails/Gemfile bundle exec overcommit --run)
- bundle exec rubocop
- bundle exec rake
7 changes: 5 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
This repository contains all of the code that runs on [worldcubeassociation.org](https://www.worldcubeassociation.org/).

## Setup

- Clone this repo! (And navigate into it)
```
git clone https://github.com/thewca/worldcubeassociation.org
Expand All @@ -22,10 +23,11 @@ This repository contains all of the code that runs on [worldcubeassociation.org]
```
- Set up git pre-commit hook. Optional, but very useful.
```shell
(cd WcaOnRails; bundle install && bundle exec pre-commit install) && git config pre-commit.ruby "scripts/ruby_in_wca_on_rails.sh"
(cd WcaOnRails; bundle install && bin/yarn && bundle exec overcommit --install)
```

## Run directly with Ruby (lightweight, but only runs the Rails portions of the site)

- Install either MySQL 5.6 or 5.7, and set it up with a user with username "root" with an empty password.
If it poses problems, try the following:
```shell
Expand All @@ -41,13 +43,14 @@ This repository contains all of the code that runs on [worldcubeassociation.org]
3. `bundle install && bin/yarn`
4. `bin/rake db:load:development` - Download and import the [developer's database export](https://github.com/thewca/worldcubeassociation.org/wiki/Developer-database-export).
5. `bin/rails server` - Run rails. The server will be accessible at localhost:3000
- Run tests. Setup instructions follow `before_script` in `.travis.yml`.
- Run tests. Setup instructions follow `before_script` in `.travis.yml`.
1. `RAILS_ENV=test bin/rake db:reset` - Set up test database.
2. `RAILS_ENV=test bin/rake assets:precompile` - Compile some assets needed for tests to run.
3. `bin/rspec` - Run tests.
- [Mailcatcher](http://mailcatcher.me/) is a good tool for catching emails in development.

## Run in Vagrant (gets everything working, but is very slow, recommended only if you need to run the PHP portions of the website)

- Install [Vagrant](https://www.vagrantup.com/), which requires
[VirtualBox](https://www.virtualbox.org/).
- `vagrant up all` - Once the VM finishes initializing (which can take some time),
Expand Down
3 changes: 1 addition & 2 deletions WcaOnRails/Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,8 @@ group :development, :test do
end

group :development do
gem 'pre-commit', require: false
gem 'overcommit', require: false
gem 'rubocop', require: false
gem 'scss_lint', require: false
gem 'better_errors'
gem 'binding_of_caller'
gem 'bullet'
Expand Down
15 changes: 7 additions & 8 deletions WcaOnRails/Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ GEM
activesupport (>= 3.2.0)
json (>= 1.7)
mime-types (>= 1.16)
childprocess (1.0.1)
rake (< 13.0)
cldr-plurals-runtime-rb (1.0.1)
cliver (0.3.2)
cocoon (1.2.14)
Expand Down Expand Up @@ -272,6 +274,7 @@ GEM
image_processing (1.9.2)
mini_magick (>= 4.9.5, < 5)
ruby-vips (>= 2.0.13, < 3)
iniparse (1.4.4)
irb (1.0.0)
iso (0.2.2)
i18n
Expand Down Expand Up @@ -376,17 +379,17 @@ GEM
openssl (2.1.2)
orm_adapter (0.5.0)
os (1.0.1)
overcommit (0.49.0)
childprocess (>= 0.6.3, < 2.0)
iniparse (~> 1.4)
parallel (1.17.0)
parser (2.6.3.0)
ast (~> 2.4.0)
phantomjs (2.1.1.0)
pluginator (1.5.0)
poltergeist (1.18.1)
capybara (>= 2.1, < 4)
cliver (~> 0.3.1)
websocket-driver (>= 0.2.0)
pre-commit (0.39.0)
pluginator (~> 1.5)
premailer (1.11.1)
addressable
css_parser (>= 1.6.0)
Expand Down Expand Up @@ -511,9 +514,6 @@ GEM
sawyer (0.8.1)
addressable (>= 2.3.5, < 2.6)
faraday (~> 0.8, < 1.0)
scss_lint (0.58.0)
rake (>= 0.9, < 13)
sass (~> 3.5, >= 3.5.5)
sdoc (1.0.0)
rdoc (>= 5.0)
seedbank (0.4.0)
Expand Down Expand Up @@ -669,9 +669,9 @@ DEPENDENCIES
octokit
oga
openssl
overcommit
phantomjs
poltergeist
pre-commit
premailer-rails
rack-cors
rails
Expand All @@ -684,7 +684,6 @@ DEPENDENCIES
rspec-rails
rubocop
sass-rails
scss_lint
sdoc
seedbank
selectize-rails!
Expand Down
4 changes: 4 additions & 0 deletions WcaOnRails/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@
"whatwg-fetch": "^3.0.0"
},
"devDependencies": {
"jshint": "^2.10.2",
"stylelint": "^10.1.0",
"stylelint-config-recommended-scss": "^3.3.0",
"stylelint-scss": "^3.9.2",
"webpack-bundle-analyzer": "^3.3.2",
"webpack-dev-server": "^3.7.2"
},
Expand Down
24 changes: 0 additions & 24 deletions config/pre_commit.rb

This file was deleted.

12 changes: 0 additions & 12 deletions config/pre_commit.yml

This file was deleted.

3 changes: 0 additions & 3 deletions scripts/ruby_in_wca_on_rails.sh

This file was deleted.

9 changes: 9 additions & 0 deletions stylelint.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module.exports = {
extends: "stylelint-config-recommended-scss",
rules: {
"no-descending-specificity": null,
"block-no-empty": null,
"font-family-no-missing-generic-family-keyword": null
},
ignoreFiles: ["WcaOnRails/app/assets/stylesheets/selectize.default.css"]
};

0 comments on commit cf1b6f4

Please sign in to comment.