forked from tinovyatkin/action-php-codesniffer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-on-blame.ts
71 lines (67 loc) · 2.46 KB
/
run-on-blame.ts
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
import { lint } from 'php-codesniffer';
import { execFileSync } from 'child_process';
import { blame } from 'git-blame-json';
import * as path from 'path';
import * as core from '@actions/core';
import * as github from '@actions/github';
import * as Webhooks from '@octokit/webhooks';
export async function runOnBlame(files: string[]): Promise<void> {
try {
const options: Record<string, string> = {};
const standard = core.getInput('standard');
if (standard) options.standard = standard;
const lintResults = await lint(
files,
core.getInput('phpcs_path', { required: true })
);
const dontFailOnError =
core.getInput('fail_on_errors') == 'false' ||
core.getInput('fail_on_errors') === 'off';
const dontFailOnWarning =
core.getInput('fail_on_warnings') == 'false' ||
core.getInput('fail_on_warnings') === 'off';
if (!lintResults.totals.errors) {
if (dontFailOnWarning) return;
if (!lintResults.totals.warnings) return;
}
// blame files and output relevant errors
// get email of author of first commit in PR
const authorEmail = execFileSync(
'git',
['--no-pager', 'log', '--format=%ae', `${github.context.sha}^!`],
{ encoding: 'utf8', windowsHide: true, timeout: 5000 }
).trim();
console.log('PR author email: %s', authorEmail);
for (const [file, results] of Object.entries(lintResults.files)) {
const blameMap = await blame(file);
let headerPrinted = false;
for (const message of results.messages) {
if (blameMap.get(message.line)?.authorMail === authorEmail) {
// that's our line
// we simulate checkstyle output to be picked up by problem matched
if (!headerPrinted) {
console.log(`<file name="${path.relative(process.cwd(), file)}">`);
headerPrinted = true;
}
// output the problem
console.log(
'<error line="%d" column="%d" severity="%s" message="%s" source="%s"/>',
message.line,
message.column,
message.type.toLowerCase(),
message.message,
message.source
);
// fail
if (message.type === 'WARNING' && !dontFailOnWarning)
core.setFailed(message.message);
else if (message.type === 'ERROR' && !dontFailOnError)
core.setFailed(message.message);
}
}
}
} catch (err) {
core.debug(err);
core.setFailed(err);
}
}