-
-
Notifications
You must be signed in to change notification settings - Fork 24
/
cs2pr
executable file
·209 lines (181 loc) · 5.89 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
#!/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.8.6-dev';
// options
$colorize = false;
$gracefulWarnings = false;
$noticeAsWarning = false;
$prependFilename = false;
$prependSource = false;
$errorsAsWarnings = false;
// parameters
$params = array();
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;
case 'prepend-filename':
$prependFilename = true;
break;
case 'prepend-source':
$prependSource = true;
break;
case 'notices-as-warnings':
$noticeAsWarning = true;
break;
case 'errors-as-warnings':
$errorsAsWarnings = 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 "https://github.com/staabm/annotate-pull-request-from-checkstyle\n\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";
echo " --notices-as-warnings Convert notices to warnings (Github does not annotate notices otherwise).\n";
echo " --errors-as-warnings Downgrade errors to warnings.\n";
echo " --prepend-filename Prepend error 'filename' attribute to the message.\n";
echo " --prepend-source Prepend error 'source' attribute to the message.\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'];
$source = isset($error['source']) ? (string) $error['source'] : null;
if ($prependSource && $source) {
$message = $source.': '.$message;
}
if ($prependFilename && $filename) {
$message = filenameOnly($filename).': '.$message;
}
$annotateType = annotateType($type, $noticeAsWarning, $errorsAsWarnings);
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)
{
$message = escapeData($message);
$filename = escapeProperty($filename);
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, $noticeAsWarning, $errorsAsWarnings)
{
$type = strtolower($type);
if (in_array($type, array('error', 'failure'))) {
if ($errorsAsWarnings) {
return 'warning';
}
return 'error';
}
if (!$noticeAsWarning && in_array($type, array('info', 'notice'))) {
return 'notice';
}
return 'warning';
}
/**
* @param string $data
* @return string
*/
function escapeData($data)
{
// see https://github.com/actions/toolkit/blob/4f7fb6513a355689f69f0849edeb369a4dc81729/packages/core/src/command.ts#L80-L85
$data = str_replace("%", '%25', $data);
$data = str_replace("\r", '%0D', $data);
$data = str_replace("\n", '%0A', $data);
return $data;
}
/**
* @param string $property
* @return string
*/
function escapeProperty($property)
{
// see https://github.com/actions/toolkit/blob/4f7fb6513a355689f69f0849edeb369a4dc81729/packages/core/src/command.ts#L87-L94
$property = str_replace("%", '%25', $property);
$property = str_replace("\r", '%0D', $property);
$property = str_replace("\n", '%0A', $property);
$property = str_replace(":", '%3A', $property);
$property = str_replace(",", '%2C', $property);
return $property;
}
/**
* Get the filename only from a filepath. Built to work across windows & linux.
*/
function filenameOnly($filepath)
{
return basename(str_replace("\\", "/", $filepath));
}