From 2dbd91a3ca4e60b4cb76bf181320993766890843 Mon Sep 17 00:00:00 2001 From: Dom Date: Mon, 15 Feb 2021 14:38:26 +0000 Subject: [PATCH] buf: add protobuf linting Adds protobuf linting hooks using Buf: https://docs.buf.build/ Expects buf to be installed locally. --- .pre-commit-hooks.yaml | 20 +++++++++++++++++++- README.md | 16 ++++++++++++++-- buf-breaking.sh | 16 ++++++++++++++++ buf-lint.sh | 13 +++++++++++++ 4 files changed, 62 insertions(+), 3 deletions(-) create mode 100755 buf-breaking.sh create mode 100755 buf-lint.sh diff --git a/.pre-commit-hooks.yaml b/.pre-commit-hooks.yaml index 683cb81..5f214a2 100644 --- a/.pre-commit-hooks.yaml +++ b/.pre-commit-hooks.yaml @@ -118,4 +118,22 @@ files: (?i)\.R$ pass_filenames: true minimum_pre_commit_version: 0.15.0 - description: 'Run lintr against R code' \ No newline at end of file + description: 'Run lintr against R code' + +- id: buf-lint + name: 'Lint protobuf files' + entry: buf-lint.sh + types: [proto] + language: 'script' + pass_filenames: false + minimum_pre_commit_version: 0.15.0 + description: "Runs buf lint at the repo root" + +- id: buf-breaking + name: 'Check protobuf for breaking changes' + entry: buf-breaking.sh + types: [proto] + language: 'script' + pass_filenames: false + minimum_pre_commit_version: 0.15.0 + description: "Runs buf breaking at the repo root" \ No newline at end of file diff --git a/README.md b/README.md index b2652cb..74abfac 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,8 @@ Hooks: * `rust-fmt`: runs `cargo fmt --all` * `r-stylr`: runs [`stylr`] to format R code * `r-lintr`: static analysis of R code with [`lintr`] +* `buf-lint`: runs [`buf`] lints against protobuf files +* `buf-breaking`: protobuf breaking change detection using [`buf`] ## Example config @@ -64,7 +66,7 @@ repos: stages: [commit, push] - id: rust-clippy - #args: [ # Optionally override default configured lints + # args: [ # Optionally override default configured lints # "-D rust_2018_idioms", # "-D missing_docs", #] @@ -81,6 +83,15 @@ repos: - id: r-lintr stages: [commit, push] + stages: [commit, push] + + - id: buf-lint + stages: [commit, push] + + - id: buf-breaking + # Checks against 'master' branch by defaut, change with: + # args: [".git#tag=v1.0.0"] + stages: [commit, push] ``` ## Tagged TODOs @@ -129,4 +140,5 @@ When adding new hooks you can run `pre-commit try-repo .` for a quick syntax che [dep]: https://github.com/golang/dep [`post-checkout`]: https://git-scm.com/docs/githooks#_post_checkout [`stylr`]: https://styler.r-lib.org/ -[`lintr`]: https://github.com/jimhester/lintr \ No newline at end of file +[`lintr`]: https://github.com/jimhester/lintr +[`buf`]: https://buf.build/ \ No newline at end of file diff --git a/buf-breaking.sh b/buf-breaking.sh new file mode 100755 index 0000000..d12292f --- /dev/null +++ b/buf-breaking.sh @@ -0,0 +1,16 @@ +#!/usr/bin/env bash + +# Protobuf breaking change detection with Buf: https://docs.buf.build/ +# +# To run the checks locally: +# buf breaking +# +# To view the breaking change lints: +# buf config ls-breaking-rules + +set -euo pipefail + +# Default to checking against the local master Git branch +against=${1-'.git#branch=master'}; + +buf breaking "${against}" \ No newline at end of file diff --git a/buf-lint.sh b/buf-lint.sh new file mode 100755 index 0000000..eada07b --- /dev/null +++ b/buf-lint.sh @@ -0,0 +1,13 @@ +#!/usr/bin/env bash + +# Protobuf linting with Buf: https://docs.buf.build/ +# +# To run the lints locally: +# buf lint +# +# To view the available lints and their descriptions: +# buf config ls-lint-rules + +set -euo pipefail + +buf lint "$@" \ No newline at end of file