Skip to content

Add unused import detection workflow #4

Add unused import detection workflow

Add unused import detection workflow #4

Workflow file for this run

name: CI / Unused Imports Check
on:
pull_request:
branches: [main]
workflow_dispatch:
jobs:
check-unused-imports:
runs-on: ubuntu-latest
permissions:
contents: read
pull-requests: write
steps:
- name: Checkout PR code
uses: actions/checkout@v3
with:
fetch-depth: 0
- name: Setup Zig
uses: mlugg/setup-zig@v2
with:
version: 0.15.1
- name: Build zigimports
run: |
git clone --depth 1 https://github.com/tusharsadhwani/zigimports.git /tmp/zigimports
cd /tmp/zigimports
zig build --release=safe
# Binary is named zigimports-<arch>-<os>, find and symlink it
ln -s /tmp/zigimports/zig-out/bin/zigimports-* /tmp/zigimports/zig-out/bin/zigimports
- name: Get changed Zig files
id: changed-files
run: |
FILES=$(git diff --name-only --diff-filter=d ${{ github.event.pull_request.base.sha }} ${{ github.event.pull_request.head.sha }} | grep '\.zig$' || true)
echo "files<<EOF" >> $GITHUB_OUTPUT
echo "$FILES" >> $GITHUB_OUTPUT
echo "EOF" >> $GITHUB_OUTPUT
- name: Check for unused imports
id: check
run: |
RESULTS=""
echo "${{ steps.changed-files.outputs.files }}" | while read file; do
if [ -n "$file" ] && [ -f "$file" ]; then
/tmp/zigimports/zig-out/bin/zigimports "$file" 2>&1 || true
fi
done | tee unused_imports.txt
# Set output for later steps
if grep -q "is unused" unused_imports.txt 2>/dev/null; then
echo "has_issues=true" >> $GITHUB_OUTPUT
else
echo "has_issues=false" >> $GITHUB_OUTPUT
fi
- name: Post PR comment
if: steps.check.outputs.has_issues == 'true'
uses: actions/github-script@v7
with:
script: |
const fs = require('fs');
const content = fs.readFileSync('unused_imports.txt', 'utf8').trim();
const lines = content.split('\n').filter(l => l.includes('is unused'));
if (lines.length === 0) return;
// Check for existing comment and update it
const marker = '<!-- unused-imports-check -->';
const comments = await github.paginate(
github.rest.issues.listComments,
{
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number
}
);
const existing = comments.find(c =>
c.user.login === 'github-actions[bot]' &&
c.body.includes(marker)
);
let body = '## ⚠️ Unused Imports Found\n\n';
body += '```\n' + lines.join('\n') + '\n```\n\n';
body += 'Run `zigimports --fix <file>` locally to automatically remove unused imports.\n\n';
body += marker;
if (existing) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id,
body: body
});
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number,
body: body
});
}
- name: Remove comment if no issues
if: steps.check.outputs.has_issues == 'false'
uses: actions/github-script@v7
with:
script: |
const marker = '<!-- unused-imports-check -->';
const comments = await github.paginate(
github.rest.issues.listComments,
{
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: context.issue.number
}
);
const existing = comments.find(c =>
c.user.login === 'github-actions[bot]' &&
c.body.includes(marker)
);
if (existing) {
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing.id
});
}