|
| 1 | +import requests |
| 2 | +from mdutils.mdutils import MdUtils |
| 3 | +import os, sys |
| 4 | +import json |
| 5 | +import re |
| 6 | +import logging |
| 7 | + |
| 8 | +class APICall(): |
| 9 | + """A GitHub API call""" |
| 10 | + |
| 11 | + def __init__(self, endpoint='', num_commits=1): |
| 12 | + self.token = os.environ.get('GITHUB_TOKEN') |
| 13 | + self.base_url = os.environ.get('BASE_URL') |
| 14 | + self.endpoint = endpoint |
| 15 | + self.url = f"{self.base_url}/{self.endpoint}" #Could use a path join? |
| 16 | + self.num_commits = num_commits |
| 17 | + self.header = { |
| 18 | + "Accept": "application/vnd.github.v3+json", |
| 19 | + "Authorization": f"Bearer {self.token}", |
| 20 | + "X-GitHub-Api-Version": "2022-11-28", |
| 21 | + "Accept": "application/vnd.github.raw" |
| 22 | + } |
| 23 | + |
| 24 | +class Log(): |
| 25 | + """A Regression Test log file.""" |
| 26 | + |
| 27 | + def __init__(self, machine): |
| 28 | + """Create the log file object for a specific machine.""" |
| 29 | + self.machine = machine.lower() |
| 30 | + self.text_per_log = [] |
| 31 | + |
| 32 | + def call_API(self, endpoint): |
| 33 | + """Call the GitHub API to get information about the log file.""" |
| 34 | + |
| 35 | + api_call = APICall(endpoint) |
| 36 | + response = requests.get(api_call.url, headers=api_call.header) |
| 37 | + if response.status_code != 200: |
| 38 | + logging.warning(response) |
| 39 | + print(response) |
| 40 | + sys.exit(1) |
| 41 | + response = json.loads(response.text) |
| 42 | + |
| 43 | + return response |
| 44 | + |
| 45 | + def _get_commits(self): |
| 46 | + """Get PR head and base commits. Structure of response: |
| 47 | + response = [{"head": {"sha": "a1b2c3d..."}, "base": {"sha": "b2c3d4e..."}}] |
| 48 | + See GitHub documentation for https://docs.github.com/en/rest/commits/commits?apiVersion=2022-11-28#list-commits |
| 49 | + """ |
| 50 | + response = self.call_API(f"pulls/{os.environ.get('PR_NUM')}") |
| 51 | + self.pr_head_commit = response['head']['sha'] |
| 52 | + self.pr_base_commit = response['base']['sha'] |
| 53 | + |
| 54 | + def _fetch_log_text(self, commit): |
| 55 | + """For each commit of a log, extract the log text.""" |
| 56 | + |
| 57 | + try: |
| 58 | + api_call = APICall(f"contents/tests/logs/RegressionTests_{self.machine}.log") |
| 59 | + |
| 60 | + url = api_call.url + (f"?ref={commit}") #Could use a path join? |
| 61 | + r = requests.get(url, headers=api_call.header) |
| 62 | + return r.text |
| 63 | + except: |
| 64 | + logging.error("An appropriate commit(s) was not provided. Call _get_commits() first.") |
| 65 | + |
| 66 | + def _get_test_data(self, log_instance): |
| 67 | + """For each instance of a log at a given commit, extract runtime and memory data from the log text |
| 68 | + Args: |
| 69 | + log_instance: Log text for a given commit |
| 70 | + Returns: |
| 71 | + tests_for_log_instance: A dictionary of tests (keys) with a tuple of warnings and remarks as the value for each test |
| 72 | + """ |
| 73 | + |
| 74 | + tests_for_log_instance = {} |
| 75 | + |
| 76 | + pattern = r"COMPILE \'(.*)\' \[\d+:\d+, \d+:\d+\] \( (\d+) warnings (\d+) remarks \)" |
| 77 | + log_instance = log_instance.splitlines() |
| 78 | + |
| 79 | + for line in log_instance: |
| 80 | + test_match = re.search(pattern, line) |
| 81 | + if test_match: |
| 82 | + test_name, warnings, remarks = test_match.groups() |
| 83 | + tests_for_log_instance[test_name] = (int(warnings), int(remarks)) |
| 84 | + |
| 85 | + return tests_for_log_instance |
| 86 | + |
| 87 | + def _get_pr_data(self, commit): |
| 88 | + """Extract warnings/remarks data for a particular commit. |
| 89 | + Returns: |
| 90 | + log_data: A dictionary of tests as the key with a tuple of (warnings, remarks) as the value |
| 91 | + """ |
| 92 | + try: |
| 93 | + log_text = self._fetch_log_text(commit) |
| 94 | + log_data = self._get_test_data(log_text) |
| 95 | + return log_data |
| 96 | + except: |
| 97 | + logging.error(f"No commit found for the ref {commit}") |
| 98 | + sys.exit(1) |
| 99 | + |
| 100 | + def compare_results(self, pr_log, base_log): |
| 101 | + """Compare warnings/remarks for PR head and base commits to determine whether warnings/remarks have increased.""" |
| 102 | + |
| 103 | + increases = {'warnings': {}, 'remarks': {}} |
| 104 | + |
| 105 | + for test in pr_log: |
| 106 | + # Check warnings |
| 107 | + if pr_log[test][0] > base_log[test][0]: |
| 108 | + increases['warnings'].update({test: pr_log[test][0] - base_log[test][0]}) |
| 109 | + # Check remarks |
| 110 | + if pr_log[test][1] > base_log[test][1]: |
| 111 | + increases['remarks'].update({test: pr_log[test][1] - base_log[test][1]}) |
| 112 | + |
| 113 | + return increases |
| 114 | + |
| 115 | +def print_html_results(dict): |
| 116 | + """Print the comparison results in HTML.""" |
| 117 | + |
| 118 | + pr_num = os.environ.get('PR_NUM') |
| 119 | + mdFile = MdUtils(file_name='summary.md', title=f'Increased Warnings/Remarks for PR #{pr_num}') |
| 120 | + |
| 121 | + for machine, results in dict.items(): |
| 122 | + for category in results.keys(): |
| 123 | + if results[category]: |
| 124 | + mdFile.write(f"\n<h3>{machine.upper()}</h3>\n") |
| 125 | + unordered_list = [f"**{category.title()}:**", []] |
| 126 | + for test, value in dict[machine][category].items(): |
| 127 | + unordered_list[1].append(f"{test}: {value}") |
| 128 | + mdFile.new_list(unordered_list, marked_with='*') |
| 129 | + return mdFile.get_md_text() |
| 130 | + |
| 131 | +def main(): |
| 132 | + """For each machine, create a log object, get current PR data, and determine |
| 133 | + which tests increase warnings and/or remarks on each machine.""" |
| 134 | + |
| 135 | + machines = os.environ.get('MACHINES').split() |
| 136 | + |
| 137 | + # For each machine, tests where warnings and/or remarks increase |
| 138 | + increased_warnings_remarks = {} |
| 139 | + |
| 140 | + for machine in machines: |
| 141 | + log = Log(machine) |
| 142 | + log._get_commits() |
| 143 | + log.pr_log_data = log._get_pr_data(log.pr_head_commit) |
| 144 | + log.base_log_data = log._get_pr_data(log.pr_base_commit) |
| 145 | + |
| 146 | + increased_warnings_remarks[machine] = log.compare_results(log.pr_log_data, log.base_log_data) |
| 147 | + |
| 148 | + results = print_html_results(increased_warnings_remarks) |
| 149 | + |
| 150 | + if len(results) > 81: # Length of HTML header |
| 151 | + print(results) |
| 152 | + sys.exit(1) |
| 153 | + else: |
| 154 | + sys.exit(0) |
| 155 | + |
| 156 | +if __name__ == "__main__": # pragma: no coverage |
| 157 | + |
| 158 | + main() |
0 commit comments