Skip to content

Commit a284902

Browse files
committed
HYPERFLEET-878 - refactor: rewrite commitlint as Go binary with container support
Replaces the Node.js-based commit validation with a Go implementation for better performance and simplified cross-component deployment. - Implement commit message validator in Go with comprehensive test coverage - Add GitHub SDK integration for PR title validation (replaces gh CLI dependency) - Support both local pre-commit hooks and Prow CI environments - Build container image for reuse across all HyperFleet components - Add commit range detection with PULL_REFS priority for Prow accuracy - Create unified CLI with --pr flag for CI mode - Include comprehensive documentation for local and CI usage - Add Makefile with build, test, lint, and validation targets
1 parent ef43789 commit a284902

File tree

16 files changed

+1461
-2
lines changed

16 files changed

+1461
-2
lines changed

.gitignore

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Binaries
2+
bin/
3+
*.exe
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test artifacts
9+
coverage.out
10+
coverage.html
11+
*.test
12+
13+
# Go workspace
14+
go.work
15+
go.work.sum
16+
17+
# IDE
18+
.idea/
19+
.vscode/
20+
*.swp
21+
*.swo
22+
*~
23+
24+
# OS
25+
.DS_Store
26+
Thumbs.db
27+
28+
# Node.js (if any legacy files exist)
29+
node_modules/
30+
package-lock.json
31+
32+
# temporary
33+
tmp/

.pre-commit-hooks.yaml

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
# HyperFleet pre-commit hooks registry
3+
# https://pre-commit.com/
4+
5+
- id: hyperfleet-commitlint
6+
name: Validate commit message (HyperFleet Standard)
7+
description: >
8+
Validates commit messages against the HyperFleet Commit Standard
9+
(Conventional Commits with optional JIRA prefix).
10+
entry: hyperfleet-commitlint
11+
language: golang
12+
stages: [commit-msg]
13+
pass_filenames: true
14+
always_run: true

Dockerfile

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
ARG BASE_IMAGE=registry.access.redhat.com/ubi9/ubi-minimal:latest
2+
3+
FROM registry.access.redhat.com/ubi9/go-toolset:1.25 AS builder
4+
5+
ARG VERSION=dev
6+
ARG GIT_COMMIT=unknown
7+
ARG BUILD_DATE=unknown
8+
9+
USER root
10+
WORKDIR /build
11+
12+
COPY go.mod go.sum ./
13+
RUN go mod download
14+
15+
COPY . .
16+
RUN CGO_ENABLED=0 GOOS=linux go build \
17+
-ldflags="-s -w \
18+
-X github.com/openshift-hyperfleet/hyperfleet-hooks/pkg/version.Version=${VERSION} \
19+
-X github.com/openshift-hyperfleet/hyperfleet-hooks/pkg/version.GitCommit=${GIT_COMMIT} \
20+
-X github.com/openshift-hyperfleet/hyperfleet-hooks/pkg/version.BuildDate=${BUILD_DATE}" \
21+
-o hyperfleet-hooks \
22+
./cmd/hyperfleet-hooks
23+
24+
# Runtime stage
25+
FROM ${BASE_IMAGE}
26+
27+
# Install git-core for commit validation
28+
RUN microdnf install -y git-core \
29+
&& microdnf clean all \
30+
&& rm -rf /var/cache/yum
31+
32+
WORKDIR /workspace
33+
34+
COPY --from=builder /build/hyperfleet-hooks /usr/local/bin/hyperfleet-hooks
35+
36+
USER 65532:65532
37+
38+
HEALTHCHECK --interval=30s --timeout=3s --start-period=5s --retries=3 \
39+
CMD hyperfleet-hooks version || exit 1
40+
41+
ENTRYPOINT ["/usr/local/bin/hyperfleet-hooks"]
42+
CMD ["--help"]
43+
44+
ARG VERSION=dev
45+
LABEL name="hyperfleet-hooks" \
46+
vendor="Red Hat" \
47+
version="${VERSION}" \
48+
summary="HyperFleet Hooks - Commit Message Validator" \
49+
description="Validates commit messages against HyperFleet standards"

