|
37 | 37 | def main() -> None: |
38 | 38 | """Main script entry point""" |
39 | 39 | rules_dict = {} |
40 | | - issue_list = {} |
| 40 | + issue_list = [] |
41 | 41 | lines = sys.stdin.read().splitlines() |
42 | 42 | i = 0 |
43 | | - line_no, file_path, start_col, end_col = None, None, None, None |
44 | | - rule_id, message = None, None |
| 43 | + sonar_issue = None |
| 44 | + issue_range = {} |
45 | 45 | nblines = len(lines) |
| 46 | + end_line = None |
46 | 47 | while i < nblines: |
47 | 48 | line = lines[i] |
48 | 49 | # Search for pattern like "sonar/projects.py:196:13: B904 Within an `except` clause, raise exceptions" |
49 | 50 | if m := re.match(r"^([^:]+):(\d+):(\d+): ([A-Z0-9]+)( \[\*\])? (.+)$", line): |
50 | | - if line_no is not None: |
51 | | - sonar_issue = { |
52 | | - "ruleId": f"{TOOLNAME}:{rule_id}", |
53 | | - "effortMinutes": 5, |
54 | | - "primaryLocation": { |
55 | | - "message": message, |
56 | | - "filePath": file_path, |
57 | | - "textRange": { |
58 | | - "startLine": line_no, |
59 | | - "endLine": line_no, |
60 | | - "startColumn": start_col, |
61 | | - "endColumn": end_col, |
62 | | - }, |
63 | | - }, |
64 | | - } |
65 | | - issue_list[f"{rule_id} - {message}"] = sonar_issue |
66 | | - rules_dict[f"{TOOLNAME}:{rule_id}"] = { |
67 | | - "id": f"{TOOLNAME}:{rule_id}", |
68 | | - "name": f"{TOOLNAME}:{rule_id}", |
69 | | - "description": message, |
70 | | - "engineId": TOOLNAME, |
71 | | - "type": "CODE_SMELL", |
72 | | - "severity": "MAJOR", |
73 | | - "cleanCodeAttribute": "LOGICAL", |
74 | | - "impacts": [{"softwareQuality": "MAINTAINABILITY", "severity": "MEDIUM"}], |
75 | | - } |
| 51 | + if sonar_issue is not None: |
| 52 | + print("WRITE:", str(sonar_issue), file=sys.stderr) |
| 53 | + issue_list.append(sonar_issue) |
| 54 | + end_line = None |
76 | 55 | file_path = m.group(1) |
77 | | - line_no = int(m.group(2)) |
78 | | - start_col = int(m.group(3)) - 1 |
79 | | - end_col = start_col + 1 |
| 56 | + issue_range = { |
| 57 | + "startLine": int(m.group(2)), |
| 58 | + "endLine": int(m.group(2)), |
| 59 | + "startColumn": int(m.group(3)) - 1, |
| 60 | + "endColumn": int(m.group(3)), |
| 61 | + } |
80 | 62 | rule_id = m.group(4) |
81 | 63 | message = m.group(6) |
82 | | - elif m := re.match(r"\s*\|\s(\s*)(\^+)", lines[i]): |
83 | | - end_col = start_col + len(m.group(2)) |
| 64 | + sonar_issue = { |
| 65 | + "ruleId": f"{TOOLNAME}:{rule_id}", |
| 66 | + "effortMinutes": 5, |
| 67 | + "primaryLocation": { |
| 68 | + "message": m.group(6), |
| 69 | + "filePath": file_path, |
| 70 | + "textRange": issue_range, |
| 71 | + }, |
| 72 | + } |
| 73 | + rules_dict[f"{TOOLNAME}:{rule_id}"] = { |
| 74 | + "id": f"{TOOLNAME}:{rule_id}", |
| 75 | + "name": f"{TOOLNAME}:{rule_id}", |
| 76 | + "description": message, |
| 77 | + "engineId": TOOLNAME, |
| 78 | + "type": "CODE_SMELL", |
| 79 | + "severity": "MAJOR", |
| 80 | + "cleanCodeAttribute": "LOGICAL", |
| 81 | + "impacts": [{"softwareQuality": "MAINTAINABILITY", "severity": "MEDIUM"}], |
| 82 | + } |
| 83 | + elif m := re.match(r"\s+\|\s\|(_+)\^.*", lines[i]): |
| 84 | + issue_range["endLine"] = end_line or issue_range["startLine"] |
| 85 | + end_line = None |
| 86 | + issue_range["endColumn"] = 0 if rule_id == "I001" else len(m.group(1)) |
| 87 | + elif m := re.match(r"\s*(\d+)\s\|\s\|.*$", lines[i]): |
| 88 | + end_line = int(m.group(1)) |
84 | 89 | i += 1 |
85 | 90 |
|
86 | | - external_issues = {"rules": list(rules_dict.values()), "issues": list(issue_list.values())} |
| 91 | + external_issues = {"rules": list(rules_dict.values()), "issues": issue_list} |
87 | 92 | print(json.dumps(external_issues, indent=3, separators=(",", ": "))) |
88 | 93 |
|
89 | 94 |
|
|
0 commit comments