|
| 1 | +name: CI / Unused Imports Check |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [main] |
| 6 | + workflow_dispatch: |
| 7 | + |
| 8 | +jobs: |
| 9 | + check-unused-imports: |
| 10 | + runs-on: ubuntu-latest |
| 11 | + permissions: |
| 12 | + contents: read |
| 13 | + pull-requests: write |
| 14 | + |
| 15 | + steps: |
| 16 | + - name: Checkout PR code |
| 17 | + uses: actions/checkout@v3 |
| 18 | + with: |
| 19 | + fetch-depth: 0 |
| 20 | + |
| 21 | + - name: Setup Zig |
| 22 | + uses: mlugg/setup-zig@v2 |
| 23 | + with: |
| 24 | + version: 0.15.1 |
| 25 | + |
| 26 | + - name: Build zigimports |
| 27 | + run: | |
| 28 | + git clone --depth 1 https://github.com/tusharsadhwani/zigimports.git /tmp/zigimports |
| 29 | + cd /tmp/zigimports |
| 30 | + zig build --release=safe |
| 31 | + # Binary is named zigimports-<arch>-<os>, find and symlink it |
| 32 | + ln -s /tmp/zigimports/zig-out/bin/zigimports-* /tmp/zigimports/zig-out/bin/zigimports |
| 33 | +
|
| 34 | + - name: Get changed Zig files |
| 35 | + id: changed-files |
| 36 | + run: | |
| 37 | + FILES=$(git diff --name-only --diff-filter=d ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | grep '\.zig$' || true) |
| 38 | + echo "files<<EOF" >> $GITHUB_OUTPUT |
| 39 | + echo "$FILES" >> $GITHUB_OUTPUT |
| 40 | + echo "EOF" >> $GITHUB_OUTPUT |
| 41 | +
|
| 42 | + - name: Check for unused imports |
| 43 | + id: check |
| 44 | + run: | |
| 45 | + RESULTS="" |
| 46 | + echo "${{ steps.changed-files.outputs.files }}" | while read file; do |
| 47 | + if [ -n "$file" ] && [ -f "$file" ]; then |
| 48 | + /tmp/zigimports/zig-out/bin/zigimports "$file" 2>&1 || true |
| 49 | + fi |
| 50 | + done | tee unused_imports.txt |
| 51 | +
|
| 52 | + # Set output for later steps |
| 53 | + if grep -q "is unused" unused_imports.txt 2>/dev/null; then |
| 54 | + echo "has_issues=true" >> $GITHUB_OUTPUT |
| 55 | + else |
| 56 | + echo "has_issues=false" >> $GITHUB_OUTPUT |
| 57 | + fi |
| 58 | +
|
| 59 | + - name: Post PR comment |
| 60 | + if: steps.check.outputs.has_issues == 'true' |
| 61 | + uses: actions/github-script@v7 |
| 62 | + with: |
| 63 | + script: | |
| 64 | + const fs = require('fs'); |
| 65 | +
|
| 66 | + const content = fs.readFileSync('unused_imports.txt', 'utf8').trim(); |
| 67 | + const lines = content.split('\n').filter(l => l.includes('is unused')); |
| 68 | +
|
| 69 | + if (lines.length === 0) return; |
| 70 | +
|
| 71 | + // Check for existing comment and update it |
| 72 | + const marker = '<!-- unused-imports-check -->'; |
| 73 | + const comments = await github.paginate( |
| 74 | + github.rest.issues.listComments, |
| 75 | + { |
| 76 | + owner: context.repo.owner, |
| 77 | + repo: context.repo.repo, |
| 78 | + issue_number: context.issue.number |
| 79 | + } |
| 80 | + ); |
| 81 | +
|
| 82 | + const existing = comments.find(c => |
| 83 | + c.user.login === 'github-actions[bot]' && |
| 84 | + c.body.includes(marker) |
| 85 | + ); |
| 86 | +
|
| 87 | + let body = '## ⚠️ Unused Imports Found\n\n'; |
| 88 | + body += '```\n' + lines.join('\n') + '\n```\n\n'; |
| 89 | + body += 'Run `zigimports --fix <file>` locally to automatically remove unused imports.\n\n'; |
| 90 | + body += marker; |
| 91 | +
|
| 92 | + if (existing) { |
| 93 | + await github.rest.issues.updateComment({ |
| 94 | + owner: context.repo.owner, |
| 95 | + repo: context.repo.repo, |
| 96 | + comment_id: existing.id, |
| 97 | + body: body |
| 98 | + }); |
| 99 | + } else { |
| 100 | + await github.rest.issues.createComment({ |
| 101 | + owner: context.repo.owner, |
| 102 | + repo: context.repo.repo, |
| 103 | + issue_number: context.issue.number, |
| 104 | + body: body |
| 105 | + }); |
| 106 | + } |
| 107 | +
|
| 108 | + - name: Remove comment if no issues |
| 109 | + if: steps.check.outputs.has_issues == 'false' |
| 110 | + uses: actions/github-script@v7 |
| 111 | + with: |
| 112 | + script: | |
| 113 | + const marker = '<!-- unused-imports-check -->'; |
| 114 | + const comments = await github.paginate( |
| 115 | + github.rest.issues.listComments, |
| 116 | + { |
| 117 | + owner: context.repo.owner, |
| 118 | + repo: context.repo.repo, |
| 119 | + issue_number: context.issue.number |
| 120 | + } |
| 121 | + ); |
| 122 | +
|
| 123 | + const existing = comments.find(c => |
| 124 | + c.user.login === 'github-actions[bot]' && |
| 125 | + c.body.includes(marker) |
| 126 | + ); |
| 127 | +
|
| 128 | + if (existing) { |
| 129 | + await github.rest.issues.deleteComment({ |
| 130 | + owner: context.repo.owner, |
| 131 | + repo: context.repo.repo, |
| 132 | + comment_id: existing.id |
| 133 | + }); |
| 134 | + } |
0 commit comments