Skip to content

Commit 06d2649

Browse files
authored
KAFKA-17606: Include Rat errors in GitHub workflow summary (#17280)
Reviewers: David Arthur <mumrah@gmail.com>
1 parent 35f55a8 commit 06d2649

File tree

2 files changed

+99
-0
lines changed

2 files changed

+99
-0
lines changed

.github/scripts/rat.py

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 sys
20+
import xml.etree.ElementTree
21+
22+
logger = logging.getLogger()
23+
logger.setLevel(logging.DEBUG)
24+
handler = logging.StreamHandler(sys.stderr)
25+
handler.setLevel(logging.DEBUG)
26+
logger.addHandler(handler)
27+
28+
29+
def get_env(key: str) -> str:
30+
value = os.getenv(key)
31+
logger.debug(f"Read env {key}: {value}")
32+
return value
33+
34+
35+
def parse_rat_report(fp):
36+
file_count = 0
37+
unapproved_licenses = []
38+
approved_count = 0
39+
root = xml.etree.ElementTree.parse(fp).getroot()
40+
41+
for resource in root.findall(".//resource"):
42+
file_name = resource.get("name")
43+
license_approval_elem = resource.find("license-approval")
44+
45+
if license_approval_elem is not None and license_approval_elem.get("name") == "false":
46+
file_count += 1
47+
unapproved_licenses.append(file_name)
48+
else:
49+
approved_count += 1
50+
51+
return approved_count, file_count, unapproved_licenses
52+
53+
54+
if __name__ == "__main__":
55+
"""
56+
Parse Apache Rat reports and generate GitHub annotations.
57+
"""
58+
if not os.getenv("GITHUB_WORKSPACE"):
59+
print("This script is intended to by run by GitHub Actions.")
60+
exit(1)
61+
62+
reports = glob(pathname="**/rat/*.xml", recursive=True)
63+
logger.debug(f"Found {len(reports)} Rat reports")
64+
65+
total_unapproved_licenses = 0
66+
total_approved_licenses = 0
67+
all_unapproved_files = []
68+
69+
workspace_path = get_env("GITHUB_WORKSPACE")
70+
71+
for report in reports:
72+
with open(report, "r") as fp:
73+
logger.debug(f"Parsing Rat report file: {report}")
74+
approved_count, unapproved_count, unapproved_files = parse_rat_report(fp)
75+
76+
total_approved_licenses += approved_count
77+
total_unapproved_licenses += unapproved_count
78+
all_unapproved_files.extend(unapproved_files)
79+
80+
if total_unapproved_licenses == 0:
81+
print(f"All {total_approved_licenses} files have approved licenses. No unapproved licenses found.")
82+
exit(0)
83+
else:
84+
print(f"{total_approved_licenses} approved licenses")
85+
print(f"{total_unapproved_licenses} unapproved licenses")
86+
87+
print("Files with unapproved licenses:")
88+
for file in all_unapproved_files:
89+
rel_path = os.path.relpath(file, workspace_path)
90+
message = f"File with unapproved license: {rel_path}"
91+
print(f"::notice file={rel_path},title=Unapproved License::{message}")
92+
93+
exit(1)

.github/workflows/build.yml

+6
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,12 @@ jobs:
9191
run: python .github/scripts/checkstyle.py
9292
env:
9393
GITHUB_WORKSPACE: ${{ github.workspace }}
94+
- name: Annotate Rat errors
95+
# Avoid duplicate annotations, only run on java 21
96+
if: ${{ failure() && matrix.java == '21' }}
97+
run: python .github/scripts/rat.py
98+
env:
99+
GITHUB_WORKSPACE: ${{ github.workspace }}
94100

95101
test:
96102
needs: validate

0 commit comments

Comments
 (0)