tomasbjerre / violations-lib

Java library for parsing report files from static code analysis.
Apache License 2.0
148 stars 39 forks source link

Generic Parser comments as a Single Line #106

Closed anirudhbagri closed 4 years ago

anirudhbagri commented 4 years ago

Actual: The violation generated by GenericParser is a POST as a single line whatever might be the source. Excepted: The comments must be multi-lined if the source has them as multi-lined.

tomasbjerre commented 4 years ago

When using Bitbucket Server?

Have you tried passing the -comment-template parameter and surrounding message with ``` before and after ?

The template is documented here: https://github.com/tomasbjerre/violation-comments-lib

anirudhbagri commented 4 years ago

Yes, bitbucket server.

I am have tried using

-comment-template "```{{violation.message}}```"

and

-comment-template "{{violation.message}}"

both didn't work.

tomasbjerre commented 4 years ago

Turns out this is a feature in Mustache. You can use tripple {{{ and }}} to avoid escaping:

npx violation-comments-to-bitbucket-server-command-line \
 -server-url http://localhost:7990 \
 -username admin \
 -password admin \
 -pk PROJ \
 -rs REPO \
 -prid 1 \
 -keep-old-comments true \
 -create-single-file-comments false \
 -create-comment-with-all-single-file-comments true \
 -comment-only-changed-files false \
 -v "GENERIC" "." ".*eneric\.txt$" "Generic" \
 -comment-template "
message: {{{violation.message}}}
"

image

anirudhbagri commented 4 years ago

This did partially what I expected.

 -comment-template "message: {{{violation.message}}}"

BUT, My file is having lines with multiple spaces in a single line.. like this:

Name                                           Stmts   Miss  Cover
------------------------------------------------------------------
file1                                              0      0   100%
path/to/file2                                      0      0   100%
a/long/file/path/herefile3                        47      5    89%
------------------------------------------------------------------
TOTAL                                            100      8    92%

But, the comment that is posted, has all the white space and line with ---- removed.

This is that I get as comment.

Name Stmts Miss Cover

file1 0 0 100%
path/to/file2 0 0 100%
a/long/file/path/herefile3 47 5 89%

TOTAL 100 8 92%
tomasbjerre commented 4 years ago

I created such a file:

Name                                           Stmts   Miss  Cover
------------------------------------------------------------------
file1                                              0      0   100%
path/to/file2                                      0      0   100%
a/long/file/path/herefile3                        47      5    89%
------------------------------------------------------------------
TOTAL                                            100      8    92%

here: https://github.com/tomasbjerre/violations-test/blob/feature/addingcrap/generic.txt

Reported with

npx violation-comments-to-bitbucket-server-command-line \
 -server-url http://localhost:7990 \
 -username admin \
 -password admin \
 -pk PROJ \
 -rs REPO \
 -prid 1 \
 -keep-old-comments false \
 -create-single-file-comments false \
 -create-comment-with-all-single-file-comments true \
 -comment-only-changed-files false \
 -v "GENERIC" "." ".*eneric\.txt$" "Generic" \
 -comment-template "
message: 
\\\`\\\`\\\`
{{{violation.message}}}
\\\`\\\`\\\`
"

Here: https://github.com/tomasbjerre/violations-test/blob/feature/addingcrap/report-to-bitbucket-server.sh#L12

And I get: image

anirudhbagri commented 4 years ago

This is not working for me :( Instead, it printed like this

message: ` Name Stmts Miss Cover ........

TOTAL 100 8 92% `

BTW, I am pushing this from Jenkins Job script.

This is the exact command:

echo "Posting Test Coverage Report" 
java -jar violation.jar \
-project-key $project_key \
-pull-request-id $pr_id \
-repo-slug $repo_slug \
-server-url $server \
-keep-old-comments false \
-create-single-file-comments false \
-create-comment-with-all-single-file-comments true \
-comment-only-changed-files false \
-keystore-path $path \
-keystore-pass $pass \
-v GENERIC "." ".*/coverage_report\.txt\$" "Code Coverage Reporter" \
-comment-template "
message: 
\\\`\\\`\\\`
{{{violation.message}}}
\\\`\\\`\\\`"
echo "Finished Posting Test Coverage Report"
anirudhbagri commented 4 years ago

I see half of the content becoming bold?

image

This is the command I used:

Java -jar violation.jar \ 
 -server-url $server \
 -pk $proj \
 -rs $repo \
 -prid $pr \
 -keep-old-comments false \
 -create-single-file-comments false \
 -create-comment-with-all-single-file-comments true \
 -comment-only-changed-files false \
 -v "GENERIC" "." ".*coverage_report\.txt$" "Generic" \
 -keystore-path $PATH \
 -keystore-pass $PASS \
  -comment-template "
message:
\\\`\\\`\\\`
{{{violation.message}}}
\\\`\\\`\\\`
"
tomasbjerre commented 4 years ago

Strange... And if you manually edit the comment, is it still missing spaces?

anirudhbagri commented 4 years ago

Yes.

anirudhbagri commented 4 years ago

Managed to get it done using this:

Java -jar violation.jar \ 
 -server-url $server \
 -pk $proj \
 -rs $repo \
 -prid $pr \
 -keep-old-comments false \
 -create-single-file-comments false \
 -create-comment-with-all-single-file-comments true \
 -comment-only-changed-files false \
 -v "GENERIC" "." ".*coverage_report\.txt$" "Generic" \
 -keystore-path $PATH \
 -keystore-pass $PASS \
  -comment-template "
message:
~~~
{{{violation.message}}}
~~~
"

Thank you so much @tomasbjerre for helping me out.