forked from jordanlambrecht/tracker-tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
188 lines (164 loc) · 6.65 KB
/
tracker-validation.yml
File metadata and controls
188 lines (164 loc) · 6.65 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
name: Tracker Validation
on:
pull_request:
branches: [main, development]
permissions:
contents: read
pull-requests: write
issues: write
jobs:
validate:
name: Tracker Registry
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v6
- uses: pnpm/action-setup@v5
- uses: actions/setup-node@v6
with:
node-version: 20
cache: pnpm
- name: Install dependencies
run: pnpm install --frozen-lockfile
- name: Detect changed tracker files
id: changed-trackers
run: |
FILES=$(gh api "repos/${{ github.repository }}/pulls/$PR_NUMBER/files" --paginate --jq '.[].filename' | grep '^src/data/trackers/.*\.ts$' | grep -v 'index\.ts$' || true)
if [ -z "$FILES" ]; then
echo "has_tracker_changes=false" >> "$GITHUB_OUTPUT"
echo "No tracker files changed."
else
SLUGS=""
for f in $FILES; do
SLUG=$(basename "$f" .ts)
SLUGS="$SLUGS $SLUG"
done
SLUGS=$(echo "$SLUGS" | xargs)
echo "has_tracker_changes=true" >> "$GITHUB_OUTPUT"
echo "slugs=$SLUGS" >> "$GITHUB_OUTPUT"
echo "Changed trackers: $SLUGS"
fi
env:
GH_TOKEN: ${{ github.token }}
PR_NUMBER: ${{ github.event.pull_request.number }}
- name: Validate changed trackers
if: steps.changed-trackers.outputs.has_tracker_changes == 'true'
id: tracker-validation
continue-on-error: true
run: |
pnpm exec tsx scripts/validate-trackers.ts $SLUGS > /tmp/tracker-results.json
echo "exit_code=$?" >> "$GITHUB_OUTPUT"
cat /tmp/tracker-results.json
env:
SLUGS: ${{ steps.changed-trackers.outputs.slugs }}
- name: Comment on PR with tracker validation results
if: steps.changed-trackers.outputs.has_tracker_changes == 'true'
uses: actions/github-script@v8
env:
VALIDATION_JSON: ${{ steps.changed-trackers.outputs.slugs }}
with:
script: |
const fs = require('fs');
let results;
try {
const raw = fs.readFileSync('/tmp/tracker-results.json', 'utf8');
results = JSON.parse(raw).results;
} catch {
console.log('No validation results to comment on.');
return;
}
const nonDraft = results.filter(r => !r.draft);
const hasErrors = nonDraft.some(r => r.errors.length > 0);
const hasWarnings = nonDraft.some(r => r.warnings.length > 0);
const marker = '<!-- tracker-registry-validation -->';
const prNumber = context.payload.pull_request?.number ?? context.issue.number;
async function findPreviousComments() {
const { data: comments } = await github.rest.issues.listComments({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
});
return comments.filter(c =>
c.user?.login === 'github-actions[bot]' &&
c.body?.includes(marker)
);
}
async function deleteAllPreviousComments() {
const existing = await findPreviousComments();
for (const c of existing) {
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: c.id,
});
}
}
if (nonDraft.length === 0 || (!hasErrors && !hasWarnings)) {
await deleteAllPreviousComments();
return;
}
const icon = hasErrors ? '🚫' : '⚠️';
const title = hasErrors
? `${icon} Tracker registry validation failed`
: `${icon} Tracker registry warnings`;
const lines = [`**${title}**`, ''];
for (const r of nonDraft) {
if (r.errors.length === 0 && r.warnings.length === 0) continue;
if (r.errors.length > 0) {
lines.push(`### ${r.name} (\`${r.slug}\`)`);
lines.push('');
lines.push('| Field | Status |');
lines.push('|---|---|');
for (const e of r.errors) {
lines.push(`| ${e} | ❌ Error |`);
}
lines.push('');
}
if (r.warnings.length > 0) {
const heading = r.errors.length > 0
? `Warnings for ${r.name}`
: `${r.name} (\`${r.slug}\`) — Warnings`;
lines.push(`<details>`);
lines.push(`<summary>${heading} (${r.warnings.length})</summary>`);
lines.push('');
lines.push('| Field | Status |');
lines.push('|---|---|');
for (const w of r.warnings) {
lines.push(`| ${w} | ⚠️ Warning |`);
}
lines.push('');
lines.push('</details>');
lines.push('');
}
}
lines.push('---');
lines.push('');
lines.push('Allowed content categories: `Movies`, `TV`, `Music`, `Games`, `Apps`, `Sports`, `Books`, `Audiobooks`, `Comics`, `Manga`, `Anime`, `XXX`, `Documentaries`, `Education`, `Tutorials`, `Fanres`');
lines.push('');
lines.push('See `src/data/__tests__/tracker-registry.test.ts` for all validation rules.');
const body = `${marker}\n${lines.join('\n')}`;
const existing = await findPreviousComments();
if (existing.length > 0) {
await github.rest.issues.updateComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: existing[0].id,
body,
});
for (const c of existing.slice(1)) {
await github.rest.issues.deleteComment({
owner: context.repo.owner,
repo: context.repo.repo,
comment_id: c.id,
});
}
} else {
await github.rest.issues.createComment({
owner: context.repo.owner,
repo: context.repo.repo,
issue_number: prNumber,
body,
});
}
- name: Fail if tracker validation has errors
if: steps.changed-trackers.outputs.has_tracker_changes == 'true' && steps.tracker-validation.outcome == 'failure'
run: exit 1