Skip to content

Commit

Permalink
feat: guarantee codegen targets are executed on pre-commit (#1258)
Browse files Browse the repository at this point in the history
  • Loading branch information
floreks authored Aug 9, 2024
1 parent 0735453 commit b790534
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 8 deletions.
69 changes: 69 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#!/usr/bin/env bash

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
ROOT_DIR=${SCRIPT_DIR%/*}
CONTROLLER_DIR=go/controller
CLIENT_DIR=go/client
CLIENT_GRAPH_DIR=${CLIENT_DIR}/graph
WARNING_UNSTAGED_MSG="[Warning] Check for any unstaged files and commit them before push"

# Keep track of executed checks to not run them multiple times
CONTROLLER_CODEGEN=false
CLIENT_GENERATE=false

function get::commit {
if git rev-parse --verify HEAD >/dev/null 2>&1; then
echo HEAD
else
# Initial commit: diff against an empty tree object
git hash-object -t tree /dev/null
fi
}

function check {
against=$1

# Get a list of files that are about to be commited
changed=$(git diff --cached --name-only "${against}")

# Loop over the files and run configured checks
for path in ${changed}; do
check::path "${path}"
done
}

function check::path {
path=$1
changed=false

if [[ "${path}" == ${CONTROLLER_DIR}/* ]] && [ "${CONTROLLER_CODEGEN}" == false ] ; then
echo Controller files changed
ensure::controller
changed=true
CONTROLLER_CODEGEN=true
fi

if [[ "${path}" == ${CLIENT_GRAPH_DIR}/* ]] && [ "${CLIENT_GENERATE}" == false ]; then
echo Client files changed
ensure::client
changed=true
CLIENT_GENERATE=true
fi

if [[ "${changed}" == true ]]; then
echo "${WARNING_UNSTAGED_MSG}"
fi
}

function ensure::controller {
echo Running codegen...
make --no-print-directory --directory="${ROOT_DIR}"/${CONTROLLER_DIR} codegen
}

function ensure::client {
echo Running generate...
make --no-print-directory --directory="${ROOT_DIR}"/${CLIENT_DIR} generate
}

echo Executing pre-commit hook
check "$(get::commit)"
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ GIT_COMMIT ?= abd123
TARGETARCH ?= amd64
ERLANG_VERSION ?= `grep erlang .tool-versions | cut -d' ' -f2`
REPO_ROOT ?= `pwd`
GIT_HOOKS_PATH = .githooks

help:
@perl -nle'print $& if m{^[a-zA-Z_-]+:.*?## .*$$}' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'
Expand Down Expand Up @@ -77,7 +78,6 @@ update-schema:
cd assets && yarn fix
@$(MAKE) --directory go/client --no-print-directory generate


k3s: ## starts a k3d cluster for testing
@read -p "cluster name: " name; \
k3d cluster create $$name --image docker.io/rancher/k3s:v1.26.11-k3s2
Expand All @@ -86,3 +86,7 @@ delete-tag: ## deletes a tag from git locally and upstream
@read -p "Version: " tag; \
git tag -d $$tag; \
git push origin :$$tag

install-git-hooks: ## enforces usage of git hooks stored under '.githooks' dir
@git config --local core.hooksPath ${GIT_HOOKS_PATH}/
@echo Successfully configured git hooks, \'core.hooksPath\' now points to \'${GIT_HOOKS_PATH}\'.
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ mix deps.get
mix test
```

### Git Hooks
Custom Git hooks are stored in `.githooks` directory. They ensure that when controller or client files are changed, automated code generation targets are executed. In order to enable git hooks for this repo run:
```sh
make install-git-hooks
```

### Troubleshooting
#### Installing Erlang
If `asdf install` fails with `cannot find required auxiliary files: install-sh config.guess config.sub` then run:
Expand All @@ -77,4 +83,4 @@ You can also use the make target in our root Makefile to automate this, eg:

```sh
make reshim
```
```
1 change: 0 additions & 1 deletion go/client/graph/backup.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,3 @@ query GetClusterRestore($id: ID!) {
...ClusterRestoreFragment
}
}

9 changes: 4 additions & 5 deletions go/controller/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ build: manifests generate fmt vet ## build manager binary
go build -o bin/manager cmd/main.go

.PHONY: run
run: manifests generate codegen-chart-rbac fmt vet ## run a controller from your host
run: manifests generate fmt vet ## run a controller from your host
go run ./cmd/main.go \
--console-url=${PLURAL_CONSOLE_URL}/gql

Expand Down Expand Up @@ -79,6 +79,9 @@ e2e: ## run e2e tests

##@ Codegen

.PHONY: codegen
codegen: manifests generate genmock codegen-helm codegen-crd-docs ## runs all codegen-related targets

.PHONY: manifests
manifests: ## generate WebhookConfiguration, ClusterRole and CustomResourceDefinition objects
$(CONTROLLER_GEN) rbac:roleName=manager-role crd:generateEmbeddedObjectMeta=true webhook paths="./..." output:crd:artifacts:config=config/crd/bases
Expand All @@ -103,10 +106,6 @@ codegen-chart-crds: ## copy CRDs to the controller helm chart
@cp -a config/crd/bases/. $(CONTROLLER_CHART_DIR)/crds
@cp -a $(CONTROLLER_CHART_DIR)/crds/. $(PLURAL_CONSOLE_CHART_DIR)/crds

.PHONY: codegen-chart-rbac
codegen-chart-rbac: codegen-helm ## update controller rbac in the controller helm chart
@cp -a tmp/charts/controller/templates/manager-rbac.yaml $(CONTROLLER_CHART_DIR)/templates

.PHONY: codegen-crd-docs
codegen-crd-docs: ## generate docs from the CRDs
$(CRDDOCS) --source-path=./api --renderer=markdown --output-path=./docs/api.md --config=config.yaml
Expand Down

0 comments on commit b790534

Please sign in to comment.