Skip to content

Commit

Permalink
CI: pre-commit check for generated files (#199)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* fix makefile dep typo

* change hook message wording to be more accurate

* update README to point out --no-verify

---------

Co-authored-by: Will Vedder <[email protected]>
  • Loading branch information
justincoh and willvedd authored Sep 19, 2024
1 parent a825149 commit 339f6b8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 3 deletions.
29 changes: 29 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -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
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
all: patch-swagger-doc format

buf-gen:
buf-gen: init-git-hooks
./buf.gen.yaml

patch-swagger-doc: buf-gen
./scripts/update_swagger.sh docs/openapiv2/apidocs.swagger.json

format: buf-gen
buf format -w

init-git-hooks:
git config --local core.hooksPath .githooks/
6 changes: 4 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 339f6b8

Please sign in to comment.