Skip to content

Commit 7568a64

Browse files
committed
build(fpb-lint): linting errors as PR comments, resolved EbookFoundation#4416
1 parent a824fc4 commit 7568a64

File tree

1 file changed

+94
-4
lines changed

1 file changed

+94
-4
lines changed

Diff for: .github/workflows/fpb-lint.yml

+94-4
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,109 @@ name: free-programming-books-lint
22

33
on: [push, pull_request]
44

5+
env:
6+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
7+
GITHUB_RUN_ID: ${{ github.run_id }}
8+
PR: ${{ github.event.pull_request.html_url }}
9+
510
jobs:
611
build:
712

813
runs-on: ubuntu-latest
914

1015
steps:
1116
- uses: actions/checkout@v3
17+
- name: Use Python
18+
uses: actions/setup-python@v4
19+
with:
20+
python-version: '3.x'
1221
- name: Use Node.js
1322
uses: actions/setup-node@v3
1423
with:
1524
node-version: '16.x'
1625
- run: npm install -g free-programming-books-lint
17-
- run: fpb-lint ./books/
18-
- run: fpb-lint ./casts/
19-
- run: fpb-lint ./courses/
20-
- run: fpb-lint ./more/
26+
27+
- name: Push
28+
if: ${{ github.event_name == 'push' }}
29+
run: |
30+
fpb-lint ./books/
31+
fpb-lint ./casts/
32+
fpb-lint ./courses/
33+
fpb-lint ./more/
34+
35+
- name: Pull Request
36+
if: ${{ always() &&
37+
github.event_name == 'pull_request' }}
38+
run: |
39+
fpb-lint ./books/ &>> output.log || echo "Analyzing..."
40+
fpb-lint ./casts/ &>> output.log || echo "Analyzing..."
41+
fpb-lint ./courses/ &>> output.log || echo "Analyzing..."
42+
fpb-lint ./more/ &>> output.log || echo "Analyzing..."
43+
44+
cat > error.log
45+
46+
- name: Clean output
47+
if: ${{ always() &&
48+
github.event_name == 'pull_request' }}
49+
uses: actions/github-script@v6
50+
with:
51+
script: |
52+
const fs = require('fs');
53+
const readline = require('readline');
54+
55+
const file = readline.createInterface({
56+
input: fs.createReadStream('output.log'),
57+
output: process.stdout,
58+
terminal: false,
59+
});
60+
61+
let lastLine = '';
62+
file.on('line', (line) => {
63+
if (lastLine) {
64+
fs.appendFile('error.log', lastLine, (err) => {
65+
if (err) {
66+
console.error(err);
67+
}
68+
});
69+
}
70+
71+
if (line.includes('/home/runner/work/free-programming-books/')) {
72+
lastLine = line.replace('/home/runner/work/free-programming-books/', '') + "\r\n";
73+
} else if (line.includes('\u26a0')) {
74+
lastLine = '\r\n\r\n';
75+
} else if (line.includes('remark-lint')) {
76+
lastLine = line + '\r\n';
77+
} else {
78+
lastLine = null;
79+
}
80+
});
81+
82+
file.on('close', () => {
83+
if (!lastLine || lastLine === '\r\n\r\n') {
84+
return;
85+
}
86+
87+
fs.appendFile('error.log', lastLine, (err) => {
88+
if (err) {
89+
console.error(err);
90+
}
91+
});
92+
});
93+
94+
- name: Print output
95+
if: ${{ always() &&
96+
github.event_name == 'pull_request' }}
97+
run: |
98+
cat error.log
99+
100+
if [ -s error.log ]
101+
then
102+
gh pr review $PR -r -b "Linter failed, fix the error(s):
103+
\`\`\`
104+
$(cat error.log)
105+
\`\`\`"
106+
gh pr edit $PR --add-label "linter error"
107+
else
108+
gh pr review $PR -a
109+
gh pr edit $PR --remove-label "linter error"
110+
fi

0 commit comments

Comments
 (0)