forked from staabm/annotate-pull-request-from-checkstyle
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpmd2pr
128 lines (109 loc) · 3.27 KB
/
pmd2pr
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
#!/usr/bin/env php
<?php
/*
* Turns PMD based XML-Reports into Github Pull Request Annotations via the
Checks API. This script is meant for use within your GithubAction.
*
* (c) Mridang Agarwalla <mridang.agarwalla@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* https://github.com/mridang/pmd-annotations
*/
error_reporting(E_ALL);
ini_set('display_errors', 'stderr');
gc_disable();
$version = '0.0.1';
// 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 "pmd2pr $version\n";
echo "Annotate a Github Pull Request based on a PMD 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 PMD formatted xml input.' ."\n\n");
}
fwrite(STDERR, $xml);
exit(2);
}
$exit = 0;
foreach ($root as $file) {
$filename = (string)$file['name'];
$type = 'warning';
foreach ($file as $violation) {
$line = (string) $violation['beginline'];
$message = trim((string) $violation);
$annotateType = annotateType($type);
annotateViolation($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 annotateViolation($type, $filename, $line, $message, $colorize)
{
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)
{
return in_array($type, ['error', 'failure']) ? 'error' : 'warning';
}