Skip to content

Commit 73da265

Browse files
committed
feat: add comprehensive code quality automation
- Add auto-fix.yml workflow for automatic code quality fixes - Create scripts/fix-code-quality.sh for local development - Improve CI feedback with clear instructions for developers - Add proper permissions and safety checks - Include [skip auto-fix] mechanism to prevent infinite loops
1 parent 80ab719 commit 73da265

File tree

3 files changed

+269
-15
lines changed

3 files changed

+269
-15
lines changed

.github/workflows/auto-fix.yml

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
name: Auto-fix Code Quality Issues
2+
3+
on:
4+
push:
5+
branches: [main, develop]
6+
workflow_dispatch: # Allow manual triggering
7+
8+
permissions:
9+
contents: write
10+
pull-requests: write
11+
12+
jobs:
13+
auto-fix:
14+
name: Auto-fix Formatting and Clippy Issues
15+
runs-on: ubuntu-latest
16+
if: "!contains(github.event.head_commit.message, '[skip auto-fix]')"
17+
18+
steps:
19+
- uses: actions/checkout@v4
20+
with:
21+
token: ${{ secrets.GITHUB_TOKEN }}
22+
fetch-depth: 0
23+
24+
- name: Install Rust
25+
uses: dtolnay/rust-toolchain@stable
26+
with:
27+
components: rustfmt, clippy
28+
29+
- name: Cache cargo registry
30+
uses: actions/cache@v3
31+
with:
32+
path: |
33+
~/.cargo/registry
34+
~/.cargo/git
35+
target
36+
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
37+
38+
- name: Check if fixes are needed
39+
id: check
40+
run: |
41+
echo "Running format check..."
42+
FORMAT_NEEDED=false
43+
if ! cargo fmt --all -- --check; then
44+
FORMAT_NEEDED=true
45+
echo "format_needed=true" >> $GITHUB_OUTPUT
46+
fi
47+
48+
echo "Running clippy check..."
49+
CLIPPY_NEEDED=false
50+
if ! cargo clippy --all-targets --all-features -- -D warnings; then
51+
CLIPPY_NEEDED=true
52+
echo "clippy_needed=true" >> $GITHUB_OUTPUT
53+
fi
54+
55+
if [ "$FORMAT_NEEDED" = true ] || [ "$CLIPPY_NEEDED" = true ]; then
56+
echo "fixes_needed=true" >> $GITHUB_OUTPUT
57+
else
58+
echo "fixes_needed=false" >> $GITHUB_OUTPUT
59+
fi
60+
61+
- name: Apply formatting fixes
62+
if: steps.check.outputs.format_needed == 'true'
63+
run: |
64+
echo "🔧 Applying formatting fixes..."
65+
cargo fmt --all
66+
67+
- name: Apply clippy fixes
68+
if: steps.check.outputs.clippy_needed == 'true'
69+
run: |
70+
echo "🔧 Applying clippy fixes..."
71+
cargo clippy --all-targets --all-features --fix --allow-dirty --allow-staged
72+
73+
- name: Commit and push fixes
74+
if: steps.check.outputs.fixes_needed == 'true'
75+
run: |
76+
git config --local user.email "41898282+github-actions[bot]@users.noreply.github.com"
77+
git config --local user.name "github-actions[bot]"
78+
79+
# Check if there are changes to commit
80+
if ! git diff --quiet; then
81+
git add .
82+
83+
# Create detailed commit message
84+
COMMIT_MSG="auto-fix: apply code quality fixes [skip auto-fix]"
85+
if [ "${{ steps.check.outputs.format_needed }}" = "true" ]; then
86+
COMMIT_MSG="$COMMIT_MSG
87+
88+
- Apply cargo fmt formatting"
89+
fi
90+
if [ "${{ steps.check.outputs.clippy_needed }}" = "true" ]; then
91+
COMMIT_MSG="$COMMIT_MSG
92+
- Apply clippy suggestions"
93+
fi
94+
95+
git commit -m "$COMMIT_MSG"
96+
git push
97+
98+
echo "✅ Code quality fixes applied and pushed!"
99+
else
100+
echo "ℹ️ No changes were made by the auto-fix tools."
101+
fi

.github/workflows/ci.yml

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,25 @@ jobs:
4242
target
4343
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}
4444

45-
- name: Check and apply formatting
45+
- name: Check formatting
4646
run: |
47-
cargo fmt --all -- --check || {
48-
echo "Code formatting issues detected. Applying fixes..."
49-
cargo fmt --all
50-
git config --local user.email "[email protected]"
51-
git config --local user.name "GitHub Action"
52-
git add .
53-
git diff --staged --quiet || git commit -m "auto-fix: apply cargo fmt formatting [skip ci]"
54-
}
47+
if ! cargo fmt --all -- --check; then
48+
echo "❌ Code formatting issues detected!"
49+
echo "Please run 'cargo fmt --all' to fix formatting."
50+
echo "Or add this to your pre-commit hook:"
51+
echo " cargo fmt --all"
52+
exit 1
53+
fi
5554
56-
- name: Run clippy with auto-fix
55+
- name: Run clippy
5756
run: |
58-
cargo clippy --all-targets --all-features --fix --allow-dirty -- -D warnings || {
59-
echo "Clippy issues detected. Applied available fixes."
60-
git add .
61-
git diff --staged --quiet || git commit -m "auto-fix: apply clippy suggestions [skip ci]"
62-
}
57+
if ! cargo clippy --all-targets --all-features -- -D warnings; then
58+
echo "❌ Clippy issues detected!"
59+
echo "Please run 'cargo clippy --all-targets --all-features --fix --allow-dirty' to fix issues."
60+
echo "Or add this to your development workflow:"
61+
echo " cargo clippy --fix --allow-dirty"
62+
exit 1
63+
fi
6364
6465
- name: Build
6566
run: cargo build --verbose

