Skip to content

Commit 2a761db

Browse files
authored
task: Adding golang precommit hooks (#503)
1 parent 98f8367 commit 2a761db

File tree

4 files changed

+38
-4
lines changed

4 files changed

+38
-4
lines changed

.lintstagedrc.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
{
22
"**/*.{js,jsx,ts,tsx,json,md}": ["prettier --write"],
3-
"**/*.js": ["jest --findRelatedTests --passWithNoTests"]
3+
"**/*.js": ["jest --findRelatedTests --passWithNoTests"],
4+
"tools/spectral/ipa/**/*.yaml": ["node tools/spectral/ipa/scripts/generateRulesetReadme.js"],
5+
"**/*.go": ["./tools/cli/precommit.sh"]
46
}

CONTRIBUTING.md

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
- Node.js v20 or later
88
- npm v8 or later
99
- Git
10+
- Go 1.21 or later (for CLI development)
1011

1112
### Installation
1213

@@ -21,11 +22,17 @@
2122
npm install
2223
```
2324

24-
3. Set up Git hooks (optional):
25+
3. Set up Git hooks (recommended):
2526
```bash
2627
npm run precommit
2728
```
2829

30+
4. If working on the Go CLI, set up the Go environment:
31+
```bash
32+
cd tools/cli
33+
make setup
34+
```
35+
2936
This will install Husky, which manages Git hooks for the project. The hooks ensure code quality by automatically running various checks before commits.
3037

3138
## Development Workflow
@@ -34,17 +41,28 @@ This will install Husky, which manages Git hooks for the project. The hooks ensu
3441

3542
This project uses the following Git hooks:
3643

37-
- **pre-commit**: Automatically formats your code using Prettier and runs tests for staged JavaScript files to ensure code quality before each commit.
44+
- **pre-commit**:
45+
- Automatically formats your code using Prettier and runs tests for staged JavaScript files
3846
- If you get the message `hint: The '.husky/pre-commit' hook was ignored because it's not set as executable.` during the commit, you can run `chmod ug+x .husky/*` to make it executable.
47+
- For Go files, runs formatting, linting, and tests using the project's Makefile
3948

4049
### Available Scripts
4150

51+
#### JavaScript/Node.js
4252
- `npm run format` - Format all files with Prettier
4353
- `npm run format-check` - Check formatting without modifying files
4454
- `npm run lint-js` - Lint JavaScript files
4555
- `npm run test` - Run all tests
4656

4757
#### IPA specific targets
48-
4958
- `npm run gen-ipa-docs` - Generate IPA ruleset documentation (see `./tools` folder for more information)
5059
- `npm run ipa-validation` - Run OpenAPI validation with Spectral
60+
61+
#### Go OpenAPI CLI (internal)
62+
When working in the `tools/cli` directory:
63+
- `make fmt` - Format Go code
64+
- `make lint` - Run golangci-lint
65+
- `make unit-test` - Run Go unit tests
66+
- `make e2e-test` - Run end-to-end tests
67+
- `make build` - Build the CLI binary
68+
- `make gen-docs` - Generate CLI documentation

tools/cli/Makefile

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ gen-mocks: ## Generate mocks
8787
@echo "==> Generating mocks"
8888
go generate ./internal...
8989

90+
.PHONY: pre-commit
91+
pre-commit: fmt lint unit-test ## Run pre-commit checks
92+
9093
.PHONY: help
9194
.DEFAULT_GOAL := help
9295
help:

tools/cli/precommit.sh

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
# Get the root directory of the project
5+
ROOT_DIR=$(git rev-parse --show-toplevel)
6+
7+
pushd "$ROOT_DIR/tools/cli"
8+
make pre-commit
9+
popd
10+
11+
exit 0

0 commit comments

Comments
 (0)