Skip to content

Commit

Permalink
ci: add pre-commit
Browse files Browse the repository at this point in the history
Signed-off-by: Tyler Gillson <[email protected]>
  • Loading branch information
TylerGillson committed Jul 23, 2024
1 parent 02aada9 commit 3ef3ec2
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 14 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
19 changes: 19 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -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
56 changes: 47 additions & 9 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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<target>\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; \
Expand All @@ -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)
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
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```

0 comments on commit 3ef3ec2

Please sign in to comment.