tomasbjerre / violations-lib

Java library for parsing report files from static code analysis.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Please add support for generic issues parser

CarstenGrohmann opened this issue · comments

Hello Tomas,

Please add support for the generic ISSUES parser to support native JSON. The issues parser is provided by Jenkins Warnings Next Generation Plugin. I want to use this parser to integrate software which writes a custom JSON format.

This ticket is related to #169.

Regards,
Carsten

Relates to #49.

If it is your own tool: Why can you not let it produce one of the supported formats?
If it is not your own tool: I would rather add that format. I think that will be easier to maintain.

This is not my own tool. I want to run ansible-later in a Jenkins pipeline. Unfortunately it uses a non-standard two-line output format as default:

ERROR: [ANSIBLE0004] Standard 'YAML should use consistent number of spaces around variables' not met:
... simple_role.yml:7: no suitable numbers of spaces (min: 1 max: 1)

Alternativly ansible-later provides a custom JSON output:

{
  "asctime": "2023-01-16 14:40:57,297",
  "levelname": "ERROR",
  "message": "[ANSIBLE0004] Standard 'YAML should use consistent number of spaces around variables' not met: simple_role.yml:7: no suitable numbers of spaces (min: 1 max: 1)",
  "later_tag": "review",
  "later_standard": "YAML should use consistent number of spaces around variables",
  "later_file": "simple_role.yml",
  "later_passed": false,
  "later_sid": "ANSIBLE0004",
  "later_lineno": 7,
  "later_message": "no suitable numbers of spaces (min: 1 max: 1)"
}

I asked for a better output format like pep8 or pylint/pycodestyle in thegeeklab/ansible-later/issues/516 and agreed to give the JSON output a try. But this fails because lack of the issues parser.

My current approch is to rewrite the output of ansible-later with jq, but this isn't done and still untested yet:

    stage('==== ansible-later ====') {
      try {
        sh '''ansible-later --config .later-jenkins.yml > output-jenkins/ansible-later.json 2>&1 | jq --raw-output '"\\(.later_file):\\(.later_lineno): [\\(.later_sid)] Standard \\"\\(.later_standard)\\" not met: \\(.later_message)"'
        '''
      }
      catch (err) {
        echo "ERROR: Ansible-later failed: ${err}"
      }
    }
  }

Steps to reproduce (requires Linux and a current Python 3):

# mkdir parser_test
# cd parser_test
# python3.10 -m venv virtualenv
# source virtualenv/bin/activate
# pip install ansible
# pip install ansible-later

# ssh localhost uptime
 14:33:17  up 31 days  4:48,  0 users,  load average: 0.11, 0.09, 0.03

# cat simple_role.yml 
---
- name: Simple Ansible role
  hosts: localhost
  tasks:
    - name: Show variables
      ansible.builtin.debug:
        msg: "Variable {{item}}"
      loop:
        - foo
        - bar
...

# ansible-playbook -i localhost, simple_role.yml

PLAY [Simple Ansible role] ****************************************************************************************************************************************

TASK [Gathering Facts] ********************************************************************************************************************************************
[WARNING]: Platform linux on host localhost is using the discovered Python interpreter at /usr/bin/python3.10, but future installation of another Python
interpreter could change the meaning of that path. See https://docs.ansible.com/ansible-core/2.14/reference_appendices/interpreter_discovery.html for more
information.
ok: [localhost]

TASK [Show variables] *********************************************************************************************************************************************
ok: [localhost] => (item=foo) => {
    "msg": "Variable foo"
}
ok: [localhost] => (item=bar) => {
    "msg": "Variable bar"
}

PLAY RECAP ********************************************************************************************************************************************************
localhost                  : ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  


# ansible-later simple_role.yml 
ERROR: [ANSIBLE0004] Standard 'YAML should use consistent number of spaces around variables' not met:
... simple_role.yml:7: no suitable numbers of spaces (min: 1 max: 1)

WARNING: [ANSIBLE9998] Best practice 'Standards version should be pinned' not met:
... simple_role.yml: Standards version not set. Using latest standards version 0.2

# cat later-json.yml 
logging:
  json: True

# ansible-later --config later-json.yml simple_role.yml 
{"asctime": "2023-01-16 14:37:28,987", "levelname": "ERROR", "message": "[ANSIBLE0004] Standard 'YAML should use consistent number of spaces around variables' not met: simple_role.yml:7: no suitable numbers of spaces (min: 1 max: 1)", "later_tag": "review", "later_standard": "YAML should use consistent number of spaces around variables", "later_file": "simple_role.yml", "later_passed": false, "later_sid": "ANSIBLE0004", "later_lineno": 7, "later_message": "no suitable numbers of spaces (min: 1 max: 1)"}
{"asctime": "2023-01-16 14:37:28,998", "levelname": "WARNING", "message": "[ANSIBLE9998] Best practice 'Standards version should be pinned' not met: simple_role.yml: Standards version not set. Using latest standards version 0.2", "later_tag": "review", "later_standard": "Standards version should be pinned", "later_file": "simple_role.yml", "later_passed": false, "later_sid": "ANSIBLE9998", "later_lineno": null, "later_message": "Standards version not set. Using latest standards version 0.2"}

# ansible-later --config later-json.yml simple_role.yml  2>&1 | jq .
{
  "asctime": "2023-01-16 14:40:57,297",
  "levelname": "ERROR",
  "message": "[ANSIBLE0004] Standard 'YAML should use consistent number of spaces around variables' not met: simple_role.yml:7: no suitable numbers of spaces (min: 1 max: 1)",
  "later_tag": "review",
  "later_standard": "YAML should use consistent number of spaces around variables",
  "later_file": "simple_role.yml",
  "later_passed": false,
  "later_sid": "ANSIBLE0004",
  "later_lineno": 7,
  "later_message": "no suitable numbers of spaces (min: 1 max: 1)"
}
{
  "asctime": "2023-01-16 14:40:57,308",
  "levelname": "WARNING",
  "message": "[ANSIBLE9998] Best practice 'Standards version should be pinned' not met: simple_role.yml: Standards version not set. Using latest standards version 0.2",
  "later_tag": "review",
  "later_standard": "Standards version should be pinned",
  "later_file": "simple_role.yml",
  "later_passed": false,
  "later_sid": "ANSIBLE9998",
  "later_lineno": null,
  "later_message": "Standards version not set. Using latest standards version 0.2"
}

Added a custom parser for this now.
Also released it in:

I installed the new version of the "Violation Comments to GitLab Plugin" and tested it with ansible-later. It worked wonderfully.

Thank you!