From 339f6b8ff0f0d25f77d374ede323daebe98cbe7e Mon Sep 17 00:00:00 2001 From: Justin Cohen Date: Thu, 19 Sep 2024 18:23:01 -0400 Subject: [PATCH] CI: pre-commit check for generated files (#199) * add pre-commit hook to check for swagger updates * update README with 'make buf-gen' * put correct exit codes in hook * remove unnecessary Makefile addition * update hook to check pb.go files and be blocking * init-hooks -> init-git-hooks Co-authored-by: Will Vedder * fix makefile dep typo * change hook message wording to be more accurate * update README to point out --no-verify --------- Co-authored-by: Will Vedder --- .githooks/pre-commit | 29 +++++++++++++++++++++++++++++ Makefile | 5 ++++- README.md | 6 ++++-- 3 files changed, 37 insertions(+), 3 deletions(-) create mode 100755 .githooks/pre-commit diff --git a/.githooks/pre-commit b/.githooks/pre-commit new file mode 100755 index 00000000..1211e6c2 --- /dev/null +++ b/.githooks/pre-commit @@ -0,0 +1,29 @@ +#!/bin/sh +# This is a simple check to ensure people remember to regenerate the docs +# and the pb.go files if they change a .proto file. +# It's not iron-clad but it's better than finding out later in github. + +# AMD = added, modified, deleted +files=$(git diff --cached --name-only --diff-filter=AMD) + +LIGHT_RED='\033[1;31m' +RESET='\033[0m' # No Color + +# naive regex check for .proto extension +if [[ "$files" =~ \.proto ]] +then + # Check if there's a staged diff in /docs and in the generated pb.go files + docs_change=$(git diff --cached --raw docs/) + go_change=$(git diff --cached --raw proto/openfga/) + if [ -n "$docs_change" ] && [ -n "$go_change" ]; then + # Both were changed, we're good + exit 0 + else + # One of the two directories does not have changes, block this commit + echo "This commit contains .proto changes but either the docs or generated /proto files are not part of this commit." + echo "${LIGHT_RED}Your changes were not committed, please run 'make all' and add the resulting changes${RESET}\n" + exit 1 + fi +fi + +exit 0 diff --git a/Makefile b/Makefile index 568e752d..c70c06cc 100644 --- a/Makefile +++ b/Makefile @@ -1,6 +1,6 @@ all: patch-swagger-doc format -buf-gen: +buf-gen: init-git-hooks ./buf.gen.yaml patch-swagger-doc: buf-gen @@ -8,3 +8,6 @@ patch-swagger-doc: buf-gen format: buf-gen buf format -w + +init-git-hooks: + git config --local core.hooksPath .githooks/ diff --git a/README.md b/README.md index 79531f2b..68935235 100644 --- a/README.md +++ b/README.md @@ -10,10 +10,12 @@ To generate source code from the protobuf definitions contained in this project > **Note**: You must have [Buf CLI](https://docs.buf.build/installation) installed to run the following command. > ```bash -./buf.gen.yaml +make buf-gen ``` -The command above will generate source code in the `proto/` directory. +The command above will generate source code in the `proto/` directory. It will also configure a local git hook to check +that files requiring auto-generation after `.proto` changes have been updated. There are some cases where that git hook +may be overly strict. In those cases you can bypass it with `commit --no-verify`. ## Use the generated sources in OpenFGA