Makefile

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
# ==============================================================================
2+
# Makefile for hyperfleet-hooks
3+
# ==============================================================================
4+
5+
.PHONY: help build build-all test test-coverage lint clean install validate-commits
6+
7+
# ==============================================================================
8+
# Configuration
9+
# ==============================================================================
10+
11+
# Binary configuration
12+
BINARY_NAME := hyperfleet-hooks
13+
BIN_DIR := bin
14+
15+
# Version information (auto-detected from git)
16+
VERSION ?= $(shell git describe --tags --always --dirty 2>/dev/null || echo "dev")
17+
GIT_COMMIT ?= $(shell git rev-parse HEAD 2>/dev/null || echo "unknown")
18+
BUILD_DATE ?= $(shell date -u '+%Y-%m-%d_%H:%M:%S')
19+
20+
# Build flags
21+
LDFLAGS := -ldflags "\
22+
-s -w \
23+
-X github.com/openshift-hyperfleet/hyperfleet-hooks/pkg/version.Version=$(VERSION) \
24+
-X github.com/openshift-hyperfleet/hyperfleet-hooks/pkg/version.GitCommit=$(GIT_COMMIT) \
25+
-X github.com/openshift-hyperfleet/hyperfleet-hooks/pkg/version.BuildDate=$(BUILD_DATE)"
26+
27+
# ==============================================================================
28+
# Targets
29+
# ==============================================================================
30+
31+
.DEFAULT_GOAL := help
32+
33+
help: ## Show this help message
34+
@echo 'Usage: make [target]'
35+
@echo ''
36+
@echo 'Available targets:'
37+
@awk 'BEGIN {FS = ":.*?## "} /^[a-zA-Z_-]+:.*?## / {printf " %-20s %s\n", $$1, $$2}' $(MAKEFILE_LIST)
38+
39+
# ------------------------------------------------------------------------------
40+
# Build
41+
# ------------------------------------------------------------------------------
42+
43+
build: ## Build the binary
44+
@echo "Building $(BINARY_NAME) $(VERSION)..."
45+
@mkdir -p $(BIN_DIR)
46+
go build $(LDFLAGS) -o $(BIN_DIR)/$(BINARY_NAME) ./cmd/hyperfleet-hooks
47+
@echo "✓ Built $(BIN_DIR)/$(BINARY_NAME)"
48+
49+
build-all: ## Build binaries for all platforms
50+
@echo "Building for all platforms..."
51+
@mkdir -p $(BIN_DIR)
52+
GOOS=linux GOARCH=amd64 go build $(LDFLAGS) -o $(BIN_DIR)/$(BINARY_NAME)-linux-amd64 ./cmd/hyperfleet-hooks
53+
GOOS=linux GOARCH=arm64 go build $(LDFLAGS) -o $(BIN_DIR)/$(BINARY_NAME)-linux-arm64 ./cmd/hyperfleet-hooks
54+
GOOS=darwin GOARCH=amd64 go build $(LDFLAGS) -o $(BIN_DIR)/$(BINARY_NAME)-darwin-amd64 ./cmd/hyperfleet-hooks
55+
GOOS=darwin GOARCH=arm64 go build $(LDFLAGS) -o $(BIN_DIR)/$(BINARY_NAME)-darwin-arm64 ./cmd/hyperfleet-hooks
56+
@echo "✓ Built all platform binaries"
57+
58+
# ------------------------------------------------------------------------------
59+
# Test
60+
# ------------------------------------------------------------------------------
61+
62+
test: ## Run tests
63+
@echo "Running tests..."
64+
go test -v -race -coverprofile=coverage.out ./...
65+
@echo "✓ Tests passed"
66+
67+
test-coverage: test ## Run tests with coverage report
68+
@echo "Generating coverage report..."
69+
go tool cover -html=coverage.out -o coverage.html
70+
@echo "✓ Coverage report generated: coverage.html"
71+
72+
# ------------------------------------------------------------------------------
73+
# Quality
74+
# ------------------------------------------------------------------------------
75+
76+
lint: ## Run linters
77+
@echo "Running linters..."
78+
@if command -v golangci-lint >/dev/null 2>&1; then \
79+
golangci-lint run --timeout=5m; \
80+
else \
81+
echo "⚠ golangci-lint not installed, running go fmt only"; \
82+
test -z "$$(gofmt -l .)" || (echo "Files need formatting:" && gofmt -l . && exit 1); \
83+
fi
84+
@echo "✓ Linting passed"
85+
86+
# ------------------------------------------------------------------------------
87+
# Utility
88+
# ------------------------------------------------------------------------------
89+
90+
clean: ## Clean build artifacts
91+
@echo "Cleaning..."
92+
rm -rf $(BIN_DIR)
93+
rm -f coverage.out coverage.html
94+
@echo "✓ Cleaned"
95+
96+
install: build ## Install binary to /usr/local/bin
97+
@echo "Installing $(BINARY_NAME) to /usr/local/bin..."
98+
@cp $(BIN_DIR)/$(BINARY_NAME) /usr/local/bin/$(BINARY_NAME)
99+
@echo "✓ Installed $(BINARY_NAME)"
100+
101+
# ------------------------------------------------------------------------------
102+
# CI
103+
# ------------------------------------------------------------------------------
104+
105+
validate-commits: build ## Validate commits in current branch (CI mode)
106+
@echo "Validating commits..."
107+
@./bin/hyperfleet-hooks commitlint --pr

README.md

Lines changed: 44 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,44 @@
1-
# hyperfleet-hooks
2-
Pre-commit hooks registry for HyperFleet project ecosystem
1+
# HyperFleet Hooks
2+
3+
Validation tools for HyperFleet projects.
4+
5+
## Features
6+
7+
### Commitlint
8+
9+
Validates commit messages and PR titles against the [HyperFleet Commit Standard](https://github.com/openshift-hyperfleet/architecture/blob/main/hyperfleet/standards/commit-standard.md).
10+
11+
**[→ Documentation](docs/commitlint.md)**
12+
13+
## Installation
14+
15+
```bash
16+
# Install pre-commit framework
17+
pip install pre-commit
18+
19+
# Add to your .pre-commit-config.yaml
20+
repos:
21+
- repo: https://github.com/openshift-hyperfleet/hyperfleet-hooks
22+
rev: v1.0.0
23+
hooks:
24+
- id: hyperfleet-commitlint
25+
26+
# Install hooks (pre-commit will automatically build the binary)
27+
pre-commit install --hook-type commit-msg
28+
```
29+
30+
**Note**: Pre-commit automatically builds the binary. No manual installation needed.
31+
32+
See [commitlint documentation](docs/commitlint.md) for Prow CI setup and detailed usage.
33+
34+
## Development
35+
36+
```bash
37+
make build # Build binary
38+
make test # Run tests
39+
make lint # Run linters
40+
```
41+
42+
## License
43+
44+
Apache 2.0

0 commit comments

Comments
 (0)