scripts/fix-code-quality.sh

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
#!/bin/bash
2+
# Code Quality Auto-Fix Script for Code Guardian
3+
# This script applies formatting and clippy fixes automatically
4+
5+
set -e
6+
7+
echo "🔧 Code Guardian - Auto-fix Code Quality Issues"
8+
echo "=============================================="
9+
10+
# Check if we're in a git repository
11+
if ! git rev-parse --git-dir > /dev/null 2>&1; then
12+
echo "❌ Error: Not in a git repository"
13+
exit 1
14+
fi
15+
16+
# Function to check if there are uncommitted changes
17+
check_git_status() {
18+
if ! git diff --quiet || ! git diff --staged --quiet; then
19+
echo "⚠️ Warning: You have uncommitted changes."
20+
echo " Consider committing or stashing them before running auto-fix."
21+
read -p " Continue anyway? (y/N): " -n 1 -r
22+
echo
23+
if [[ ! $REPLY =~ ^[Yy]$ ]]; then
24+
echo "❌ Aborted."
25+
exit 1
26+
fi
27+
fi
28+
}
29+
30+
# Function to apply formatting
31+
apply_formatting() {
32+
echo "🎨 Checking code formatting..."
33+
34+
if cargo fmt --all -- --check > /dev/null 2>&1; then
35+
echo "✅ Code formatting is already correct."
36+
return 0
37+
else
38+
echo "🔧 Applying formatting fixes..."
39+
cargo fmt --all
40+
echo "✅ Formatting applied."
41+
return 1
42+
fi
43+
}
44+
45+
# Function to apply clippy fixes
46+
apply_clippy() {
47+
echo "📎 Checking clippy issues..."
48+
49+
if cargo clippy --all-targets --all-features -- -D warnings > /dev/null 2>&1; then
50+
echo "✅ No clippy issues found."
51+
return 0
52+
else
53+
echo "🔧 Applying clippy fixes..."
54+
cargo clippy --all-targets --all-features --fix --allow-dirty --allow-staged
55+
echo "✅ Clippy fixes applied."
56+
return 1
57+
fi
58+
}
59+
60+
# Function to commit changes
61+
commit_changes() {
62+
if ! git diff --quiet; then
63+
echo "📝 Committing auto-fix changes..."
64+
git add .
65+
git commit -m "auto-fix: apply code quality fixes
66+
67+
- Apply cargo fmt formatting
68+
- Apply clippy suggestions
69+
70+
[automated commit]"
71+
echo "✅ Changes committed."
72+
else
73+
echo "ℹ️ No changes to commit."
74+
fi
75+
}
76+
77+
# Main execution
78+
main() {
79+
local format_changed=false
80+
local clippy_changed=false
81+
local auto_commit=false
82+
83+
# Parse command line arguments
84+
while [[ $# -gt 0 ]]; do
85+
case $1 in
86+
--commit|-c)
87+
auto_commit=true
88+
shift
89+
;;
90+
--help|-h)
91+
echo "Usage: $0 [OPTIONS]"
92+
echo ""
93+
echo "Options:"
94+
echo " --commit, -c Automatically commit changes"
95+
echo " --help, -h Show this help message"
96+
echo ""
97+
echo "This script applies cargo fmt and clippy --fix to improve code quality."
98+
exit 0
99+
;;
100+
*)
101+
echo "❌ Unknown option: $1"
102+
echo "Use --help for usage information."
103+
exit 1
104+
;;
105+
esac
106+
done
107+
108+
if [ "$auto_commit" = true ]; then
109+
check_git_status
110+
fi
111+
112+
# Apply fixes
113+
if ! apply_formatting; then
114+
format_changed=true
115+
fi
116+
117+
if ! apply_clippy; then
118+
clippy_changed=true
119+
fi
120+
121+
# Summary
122+
echo ""
123+
echo "📊 Summary:"
124+
if [ "$format_changed" = true ]; then
125+
echo " 🎨 Formatting: Fixed"
126+
else
127+
echo " 🎨 Formatting: Already correct"
128+
fi
129+
130+
if [ "$clippy_changed" = true ]; then
131+
echo " 📎 Clippy: Fixed"
132+
else
133+
echo " 📎 Clippy: No issues"
134+
fi
135+
136+
# Auto-commit if requested and there are changes
137+
if [ "$auto_commit" = true ] && ([ "$format_changed" = true ] || [ "$clippy_changed" = true ]); then
138+
echo ""
139+
commit_changes
140+
fi
141+
142+
echo ""
143+
echo "🎉 Code quality check complete!"
144+
145+
if [ "$format_changed" = true ] || [ "$clippy_changed" = true ]; then
146+
if [ "$auto_commit" = false ]; then
147+
echo "💡 Tip: Use --commit flag to automatically commit these changes."
148+
fi
149+
fi
150+
}
151+
152+
main "$@"

0 commit comments

Comments
 (0)