Debian Codegraph @

.ci/premerge_advisor_explain.py

raw
1 # Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
2 # See https://llvm.org/LICENSE.txt for license information.
3 # SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
4 """Script for getting explanations from the premerge advisor."""
5
6 import argparse
7 import platform
8 import sys
9 import json
10
11 import requests
12 import github
13 import github.PullRequest
14
15 import generate_test_report_lib
16
17 PREMERGE_ADVISOR_URL = (
18 "http://premerge-advisor.premerge-advisor.svc.cluster.local:5000/explain"
19 )
20 COMMENT_TAG = "<!--PREMERGE ADVISOR COMMENT: {platform}-->"
21
22
23 def get_comment_id(platform: str, pr: github.PullRequest.PullRequest) -> int | None:
24 platform_comment_tag = COMMENT_TAG.format(platform=platform)
25 for comment in pr.as_issue().get_comments():
26 if platform_comment_tag in comment.body:
27 return comment.id
28 return None
29
30
31 def get_comment(
32 github_token: str,
33 pr_number: int,
34 body: str,
35 ) -> dict[str, str]:
36 repo = github.Github(github_token).get_repo("llvm/llvm-project")
37 pr = repo.get_issue(pr_number).as_pull_request()
38 body = COMMENT_TAG.format(platform=platform.system()) + "\n" + body
39 comment = {"body": body}
40 comment_id = get_comment_id(platform.system(), pr)
41 if comment_id:
42 comment["id"] = comment_id
43 return comment
44
45
46 def main(
47 commit_sha: str,
48 build_log_files: list[str],
49 github_token: str,
50 pr_number: int,
51 return_code: int,
52 ) -> bool:
53 """The main entrypoint for the script.
54
55 This function parses failures from files, requests information from the
56 premerge advisor, and may write a Github comment depending upon the output.
57 There are four different scenarios:
58 1. There has never been a previous failure and the job passes - We do not
59 create a comment. We write out an empty file to the comment path so the
60 issue-write workflow knows not to create anything.
61 2. There has never been a previous failure and the job fails - We create a
62 new comment containing the failure information and any possible premerge
63 advisor findings.
64 3. There has been a previous failure and the job passes - We update the
65 existing comment by passing its ID and a passed message to the
66 issue-write workflow.
67 4. There has been a previous failure and the job fails - We update the
68 existing comment in the same manner as above, but generate the comment
69 as if we have a failure.
70
71 Args:
72 commit_sha: The base commit SHA for this PR run.
73 build_log_files: The list of JUnit XML files and ninja logs.
74 github_token: The token to use to access the Github API.
75 pr_number: The number of the PR associated with this run.
76 return_code: The numerical return code of ninja/CMake.
77 """
78 junit_objects, ninja_logs = generate_test_report_lib.load_info_from_files(
79 build_log_files
80 )
81 test_failures = generate_test_report_lib.get_failures(junit_objects)
82 current_platform = f"{platform.system()}-{platform.machine()}".lower()
83 explanation_request = {
84 "base_commit_sha": commit_sha,
85 "platform": current_platform,
86 "failures": [],
87 }
88 if test_failures:
89 for _, failures in test_failures.items():
90 for name, failure_messsage in failures:
91 explanation_request["failures"].append(
92 {"name": name, "message": failure_messsage}
93 )
94 elif return_code != 0:
95 ninja_failures = generate_test_report_lib.find_failure_in_ninja_logs(ninja_logs)
96 for name, failure_message in ninja_failures:
97 explanation_request["failures"].append(
98 {"name": name, "message": failure_message}
99 )
100 comments = []
101 advisor_explanations = []
102 if return_code != 0:
103 advisor_response = requests.get(
104 PREMERGE_ADVISOR_URL, json=explanation_request, timeout=5
105 )
106 if advisor_response.status_code == 200:
107 print(advisor_response.json())
108 advisor_explanations = advisor_response.json()
109 else:
110 print(advisor_response.reason)
111 report, failures_explained = generate_test_report_lib.generate_report(
112 generate_test_report_lib.compute_platform_title(),
113 return_code,
114 junit_objects,
115 ninja_logs,
116 failure_explanations_list=advisor_explanations,
117 )
118 comments.append(get_comment(github_token, pr_number, report))
119 if return_code == 0 and "id" not in comments[0]:
120 # If the job succeeds and there is not an existing comment, we
121 # should not write one to reduce noise.
122 comments = []
123 comments_file_name = f"comments-{platform.system()}-{platform.machine()}"
124 with open(comments_file_name, "w") as comment_file_handle:
125 json.dump(comments, comment_file_handle)
126 print(f"Wrote comments to {comments_file_name}")
127 return failures_explained
128
129
130 if __name__ == "__main__":
131 parser = argparse.ArgumentParser()
132 parser.add_argument("commit_sha", help="The base commit SHA for the test.")
133 parser.add_argument("return_code", help="The build's return code", type=int)
134 parser.add_argument("github_token", help="Github authentication token", type=str)
135 parser.add_argument("pr_number", help="The PR number", type=int)
136 parser.add_argument(
137 "build_log_files", help="Paths to JUnit report files and ninja logs.", nargs="*"
138 )
139 args = parser.parse_args()
140
141 # Skip looking for results on AArch64 for now because the premerge advisor
142 # service is not available on AWS currently.
143 if platform.machine() == "arm64" or platform.machine() == "aarch64":
144 sys.exit(0)
145
146 failures_explained = main(
147 args.commit_sha,
148 args.build_log_files,
149 args.github_token,
150 args.pr_number,
151 args.return_code,
152 )
153
154 if failures_explained:
155 sys.exit(0)
156
157 sys.exit(args.return_code)