|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one or more |
| 2 | +# contributor license agreements. See the NOTICE file distributed with |
| 3 | +# this work for additional information regarding copyright ownership. |
| 4 | +# The ASF licenses this file to You under the Apache License, Version 2.0 |
| 5 | +# (the "License"); you may not use this file except in compliance with |
| 6 | +# the License. You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +from glob import glob |
| 17 | +import logging |
| 18 | +import os |
| 19 | +import os.path |
| 20 | +import sys |
| 21 | +from typing import Tuple, Optional |
| 22 | +import xml.etree.ElementTree |
| 23 | + |
| 24 | + |
| 25 | +logger = logging.getLogger() |
| 26 | +logger.setLevel(logging.DEBUG) |
| 27 | +handler = logging.StreamHandler(sys.stderr) |
| 28 | +handler.setLevel(logging.DEBUG) |
| 29 | +logger.addHandler(handler) |
| 30 | + |
| 31 | + |
| 32 | +def get_env(key: str) -> str: |
| 33 | + value = os.getenv(key) |
| 34 | + logger.debug(f"Read env {key}: {value}") |
| 35 | + return value |
| 36 | + |
| 37 | + |
| 38 | +def parse_report(workspace_path, fp) -> Tuple[int, int]: |
| 39 | + stack = [] |
| 40 | + errors = [] |
| 41 | + file_count = 0 |
| 42 | + error_count = 0 |
| 43 | + for (event, elem) in xml.etree.ElementTree.iterparse(fp, events=["start", "end"]): |
| 44 | + if event == "start": |
| 45 | + stack.append(elem) |
| 46 | + if elem.tag == "file": |
| 47 | + file_count += 1 |
| 48 | + errors.clear() |
| 49 | + if elem.tag == "error": |
| 50 | + logger.debug(f"Found checkstyle error: {elem.attrib}") |
| 51 | + errors.append(elem) |
| 52 | + error_count += 1 |
| 53 | + elif event == "end": |
| 54 | + if elem.tag == "file" and len(errors) > 0: |
| 55 | + filename = elem.get("name") |
| 56 | + rel_path = os.path.relpath(filename, workspace_path) |
| 57 | + logger.debug(f"Outputting errors for file: {elem.attrib}") |
| 58 | + for error in errors: |
| 59 | + line = error.get("line") |
| 60 | + col = error.get("column") |
| 61 | + severity = error.get("severity") |
| 62 | + message = error.get('message') |
| 63 | + title = f"Checkstyle {severity}" |
| 64 | + print(f"::notice file={rel_path},line={line},col={col},title={title}::{message}") |
| 65 | + stack.pop() |
| 66 | + else: |
| 67 | + logger.error(f"Unhandled xml event {event}: {elem}") |
| 68 | + return file_count, error_count |
| 69 | + |
| 70 | + |
| 71 | +if __name__ == "__main__": |
| 72 | + """ |
| 73 | + Parse checkstyle XML reports and generate GitHub annotations. |
| 74 | +
|
| 75 | + See: https://docs.github.com/en/actions/writing-workflows/choosing-what-your-workflow-does/workflow-commands-for-github-actions#setting-a-notice-message |
| 76 | + """ |
| 77 | + if not os.getenv("GITHUB_WORKSPACE"): |
| 78 | + print("This script is intended to by run by GitHub Actions.") |
| 79 | + exit(1) |
| 80 | + |
| 81 | + reports = glob(pathname="**/checkstyle/*.xml", recursive=True) |
| 82 | + logger.debug(f"Found {len(reports)} checkstyle reports") |
| 83 | + total_file_count = 0 |
| 84 | + total_error_count = 0 |
| 85 | + |
| 86 | + workspace_path = get_env("GITHUB_WORKSPACE") # e.g., /home/runner/work/apache/kafka |
| 87 | + |
| 88 | + for report in reports: |
| 89 | + with open(report, "r") as fp: |
| 90 | + logger.debug(f"Parsing report file: {report}") |
| 91 | + file_count, error_count = parse_report(workspace_path, fp) |
| 92 | + if error_count == 1: |
| 93 | + logger.debug(f"Checked {file_count} files from {report} and found 1 error") |
| 94 | + else: |
| 95 | + logger.debug(f"Checked {file_count} files from {report} and found {error_count} errors") |
| 96 | + total_file_count += file_count |
| 97 | + total_error_count += error_count |
| 98 | + exit(0) |
0 commit comments