-
Notifications
You must be signed in to change notification settings - Fork 2.3k
143 lines (119 loc) · 5.89 KB
/
linter.yml
File metadata and controls
143 lines (119 loc) · 5.89 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
name: Check for linter warnings / exceptions
on:
pull_request:
branches:
- master
permissions:
contents: read
jobs:
check-linter:
runs-on: ubuntu-latest
steps:
- name: Set up Node.js
uses: actions/setup-node@v6
with:
node-version: "20"
- name: Checkout code
uses: actions/checkout@v6
with:
fetch-depth: 0
ref: ${{ github.event.pull_request.base.sha }}
- name: Fetch base and target branches
run: |
git fetch origin +refs/heads/${{ github.event.pull_request.base.ref }}:refs/remotes/origin/${{ github.event.pull_request.base.ref }}
git fetch origin +refs/pull/${{ github.event.pull_request.number }}/merge:refs/remotes/pull/${{ github.event.pull_request.number }}/merge
- name: Install dependencies
run: npm ci
- name: Get the diff
run: git diff --name-only origin/${{ github.event.pull_request.base.ref }}...refs/remotes/pull/${{ github.event.pull_request.number }}/merge | grep '^\(modules\|src\|libraries\|creative\)/.*\.js$' > __changed_files.txt || true
- name: Get newly added JS files in TS migration paths
run: git diff --name-only --diff-filter=A origin/${{ github.event.pull_request.base.ref }}...refs/remotes/pull/${{ github.event.pull_request.number }}/merge | grep '^\(modules\|src\|libraries\)/.*\.js$' > __new_js_files.txt || true
- name: Run linter on base branch
run: npx eslint --no-inline-config --format json $(cat __changed_files.txt | xargs stat --printf '%n\n' 2> /dev/null) > __base.json || true
- name: Check out PR
run: git checkout ${{ github.event.pull_request.head.sha }}
- name: Install dependencies
run: npm ci
- name: Run linter on PR
run: npx eslint --no-inline-config --format json $(cat __changed_files.txt | xargs stat --printf '%n\n' 2> /dev/null) > __pr.json || true
- name: Compare them and post comment if necessary
uses: actions/github-script@v8
id: comment
with:
result-encoding: string
script: |
const fs = require('fs');
const path = require('path');
const process = require('process');
function parse(fn) {
return JSON.parse(fs.readFileSync(fn)).reduce((memo, data) => {
const file = path.relative(process.cwd(), data.filePath);
if (!memo.hasOwnProperty(file)) { memo[file] = { errors: 0, warnings: 0} }
data.messages.forEach(({severity}) => {
memo[file][severity > 1 ? 'errors' : 'warnings']++;
});
return memo;
}, {})
}
function mkDiff(old, new_) {
const files = Object.fromEntries(
Object.entries(new_)
.map(([file, {errors, warnings}]) => {
const {errors: oldErrors, warnings: oldWarnings} = old[file] || {};
return [file, {errors: Math.max(0, errors - (oldErrors ?? 0)), warnings: Math.max(0, warnings - (oldWarnings ?? 0))}]
})
.filter(([_, {errors, warnings}]) => errors > 0 || warnings > 0)
)
return Object.values(files).reduce((memo, {warnings, errors}) => {
memo.errors += errors;
memo.warnings += warnings;
return memo;
}, {errors: 0, warnings: 0, files})
}
function mkComment({errors, warnings, files}, newJsFiles) {
function pl(noun, number) {
return noun + (number === 1 ? '' : 's')
}
const comments = [];
if (newJsFiles.length > 0) {
let jsComment = 'Whoa there partner! This project is migrating to typescript. Consider changing the new JS files to TS, with well-defined types for what interacts with the prebid public API (for example: bid params and configuration). Thanks!\n\n';
newJsFiles.forEach((file) => {
jsComment += ` * \`${file}\`\n`;
});
comments.push(jsComment);
}
if (errors === 0 && warnings === 0) return comments.length > 0 ? comments.join('\n') : undefined;
const summary = [];
if (errors) summary.push(`**${errors}** linter ${pl('error', errors)}`)
if (warnings) summary.push(`**${warnings}** linter ${pl('warning', warnings)}`)
let cm = `Tread carefully! This PR adds ${summary.join(' and ')} (possibly disabled through directives):\n\n`;
Object.entries(files).forEach(([file, {errors, warnings}]) => {
const summary = [];
if (errors) summary.push(`+${errors} ${pl('error', errors)}`);
if (warnings) summary.push(`+${warnings} ${pl('warning', warnings)}`)
cm += ` * \`${file}\` (${summary.join(', ')})\n`
})
comments.push(cm);
return comments.join('\n');
}
function readLines(fn) {
if (!fs.existsSync(fn)) return [];
return fs.readFileSync(fn, 'utf8').split('\n').map(line => line.trim()).filter(Boolean);
}
const [base, pr] = ['__base.json', '__pr.json'].map(parse);
const comment = mkComment(mkDiff(base, pr), readLines('__new_js_files.txt'));
if (comment) {
fs.writeFileSync("${{ runner.temp }}/comment.json", JSON.stringify({
issue_number: context.issue.number,
body: comment
}))
return "true";
} else {
return "false";
}
- name: Upload comment data
if: ${{ steps.comment.outputs.result == 'true' }}
uses: actions/upload-artifact@v7
with:
name: comment
path: ${{ runner.temp }}/comment.json