|
| 1 | +"""Convert sqruff JSON lint output to SARIF 2.1.0 format.""" |
| 2 | + |
| 3 | +import hashlib |
| 4 | +import json |
| 5 | +import os |
| 6 | +from pathlib import Path |
| 7 | +from urllib.parse import quote |
| 8 | + |
| 9 | +SARIF_SCHEMA = "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json" |
| 10 | +SQRUFF_VERSION = "0.38.0" |
| 11 | +SEVERITY_MAP = {"Warning": "warning", "Error": "error", "Note": "note"} |
| 12 | + |
| 13 | + |
| 14 | +def uri(path: str) -> str: |
| 15 | + return quote(path, safe="/") |
| 16 | + |
| 17 | + |
| 18 | +def to_region(r: dict) -> dict: |
| 19 | + return { |
| 20 | + "startLine": max(r["start"]["line"], 1), |
| 21 | + "startColumn": max(r["start"]["character"], 1), |
| 22 | + "endLine": max(r["end"]["line"], 1), |
| 23 | + "endColumn": max(r["end"]["character"], 1), |
| 24 | + } |
| 25 | + |
| 26 | + |
| 27 | +def fingerprint(rule_id: str, file_uri: str, line: int, message: str) -> str: |
| 28 | + return hashlib.sha256(f"{rule_id}:{file_uri}:{line}:{message}".encode()).hexdigest() |
| 29 | + |
| 30 | + |
| 31 | +def build_rule(rule_id: str) -> dict: |
| 32 | + return { |
| 33 | + "id": rule_id, |
| 34 | + "shortDescription": {"text": f"sqruff rule {rule_id}"}, |
| 35 | + "helpUri": f"https://docs.sqruff.com/rules/{rule_id.lower()}", |
| 36 | + "properties": {"tags": ["sql", "sqruff"]}, |
| 37 | + } |
| 38 | + |
| 39 | + |
| 40 | +def build_result(file_path: str, diag: dict) -> dict: |
| 41 | + rule_id = diag.get("code") or "unknown" |
| 42 | + file_uri = uri(file_path) |
| 43 | + line = diag["range"]["start"]["line"] |
| 44 | + message = diag["message"] |
| 45 | + |
| 46 | + return { |
| 47 | + "ruleId": rule_id, |
| 48 | + "level": SEVERITY_MAP.get(diag.get("severity", "Warning"), "warning"), |
| 49 | + "message": {"text": message}, |
| 50 | + "partialFingerprints": {"sqruffFingerprint/v1": fingerprint(rule_id, file_uri, line, message)}, |
| 51 | + "locations": [{ |
| 52 | + "physicalLocation": { |
| 53 | + "artifactLocation": {"uri": file_uri, "uriBaseId": "%SRCROOT%"}, |
| 54 | + "region": to_region(diag["range"]), |
| 55 | + } |
| 56 | + }], |
| 57 | + } |
| 58 | + |
| 59 | + |
| 60 | +def build_sarif(data: dict) -> dict: |
| 61 | + pairs = [(fp, diag) for fp, diags in data.items() for diag in diags] |
| 62 | + rules = sorted({diag.get("code") for _, diag in pairs if diag.get("code")}) |
| 63 | + |
| 64 | + return { |
| 65 | + "$schema": SARIF_SCHEMA, |
| 66 | + "version": "2.1.0", |
| 67 | + "runs": [{ |
| 68 | + "tool": { |
| 69 | + "driver": { |
| 70 | + "name": "sqruff", |
| 71 | + "informationUri": "https://github.com/quarylabs/sqruff", |
| 72 | + "version": SQRUFF_VERSION, |
| 73 | + "rules": [build_rule(r) for r in rules], |
| 74 | + } |
| 75 | + }, |
| 76 | + "results": [build_result(fp, diag) for fp, diag in pairs], |
| 77 | + "artifacts": [ |
| 78 | + {"location": {"uri": uri(fp), "uriBaseId": "%SRCROOT%"}} |
| 79 | + for fp in data |
| 80 | + ], |
| 81 | + }], |
| 82 | + } |
| 83 | + |
| 84 | + |
| 85 | +def main() -> None: |
| 86 | + tmp = Path(os.environ["RUNNER_TEMP"]) |
| 87 | + input_path = tmp / "sqruff-raw.json" |
| 88 | + output_path = tmp / "sqruff.sarif" |
| 89 | + |
| 90 | + data = json.loads(input_path.read_text()) |
| 91 | + output_path.write_text(json.dumps(build_sarif(data), indent=2)) |
| 92 | + |
| 93 | + total = sum(len(v) for v in data.values()) |
| 94 | + print(f"✅ {total} violation(s) across {len(data)} file(s) → {output_path}") |
| 95 | + if not total: |
| 96 | + print("🎉 No violations — clean SARIF will auto-close stale alerts") |
| 97 | + |
| 98 | + |
| 99 | +if __name__ == "__main__": |
| 100 | + main() |
0 commit comments