1#!/usr/bin/env python3
2# coding: utf-8
3#
4# © 2023 Qualcomm Innovation Center, Inc. All rights reserved.
5#
6# SPDX-License-Identifier: BSD-3-Clause
7
8"""
9Run as a part of gitlab CI, after Parasoft reports have been generated.
10
11This script converts the Parasoft XML-format report to a Code Climate
12compatible json file, that gitlab code quality can interpret.
13"""
14
15import xml.etree.ElementTree as ET
16import json
17import argparse
18import sys
19import os
20
21argparser = argparse.ArgumentParser(
22    description="Convert Parasoft XML to Code Climate JSON")
23argparser.add_argument('input', type=argparse.FileType('r'), nargs='?',
24                       default=sys.stdin, help="the Parasoft XML input")
25argparser.add_argument('--output', '-o', type=argparse.FileType('w'),
26                       default=sys.stdout, help="the Code Climate JSON output")
27args = argparser.parse_args()
28
29tree = ET.parse(args.input)
30
31parasoft_viols = tree.findall(".//MetViol")
32
33cc_viols = []
34
35severity_map = {
36    1: "blocker",
37    2: "critical",
38    3: "major",
39    4: "minor",
40    5: "info",
41}
42
43# info warning between 15-20
44start__threshold = 15
45end__threshold = 20
46
47
48def cc_info_warning(msg, sev):
49    warning = int(msg.split(" ")[1])
50    if (warning >= start__threshold) and (warning <= end__threshold):
51        return 5
52    else:
53        return sev
54
55
56cc_viols = [
57    ({
58        "type": "issue",
59        "categories": ["Bug Risk"],
60        "severity": (severity_map[cc_info_warning(v.attrib['msg'],
61                                                  int(v.attrib['sev']))]),
62        "check_name": v.attrib['rule'],
63        "description": (v.attrib['msg'] + '. ' +
64                        v.attrib['rule.header'] + '. (' +
65                        v.attrib['rule'] + ')'),
66        "fingerprint": v.attrib['unbViolId'],
67        "location": {
68            "path": v.attrib['locFile'].split(os.sep, 2)[2],
69            "lines": {
70                "begin": int(v.attrib['locStartln']),
71                "end": int(v.attrib['locEndLn'])
72            }
73        }
74    })
75    for v in parasoft_viols]
76
77args.output.write(json.dumps(cc_viols))
78args.output.close()
79