1818 }
1919 nodes {
2020 commit {
21- checkSuites(first: 3 ) {
21+ checkSuites(first: 100 ) {
2222 nodes {
2323 conclusion
2424 workflowRun {
3939}
4040"""
4141
42- QUERY_CHECKRUNS = """
42+ QUERY_CHECK_RUNS = """
4343query ($checkSuiteID: ID!,
4444 $afterFailedRun: String, $afterIncompleteRun: String,
4545 $includeFailedRuns: Boolean!, $includeIncompleteRuns: Boolean!) {
9292}
9393
9494
95- query_variables_checkruns = {
95+ query_variables_check_runs = {
9696 "checkSuiteID" : "" ,
9797 "afterFailedRun" : None ,
9898 "afterIncompleteRun" : None ,
@@ -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 ):
@@ -141,28 +139,31 @@ def set_output(name, value):
141139 print (f"Would set GitHub actions output { name } to '{ value } '" )
142140
143141
144- def get_commit_and_checksuite (query_commits ):
145- commits = query_commits .fetch ()["data" ]["repository" ]["pullRequest" ]["commits" ]
146-
147- if commits ["totalCount" ] > 0 :
148- for commit in reversed (commits ["nodes" ]):
149- commit = commit ["commit" ]
150- commit_sha = commit ["oid" ]
151- if commit_sha == os .environ ["EXCLUDE_COMMIT" ]:
152- continue
153- checksuites = commit ["checkSuites" ]
154- if checksuites ["totalCount" ] > 0 :
155- for checksuite in checksuites ["nodes" ]:
156- if checksuite ["workflowRun" ]["workflow" ]["name" ] == "Build CI" :
157- return [
158- commit_sha ,
159- checksuite ["id" ] if checksuite ["conclusion" ] != "SUCCESS" else None ,
160- ]
161- else :
162- if query_commits .paginate (commits ["pageInfo" ], "beforeCommit" ):
163- return get_commit_and_checksuite (query_commits )
164-
165- return [None , None ]
142+ def get_commit_depth_and_check_suite (query_commits ):
143+ commit_depth = 0
144+ while True :
145+ commits = query_commits .fetch ()["data" ]["repository" ]["pullRequest" ]["commits" ]
146+ if commits ["totalCount" ] > 0 :
147+ nodes = commits ["nodes" ]
148+ nodes .reverse ()
149+ if nodes [0 ]["commit" ]["oid" ] == os .environ ["EXCLUDE_COMMIT" ]:
150+ nodes .pop (0 )
151+ for commit in nodes :
152+ commit_depth += 1
153+ commit = commit ["commit" ]
154+ commit_sha = commit ["oid" ]
155+ check_suites = commit ["checkSuites" ]
156+ if check_suites ["totalCount" ] > 0 :
157+ for check_suite in check_suites ["nodes" ]:
158+ if check_suite ["workflowRun" ]["workflow" ]["name" ] == "Build CI" :
159+ return [
160+ {"sha" : commit_sha , "depth" : commit_depth },
161+ check_suite ["id" ]
162+ if check_suite ["conclusion" ] != "SUCCESS"
163+ else None ,
164+ ]
165+ if not query_commits .paginate (commits ["pageInfo" ], "beforeCommit" ):
166+ return [None , None ]
166167
167168
168169def append_runs_to_list (runs , bad_runs_by_matrix ):
@@ -180,53 +181,61 @@ def append_runs_to_list(runs, bad_runs_by_matrix):
180181 bad_runs_by_matrix [matrix ].append (res_board .group ()[1 :- 1 ])
181182
182183
183- def get_bad_checkruns ( query_checkruns ):
184+ def get_bad_check_runs ( query_check_runs ):
184185 more_pages = True
185186 bad_runs_by_matrix = {}
187+ run_types = ["failed" , "incomplete" ]
188+
186189 while more_pages :
187- checkruns = query_checkruns .fetch ()["data" ]["node" ]
188- run_types = ["failed" , "incomplete" ]
190+ check_runs = query_check_runs .fetch ()["data" ]["node" ]
189191 more_pages = False
190192
191193 for run_type in run_types :
192194 run_type_camel = run_type .capitalize () + "Run"
193195 run_type = run_type + "Runs"
194196
195- append_runs_to_list (checkruns [run_type ], bad_runs_by_matrix )
197+ append_runs_to_list (check_runs [run_type ], bad_runs_by_matrix )
196198
197- if query_checkruns .paginate (checkruns [run_type ]["pageInfo" ], "after" + run_type_camel ):
198- query_checkruns .variables ["include" + run_type_camel ] = True
199+ if query_check_runs .paginate (
200+ check_runs [run_type ]["pageInfo" ], "after" + run_type_camel
201+ ):
202+ query_check_runs .variables ["include" + run_type_camel ] = True
199203 more_pages = True
200204
201205 return bad_runs_by_matrix
202206
203207
208+ def set_commit (commit ):
209+ set_output ("commit_sha" , commit ["sha" ])
210+ set_output ("commit_depth" , commit ["depth" ])
211+
212+
204213def main ():
205214 query_commits = Query (QUERY_COMMITS , query_variables_commits , headers )
206215 query_commits .variables ["owner" ], query_commits .variables ["name" ] = os .environ ["REPO" ].split (
207216 "/"
208217 )
209218
210- commit , checksuite = get_commit_and_checksuite (query_commits )
219+ commit , check_suite = get_commit_depth_and_check_suite (query_commits )
211220
212- if checksuite is None :
213- if commit is None :
214- print ( "No checkSuites found -> Abort" )
221+ if not check_suite :
222+ if commit :
223+ set_commit ( commit )
215224 else :
216- set_output ( "commit" , commit )
225+ print ( "Abort: No check suite found" )
217226 quit ()
218227
219- query_checkruns = Query (QUERY_CHECKRUNS , query_variables_checkruns , headers )
220- query_checkruns .variables ["checkSuiteID" ] = checksuite
228+ query_check_runs = Query (QUERY_CHECK_RUNS , query_variables_check_runs , headers )
229+ query_check_runs .variables ["checkSuiteID" ] = check_suite
221230
222- checkruns = get_bad_checkruns ( query_checkruns )
231+ check_runs = get_bad_check_runs ( query_check_runs )
223232
224- if len ( checkruns ) == 0 :
225- print ("No checkRuns found -> Abort " )
233+ if not check_runs :
234+ print ("Abort: No check runs found " )
226235 quit ()
227236
228- set_output ( "commit" , commit )
229- set_output ("checkruns " , json .dumps (checkruns ))
237+ set_commit ( commit )
238+ set_output ("check_runs " , json .dumps (check_runs ))
230239
231240
232241if __name__ == "__main__" :
0 commit comments