|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# sonar-tools |
| 4 | +# Copyright (C) 2025 Olivier Korach |
| 5 | +# mailto:olivier.korach AT gmail DOT com |
| 6 | +# |
| 7 | +# This program is free software; you can redistribute it and/or |
| 8 | +# modify it under the terms of the GNU Lesser General Public |
| 9 | +# License as published by the Free Software Foundation; either |
| 10 | +# version 3 of the License, or (at your option) any later version. |
| 11 | +# |
| 12 | +# This program is distributed in the hope that it will be useful, |
| 13 | +# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 15 | +# Lesser General Public License for more details. |
| 16 | +# |
| 17 | +# You should have received a copy of the GNU Lesser General Public License |
| 18 | +# along with this program; if not, write to the Free Software Foundation, |
| 19 | +# Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 20 | +# |
| 21 | +""" |
| 22 | +
|
| 23 | + Converts Ruff report format to Sonar external issues format |
| 24 | +
|
| 25 | +""" |
| 26 | +import sys |
| 27 | +import json |
| 28 | +import re |
| 29 | + |
| 30 | +TOOLNAME = "ruff" |
| 31 | + |
| 32 | +TRIVY_TO_MQR_MAPPING = {"CRITICAL": "BLOCKER"} |
| 33 | +MAPPING = {"LOW": "MINOR", "MEDIUM": "MAJOR", "HIGH": "CRITICAL", "BLOCKER": "BLOCKER"} |
| 34 | + |
| 35 | + |
| 36 | +def main() -> None: |
| 37 | + """Main script entry point""" |
| 38 | + rules_dict = {} |
| 39 | + issue_list = {} |
| 40 | + lines = sys.stdin.read().splitlines() |
| 41 | + i = 0 |
| 42 | + nblines = len(lines) |
| 43 | + while i < nblines: |
| 44 | + line = lines[i] |
| 45 | + i += 1 |
| 46 | + # Search for pattern like "F401 [*] `sys` imported but unused" |
| 47 | + if not (m := re.match(r"^([A-Za-z0-9]+) (\[\*\]) (.+)$", line)): |
| 48 | + continue |
| 49 | + rule_id = m.group(1) |
| 50 | + message = m.group(3) |
| 51 | + line = lines[i] |
| 52 | + i += 1 |
| 53 | + # Search for pattern like " --> cli/cust_measures.py:28:8" |
| 54 | + if not (m := re.match(r"^\s*--> ([^:]+):(\d+):(\d+)$", line)): |
| 55 | + continue |
| 56 | + file_path = m.group(1) |
| 57 | + line_no = int(m.group(2)) |
| 58 | + |
| 59 | + # Search for " | ^^^" pattern" |
| 60 | + while i < nblines and not (m := re.match(r"^\s*\|\s(\s*)(\^+)", lines[i])): |
| 61 | + i += 1 |
| 62 | + if not m: |
| 63 | + continue |
| 64 | + start_col = len(m.group(1)) |
| 65 | + end_col = start_col + len(m.group(2)) |
| 66 | + |
| 67 | + sonar_issue = { |
| 68 | + "ruleId": f"{TOOLNAME}:{rule_id}", |
| 69 | + "effortMinutes": 5, |
| 70 | + "primaryLocation": { |
| 71 | + "message": message, |
| 72 | + "filePath": file_path, |
| 73 | + "textRange": { |
| 74 | + "startLine": line_no, |
| 75 | + "endLine": line_no, |
| 76 | + "startColumn": start_col, |
| 77 | + "endColumn": end_col, |
| 78 | + }, |
| 79 | + }, |
| 80 | + } |
| 81 | + |
| 82 | + issue_list[f"{rule_id} - {message}"] = sonar_issue |
| 83 | + rules_dict[f"{TOOLNAME}:{rule_id}"] = { |
| 84 | + "id": f"{TOOLNAME}:{rule_id}", |
| 85 | + "name": f"{TOOLNAME}:{rule_id}", |
| 86 | + "description": message, |
| 87 | + "engineId": TOOLNAME, |
| 88 | + "type": "CODE_SMELL", |
| 89 | + "severity": "MAJOR", |
| 90 | + "cleanCodeAttribute": "LOGICAL", |
| 91 | + "impacts": [{"softwareQuality": "MAINTAINABILITY", "severity": "MEDIUM"}], |
| 92 | + } |
| 93 | + |
| 94 | + external_issues = {"rules": list(rules_dict.values()), "issues": list(issue_list.values())} |
| 95 | + print(json.dumps(external_issues, indent=3, separators=(",", ": "))) |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + main() |
0 commit comments