Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ on:
pull_request:

jobs:
rust:
quality:
name: Code Quality & Format Checks
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
Expand All @@ -15,7 +16,7 @@ jobs:
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
targets: wasm32v1-none
targets: wasm32-unknown-unknown

- name: Cache cargo
uses: Swatinem/rust-cache@v2
Expand All @@ -26,8 +27,24 @@ jobs:
- name: Run clippy linter
run: cargo clippy --workspace --all-features -- -D warnings

build-and-test:
name: Build & Test
needs: quality
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4

- name: Install Rust toolchain
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
targets: wasm32-unknown-unknown

- name: Cache cargo
uses: Swatinem/rust-cache@v2

- name: Build WASM (release)
run: cargo build --target wasm32v1-none --release
run: cargo build --target wasm32-unknown-unknown --release

- name: Run unit tests
run: cargo test --workspace --lib
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/regression.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
uses: dtolnay/rust-toolchain@stable
with:
components: rustfmt, clippy
targets: wasm32v1-none
targets: wasm32-unknown-unknown

- name: Cache cargo
uses: Swatinem/rust-cache@v2
Expand Down
120 changes: 120 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
# Pre-commit hooks configuration for TeachLink contract
# Install with: pre-commit install
# Update hooks with: pre-commit autoupdate
# Run manually with: pre-commit run --all-files

repos:
# General file checks
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.5.0
hooks:
- id: trailing-whitespace
name: Trim trailing whitespace
stages: [commit]

- id: end-of-file-fixer
name: Fix end of file
stages: [commit]

- id: check-yaml
name: Check YAML syntax
args: [--unsafe]
stages: [commit]

- id: check-toml
name: Check TOML syntax
stages: [commit]

- id: check-json
name: Check JSON syntax
stages: [commit]

- id: check-merge-conflict
name: Check for merge conflicts
stages: [commit]

- id: check-case-conflict
name: Check for case conflicts
stages: [commit]

- id: detect-private-key
name: Detect private keys
stages: [commit]

- id: mixed-line-ending
name: Fix mixed line endings
args: [--fix=lf]
stages: [commit]

# Rust formatting with rustfmt
- repo: https://github.com/doublify/pre-commit-rust
rev: v1.0
hooks:
- id: fmt
name: Rustfmt
entry: bash -c 'cargo fmt --all -- --check || (cargo fmt --all && exit 1)'
language: system
types: [rust]
pass_filenames: false
stages: [commit]

# Rust linting with clippy
- id: clippy
name: Clippy
entry: bash -c 'cargo clippy --workspace --all-features -- -D warnings'
language: system
types: [rust]
pass_filenames: false
stages: [commit]

# Security checks
- repo: https://github.com/Yelp/detect-secrets
rev: v1.4.0
hooks:
- id: detect-secrets
name: Detect secrets
args: [--baseline, .secrets.baseline]
stages: [commit]

# Markdown linting
- repo: https://github.com/igorshubovych/markdownlint-cli
rev: v0.35.0
hooks:
- id: markdownlint
name: Markdown lint
stages: [commit]

# Spell checking for documentation
- repo: https://github.com/codespell-project/codespell
rev: v2.2.6
hooks:
- id: codespell
name: Codespell (spell checker)
args: [--ignore-words-list=crate, nd, sav]
stages: [commit]
exclude: |
(?x)^(
Cargo.lock|
gas_baseline.json|
gas_thresholds.json
)$

# Configuration for all hooks
default_language_version:
rust: stable

# Files to exclude
exclude: |
(?x)^(
\.git/|
target/|
Cargo.lock|
\.vscode/|
\.idea/|
node_modules/|
build/|
dist/
)$

# Default stages
default_stages: [commit]
113 changes: 113 additions & 0 deletions PRE_COMMIT_QUICK_START.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Pre-commit Hooks Quick Reference

## Installation

```bash
# One command to install everything
./scripts/setup-hooks.sh
```

That's it! The hooks are now installed and will run automatically before each commit.

## What Happens on `git commit`

1. **Pre-commit hook runs** (`.git/hooks/pre-commit`):
- ✓ Code formatting check with rustfmt
- ✓ Clippy linter runs on all code
- ✓ Merge conflict detection
- ✓ Debug statement warnings
- ✓ Script permission checking
- ✓ Cargo validation

2. **If all checks pass**: Commit proceeds
3. **If any check fails**: Commit is blocked, error details shown

## Commit Message Format

```bash
git commit -m "type(scope): description"
```

### Examples

✅ Good commit messages:
```
feat(insurance): add policy renewal calculation
fix(market): resolve order matching bug
docs: update API reference
refactor(tokenization): simplify token minting
perf: optimize escrow lookups
test(bridge): add cross-chain integration tests
chore: update dependencies
```

❌ Bad commit messages:
```
fixed stuff
update code
WIP
changed things
```

## Bypass Hooks (Not Recommended)

```bash
git commit --no-verify -m "Your message"
```

## Fix Common Issues

### Formatting fails
```bash
cargo fmt --all
git add .
git commit -m "type: message"
```

### Clippy warnings
```bash
cargo clippy --workspace --all-features
# Fix the issues shown
git commit -m "type: message"
```

### Commit message rejected
Reword to follow: `type(scope): description`

Example: `feat(insurance): add renewal logic`

## Files Included

| File | Purpose |
|------|---------|
| `.pre-commit-config.yaml` | Optional framework config (run `pre-commit install`) |
| `.git/hooks/pre-commit` | Main validation hook |
| `.git/hooks/commit-msg` | Commit message validator |
| `scripts/pre-commit` | Source script for pre-commit hook |
| `scripts/commit-msg` | Source script for commit-msg hook |
| `scripts/setup-hooks.sh` | Installation script |
| `docs/PRE_COMMIT_HOOKS_GUIDE.md` | Detailed documentation |

## Useful Commands

```bash
# Run pre-commit manually
.git/hooks/pre-commit

# Format all code
cargo fmt --all

# Run clippy
cargo clippy --workspace --all-features -- -D warnings

# Run tests
cargo test --workspace

# View hook files
cat .git/hooks/pre-commit
cat .git/hooks/commit-msg
```

## Need Help?

See [docs/PRE_COMMIT_HOOKS_GUIDE.md](../../docs/PRE_COMMIT_HOOKS_GUIDE.md) for detailed documentation and troubleshooting.
Loading
Loading