forked from staabm/annotate-pull-request-from-checkstyle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cs2pr
137 lines (117 loc) · 3.59 KB
/
cs2pr
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
#!/usr/bin/env php
<?php
/*
* Turns checkstyle based XML-Reports into Github Pull Request Annotations via the Checks API. This script is meant for use within your GithubAction.
*
* (c) Markus Staab <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://github.com/staabm/annotate-pull-request-from-checkstyle
*/
error_reporting(E_ALL);
ini_set('display_errors', 'stderr');
gc_disable();
$version = '1.4.1-dev';
// options
$colorize = false;
$gracefulWarnings = false;
// parameters
$params = [];
foreach ($argv as $arg) {
if (substr($arg, 0, 2) === '--') {
$option = substr($arg, 2);
switch ($option) {
case 'graceful-warnings':
$gracefulWarnings = true;
break;
case 'colorize':
$colorize = true;
break;
default:
echo "Unknown option ".$option."\n";
exit(9);
}
} else {
$params[] = $arg;
}
}
if (count($params) === 1) {
$xml = stream_get_contents(STDIN);
} elseif (count($params) === 2 && file_exists($params[1])) {
$xml = file_get_contents($params[1]);
} else {
echo "cs2pr $version\n";
echo "Annotate a Github Pull Request based on a Checkstyle XML-report.\n";
echo "Usage: ". $params[0] ." [OPTION]... <filename>\n";
echo "\n";
echo "Supported options:\n";
echo " --graceful-warnings Don't exit with error codes if there are only warnings.\n";
echo " --colorize Colorize the output (still compatible with Github Annotations)\n";
exit(9);
}
// enable user error handling
libxml_use_internal_errors(true);
$root = @simplexml_load_string($xml);
if ($root === false) {
$errors = libxml_get_errors();
if ($errors) {
fwrite(STDERR, 'Error: '. rtrim($errors[0]->message).' on line '.$errors[0]->line.', column '.$errors[0]->column ."\n\n");
} elseif (stripos($xml, '<?xml') !== 0) {
fwrite(STDERR, 'Error: Expecting xml stream starting with a xml opening tag.' ."\n\n");
} else {
fwrite(STDERR, 'Error: Unknown error. Expecting checkstyle formatted xml input.' ."\n\n");
}
fwrite(STDERR, $xml);
exit(2);
}
$exit = 0;
foreach ($root as $file) {
$filename = (string)$file['name'];
foreach ($file as $error) {
$type = (string) $error['severity'];
$line = (string) $error['line'];
$message = (string) $error['message'];
$annotateType = annotateType($type);
annotateCheck($annotateType, relativePath($filename), $line, $message, $colorize);
if (!$gracefulWarnings || $annotateType === 'error') {
$exit = 1;
}
}
}
exit($exit);
/**
* @param 'error'|'warning' $type
* @param string $filename
* @param int $line
* @param string $message
* @param boolean $colorize
*/
function annotateCheck($type, $filename, $line, $message, $colorize)
{
// newlines need to be encoded
// see https://github.com/actions/starter-workflows/issues/68#issuecomment-581479448
$message = str_replace("\n", '%0A', $message);
if ($colorize) {
echo "\033[".($type==='error' ? '91' : '93')."m\n";
}
echo "::{$type} file={$filename},line={$line}::{$message}\n";
if ($colorize) {
echo "\033[0m";
}
}
function relativePath($path)
{
return str_replace(getcwd().'/', '', $path);
}
function annotateType($type)
{
if (in_array($type, ['error', 'failure'])) {
return 'error';
}
if (in_array($type, ['info', 'notice'])) {
return 'notice';
}
return 'warning';
}