Skip to content

Commit c94f83f

Browse files
committed
fix pagination and some refactoring
1 parent 565bbd5 commit c94f83f

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

tools/ci_changes_per_commit.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -111,13 +111,11 @@ def __init__(self, query, variables={}, headers={}):
111111
self.headers = headers
112112

113113
def paginate(self, page_info, name):
114-
has_page = (
115-
page_info["hasNextPage"] if name.startswith("after") else page_info["hasPreviousPage"]
116-
)
114+
has_page = page_info["hasNextPage" if name.startswith("after") else "hasPreviousPage"]
117115
if has_page:
118-
self.variables[name] = (
119-
page_info["endCursor"] if name.startswith("after") else page_info["startCursor"]
120-
)
116+
self.variables[name] = page_info[
117+
"endCursor" if name.startswith("after") else "startCursor"
118+
]
121119
return has_page
122120

123121
def fetch(self):
@@ -142,32 +140,30 @@ def set_output(name, value):
142140

143141

144142
def get_commit_depth_and_check_suite(query_commits):
143+
commit_depth = 0
145144
while True:
146145
commits = query_commits.fetch()["data"]["repository"]["pullRequest"]["commits"]
147-
148146
if commits["totalCount"] > 0:
149147
nodes = commits["nodes"]
150148
nodes.reverse()
151149
if nodes[0]["commit"]["oid"] == os.environ["EXCLUDE_COMMIT"]:
152150
nodes.pop(0)
153-
for index, commit in enumerate(nodes):
151+
for commit in nodes:
152+
commit_depth += 1
154153
commit = commit["commit"]
155154
commit_sha = commit["oid"]
156155
check_suites = commit["checkSuites"]
157156
if check_suites["totalCount"] > 0:
158157
for check_suite in check_suites["nodes"]:
159158
if check_suite["workflowRun"]["workflow"]["name"] == "Build CI":
160159
return [
161-
{"sha": commit_sha, "depth": index + 1},
160+
{"sha": commit_sha, "depth": commit_depth},
162161
check_suite["id"]
163162
if check_suite["conclusion"] != "SUCCESS"
164163
else None,
165164
]
166-
else:
167-
if not query_commits.paginate(commits["pageInfo"], "beforeCommit"):
168-
break
169-
170-
return [None, None]
165+
if not query_commits.paginate(commits["pageInfo"], "beforeCommit"):
166+
return [None, None]
171167

172168

173169
def append_runs_to_list(runs, bad_runs_by_matrix):
@@ -188,9 +184,10 @@ def append_runs_to_list(runs, bad_runs_by_matrix):
188184
def get_bad_check_runs(query_check_runs):
189185
more_pages = True
190186
bad_runs_by_matrix = {}
187+
run_types = ["failed", "incomplete"]
188+
191189
while more_pages:
192190
check_runs = query_check_runs.fetch()["data"]["node"]
193-
run_types = ["failed", "incomplete"]
194191
more_pages = False
195192

196193
for run_type in run_types:
@@ -221,19 +218,19 @@ def main():
221218

222219
commit, check_suite = get_commit_depth_and_check_suite(query_commits)
223220

224-
if check_suite is None:
225-
if commit is None:
226-
print("Abort: No check suite found")
227-
else:
221+
if not check_suite:
222+
if commit:
228223
set_commit(commit)
224+
else:
225+
print("Abort: No check suite found")
229226
quit()
230227

231228
query_check_runs = Query(QUERY_CHECK_RUNS, query_variables_check_runs, headers)
232229
query_check_runs.variables["checkSuiteID"] = check_suite
233230

234231
check_runs = get_bad_check_runs(query_check_runs)
235232

236-
if len(check_runs) == 0:
233+
if not check_runs:
237234
print("Abort: No check runs found")
238235
quit()
239236

0 commit comments

Comments
 (0)