|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +""" |
| 4 | +renovate-config-validae output looks like this: |
| 5 | +
|
| 6 | +``` |
| 7 | +INFO: Validating renovate.json |
| 8 | +ERROR: renovate.json contains errors |
| 9 | + "errors": [ |
| 10 | + { |
| 11 | + "topic": "Configuration Error", |
| 12 | + "message": "Invalid configuration option: packageRules[0].matchUpdateTypesd" |
| 13 | + } |
| 14 | + ] |
| 15 | +``` |
| 16 | +
|
| 17 | +Bad JSON parses like this |
| 18 | +``` |
| 19 | +WARN: linters/renovate/test_data/bad_renovate.json could not be parsed |
| 20 | + "err": { |
| 21 | + "lineNumber": 12, |
| 22 | + "columnNumber": 1, |
| 23 | + "message": "JSON5: invalid end of input at 12:1", |
| 24 | + "stack": "SyntaxError: JSON5: invalid end of input at 12:1\n at syntaxError (/home/eli/.cache/trunk/tools/renovate/34.122.0-55cc21ab9187b50133f5b632efea6883/node_modules/json5/lib/parse.js:1110:17)\n at invalidEOF (/home/eli/.cache/trunk/tools/renovate/34.122.0-55cc21ab9187b50133f5b632efea6883/node_modules/json5/lib/parse.js:1059:12)\n at Object.afterPropertyValue (/home/eli/.cache/trunk/tools/renovate/34.122.0-55cc21ab9187b50133f5b632efea6883/node_modules/json5/lib/parse.js:915:19)\n at Object.parse (/home/eli/.cache/trunk/tools/renovate/34.122.0-55cc21ab9187b50133f5b632efea6883/node_modules/json5/lib/parse.js:32:32)\n at getParsedContent (/home/eli/.cache/trunk/tools/renovate/34.122.0-55cc21ab9187b50133f5b632efea6883/node_modules/renovate/lib/workers/global/config/parse/file.ts:20:20)\n at /home/eli/.cache/trunk/tools/renovate/34.122.0-55cc21ab9187b50133f5b632efea6883/node_modules/renovate/lib/config-validator.ts:62:31" |
| 25 | + } |
| 26 | +``` |
| 27 | +""" |
| 28 | + |
| 29 | +import json |
| 30 | +import re |
| 31 | +import sys |
| 32 | + |
| 33 | + |
| 34 | +def to_result_sarif( |
| 35 | + path: str, line_number: int, column_number: int, rule_id: str, message: str |
| 36 | +): |
| 37 | + return { |
| 38 | + "level": "error", |
| 39 | + "locations": [ |
| 40 | + { |
| 41 | + "physicalLocation": { |
| 42 | + "artifactLocation": { |
| 43 | + "uri": path, |
| 44 | + }, |
| 45 | + "region": { |
| 46 | + "startColumn": column_number, |
| 47 | + "startLine": line_number, |
| 48 | + }, |
| 49 | + } |
| 50 | + } |
| 51 | + ], |
| 52 | + "message": { |
| 53 | + "text": message, |
| 54 | + }, |
| 55 | + "ruleId": rule_id, |
| 56 | + } |
| 57 | + |
| 58 | + |
| 59 | +def main(argv): |
| 60 | + results = [] |
| 61 | + content = sys.stdin.read() |
| 62 | + |
| 63 | + parse_reg = "(.*WARN:.*could not be parsed)(.*)" |
| 64 | + error_section = content.find('"errors": [') |
| 65 | + |
| 66 | + parse_result = re.fullmatch(parse_reg, content, flags=re.DOTALL) |
| 67 | + if parse_result: |
| 68 | + warn_section = parse_result.group(2) |
| 69 | + json_content = "{" + warn_section + "}" |
| 70 | + error_output = json.loads(json_content) |
| 71 | + err = error_output.get("err") |
| 72 | + results.append( |
| 73 | + to_result_sarif( |
| 74 | + ".", err["lineNumber"], err["columnNumber"], "JSON", err["message"] |
| 75 | + ) |
| 76 | + ) |
| 77 | + pass |
| 78 | + # TODO - fix this up to read the exit code once that is available in the parser |
| 79 | + elif content.find("Config validated successfully") != -1: |
| 80 | + pass |
| 81 | + elif error_section == -1: |
| 82 | + # could not parse - dumping all content as the error |
| 83 | + results.append(to_result_sarif(".", 0, 0, "error", content)) |
| 84 | + else: |
| 85 | + # Parse the output as json |
| 86 | + json_content = "{" + content[error_section:] + "}" |
| 87 | + error_output = json.loads(json_content) |
| 88 | + for entry in error_output.get("errors", []): |
| 89 | + results.append(to_result_sarif(".", 0, 0, entry["topic"], entry["message"])) |
| 90 | + |
| 91 | + sarif = { |
| 92 | + "$schema": "https://raw.githubusercontent.com/oasis-tcs/sarif-spec/master/Schemata/sarif-schema-2.1.0.json", |
| 93 | + "version": "2.1.0", |
| 94 | + "runs": [{"results": results}], |
| 95 | + } |
| 96 | + |
| 97 | + print(json.dumps(sarif, indent=2)) |
| 98 | + |
| 99 | + |
| 100 | +if __name__ == "__main__": |
| 101 | + main(sys.argv) |
0 commit comments