From 3ef3ec27db2738f730c3917dbbc9b38847030531 Mon Sep 17 00:00:00 2001 From: Tyler Gillson Date: Tue, 23 Jul 2024 10:23:08 -0600 Subject: [PATCH] ci: add pre-commit Signed-off-by: Tyler Gillson --- .github/workflows/ci.yml | 7 ++--- .pre-commit-config.yaml | 19 ++++++++++++++ Makefile | 56 +++++++++++++++++++++++++++++++++------- README.md | 14 ++++++++++ 4 files changed, 82 insertions(+), 14 deletions(-) create mode 100644 .pre-commit-config.yaml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1130e825..d3fa2e90 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -28,11 +28,8 @@ jobs: with: go-version-file: go.mod - - name: Vet - run: make vet - - - name: Lint - run: make lint + - name: Ensure Reviewable + run: make check-diff - name: Test run: make test \ No newline at end of file diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 00000000..3b08072f --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,19 @@ +repos: + - repo: https://github.com/zricethezav/gitleaks + rev: v8.18.0 + hooks: + - id: gitleaks + - repo: https://github.com/compilerla/conventional-pre-commit + rev: v2.4.0 + hooks: + - id: conventional-pre-commit + stages: [commit-msg] + - repo: https://github.com/golangci/golangci-lint + rev: v1.59.1 + hooks: + - id: golangci-lint + entry: golangci-lint run --new-from-rev HEAD --whole-files + types: [go] + language: golang + require_serial: true + pass_filenames: false diff --git a/Makefile b/Makefile index ce1e8ba6..c1742e47 100644 --- a/Makefile +++ b/Makefile @@ -3,34 +3,63 @@ .DEFAULT_GOAL:=help -GOLANGCI_VERSION ?= 1.54.2 - -BIN_DIR ?= ./bin +# Go vars GOOS ?= $(shell go env GOOS) GOARCH ?= $(shell go env GOARCH) +# Output +TIME = `date +%H:%M:%S` +GREEN := $(shell printf "\033[32m") +RED := $(shell printf "\033[31m") +CNone := $(shell printf "\033[0m") +OK = echo ${TIME} ${GREEN}[ OK ]${CNone} +ERR = echo ${TIME} ${RED}[ ERR ]${CNone} "error:" + ##@ Help Targets + help: ## Display this help @awk 'BEGIN {FS = ":.*##"; printf "\nUsage:\n make \033[36m\033[0m\n"} /^[a-zA-Z_-]+:.*?##/ { printf " \033[36m%-15s\033[0m %s\n", $$1, $$2 } /^##@/ { printf "\n\033[0m%s\033[0m\n", substr($$0, 5) } ' $(MAKEFILE_LIST) -bin-dir: - test -d $(BIN_DIR) || mkdir $(BIN_DIR) - ##@ Static Analysis Targets + +check-diff: reviewable ## Execute branch is clean + git --no-pager diff + git diff --quiet || ($(ERR) please run 'make reviewable' to include all changes && false) + @$(OK) branch is clean + +reviewable: pre-commit-install fmt vet lint ## Ensure code is ready for review + git submodule update --remote + go mod tidy + fmt: ## Run go fmt against code go fmt ./... + vet: ## Run go vet against code go vet ./... + lint: golangci-lint ## Run golangci-lint against code $(GOLANGCI_LINT) run +pre-commit-install: pre-commit ## Install pre-commit hooks + @if [ "$(GITHUB_ACTIONS)" != "true" ]; then \ + pre-commit install --hook-type commit-msg; \ + pre-commit install --hook-type pre-commit; \ + fi + ##@ Test Targets + .PHONY: test test: ## Run acceptance tests - go test -covermode=atomic -coverpkg=./... -coverprofile=profile.cov ./... -timeout 120m + go test -covermode=atomic -coverpkg=./... -coverprofile=profile.cov ./... -timeout 10m + +##@ Tools Targets -# Tools Section +BIN_DIR ?= ./bin +bin-dir: + test -d $(BIN_DIR) || mkdir $(BIN_DIR) +GOLANGCI_VERSION ?= 1.59.1 +.PHONY: golangci-lint golangci-lint: bin-dir if ! test -f $(BIN_DIR)/golangci-lint-linux-amd64; then \ curl -LOs https://github.com/golangci/golangci-lint/releases/download/v$(GOLANGCI_VERSION)/golangci-lint-$(GOLANGCI_VERSION)-linux-amd64.tar.gz; \ @@ -46,4 +75,13 @@ golangci-lint: bin-dir chmod +x $(BIN_DIR)/golangci-lint-$(GOOS)-$(GOARCH); \ rm -rf ./golangci-lint-$(GOLANGCI_VERSION)-$(GOOS)-$(GOARCH)*; \ fi -GOLANGCI_LINT=$(BIN_DIR)/golangci-lint-$(GOOS)-$(GOARCH) \ No newline at end of file +GOLANGCI_LINT=$(BIN_DIR)/golangci-lint-$(GOOS)-$(GOARCH) + +.PHONY: pre-commit +pre-commit: + @if [ "$(GITHUB_ACTIONS)" != "true" ]; then \ + @command -v pre-commit >/dev/null 2>&1 || { \ + echo "pre-commit not found, downloading..."; \ + pip install pre-commit; \ + } \ + fi diff --git a/README.md b/README.md index b920cf4e..774bf909 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,17 @@ Palette SDK for the Go programming language. See [terraform-provider-spectrocloud](https://github.com/spectrocloud/terraform-provider-spectrocloud) for example usage. + +# Usage + +# Contributing +All contributions are welcome! Feel free to reach out on the [Spectro Cloud community Slack](https://spectrocloudcommunity.slack.com/join/shared_invite/zt-g8gfzrhf-cKavsGD_myOh30K24pImLA#/shared-invite/email). + +Make sure `pre-commit` is [installed](https://pre-commit.com#install). + +Install the `pre-commit` scripts: + +```console +pre-commit install --hook-type commit-msg +pre-commit install --hook-type pre-commit +``` \ No newline at end of file