diff --git a/scripts/pre-commit-check.sh b/scripts/pre-commit-check.sh new file mode 100755 index 0000000..8c2218a --- /dev/null +++ b/scripts/pre-commit-check.sh @@ -0,0 +1,68 @@ +#!/bin/bash +# Pre-commit check script that runs lint, fmt-check, test, and build in gitea-runner container + +set -e + +SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" +PROJECT_ROOT="$(cd "$SCRIPT_DIR/.." && pwd)" +IMAGE_NAME="wirelos/pre-commit" +CONTAINER_NAME="goplt-pre-commit-check" + +echo "๐Ÿ” Checking for Docker image: $IMAGE_NAME" +if ! docker images --format "{{.Repository}}:{{.Tag}}" | grep -q "^${IMAGE_NAME}:latest$"; then + echo "๐Ÿ“ฆ Image not found. Building $IMAGE_NAME from ci/pre-commit/Dockerfile..." + docker build -t "$IMAGE_NAME:latest" -f "$PROJECT_ROOT/ci/pre-commit/Dockerfile" "$PROJECT_ROOT/ci/pre-commit" || { + echo "โŒ Failed to build Docker image" + exit 1 + } + echo "โœ… Image built successfully" +else + echo "โœ… Image found locally" +fi + +echo "๐Ÿงน Cleaning up any existing container..." +docker rm -f "$CONTAINER_NAME" 2>/dev/null || true + +echo "๐Ÿš€ Starting pre-commit container..." +docker run --rm \ + --name "$CONTAINER_NAME" \ + -v "$PROJECT_ROOT:/workspace" \ + -w /workspace \ + "$IMAGE_NAME:latest" \ + sh -c " + echo '๐Ÿ“ฆ Installing Go and required tools...' + apk add --no-cache go protobuf protobuf-dev bash git || exit 1 + + echo '๐Ÿ“ฅ Installing Go protobuf plugins...' + export PATH=\$PATH:/root/go/bin + go install google.golang.org/protobuf/cmd/protoc-gen-go@latest || exit 1 + go install google.golang.org/grpc/cmd/protoc-gen-go-grpc@latest || exit 1 + + echo '๐Ÿ“ฅ Installing golangci-lint...' + curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b /root/go/bin || exit 1 + + echo '๐Ÿ“‹ Running make fmt-check...' + make fmt-check || exit 1 + + echo '๐Ÿ” Running make lint...' + make lint || exit 1 + + echo '๐Ÿงช Running make test...' + make test || exit 1 + + echo '๐Ÿ”จ Running make build...' + make build || exit 1 + + echo 'โœ… All checks passed!' + " + +EXIT_CODE=$? + +if [ $EXIT_CODE -eq 0 ]; then + echo "โœ… All pre-commit checks passed!" +else + echo "โŒ Pre-commit checks failed. Please fix the issues above." +fi + +exit $EXIT_CODE +