Skip to content

Repo containing custom turo pre-commit hooks

License

Notifications You must be signed in to change notification settings

turo/pre-commit-hooks

Repository files navigation

pre-commit-hooks

This repository contains and bundles pre-commit hooks that we use for development, linting and CI.

Licenses

This repository derives code from, wraps, or directly leverages the following open source projects.

Installation

This section tells you how to install.

Pre-requisites

First you must have pre-commit installed and configured.

The pre-commit hooks in this section require that the golang environment be installed to run successfully:

  • go-test-unit
  • go-build
  • golangci-lint
  • go-mod-tidy
  • go-vet
  • go-fmt
  • gomarkdoc

The remaining hooks all run in containers, so require Docker installed to run successfully:

  • gocyclo
  • goimports
  • gofmt

Hooks installation

Use the following in your .pre-commit-config.yaml to enable hooks. Note that in this example, some of the hooks enabled would be redundant.

repos:
  - repo: https://github.com/turo/pre-commit-hooks
    rev: v3.5.0  # You may version pin this if desired
    hooks:
    - id: go-test-unit
    - id: go-build
    - id: go-mod-tidy
    - id: go-vet
    - id: go-fmt
    - id: gomarkdoc
    - id: gocyclo
    - id: goimports
    - id: gofmt
    - id: golangci-lint
    - id: docker-lint
    - id: docker-compose-lint

Usage

This repository provides a single source for commonly used hooks. Usage is via the pre-commit git and CI hooks, rather than direct activation.

The following hooks are available:

Go

  • golangci-lint (requires golang, golangci-lint) - Mega-meta-linter, contains all the other linters, may be slow to run the first time, but uses caching within the repository to speed up the container on subsequent runs. This should be your preferred Golang linter unless its just plain too slow.
  • gocyclo (requires docker) - Cyclomatic complexity checker
  • goimports (requres docker) - Superceded go fmt as the Go style formatter
  • gofmt (requires docker) - Original Go style formatter, a bit more relaxed
  • go-test-unit (requires golang) - Runs all tests tagged with unit
  • go-build (requires golang) - Build the repository to ensure it compiles
  • go-mod-tidy (requires golang) - Clean up go.mod and go.sum
  • go-vet (requires golang) - Additional checks not performed by compilation
  • go-fmt (requires golang) - Original style formatter, local version
  • gomarkdoc (requires golang, gomarkdoc) - Generate documentation from packages

Docker

  • docker-lint (requires docker) - Runs hadolint on Dockerfiles
  • docker-compose-lint (requires docker) - Runs docker-compose config on compose files

Example

A typical pre-commit-config.yaml for a Golang project would look like this:

repos:
  - repo: https://github.com/turo/pre-commit-hooks
    rev: v3.5.0  # You may version pin this if desired
    hooks:
    - id: go-mod-tidy  # Clean up go.mod
    - id: go-build  # Check compilation
    - id: golangci-lint  # Lint everything
    - id: go-test-unit  # Run unit tests
    - id: gomarkdoc  # Generate documenation

Updating hook versions

The version of the hook, e.g. golangci-lint, that is actually used by this Action may be made available via instructions in the packer-github-actions-general GitHub repository. Thus, please refer to that repository to upgrade any specific hook version.

Contributing

Feel free to Pull Request this repository to add any commonly used hooks which you feel belong here.

Testing hooks locally

https://pre-commit.com/#developing-hooks-interactively

pre-commit try-repo ../pre-commit-hooks go-cyclo --verbose --all-files

Manually publishing hook images

Right now we are manually publishing updates to hook images based on their upstream changes. To facilitate this, the repo provides a script/build script to build either individual images or all of them.

  • script/build <hook_name> - Build an individual image
  • script/build all - Build all images

Use docker push to publish images if you have write access to the GitHub Packages.

Contributors

Changelog

For changes, see the Releases documentation.

Breadcrumbs

Pre-commit hooks, tools, linters, etc.

Actions

Reference material