mszostok / codeowners-validator

The GitHub CODEOWNERS file validator
Apache License 2.0
212 stars 48 forks source link

Add native support for pre-commit hook #140

Open mszostok opened 2 years ago

mszostok commented 2 years ago

Description

The native support for pre-commit hook was requested by external users.

To integrate with https://pre-commit.com/ the .pre-commit-hooks.yaml file needs to be added to the root for repository.

Example:

- id: codeowners-validator-docker
  name: Validate CODEOWNERS file
  description: Ensures the correctness of your CODEOWNERS file.
  language: docker_image
  # TODO: types: hardcode expected file location?
  entry:  ghcr.io/mszostok/codeowners-validator:stable
- id: codeowners-validator
  name: Validate CODEOWNERS file
  description: Ensures the correctness of your CODEOWNERS file.
  language: golang
  # TODO: types: hardcode expected file location?
  entry: codeowners-validator

To support the input parameters, the codeowners-validator CLI should accept flags or config file.

Reasons

Support https://pre-commit.com/ out of the box to simplify external user workflow.

mszostok commented 2 years ago

Hi @peterdemin and @jeremycohen!

You both posted that the pre-commit support would be nice to see in codeowners-validator.

Do you already have snippets on how you use it with pre-commit? I see, one problem that the CLI supports for now only env variables, where pre-commit allows only flags. For example:

-   repo: https://github.com/path/to/your/hook/repo
    rev: badf00ddeadbeef
    hooks:
    -   id: my-hook-script-id
        args: [--myarg1=1, --myarg1=2]

Additionally, you want to use the docker_image, go or system option? The last option, AFAIK, the bin needs to be preinstalled.

peterdemin commented 2 years ago

I don't like our setup, because we don't have control over which version to use. I still put it here so others could avoid our mistakes:

We use gh CLI to figure out binary management:

gh extension install mszostok/gh-validate-codeowners

.pre-commit-config.yaml:

  - id: verify_codeowners
    name: verify_codeowners
    entry: tools/validate_codeowners.sh
    language: script
    files: CODEOWNERS

tools/validate_codeowners.sh:

env REPOSITORY_PATH="." CHECKS="syntax,files,duppatterns" gh validate-codeowners
jeremycohen commented 2 years ago

Sorry I didn't get back sooner. Here's roughly how we currently use this script to look for unowned files in a specific subdirectory:

REPOSITORY_PATH=... EXPERIMENTAL_CHECKS="notowned" NOT_OWNED_CHECKER_SKIP_PATTERNS=... CHECKS="files,duppatterns,syntax" NOT_OWNED_CHECKER_SUBDIRECTORIES=... ./bin/codeowners-validator

We don't have a precommit hook yet for this, but I would guess it would look like @peterdemin's setup if we were to do so (lower-pri item for us, not typically a common error to hit for devs).

dudicoco commented 1 year ago

Hi @mszostok, any updates on this one?

dannysauer commented 3 months ago

Since this is apparently dead, what about #199?

bobertrublik commented 3 months ago

@dannysauer @mszostok I'd be happy to fix any issues I had when I wrote the PR last year. :)