Skip to content

Commit f40cd1a

Browse files
committed
feat: Add git hooks for TFO-GO-SDK
1 parent ee390a1 commit f40cd1a

3 files changed

Lines changed: 250 additions & 0 deletions

File tree

docs/githooks/commit-msg

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
#!/bin/bash
2+
3+
# Commit-msg hook for TelemetryFlow projects
4+
# Enforces Conventional Commits format
5+
# https://www.conventionalcommits.org/
6+
7+
set -e
8+
9+
COMMIT_MSG_FILE=$1
10+
COMMIT_MSG=$(cat "$COMMIT_MSG_FILE")
11+
12+
# Colors for output
13+
RED='\033[0;31m'
14+
GREEN='\033[0;32m'
15+
YELLOW='\033[1;33m'
16+
NC='\033[0m' # No Color
17+
18+
# Skip merge commits
19+
if echo "$COMMIT_MSG" | grep -qE "^Merge"; then
20+
exit 0
21+
fi
22+
23+
# Conventional commit pattern
24+
# Types: feat, fix, docs, style, refactor, perf, test, build, ci, chore, revert
25+
# Optional scope in parentheses
26+
# Optional breaking change indicator (!)
27+
# Colon and space, then description
28+
PATTERN="^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\(.+\))?(!)?: .{1,}"
29+
30+
# Check if commit message follows conventional commits format
31+
if ! echo "$COMMIT_MSG" | head -1 | grep -qE "$PATTERN"; then
32+
echo -e "${RED}ERROR: Commit message does not follow Conventional Commits format.${NC}"
33+
echo ""
34+
echo -e "${YELLOW}Your commit message:${NC}"
35+
echo "$COMMIT_MSG" | head -1
36+
echo ""
37+
echo -e "${YELLOW}Expected format:${NC}"
38+
echo " <type>[optional scope][!]: <description>"
39+
echo ""
40+
echo -e "${YELLOW}Allowed types:${NC}"
41+
echo " feat: A new feature"
42+
echo " fix: A bug fix"
43+
echo " docs: Documentation only changes"
44+
echo " style: Code style changes (formatting, semicolons, etc)"
45+
echo " refactor: Code change that neither fixes a bug nor adds a feature"
46+
echo " perf: Performance improvement"
47+
echo " test: Adding or updating tests"
48+
echo " build: Build system or external dependency changes"
49+
echo " ci: CI configuration changes"
50+
echo " chore: Other changes that don't modify src or test files"
51+
echo " revert: Reverts a previous commit"
52+
echo ""
53+
echo -e "${YELLOW}Examples:${NC}"
54+
echo " feat: add user authentication"
55+
echo " fix(api): resolve null pointer in handler"
56+
echo " docs: update README with installation steps"
57+
echo " feat(auth)!: change token format (breaking change)"
58+
echo ""
59+
exit 1
60+
fi
61+
62+
# Check minimum description length (at least 10 characters after the type)
63+
DESCRIPTION=$(echo "$COMMIT_MSG" | head -1 | sed -E 's/^(feat|fix|docs|style|refactor|perf|test|build|ci|chore|revert)(\(.+\))?(!)?: //')
64+
if [ ${#DESCRIPTION} -lt 10 ]; then
65+
echo -e "${RED}ERROR: Commit description is too short (minimum 10 characters).${NC}"
66+
echo -e "${YELLOW}Current description:${NC} $DESCRIPTION"
67+
exit 1
68+
fi
69+
70+
# Check that description doesn't start with capital letter (optional but recommended)
71+
if echo "$DESCRIPTION" | grep -qE "^[A-Z]"; then
72+
echo -e "${YELLOW}WARNING: Description should start with lowercase letter.${NC}"
73+
echo -e "${YELLOW}Current:${NC} $DESCRIPTION"
74+
# This is just a warning, not a failure
75+
fi
76+
77+
# Check commit message line length (first line should be <= 72 characters)
78+
FIRST_LINE_LENGTH=$(echo "$COMMIT_MSG" | head -1 | wc -c | tr -d ' ')
79+
if [ "$FIRST_LINE_LENGTH" -gt 72 ]; then
80+
echo -e "${YELLOW}WARNING: First line exceeds 72 characters (${FIRST_LINE_LENGTH} chars).${NC}"
81+
echo -e "${YELLOW}Consider shortening the commit message.${NC}"
82+
# This is just a warning, not a failure
83+
fi
84+
85+
echo -e "${GREEN}Commit message follows Conventional Commits format.${NC}"
86+
exit 0

docs/githooks/pre-commit

Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
#!/bin/bash
2+
3+
# Pre-commit hook for TelemetryFlow Go projects
4+
# Runs: gofmt, golint, go vet, and security scan
5+
6+
set -e
7+
8+
echo "Running pre-commit checks..."
9+
10+
# Colors for output
11+
RED='\033[0;31m'
12+
GREEN='\033[0;32m'
13+
YELLOW='\033[1;33m'
14+
NC='\033[0m' # No Color
15+
16+
# Get staged Go files
17+
STAGED_GO_FILES=$(git diff --cached --name-only --diff-filter=ACM | grep '\.go$' || true)
18+
19+
if [ -z "$STAGED_GO_FILES" ]; then
20+
echo -e "${YELLOW}No Go files staged for commit. Skipping Go checks.${NC}"
21+
exit 0
22+
fi
23+
24+
# Function to check if a command exists
25+
command_exists() {
26+
command -v "$1" >/dev/null 2>&1
27+
}
28+
29+
FAILED=0
30+
31+
# 1. Check gofmt
32+
echo -e "\n${YELLOW}[1/4] Running gofmt...${NC}"
33+
GOFMT_OUTPUT=$(gofmt -l $STAGED_GO_FILES 2>&1 || true)
34+
if [ -n "$GOFMT_OUTPUT" ]; then
35+
echo -e "${RED}The following files need formatting:${NC}"
36+
echo "$GOFMT_OUTPUT"
37+
echo -e "${YELLOW}Run 'gofmt -w <file>' to fix${NC}"
38+
FAILED=1
39+
else
40+
echo -e "${GREEN}gofmt passed${NC}"
41+
fi
42+
43+
# 2. Run go vet
44+
echo -e "\n${YELLOW}[2/4] Running go vet...${NC}"
45+
if ! go vet ./... 2>&1; then
46+
echo -e "${RED}go vet found issues${NC}"
47+
FAILED=1
48+
else
49+
echo -e "${GREEN}go vet passed${NC}"
50+
fi
51+
52+
# 3. Run golint (if available)
53+
echo -e "\n${YELLOW}[3/4] Running golint...${NC}"
54+
if command_exists golint; then
55+
LINT_OUTPUT=$(golint ./... 2>&1 || true)
56+
if [ -n "$LINT_OUTPUT" ]; then
57+
echo -e "${RED}golint found issues:${NC}"
58+
echo "$LINT_OUTPUT"
59+
# golint issues are warnings, not failures
60+
echo -e "${YELLOW}(golint issues are warnings only)${NC}"
61+
else
62+
echo -e "${GREEN}golint passed${NC}"
63+
fi
64+
elif command_exists staticcheck; then
65+
if ! staticcheck ./... 2>&1; then
66+
echo -e "${RED}staticcheck found issues${NC}"
67+
FAILED=1
68+
else
69+
echo -e "${GREEN}staticcheck passed${NC}"
70+
fi
71+
else
72+
echo -e "${YELLOW}golint/staticcheck not installed. Skipping lint check.${NC}"
73+
echo -e "${YELLOW}Install with: go install golang.org/x/lint/golint@latest${NC}"
74+
echo -e "${YELLOW}Or: go install honnef.co/go/tools/cmd/staticcheck@latest${NC}"
75+
fi
76+
77+
# 4. Run security scan (gosec if available)
78+
echo -e "\n${YELLOW}[4/4] Running security scan...${NC}"
79+
if command_exists gosec; then
80+
GOSEC_OUTPUT=$(gosec -quiet ./... 2>&1 || true)
81+
if [ -n "$GOSEC_OUTPUT" ] && echo "$GOSEC_OUTPUT" | grep -q "Severity"; then
82+
echo -e "${RED}gosec found security issues:${NC}"
83+
echo "$GOSEC_OUTPUT"
84+
FAILED=1
85+
else
86+
echo -e "${GREEN}gosec passed${NC}"
87+
fi
88+
elif command_exists govulncheck; then
89+
if ! govulncheck ./... 2>&1; then
90+
echo -e "${RED}govulncheck found vulnerabilities${NC}"
91+
FAILED=1
92+
else
93+
echo -e "${GREEN}govulncheck passed${NC}"
94+
fi
95+
else
96+
echo -e "${YELLOW}gosec/govulncheck not installed. Skipping security scan.${NC}"
97+
echo -e "${YELLOW}Install with: go install github.com/securego/gosec/v2/cmd/gosec@latest${NC}"
98+
echo -e "${YELLOW}Or: go install golang.org/x/vuln/cmd/govulncheck@latest${NC}"
99+
fi
100+
101+
if [ $FAILED -ne 0 ]; then
102+
echo -e "\n${RED}Pre-commit checks failed. Please fix the issues above.${NC}"
103+
exit 1
104+
fi
105+
106+
echo -e "\n${GREEN}All pre-commit checks passed!${NC}"
107+
exit 0

docs/githooks/pre-push

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#!/bin/bash
2+
3+
# Pre-push hook for TelemetryFlow Go projects
4+
# Runs tests before pushing to remote
5+
6+
set -e
7+
8+
echo "Running pre-push checks..."
9+
10+
# Colors for output
11+
RED='\033[0;31m'
12+
GREEN='\033[0;32m'
13+
YELLOW='\033[1;33m'
14+
NC='\033[0m' # No Color
15+
16+
REMOTE="$1"
17+
URL="$2"
18+
19+
# Read stdin for push info
20+
while read local_ref local_sha remote_ref remote_sha; do
21+
# Skip delete operations
22+
if [ "$local_sha" = "0000000000000000000000000000000000000000" ]; then
23+
continue
24+
fi
25+
26+
echo -e "${YELLOW}Pushing to: ${REMOTE} (${URL})${NC}"
27+
echo -e "${YELLOW}Branch: ${local_ref}${NC}"
28+
done
29+
30+
FAILED=0
31+
32+
# 1. Run tests
33+
echo -e "\n${YELLOW}[1/2] Running tests...${NC}"
34+
if go test ./... -v -race -timeout 5m 2>&1; then
35+
echo -e "${GREEN}All tests passed${NC}"
36+
else
37+
echo -e "${RED}Tests failed${NC}"
38+
FAILED=1
39+
fi
40+
41+
# 2. Run build to ensure project compiles
42+
echo -e "\n${YELLOW}[2/2] Running build check...${NC}"
43+
if go build ./... 2>&1; then
44+
echo -e "${GREEN}Build successful${NC}"
45+
else
46+
echo -e "${RED}Build failed${NC}"
47+
FAILED=1
48+
fi
49+
50+
if [ $FAILED -ne 0 ]; then
51+
echo -e "\n${RED}Pre-push checks failed. Push aborted.${NC}"
52+
echo -e "${YELLOW}Fix the issues above and try again.${NC}"
53+
exit 1
54+
fi
55+
56+
echo -e "\n${GREEN}All pre-push checks passed! Pushing...${NC}"
57+
exit 0

0 commit comments

Comments
 (0)