-
Notifications
You must be signed in to change notification settings - Fork 174
134 lines (116 loc) · 4.33 KB
/
unused-imports.yml
File metadata and controls
134 lines (116 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
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
});
}