|
| 1 | +name: Bash Linting with Auto-Fix Suggestions |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: |
| 6 | + - main |
| 7 | + - master |
| 8 | + - develop |
| 9 | + paths: |
| 10 | + - '**.sh' |
| 11 | + - '.github/workflows/bash-lint-advanced.yml' |
| 12 | + pull_request: |
| 13 | + branches: |
| 14 | + - main |
| 15 | + - master |
| 16 | + - develop |
| 17 | + paths: |
| 18 | + - '**.sh' |
| 19 | + workflow_dispatch: |
| 20 | + inputs: |
| 21 | + auto_fix: |
| 22 | + description: 'Attempt automatic fixes for common issues' |
| 23 | + required: false |
| 24 | + default: 'false' |
| 25 | + |
| 26 | +jobs: |
| 27 | + shellcheck-analysis: |
| 28 | + name: ShellCheck Analysis & Suggestions |
| 29 | + runs-on: ubuntu-latest |
| 30 | + permissions: |
| 31 | + contents: read |
| 32 | + pull-requests: write |
| 33 | + checks: write |
| 34 | + |
| 35 | + steps: |
| 36 | + - name: Checkout code |
| 37 | + uses: actions/checkout@v4 |
| 38 | + with: |
| 39 | + fetch-depth: 0 |
| 40 | + |
| 41 | + - name: Install tools |
| 42 | + run: | |
| 43 | + sudo apt-get update |
| 44 | + sudo apt-get install -y shellcheck |
| 45 | +
|
| 46 | + - name: Run ShellCheck with JSON output |
| 47 | + id: shellcheck |
| 48 | + continue-on-error: true |
| 49 | + run: | |
| 50 | + mkdir -p /tmp/lint |
| 51 | + |
| 52 | + # Run ShellCheck for all scripts |
| 53 | + find . -type f -name "*.sh" ! -path "./.git/*" ! -path "./node_modules/*" ! -path "./vendor/*" \ |
| 54 | + -exec shellcheck -f json {} + > /tmp/lint/results.json 2>&1 || true |
| 55 | + |
| 56 | + # Count issues |
| 57 | + ERRORS=$(jq '[.[] | select(.level=="error")] | length' /tmp/lint/results.json 2>/dev/null || echo "0") |
| 58 | + WARNINGS=$(jq '[.[] | select(.level=="warning")] | length' /tmp/lint/results.json 2>/dev/null || echo "0") |
| 59 | + |
| 60 | + echo "errors=$ERRORS" >> $GITHUB_OUTPUT |
| 61 | + echo "warnings=$WARNINGS" >> $GITHUB_OUTPUT |
| 62 | + |
| 63 | + cat /tmp/lint/results.json |
| 64 | +
|
| 65 | + - name: Parse and annotate issues |
| 66 | + if: always() |
| 67 | + run: | |
| 68 | + if [[ ! -f /tmp/lint/results.json ]]; then |
| 69 | + echo "No linting results found" |
| 70 | + exit 0 |
| 71 | + fi |
| 72 | + |
| 73 | + jq -r '.[] | "::notice file=\(.file),line=\(.line),col=\(.column)::[\(.code)] \(.message) - See: https://www.shellcheck.net/wiki/\(.code)"' /tmp/lint/results.json 2>/dev/null || true |
| 74 | +
|
| 75 | + - name: Generate suggestions markdown |
| 76 | + id: suggestions |
| 77 | + run: | |
| 78 | + { |
| 79 | + echo "# 🔍 Bash Linting Report" |
| 80 | + echo "" |
| 81 | + echo "**Errors**: ${{ steps.shellcheck.outputs.errors }}" |
| 82 | + echo "**Warnings**: ${{ steps.shellcheck.outputs.warnings }}" |
| 83 | + echo "" |
| 84 | + |
| 85 | + if [[ -f /tmp/lint/results.json ]]; then |
| 86 | + echo "## Issues Found:" |
| 87 | + echo "" |
| 88 | + |
| 89 | + jq -r '.[] | |
| 90 | + "### [\(.code)] \(.message)\n" + |
| 91 | + "- **File**: `\(.file)` (line \(.line), col \(.column))\n" + |
| 92 | + "- **Severity**: \(.level)\n" + |
| 93 | + "- **Wiki**: https://www.shellcheck.net/wiki/\(.code)\n"' /tmp/lint/results.json 2>/dev/null || echo "No issues parsed" |
| 94 | + fi |
| 95 | + } > /tmp/lint/suggestions.md |
| 96 | + |
| 97 | + cat /tmp/lint/suggestions.md |
| 98 | +
|
| 99 | + - name: Suggest fixes on PR |
| 100 | + if: github.event_name == 'pull_request' && (steps.shellcheck.outputs.errors > 0 || steps.shellcheck.outputs.warnings > 0) |
| 101 | + uses: actions/github-script@v8 |
| 102 | + with: |
| 103 | + script: | |
| 104 | + const fs = require('fs'); |
| 105 | + const results = JSON.parse(fs.readFileSync('/tmp/lint/results.json', 'utf8')); |
| 106 | + |
| 107 | + if (results.length === 0) return; |
| 108 | + |
| 109 | + // Group issues by file |
| 110 | + const issuesByFile = {}; |
| 111 | + results.forEach(issue => { |
| 112 | + if (!issuesByFile[issue.file]) { |
| 113 | + issuesByFile[issue.file] = []; |
| 114 | + } |
| 115 | + issuesByFile[issue.file].push(issue); |
| 116 | + }); |
| 117 | + |
| 118 | + // Create review with suggestions |
| 119 | + const reviewComments = []; |
| 120 | + Object.entries(issuesByFile).forEach(([file, issues]) => { |
| 121 | + issues.forEach(issue => { |
| 122 | + reviewComments.push({ |
| 123 | + path: file, |
| 124 | + line: issue.line, |
| 125 | + side: 'RIGHT', |
| 126 | + body: `**[SC${issue.code}]** ${issue.message}\n\n[📖 Read more](https://www.shellcheck.net/wiki/${issue.code})` |
| 127 | + }); |
| 128 | + }); |
| 129 | + }); |
| 130 | + |
| 131 | + if (reviewComments.length > 0) { |
| 132 | + github.rest.pulls.createReview({ |
| 133 | + owner: context.repo.owner, |
| 134 | + repo: context.repo.repo, |
| 135 | + pull_number: context.issue.number, |
| 136 | + event: 'COMMENT', |
| 137 | + comments: reviewComments.slice(0, 50) // GitHub limit: 50 comments per review |
| 138 | + }).catch(err => { |
| 139 | + console.log('Note: Could not create inline review (file may not be in diff)'); |
| 140 | + console.log(err.message); |
| 141 | + }); |
| 142 | + } |
| 143 | +
|
| 144 | + summary: |
| 145 | + name: Linting Summary |
| 146 | + runs-on: ubuntu-latest |
| 147 | + needs: [shellcheck-analysis] |
| 148 | + if: always() |
| 149 | + |
| 150 | + steps: |
| 151 | + - name: Create summary |
| 152 | + run: | |
| 153 | + echo "## 📊 Bash Linting Summary" >> $GITHUB_STEP_SUMMARY |
| 154 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 155 | + echo "**Errors**: ${{ needs.shellcheck-analysis.outputs.errors }}" >> $GITHUB_STEP_SUMMARY |
| 156 | + echo "**Warnings**: ${{ needs.shellcheck-analysis.outputs.warnings }}" >> $GITHUB_STEP_SUMMARY |
| 157 | + echo "" >> $GITHUB_STEP_SUMMARY |
| 158 | + echo "### Next Steps:" >> $GITHUB_STEP_SUMMARY |
| 159 | + echo "1. Review issues in workflow annotations" >> $GITHUB_STEP_SUMMARY |
| 160 | + echo "2. Check PR comments for detailed suggestions" >> $GITHUB_STEP_SUMMARY |
| 161 | + echo "3. Follow linked wiki pages for explanations" >> $GITHUB_STEP_SUMMARY |
| 162 | + echo "4. Create PR with fixes" >> $GITHUB_STEP_SUMMARY |
0 commit comments