Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ cs2pr /path/to/checkstyle-report.xml
- `--graceful-warnings`: Don't exit with error codes if there are only warnings
- `--colorize`: Colorize the output. Useful if the same lint script should be used locally on the command line and remote on GitHub Actions. With this option, errors and warnings are better distinguishable on the command line and the output is still compatible with GitHub Annotations
- `--notices-as-warnings` Converts notices to warnings. This can be useful because GitHub does not annotate notices.
- `--prepend-filename` Prepend the filename to the output message
- `--prepend-source` When the checkstyle generating tool provides a `source` attribute, prepend the source to the output message.


Expand Down
17 changes: 17 additions & 0 deletions cs2pr
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ $version = '1.7.2-dev';
$colorize = false;
$gracefulWarnings = false;
$noticeAsWarning = false;
$prependFilename = false;
$prependSource = false;

// parameters
Expand All @@ -36,6 +37,9 @@ foreach ($argv as $arg) {
case 'colorize':
$colorize = true;
break;
case 'prepend-filename':
$prependFilename = true;
break;
case 'prepend-source':
$prependSource = true;
break;
Expand Down Expand Up @@ -64,6 +68,7 @@ if (count($params) === 1) {
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 " --prepend-filename Prepend error 'filename' attribute to the message.\n";
echo " --prepend-source Prepend error 'source' attribute to the message.\n";
exit(9);
}
Expand Down Expand Up @@ -102,6 +107,10 @@ foreach ($root as $file) {
$message = $source.': '.$message;
}

if ($prependFilename && $filename) {
$message = filenameOnly($filename).': '.$message;
}

$annotateType = annotateType($type, $noticeAsWarning);
annotateCheck($annotateType, relativePath($filename), $line, $message, $colorize);

Expand Down Expand Up @@ -180,3 +189,11 @@ function escapeProperty($property)

return $property;
}

/**
* Get the filename only from a filepath. Built to work across windows & linux.
*/
function filenameOnly($filepath)
{
return basename(str_replace("\\", "/", $filepath));
}
4 changes: 4 additions & 0 deletions tests/errors/prepend-filename.expect
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
::error file=redaxo\src\addons\2factor_auth\boot.php,line=6::boot.php: Call to static method getInstance() on an unknown class rex_one_time_password.
::error file=redaxo\src\addons\2factor_auth\boot.php,line=9::boot.php: Call to static method getInstance() on an unknown class rex_minibar.
::error file=redaxo\src\addons\2factor_auth\lib\one_time_password.php,line=0::one_time_password.php: Class rex_one_time_password was not found while trying to analyse it - autoloading is probably not configured properly.
::error file=redaxo/src/addons/2factor_auth/lib/linux_filepath.php,line=0::linux_filepath.php: Class rex_one_time_password was not found while trying to analyse it - autoloading is probably not configured properly.
13 changes: 13 additions & 0 deletions tests/errors/prepend-filename.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<checkstyle>
<file name="redaxo\src\addons\2factor_auth\boot.php">
<error line="6" column="1" severity="error" message="Call to static method getInstance() on an unknown class rex_one_time_password." />
<error line="9" column="1" severity="error" message="Call to static method getInstance() on an unknown class rex_minibar." />
</file>
<file name="redaxo\src\addons\2factor_auth\lib\one_time_password.php">
<error line="0" column="1" severity="error" message="Class rex_one_time_password was not found while trying to analyse it - autoloading is probably not configured properly." />
</file>
<file name="redaxo/src/addons/2factor_auth/lib/linux_filepath.php">
<error line="0" column="1" severity="error" message="Class rex_one_time_password was not found while trying to analyse it - autoloading is probably not configured properly." />
</file>
</checkstyle>
1 change: 1 addition & 0 deletions tests/tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ function testXml($xmlPath, $expectedExit, $expectedOutput = null, $options = '')
testXml(__DIR__.'/errors/notices.xml', 1, file_get_contents(__DIR__.'/errors/notices.expect'));
testXml(__DIR__.'/errors/notices.xml', 1, file_get_contents(__DIR__.'/errors/notices-as-warnings.expect'), '--notices-as-warnings');

testXml(__DIR__.'/errors/prepend-filename.xml', 1, file_get_contents(__DIR__.'/errors/prepend-filename.expect'), '--prepend-filename');
testXml(__DIR__.'/errors/mixed-source-attributes.xml', 1, file_get_contents(__DIR__.'/errors/mixed-source-attributes.expect'), '--prepend-source');

testXml(__DIR__.'/errors/mixed.xml', 1, file_get_contents(__DIR__.'/errors/mixed-colors.expect'), '--colorize');
Expand Down