@tisankan/slop-check 0.0.0-stage → 1.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +223 -2
- package/action.yml +29 -0
- package/bin/slop-check.js +147 -0
- package/package.json +44 -4
- package/src/action-main.js +9 -0
- package/src/action.js +203 -0
- package/src/check.js +136 -0
- package/src/comments.js +53 -0
- package/src/config.js +133 -0
- package/src/diff.js +65 -0
- package/src/git.js +166 -0
- package/src/languages.js +54 -0
- package/src/report.js +105 -0
- package/src/rules.js +417 -0
- package/src/run.js +64 -0
- package/src/score.js +40 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Tisankan Jeyakumar
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,224 @@
|
|
|
1
|
-
#
|
|
1
|
+
# slop-check
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
[](https://github.com/tisankanj/slop-check)
|
|
4
|
+
[](https://github.com/tisankanj/slop-check/actions/workflows/ci.yml)
|
|
5
|
+
[](https://www.npmjs.com/package/@tisankan/slop-check)
|
|
6
|
+
[](LICENSE)
|
|
7
|
+
|
|
8
|
+
**Catch AI slop in your diff before your reviewer does.**
|
|
9
|
+
|
|
10
|
+
AI writes code fast, and it leaves things behind: swallowed errors,
|
|
11
|
+
`// ... existing code ...` pasted from a chat, comments that narrate the edit,
|
|
12
|
+
`as any`, debug logs. slop-check reads only the lines you added, flags those
|
|
13
|
+
leftovers with a one-line reason, and gives the change one number: the slop score.
|
|
14
|
+
|
|
15
|
+

|
|
16
|
+
|
|
17
|
+
<details>
|
|
18
|
+
<summary>The 16 lines behind this screenshot</summary>
|
|
19
|
+
|
|
20
|
+
```ts
|
|
21
|
+
import { db } from './db';
|
|
22
|
+
|
|
23
|
+
// Get user by id
|
|
24
|
+
export async function getUserById(id: string) {
|
|
25
|
+
try {
|
|
26
|
+
const user = await db.users.find(id);
|
|
27
|
+
console.log('user:', user);
|
|
28
|
+
return user;
|
|
29
|
+
} catch (error) {}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
// Updated to use the new cache API
|
|
33
|
+
export function saveUser(user: any) {
|
|
34
|
+
// ... existing code ...
|
|
35
|
+
console.log('✅ User saved');
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// In a real application you would rotate this key
|
|
39
|
+
const apiKey = 'your-api-key-here';
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Commit message: `feat: supercharge user caching`. <!-- slop-check-ignore -->
|
|
43
|
+
|
|
44
|
+
</details>
|
|
45
|
+
|
|
46
|
+
## Try it
|
|
47
|
+
|
|
48
|
+
Run it inside any git repo, on any branch. No config, no dependencies, no API keys.
|
|
49
|
+
|
|
50
|
+
```bash
|
|
51
|
+
npx @tisankan/slop-check
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
It compares your branch with `main` (or `master`, or `origin/HEAD`) and includes
|
|
55
|
+
uncommitted changes to tracked files. New files count once you `git add` them.
|
|
56
|
+
Needs Node.js 20 or newer. To install it once: `npm i -g @tisankan/slop-check`,
|
|
57
|
+
then run `slop-check`.
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
npx @tisankan/slop-check --all # every tracked file
|
|
61
|
+
npx @tisankan/slop-check --staged # staged changes, for a git hook
|
|
62
|
+
npx @tisankan/slop-check develop # compare with another base
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
## What's your repo's slop score?
|
|
66
|
+
|
|
67
|
+
```bash
|
|
68
|
+
npx @tisankan/slop-check --all --badge
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
That prints a badge for your README, like the one at the top of this page:
|
|
72
|
+
|
|
73
|
+
```markdown
|
|
74
|
+
[](https://github.com/tisankanj/slop-check)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
| Score | Label |
|
|
78
|
+
| --- | --- |
|
|
79
|
+
| 0 | Hand-written |
|
|
80
|
+
| 1 to 10 | Mostly human |
|
|
81
|
+
| 11 to 30 | Needs a once-over |
|
|
82
|
+
| 31 to 60 | Sloppy |
|
|
83
|
+
| 61 to 100 | Pure slop |
|
|
84
|
+
|
|
85
|
+
The score is slop points per 100 added lines, capped at 100. A high finding is
|
|
86
|
+
worth 5 points, a medium one 3, a low one 1. Changes under 20 lines count as 20,
|
|
87
|
+
so one small slip does not score like a disaster.
|
|
88
|
+
|
|
89
|
+
## Check every pull request
|
|
90
|
+
|
|
91
|
+
```yaml
|
|
92
|
+
# .github/workflows/slop-check.yml
|
|
93
|
+
name: slop-check
|
|
94
|
+
on: pull_request
|
|
95
|
+
|
|
96
|
+
permissions:
|
|
97
|
+
contents: read
|
|
98
|
+
pull-requests: write
|
|
99
|
+
|
|
100
|
+
jobs:
|
|
101
|
+
slop-check:
|
|
102
|
+
runs-on: ubuntu-latest
|
|
103
|
+
steps:
|
|
104
|
+
- uses: actions/checkout@v7
|
|
105
|
+
with:
|
|
106
|
+
fetch-depth: 0
|
|
107
|
+
- uses: tisankanj/slop-check@v1
|
|
108
|
+
with:
|
|
109
|
+
max-score: 30
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
The action writes the report to the job summary, prints inline annotations on
|
|
113
|
+
the diff, and keeps one comment on the pull request up to date.
|
|
114
|
+
|
|
115
|
+
| Input | Default | What it does |
|
|
116
|
+
| --- | --- | --- |
|
|
117
|
+
| `max-score` | empty | Fail the job when the score is above this number. Empty never fails. |
|
|
118
|
+
| `comment` | `true` | Post one PR comment and update it on every push. |
|
|
119
|
+
| `annotations` | `true` | Show findings as inline annotations on the diff. |
|
|
120
|
+
| `github-token` | `github.token` | Token used for the comment. |
|
|
121
|
+
|
|
122
|
+
Output: `score`. Pull requests from forks get a read-only token, so they get the
|
|
123
|
+
job summary but no comment.
|
|
124
|
+
|
|
125
|
+
## Rules
|
|
126
|
+
|
|
127
|
+
| Rule | Weight | Catches |
|
|
128
|
+
| --- | --- | --- |
|
|
129
|
+
| `empty-catch` | high | `catch (e) {}`, `except: pass`, `if err != nil {}` and empty Ruby `rescue` |
|
|
130
|
+
| `placeholder-code` | high | `// ... existing code ...`, `// TODO: implement` |
|
|
131
|
+
| `catch-rethrow` | medium | A catch block that only rethrows |
|
|
132
|
+
| `debug-leftover` | medium | `debugger`, `console.log('user:', user)`, `breakpoint()` |
|
|
133
|
+
| `type-escape` | medium | `as any`, `: any`, `@ts-ignore`, bare `# type: ignore` |
|
|
134
|
+
| `change-history-comment` | medium | `// Updated to use ...`, `// Added error handling` |
|
|
135
|
+
| `narration-comment` | medium | `// Let me ...`, `// I've added ...`, `// As requested` |
|
|
136
|
+
| `disclaimer-comment` | medium | `// In a real application you would ...` |
|
|
137
|
+
| `placeholder-value` | medium | `'your-api-key-here'`, `YOUR_API_KEY`, `REPLACE_ME` |
|
|
138
|
+
| `restating-comment` | low | `// get user by id` above `getUserById(id)` |
|
|
139
|
+
| `emoji-in-code` | low | Emoji in log calls and at the start of comments |
|
|
140
|
+
| `buzzword` | low | Brochure phrases in comments, docs and commit messages |
|
|
141
|
+
| `em-dash` | low | The em dash character. **Off by default.** |
|
|
142
|
+
| `ai-attribution` | high | Tool credit lines in commits. **Off by default.** |
|
|
143
|
+
|
|
144
|
+
Every rule, with bad and good examples: [docs/rules.md](docs/rules.md).
|
|
145
|
+
|
|
146
|
+
## Quiet on human code
|
|
147
|
+
|
|
148
|
+
Before release, every rule ran against express, requests, cobra and ky: about
|
|
149
|
+
83,000 lines of code written by people. Any rule that fired on normal human code
|
|
150
|
+
more than once in ten was narrowed or switched off. On those four repos the
|
|
151
|
+
default rules now report 23 findings in total, almost all of them real restating
|
|
152
|
+
comments and real `as any` casts.
|
|
153
|
+
|
|
154
|
+
If slop-check flags something a careful person wrote on purpose, that is a bug.
|
|
155
|
+
Please [open an issue](https://github.com/tisankanj/slop-check/issues) with the line.
|
|
156
|
+
|
|
157
|
+
## Anti-slop, not anti-AI
|
|
158
|
+
|
|
159
|
+
slop-check does not try to guess whether AI wrote your code, and it does not care.
|
|
160
|
+
It flags leftovers a reviewer would ask you to remove, whoever typed them. That is
|
|
161
|
+
also why AI credit lines in commits are off by default: many teams require them.
|
|
162
|
+
|
|
163
|
+
## Config
|
|
164
|
+
|
|
165
|
+
Optional `.slopcheckrc.json` in the repo root. Command line flags win over it.
|
|
166
|
+
|
|
167
|
+
```json
|
|
168
|
+
{
|
|
169
|
+
"rules": { "em-dash": "on", "restating-comment": "off" },
|
|
170
|
+
"ignore": ["docs/legacy/**", "**/*.generated.ts"],
|
|
171
|
+
"maxScore": 30
|
|
172
|
+
}
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
`ignore` takes `**`, `*` and `?`. Dependencies, build output, lockfiles and
|
|
176
|
+
minified files are skipped already. Add `slop-check-ignore` anywhere on a line to
|
|
177
|
+
skip that line.
|
|
178
|
+
|
|
179
|
+
## Git hook
|
|
180
|
+
|
|
181
|
+
```bash
|
|
182
|
+
# .git/hooks/pre-commit
|
|
183
|
+
npx --yes @tisankan/slop-check --staged --max-score 30
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
## All options
|
|
187
|
+
|
|
188
|
+
```text
|
|
189
|
+
slop-check [base] check the current branch against base
|
|
190
|
+
--staged check staged changes only
|
|
191
|
+
--all check every tracked file
|
|
192
|
+
--json print JSON instead of text
|
|
193
|
+
--badge print a README badge for this score
|
|
194
|
+
--max-score <n> exit 1 if the score is above n
|
|
195
|
+
--list-rules print every rule with its weight and status
|
|
196
|
+
--no-color plain output (NO_COLOR is also respected)
|
|
197
|
+
-h, --help show help
|
|
198
|
+
-v, --version show the version
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
Exit codes: `0` done, `1` score above `--max-score`, `2` usage or git error.
|
|
202
|
+
|
|
203
|
+
`--json` prints `{ score, label, base, mode, files, addedLines, findings }`. Each
|
|
204
|
+
finding has `rule`, `weight`, `message`, and either `file` and `line` or `commit`.
|
|
205
|
+
|
|
206
|
+
## Contributing
|
|
207
|
+
|
|
208
|
+
False positives are the most useful thing you can report.
|
|
209
|
+
[CONTRIBUTING.md](CONTRIBUTING.md) explains how to run the tests and add a rule,
|
|
210
|
+
and there are [good first issues](https://github.com/tisankanj/slop-check/labels/good%20first%20issue)
|
|
211
|
+
ready to pick up.
|
|
212
|
+
|
|
213
|
+
A new rule is one object in [src/rules.js](src/rules.js), a test file with
|
|
214
|
+
"flags" and "does not flag" cases, a section in [docs/rules.md](docs/rules.md), and
|
|
215
|
+
a run against a few human-written repos to show it stays quiet. `node --test` runs
|
|
216
|
+
everything. Zero dependencies is on purpose; please keep it that way.
|
|
217
|
+
|
|
218
|
+
This repo checks itself on every pull request with `max-score: 0`. Its test
|
|
219
|
+
fixtures and `docs/rules.md` are full of slop on purpose, so
|
|
220
|
+
[.slopcheckrc.json](.slopcheckrc.json) ignores them.
|
|
221
|
+
|
|
222
|
+
## License
|
|
223
|
+
|
|
224
|
+
[MIT](LICENSE). Made by [Tisankan Jeyakumar](https://github.com/tisankanj).
|
package/action.yml
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
name: slop-check
|
|
2
|
+
description: Catch AI slop in your diff before your reviewer does.
|
|
3
|
+
author: Tisankan Jeyakumar
|
|
4
|
+
branding:
|
|
5
|
+
icon: filter
|
|
6
|
+
color: green
|
|
7
|
+
inputs:
|
|
8
|
+
max-score:
|
|
9
|
+
description: Fail the job when the slop score is above this number (0 to 100). Leave empty to never fail.
|
|
10
|
+
required: false
|
|
11
|
+
default: ''
|
|
12
|
+
comment:
|
|
13
|
+
description: Post one PR comment with the score and keep it updated. Set to false to only write the job summary.
|
|
14
|
+
required: false
|
|
15
|
+
default: 'true'
|
|
16
|
+
annotations:
|
|
17
|
+
description: Print inline annotations for findings on the PR diff. Set to false to disable.
|
|
18
|
+
required: false
|
|
19
|
+
default: 'true'
|
|
20
|
+
github-token:
|
|
21
|
+
description: Token used to post the PR comment.
|
|
22
|
+
required: false
|
|
23
|
+
default: ${{ github.token }}
|
|
24
|
+
outputs:
|
|
25
|
+
score:
|
|
26
|
+
description: The slop score, from 0 to 100.
|
|
27
|
+
runs:
|
|
28
|
+
using: node24
|
|
29
|
+
main: src/action-main.js
|
|
@@ -0,0 +1,147 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import { readFileSync } from 'node:fs';
|
|
3
|
+
import { parseArgs } from 'node:util';
|
|
4
|
+
import { enabledRules, loadConfig } from '../src/config.js';
|
|
5
|
+
import { findRepoRoot, SlopCheckError } from '../src/git.js';
|
|
6
|
+
import { formatJson, formatText } from '../src/report.js';
|
|
7
|
+
import { RULES } from '../src/rules.js';
|
|
8
|
+
import { runCheck } from '../src/run.js';
|
|
9
|
+
import { badgeMarkdown } from '../src/score.js';
|
|
10
|
+
|
|
11
|
+
const HELP = `Usage: slop-check [base] [options]
|
|
12
|
+
|
|
13
|
+
Checks the lines your branch added for leftovers of unreviewed AI output.
|
|
14
|
+
|
|
15
|
+
slop-check check the current branch against main
|
|
16
|
+
slop-check <base> check the current branch against <base>
|
|
17
|
+
|
|
18
|
+
Options:
|
|
19
|
+
--staged check staged changes only
|
|
20
|
+
--all check every tracked file
|
|
21
|
+
--json print JSON instead of text
|
|
22
|
+
--badge print a README badge for this score
|
|
23
|
+
--max-score <n> exit 1 if the score is above n
|
|
24
|
+
--list-rules print every rule with its weight and status
|
|
25
|
+
--no-color plain output (NO_COLOR is also respected)
|
|
26
|
+
-h, --help show this help
|
|
27
|
+
-v, --version show the version
|
|
28
|
+
`;
|
|
29
|
+
|
|
30
|
+
const OPTIONS = {
|
|
31
|
+
staged: { type: 'boolean' },
|
|
32
|
+
all: { type: 'boolean' },
|
|
33
|
+
json: { type: 'boolean' },
|
|
34
|
+
badge: { type: 'boolean' },
|
|
35
|
+
'max-score': { type: 'string' },
|
|
36
|
+
'list-rules': { type: 'boolean' },
|
|
37
|
+
'no-color': { type: 'boolean' },
|
|
38
|
+
help: { type: 'boolean', short: 'h' },
|
|
39
|
+
version: { type: 'boolean', short: 'v' },
|
|
40
|
+
};
|
|
41
|
+
|
|
42
|
+
function weightName(weight) {
|
|
43
|
+
if (weight >= 5) {
|
|
44
|
+
return 'high';
|
|
45
|
+
}
|
|
46
|
+
return weight >= 3 ? 'medium' : 'low';
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function readVersion() {
|
|
50
|
+
const packageJson = JSON.parse(readFileSync(new URL('../package.json', import.meta.url), 'utf8'));
|
|
51
|
+
return packageJson.version;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// Outside a repo there is no config file, but --list-rules should still work.
|
|
55
|
+
function loadConfigIfInRepo() {
|
|
56
|
+
try {
|
|
57
|
+
return loadConfig(findRepoRoot(process.cwd()));
|
|
58
|
+
} catch {
|
|
59
|
+
return loadConfig(process.cwd());
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function listRules() {
|
|
64
|
+
const enabled = enabledRules(loadConfigIfInRepo());
|
|
65
|
+
const rows = [];
|
|
66
|
+
for (const rule of RULES) {
|
|
67
|
+
const status = enabled.has(rule.id) ? 'on' : 'off';
|
|
68
|
+
rows.push(`${rule.id.padEnd(24)}${weightName(rule.weight).padEnd(8)}${status.padEnd(5)}${rule.message}`);
|
|
69
|
+
}
|
|
70
|
+
return `${rows.join('\n')}\n`;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function parseMaxScore(value) {
|
|
74
|
+
if (value === undefined) {
|
|
75
|
+
return null;
|
|
76
|
+
}
|
|
77
|
+
const number = Number(value);
|
|
78
|
+
if (!Number.isInteger(number) || number < 0 || number > 100) {
|
|
79
|
+
throw new SlopCheckError('--max-score must be a whole number from 0 to 100');
|
|
80
|
+
}
|
|
81
|
+
return number;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
function main() {
|
|
85
|
+
let parsed;
|
|
86
|
+
try {
|
|
87
|
+
parsed = parseArgs({ options: OPTIONS, allowPositionals: true, strict: true });
|
|
88
|
+
} catch (error) {
|
|
89
|
+
throw new SlopCheckError(`${error.message}. Run slop-check --help.`);
|
|
90
|
+
}
|
|
91
|
+
const flags = parsed.values;
|
|
92
|
+
|
|
93
|
+
if (flags.help) {
|
|
94
|
+
process.stdout.write(HELP);
|
|
95
|
+
return 0;
|
|
96
|
+
}
|
|
97
|
+
if (flags.version) {
|
|
98
|
+
process.stdout.write(`${readVersion()}\n`);
|
|
99
|
+
return 0;
|
|
100
|
+
}
|
|
101
|
+
if (flags['list-rules']) {
|
|
102
|
+
process.stdout.write(listRules());
|
|
103
|
+
return 0;
|
|
104
|
+
}
|
|
105
|
+
if (flags.staged && flags.all) {
|
|
106
|
+
throw new SlopCheckError('use --staged or --all, not both');
|
|
107
|
+
}
|
|
108
|
+
if (parsed.positionals.length > 1) {
|
|
109
|
+
throw new SlopCheckError('give at most one base. Run slop-check --help.');
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
const flagMaxScore = parseMaxScore(flags['max-score']);
|
|
113
|
+
const config = loadConfig(findRepoRoot(process.cwd()));
|
|
114
|
+
for (const warning of config.warnings) {
|
|
115
|
+
process.stderr.write(`slop-check: warning: ${warning}\n`);
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
let mode = 'branch';
|
|
119
|
+
if (flags.staged) {
|
|
120
|
+
mode = 'staged';
|
|
121
|
+
} else if (flags.all) {
|
|
122
|
+
mode = 'all';
|
|
123
|
+
}
|
|
124
|
+
const result = runCheck({ cwd: process.cwd(), mode, ref: parsed.positionals[0], config });
|
|
125
|
+
|
|
126
|
+
if (flags.json) {
|
|
127
|
+
process.stdout.write(formatJson(result));
|
|
128
|
+
} else if (flags.badge) {
|
|
129
|
+
process.stdout.write(`${badgeMarkdown(result.score, result.color)}\n`);
|
|
130
|
+
} else {
|
|
131
|
+
const color = Boolean(process.stdout.isTTY) && !flags['no-color'] && !process.env.NO_COLOR;
|
|
132
|
+
process.stdout.write(formatText(result, { color }));
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
const maxScore = flagMaxScore === null ? config.maxScore : flagMaxScore;
|
|
136
|
+
return maxScore !== null && result.score > maxScore ? 1 : 0;
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
try {
|
|
140
|
+
process.exitCode = main();
|
|
141
|
+
} catch (error) {
|
|
142
|
+
if (!(error instanceof SlopCheckError)) {
|
|
143
|
+
throw error;
|
|
144
|
+
}
|
|
145
|
+
process.stderr.write(`slop-check: ${error.message}\n`);
|
|
146
|
+
process.exitCode = 2;
|
|
147
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,46 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@tisankan/slop-check",
|
|
3
|
-
"version": "
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "1.1.0",
|
|
4
|
+
"description": "Catch AI slop in your diff before your reviewer does.",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"bin": {
|
|
7
|
+
"slop-check": "bin/slop-check.js"
|
|
8
|
+
},
|
|
9
|
+
"files": [
|
|
10
|
+
"bin",
|
|
11
|
+
"src",
|
|
12
|
+
"action.yml",
|
|
13
|
+
"README.md",
|
|
14
|
+
"LICENSE"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "node --test"
|
|
18
|
+
},
|
|
19
|
+
"engines": {
|
|
20
|
+
"node": ">=20"
|
|
21
|
+
},
|
|
22
|
+
"repository": {
|
|
23
|
+
"type": "git",
|
|
24
|
+
"url": "git+https://github.com/tisankanj/slop-check.git"
|
|
25
|
+
},
|
|
26
|
+
"homepage": "https://github.com/tisankanj/slop-check#readme",
|
|
27
|
+
"bugs": {
|
|
28
|
+
"url": "https://github.com/tisankanj/slop-check/issues"
|
|
29
|
+
},
|
|
30
|
+
"keywords": [
|
|
31
|
+
"ai",
|
|
32
|
+
"ai-slop",
|
|
33
|
+
"linter",
|
|
34
|
+
"code-review",
|
|
35
|
+
"cli",
|
|
36
|
+
"github-action",
|
|
37
|
+
"claude-code",
|
|
38
|
+
"cursor",
|
|
39
|
+
"copilot"
|
|
40
|
+
],
|
|
41
|
+
"author": "Tisankan Jeyakumar",
|
|
42
|
+
"publishConfig": {
|
|
43
|
+
"access": "public"
|
|
44
|
+
},
|
|
45
|
+
"license": "MIT"
|
|
46
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// The file GitHub runs. It always starts the action, so a symlinked action path
|
|
2
|
+
// can never skip the check and pass by doing nothing.
|
|
3
|
+
import { runAction } from './action.js';
|
|
4
|
+
|
|
5
|
+
const cwd = process.env.GITHUB_WORKSPACE || process.cwd();
|
|
6
|
+
const outcome = await runAction({ env: process.env, fetchFn: fetch, cwd });
|
|
7
|
+
if (outcome.failed) {
|
|
8
|
+
process.exitCode = 1;
|
|
9
|
+
}
|
package/src/action.js
ADDED
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
// GitHub Action entry: runs the check on a pull request, writes the job summary,
|
|
2
|
+
// and keeps one PR comment up to date with the score.
|
|
3
|
+
|
|
4
|
+
import { randomUUID } from 'node:crypto';
|
|
5
|
+
import { appendFileSync, readFileSync } from 'node:fs';
|
|
6
|
+
import { loadConfig } from './config.js';
|
|
7
|
+
import { commitExists, findRepoRoot, SlopCheckError } from './git.js';
|
|
8
|
+
import { formatText } from './report.js';
|
|
9
|
+
import { runCheck } from './run.js';
|
|
10
|
+
import { badgeMarkdown } from './score.js';
|
|
11
|
+
|
|
12
|
+
const MARKER = '<!-- slop-check -->';
|
|
13
|
+
const ZERO_SHA = '0'.repeat(40);
|
|
14
|
+
// GitHub rejects comments over 65536 characters.
|
|
15
|
+
const MAX_REPORT_LENGTH = 60000;
|
|
16
|
+
// GitHub shows at most 10 warning annotations per step.
|
|
17
|
+
const MAX_ANNOTATIONS = 10;
|
|
18
|
+
|
|
19
|
+
function readInputs(env) {
|
|
20
|
+
const maxScoreText = (env['INPUT_MAX-SCORE'] || '').trim();
|
|
21
|
+
return {
|
|
22
|
+
maxScore: maxScoreText === '' ? null : Number(maxScoreText),
|
|
23
|
+
comment: (env.INPUT_COMMENT || 'true').trim() !== 'false',
|
|
24
|
+
annotations: (env.INPUT_ANNOTATIONS || 'true').trim().toLowerCase() !== 'false',
|
|
25
|
+
token: env['INPUT_GITHUB-TOKEN'] || '',
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function readEvent(env) {
|
|
30
|
+
if (!env.GITHUB_EVENT_PATH) {
|
|
31
|
+
return {};
|
|
32
|
+
}
|
|
33
|
+
return JSON.parse(readFileSync(env.GITHUB_EVENT_PATH, 'utf8'));
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function pickBaseSha(eventName, event, root) {
|
|
37
|
+
if (event.pull_request) {
|
|
38
|
+
return event.pull_request.base.sha;
|
|
39
|
+
}
|
|
40
|
+
// After a force push the old `before` commit is gone, so use the default base.
|
|
41
|
+
if (eventName === 'push' && event.before && event.before !== ZERO_SHA && commitExists(root, event.before)) {
|
|
42
|
+
return event.before;
|
|
43
|
+
}
|
|
44
|
+
return undefined;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// A fence longer than any backtick run in the text, so file names cannot close it.
|
|
48
|
+
function codeFence(text) {
|
|
49
|
+
let longest = 0;
|
|
50
|
+
for (const run of text.match(/`+/g) || []) {
|
|
51
|
+
longest = Math.max(longest, run.length);
|
|
52
|
+
}
|
|
53
|
+
return '`'.repeat(Math.max(3, longest + 1));
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
// File names are printed in the log. Pausing workflow commands stops a file named
|
|
57
|
+
// `::error::x.js` from being run as a command.
|
|
58
|
+
function writeLog(text) {
|
|
59
|
+
const token = randomUUID();
|
|
60
|
+
process.stdout.write(`::stop-commands::${token}\n${text}::${token}::\n`);
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function escapeMessage(value) {
|
|
64
|
+
return String(value)
|
|
65
|
+
.replaceAll('%', '%25')
|
|
66
|
+
.replaceAll('\r', '%0D')
|
|
67
|
+
.replaceAll('\n', '%0A');
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function escapeProperty(value) {
|
|
71
|
+
return escapeMessage(value)
|
|
72
|
+
.replaceAll(':', '%3A')
|
|
73
|
+
.replaceAll(',', '%2C');
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function byWeightThenLocation(a, b) {
|
|
77
|
+
if (a.weight !== b.weight) {
|
|
78
|
+
return b.weight - a.weight;
|
|
79
|
+
}
|
|
80
|
+
if (a.file !== b.file) {
|
|
81
|
+
return a.file < b.file ? -1 : 1;
|
|
82
|
+
}
|
|
83
|
+
return a.line - b.line;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export function formatAnnotations(findings) {
|
|
87
|
+
const fileFindings = findings.filter((finding) => finding.file && Number.isInteger(finding.line));
|
|
88
|
+
fileFindings.sort(byWeightThenLocation);
|
|
89
|
+
return fileFindings.slice(0, MAX_ANNOTATIONS).map((finding) => {
|
|
90
|
+
const file = escapeProperty(finding.file);
|
|
91
|
+
const title = escapeProperty(finding.rule);
|
|
92
|
+
const message = escapeMessage(finding.message);
|
|
93
|
+
return `::warning file=${file},line=${finding.line},title=${title}::${message}`;
|
|
94
|
+
});
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
function writeAnnotations(findings) {
|
|
98
|
+
for (const line of formatAnnotations(findings)) {
|
|
99
|
+
process.stdout.write(`${line}\n`);
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
function buildReport(result) {
|
|
104
|
+
let text = formatText(result, { color: false });
|
|
105
|
+
if (text.length > MAX_REPORT_LENGTH) {
|
|
106
|
+
text = `${text.slice(0, MAX_REPORT_LENGTH)}\n... report cut short, run slop-check locally for the rest.\n`;
|
|
107
|
+
}
|
|
108
|
+
const fence = codeFence(text);
|
|
109
|
+
return [
|
|
110
|
+
MARKER,
|
|
111
|
+
`### Slop score: ${result.score}/100 (${result.label})`,
|
|
112
|
+
'',
|
|
113
|
+
`${fence}text`,
|
|
114
|
+
text.trimEnd(),
|
|
115
|
+
fence,
|
|
116
|
+
'',
|
|
117
|
+
badgeMarkdown(result.score, result.color),
|
|
118
|
+
'',
|
|
119
|
+
].join('\n');
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
async function upsertComment(env, inputs, prNumber, body, fetchFn) {
|
|
123
|
+
const api = `${env.GITHUB_API_URL || 'https://api.github.com'}/repos/${env.GITHUB_REPOSITORY}`;
|
|
124
|
+
const headers = {
|
|
125
|
+
Authorization: `Bearer ${inputs.token}`,
|
|
126
|
+
Accept: 'application/vnd.github+json',
|
|
127
|
+
'X-GitHub-Api-Version': '2022-11-28',
|
|
128
|
+
'User-Agent': 'slop-check',
|
|
129
|
+
};
|
|
130
|
+
|
|
131
|
+
const list = await fetchFn(`${api}/issues/${prNumber}/comments?per_page=100`, { headers });
|
|
132
|
+
if (!list.ok) {
|
|
133
|
+
return list.status;
|
|
134
|
+
}
|
|
135
|
+
const comments = await list.json();
|
|
136
|
+
const existing = comments.find((comment) => typeof comment.body === 'string' && comment.body.startsWith(MARKER));
|
|
137
|
+
|
|
138
|
+
const request = existing
|
|
139
|
+
? { url: `${api}/issues/comments/${existing.id}`, method: 'PATCH' }
|
|
140
|
+
: { url: `${api}/issues/${prNumber}/comments`, method: 'POST' };
|
|
141
|
+
const response = await fetchFn(request.url, { method: request.method, headers, body: JSON.stringify({ body }) });
|
|
142
|
+
return response.ok ? null : response.status;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
export async function runAction({ env, fetchFn, cwd }) {
|
|
146
|
+
const inputs = readInputs(env);
|
|
147
|
+
const event = readEvent(env);
|
|
148
|
+
|
|
149
|
+
let result;
|
|
150
|
+
let config;
|
|
151
|
+
try {
|
|
152
|
+
const validMaxScore = Number.isInteger(inputs.maxScore) && inputs.maxScore >= 0 && inputs.maxScore <= 100;
|
|
153
|
+
if (inputs.maxScore !== null && !validMaxScore) {
|
|
154
|
+
throw new SlopCheckError('max-score must be a whole number from 0 to 100');
|
|
155
|
+
}
|
|
156
|
+
const root = findRepoRoot(cwd);
|
|
157
|
+
config = loadConfig(root);
|
|
158
|
+
for (const warning of config.warnings) {
|
|
159
|
+
process.stdout.write(`::warning::slop-check: ${warning}\n`);
|
|
160
|
+
}
|
|
161
|
+
result = runCheck({ cwd, mode: 'branch', baseSha: pickBaseSha(env.GITHUB_EVENT_NAME, event, root), config });
|
|
162
|
+
} catch (error) {
|
|
163
|
+
if (!(error instanceof SlopCheckError)) {
|
|
164
|
+
throw error;
|
|
165
|
+
}
|
|
166
|
+
process.stdout.write(`::error::slop-check: ${error.message}\n`);
|
|
167
|
+
return { score: null, failed: true };
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
writeLog(formatText(result, { color: false }));
|
|
171
|
+
if (inputs.annotations) {
|
|
172
|
+
writeAnnotations(result.findings);
|
|
173
|
+
}
|
|
174
|
+
const report = buildReport(result);
|
|
175
|
+
if (env.GITHUB_STEP_SUMMARY) {
|
|
176
|
+
appendFileSync(env.GITHUB_STEP_SUMMARY, report);
|
|
177
|
+
}
|
|
178
|
+
if (env.GITHUB_OUTPUT) {
|
|
179
|
+
appendFileSync(env.GITHUB_OUTPUT, `score=${result.score}\n`);
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
const prNumber = event.pull_request ? event.pull_request.number : null;
|
|
183
|
+
if (inputs.comment && prNumber && inputs.token) {
|
|
184
|
+
let failedStatus;
|
|
185
|
+
try {
|
|
186
|
+
failedStatus = await upsertComment(env, inputs, prNumber, report, fetchFn);
|
|
187
|
+
} catch (error) {
|
|
188
|
+
failedStatus = error.message;
|
|
189
|
+
}
|
|
190
|
+
// Fork PRs get a read-only token. The job summary still has the full report.
|
|
191
|
+
if (failedStatus) {
|
|
192
|
+
process.stdout.write(`::warning::slop-check could not post the PR comment (${failedStatus}). The job summary has the report.\n`);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
const maxScore = inputs.maxScore === null ? config.maxScore : inputs.maxScore;
|
|
197
|
+
const failed = maxScore !== null && result.score > maxScore;
|
|
198
|
+
if (failed) {
|
|
199
|
+
process.stdout.write(`::error::Slop score ${result.score} is above max-score ${maxScore}.\n`);
|
|
200
|
+
}
|
|
201
|
+
return { score: result.score, failed };
|
|
202
|
+
}
|
|
203
|
+
|