|
| 1 | +#!/usr/bin/env python3 |
| 2 | +# |
| 3 | +# Copyright 2024 The Bazel Authors. All rights reserved. |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# pylint: disable=line-too-long |
| 17 | +# pylint: disable=missing-function-docstring |
| 18 | +# pylint: disable=unspecified-encoding |
| 19 | +# pylint: disable=invalid-name |
| 20 | +"""The CI script for generate report for BCR Bazel Compatibility Test pipeline.""" |
| 21 | + |
| 22 | + |
| 23 | +import argparse |
| 24 | +import collections |
| 25 | +import os |
| 26 | +import json |
| 27 | +import re |
| 28 | +import sys |
| 29 | + |
| 30 | +import bazelci |
| 31 | +import bcr_presubmit |
| 32 | + |
| 33 | +BUILDKITE_ORG = os.environ["BUILDKITE_ORGANIZATION_SLUG"] |
| 34 | + |
| 35 | +PIPELINE = os.environ["BUILDKITE_PIPELINE_SLUG"] |
| 36 | + |
| 37 | +MODULE_VERSION_PATTERN = re.compile(r'(?P<module_version>[a-z](?:[a-z0-9._-]*[a-z0-9])?@[^\s]+)') |
| 38 | + |
| 39 | +def extract_module_version(line): |
| 40 | + match = MODULE_VERSION_PATTERN.search(line) |
| 41 | + if match: |
| 42 | + return match.group("module_version") |
| 43 | + |
| 44 | + |
| 45 | +def get_github_maintainer(module_name): |
| 46 | + metadata = json.load(open(bcr_presubmit.get_metadata_json(module_name), "r")) |
| 47 | + github_maintainers = [] |
| 48 | + for maintainer in metadata["maintainers"]: |
| 49 | + if "github" in maintainer: |
| 50 | + github_maintainers.append(maintainer["github"]) |
| 51 | + |
| 52 | + if not github_maintainers: |
| 53 | + github_maintainers.append("bazelbuild/bcr-maintainers") |
| 54 | + return github_maintainers |
| 55 | + |
| 56 | + |
| 57 | +def print_report_in_markdown(failed_jobs_per_module, pipeline_url): |
| 58 | + bazel_version = os.environ.get("USE_BAZEL_VERSION") |
| 59 | + print("\n") |
| 60 | + print("## The following modules are broken%s:" % (f" with Bazel@{bazel_version}" if bazel_version else "")) |
| 61 | + print("BCR Bazel Compatibility Test: ", pipeline_url) |
| 62 | + for module, jobs in failed_jobs_per_module.items(): |
| 63 | + module_name = module.strip().split("@")[0] |
| 64 | + github_maintainers = get_github_maintainer(module_name) |
| 65 | + print(f"### {module}") |
| 66 | + print("Maintainers: ", ", ".join(f"@{maintainer}" for maintainer in github_maintainers)) |
| 67 | + for job in jobs: |
| 68 | + print(f"- [{job['name']}]({job['web_url']})") |
| 69 | + print("\n") |
| 70 | + |
| 71 | + |
| 72 | +def main(argv=None): |
| 73 | + if argv is None: |
| 74 | + argv = sys.argv[1:] |
| 75 | + |
| 76 | + parser = argparse.ArgumentParser(description="Script to report BCR Bazel Compatibility Test result.") |
| 77 | + parser.add_argument("--build_number", type=str) |
| 78 | + |
| 79 | + args = parser.parse_args(argv) |
| 80 | + if not args.build_number: |
| 81 | + parser.print_help() |
| 82 | + return 2 |
| 83 | + |
| 84 | + client = bazelci.BuildkiteClient(org=BUILDKITE_ORG, pipeline=PIPELINE) |
| 85 | + build_info = client.get_build_info(args.build_number) |
| 86 | + failed_jobs_per_module = collections.defaultdict(list) |
| 87 | + for job in build_info["jobs"]: |
| 88 | + if job.get("state") == "failed" and "name" in job: |
| 89 | + module = extract_module_version(job["name"]) |
| 90 | + if not module: |
| 91 | + continue |
| 92 | + failed_jobs_per_module[module].append(job) |
| 93 | + |
| 94 | + print_report_in_markdown(failed_jobs_per_module, build_info["web_url"]) |
| 95 | + |
| 96 | +if __name__ == "__main__": |
| 97 | + sys.exit(main()) |
0 commit comments