Skip to content
Merged
42 changes: 25 additions & 17 deletions .ci/generate_test_report_lib.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,23 @@ def _format_ninja_failures(ninja_failures: list[tuple[str, str]]) -> list[str]:
)
return output

def get_failures(junit_objects) -> dict[str, list[tuple[str, str]]]:
failures = {}
for results in junit_objects:
for testsuite in results:
for test in testsuite:
if (
not test.is_passed
and test.result
and isinstance(test.result[0], Failure)
):
if failures.get(testsuite.name) is None:
failures[testsuite.name] = []
failures[testsuite.name].append(
(test.classname + "/" + test.name, test.result[0].text)
)
return failures


# Set size_limit to limit the byte size of the report. The default is 1MB as this
# is the most that can be put into an annotation. If the generated report exceeds
Expand All @@ -113,7 +130,7 @@ def generate_report(
size_limit=1024 * 1024,
list_failures=True,
):
failures = {}
failures = get_failures(junit_objects)
tests_run = 0
tests_skipped = 0
tests_failed = 0
Expand All @@ -124,18 +141,6 @@ def generate_report(
tests_skipped += testsuite.skipped
tests_failed += testsuite.failures

for test in testsuite:
if (
not test.is_passed
and test.result
and isinstance(test.result[0], Failure)
):
if failures.get(testsuite.name) is None:
failures[testsuite.name] = []
failures[testsuite.name].append(
(test.classname + "/" + test.name, test.result[0].text)
)

report = [f"# {title}", ""]

if tests_run == 0:
Expand Down Expand Up @@ -258,7 +263,7 @@ def plural(num_tests):
return report


def generate_report_from_files(title, return_code, build_log_files):
def load_info_from_files(build_log_files):
junit_files = [
junit_file for junit_file in build_log_files if junit_file.endswith(".xml")
]
Expand All @@ -271,6 +276,9 @@ def generate_report_from_files(title, return_code, build_log_files):
ninja_logs.append(
[log_line.strip() for log_line in ninja_log_file_handle.readlines()]
)
return generate_report(
title, return_code, [JUnitXml.fromfile(p) for p in junit_files], ninja_logs
)
return [JUnitXml.fromfile(p) for p in junit_files], ninja_logs


def generate_report_from_files(title, return_code, build_log_files):
junit_objects, ninja_logs = load_info_from_files(build_log_files)
return generate_report(title, return_code, junit_objects, ninja_logs)
47 changes: 47 additions & 0 deletions .ci/premerge_advisor_upload.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
"""Script for uploading results to the premerge advisor."""

import argparse
import os

import requests

import generate_test_report_lib

PREMERGE_ADVISOR_URL = "http://premerge-advisor:5000/upload"


def main(commit_sha, workflow_run_number, build_log_files):
junit_objects, ninja_logs = generate_test_report_lib.load_info_from_files(
build_log_files
)
test_failures = generate_test_report_lib.get_failures(junit_objects)
source = "pull_request" if "GITHUB_ACTIONS" in os.environ else "postcommit"
failure_info = {
"source_type": source,
"base_commit_sha": commit_sha,
"source_id": workflow_run_number,
"failures": [],
}
if test_failures:
for name, failure_message in test_failures:
failure_info["failures"].append({"name": name, "message": failure_message})
else:
ninja_failures = generate_test_report_lib.find_failure_in_ninja_logs(ninja_logs)
for name, failure_message in ninja_failures:
failure_info["failures"].append({"name": name, "message": failure_message})
requests.post(PREMERGE_ADVISOR_URL, json=failure_info)


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("commit_sha", help="The base commit SHA for the test.")
parser.add_argument("workflow_run_number", help="The run number from GHA.")
parser.add_argument(
"build_log_files", help="Paths to JUnit report files and ninja logs.", nargs="*"
)
args = parser.parse_args()

main(args.commit_sha, args.workflow_run_number, args.build_log_files)
6 changes: 6 additions & 0 deletions .ci/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,12 @@ function at-exit {
$retcode "${BUILD_DIR}"/test-results.*.xml "${MONOREPO_ROOT}"/ninja*.log \
>> $GITHUB_STEP_SUMMARY
fi

if [[ "$retcode" != "0" ]]; then
python "${MONOREPO_ROOT}"/.ci/premerge_advisor_upload.py \
$(git rev-parse HEAD~1) $GITHUB_RUN_NUMBER \
"${BUILD_DIR}"/test-results.*.xml "${MONOREPO_ROOT}"/ninja*.log
fi
}
trap at-exit EXIT

Expand Down