|
| 1 | +#! /usr/bin/env python3 |
| 2 | + |
| 3 | +# SPDX-FileCopyrightText: 2021 microDev |
| 4 | +# |
| 5 | +# SPDX-License-Identifier: MIT |
| 6 | + |
| 7 | +# GraphQL Query |
| 8 | + |
| 9 | +QUERY_COMMITS = """ |
| 10 | +query ($owner: String!, $name: String!, $pullNumber: Int!, $commitsPerPage: Int!, $beforeCommit: String) { |
| 11 | + repository(owner: $owner, name: $name) { |
| 12 | + pullRequest(number: $pullNumber) { |
| 13 | + commits(last: $commitsPerPage, before: $beforeCommit) { |
| 14 | + totalCount |
| 15 | + pageInfo { |
| 16 | + startCursor |
| 17 | + hasPreviousPage |
| 18 | + } |
| 19 | + nodes { |
| 20 | + commit { |
| 21 | + checkSuites(first: 3) { |
| 22 | + nodes { |
| 23 | + conclusion |
| 24 | + workflowRun { |
| 25 | + workflow { |
| 26 | + name |
| 27 | + } |
| 28 | + } |
| 29 | + id |
| 30 | + } |
| 31 | + totalCount |
| 32 | + } |
| 33 | + oid |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | +} |
| 40 | +""" |
| 41 | + |
| 42 | +QUERY_CHECKRUNS = """ |
| 43 | +query ($checkSuiteID: ID!, |
| 44 | + $afterFailedRun: String, $afterIncompleteRun: String, |
| 45 | + $includeFailedRuns: Boolean!, $includeIncompleteRuns: Boolean!) { |
| 46 | + node(id: $checkSuiteID) { |
| 47 | + ... on CheckSuite { |
| 48 | + failedRuns: checkRuns( |
| 49 | + first: 100 |
| 50 | + after: $afterFailedRun |
| 51 | + filterBy: {checkType: LATEST, conclusions: [ACTION_REQUIRED, TIMED_OUT, CANCELLED, FAILURE, NEUTRAL, STARTUP_FAILURE]} |
| 52 | + ) @include(if: $includeFailedRuns) { |
| 53 | + nodes { |
| 54 | + name |
| 55 | + } |
| 56 | + pageInfo { |
| 57 | + endCursor |
| 58 | + hasNextPage |
| 59 | + } |
| 60 | + } |
| 61 | + incompleteRuns: checkRuns( |
| 62 | + first: 100 |
| 63 | + after: $afterIncompleteRun |
| 64 | + filterBy: {checkType: LATEST, statuses: [QUEUED, IN_PROGRESS, WAITING, PENDING, REQUESTED]} |
| 65 | + ) @include(if: $includeIncompleteRuns) { |
| 66 | + nodes { |
| 67 | + name |
| 68 | + } |
| 69 | + pageInfo { |
| 70 | + endCursor |
| 71 | + hasNextPage |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + } |
| 76 | +} |
| 77 | +""" |
| 78 | + |
| 79 | + |
| 80 | +import os |
| 81 | +import re |
| 82 | +import json |
| 83 | +import requests |
| 84 | + |
| 85 | + |
| 86 | +query_variables_commits = { |
| 87 | + "owner": "", |
| 88 | + "name": "", |
| 89 | + "pullNumber": int(os.environ["PULL"]), |
| 90 | + "commitsPerPage": 20, |
| 91 | + "beforeCommit": None, |
| 92 | +} |
| 93 | + |
| 94 | + |
| 95 | +query_variables_checkruns = { |
| 96 | + "checkSuiteID": "", |
| 97 | + "afterFailedRun": None, |
| 98 | + "afterIncompleteRun": None, |
| 99 | + "includeFailedRuns": True, |
| 100 | + "includeIncompleteRuns": True, |
| 101 | +} |
| 102 | + |
| 103 | + |
| 104 | +headers = {"Authorization": f"Bearer {os.environ['GITHUB_TOKEN']}"} |
| 105 | + |
| 106 | + |
| 107 | +class Query: |
| 108 | + def __init__(self, query, variables={}, headers={}): |
| 109 | + self.query = query |
| 110 | + self.variables = variables |
| 111 | + self.headers = headers |
| 112 | + |
| 113 | + def paginate(self, page_info, name): |
| 114 | + has_page = ( |
| 115 | + page_info["hasNextPage"] if name.startswith("after") else page_info["hasPreviousPage"] |
| 116 | + ) |
| 117 | + if has_page: |
| 118 | + self.variables[name] = ( |
| 119 | + page_info["endCursor"] if name.startswith("after") else page_info["startCursor"] |
| 120 | + ) |
| 121 | + return has_page |
| 122 | + |
| 123 | + def fetch(self): |
| 124 | + request = requests.post( |
| 125 | + "https://api.github.com/graphql", |
| 126 | + json={"query": self.query, "variables": self.variables}, |
| 127 | + headers=self.headers, |
| 128 | + ) |
| 129 | + if request.status_code == 200: |
| 130 | + return request.json() |
| 131 | + else: |
| 132 | + raise Exception("Query Failed: {}".format(request.status_code)) |
| 133 | + |
| 134 | + |
| 135 | +def set_output(name, value): |
| 136 | + if "GITHUB_OUTPUT" in os.environ: |
| 137 | + with open(os.environ["GITHUB_OUTPUT"], "at") as f: |
| 138 | + print(f"{name}={value}", file=f) |
| 139 | + else: |
| 140 | + print(f"Would set GitHub actions output {name} to '{value}'") |
| 141 | + |
| 142 | + |
| 143 | +def get_commit_and_checksuite(query_commits): |
| 144 | + commits = query_commits.fetch()["data"]["repository"]["pullRequest"]["commits"] |
| 145 | + |
| 146 | + if commits["totalCount"] > 0: |
| 147 | + for commit in reversed(commits["nodes"]): |
| 148 | + commit = commit["commit"] |
| 149 | + commit_sha = commit["oid"] |
| 150 | + if commit_sha == os.environ["EXCLUDE_COMMIT"]: |
| 151 | + continue |
| 152 | + checksuites = commit["checkSuites"] |
| 153 | + if checksuites["totalCount"] > 0: |
| 154 | + for checksuite in checksuites["nodes"]: |
| 155 | + if checksuite["workflowRun"]["workflow"]["name"] == "Build CI": |
| 156 | + return [ |
| 157 | + commit_sha, |
| 158 | + checksuite["id"] if checksuite["conclusion"] != "SUCCESS" else None, |
| 159 | + ] |
| 160 | + else: |
| 161 | + if query_commits.paginate(commits["pageInfo"], "beforeCommit"): |
| 162 | + return get_commit_and_checksuite(query_commits) |
| 163 | + |
| 164 | + return [None, None] |
| 165 | + |
| 166 | + |
| 167 | +def append_runs_to_list(runs, list): |
| 168 | + regex_matrix = re.compile("^build-[^ ]+") |
| 169 | + regex_board = re.compile("\([^ ]+\)$") |
| 170 | + for run in runs["nodes"]: |
| 171 | + name = run["name"] |
| 172 | + res_matrix = regex_matrix.search(name) |
| 173 | + if res_matrix: |
| 174 | + matrix = res_matrix.group() |
| 175 | + if matrix not in list: |
| 176 | + list[matrix] = [] |
| 177 | + list[matrix].append(regex_board.search(name).group()[1:-1]) |
| 178 | + |
| 179 | + |
| 180 | +def get_bad_checkruns(query_checkruns, list={}): |
| 181 | + checkruns = query_checkruns.fetch()["data"]["node"] |
| 182 | + run_types = ["failed", "incomplete"] |
| 183 | + paginate = False |
| 184 | + |
| 185 | + for run_type in run_types: |
| 186 | + run_type_camel = run_type.capitalize() + "Run" |
| 187 | + run_type = run_type + "Runs" |
| 188 | + |
| 189 | + append_runs_to_list(checkruns[run_type], list) |
| 190 | + |
| 191 | + if query_checkruns.paginate(checkruns[run_type]["pageInfo"], "after" + run_type_camel): |
| 192 | + query_checkruns.variables["include" + run_type_camel] = True |
| 193 | + paginate = True |
| 194 | + |
| 195 | + return get_bad_checkruns(query_checkruns, list) if paginate else list |
| 196 | + |
| 197 | + |
| 198 | +def main(): |
| 199 | + query_commits = Query(QUERY_COMMITS, query_variables_commits, headers) |
| 200 | + query_commits.variables["owner"], query_commits.variables["name"] = os.environ["REPO"].split( |
| 201 | + "/" |
| 202 | + ) |
| 203 | + |
| 204 | + commit, checksuite = get_commit_and_checksuite(query_commits) |
| 205 | + |
| 206 | + if checksuite is None: |
| 207 | + if commit is None: |
| 208 | + print("No checkSuites found -> Abort") |
| 209 | + else: |
| 210 | + set_output("commit", commit) |
| 211 | + quit() |
| 212 | + |
| 213 | + query_checkruns = Query(QUERY_CHECKRUNS, query_variables_checkruns, headers) |
| 214 | + query_checkruns.variables["checkSuiteID"] = checksuite |
| 215 | + |
| 216 | + checkruns = get_bad_checkruns(query_checkruns) |
| 217 | + |
| 218 | + if len(checkruns) == 0: |
| 219 | + print("No checkRuns found -> Abort") |
| 220 | + quit() |
| 221 | + |
| 222 | + set_output("commit", commit) |
| 223 | + set_output("checkruns", json.dumps(checkruns)) |
| 224 | + |
| 225 | + |
| 226 | +if __name__ == "__main__": |
| 227 | + main() |
0 commit comments