1515"""
1616
1717from typing import Optional
18+ import argparse
1819from github .Repository import Repository
1920from github .Workflow import Workflow
2021from github .GithubException import GithubException
21- from tools . library_functions import StrPath
22- from tools . iterate_libraries import (
22+ from library_functions import StrPath
23+ from iterate_libraries import (
2324 iter_remote_bundle_with_func ,
2425 RemoteLibFunc_IterResult ,
2526)
2829def run_gh_rest_check (
2930 lib_repo : Repository ,
3031 user : Optional [str ] = None ,
32+ branch : Optional [str ] = None ,
3133 workflow_filename : Optional [str ] = "build.yml" ,
3234) -> str :
3335 """Uses ``PyGithub`` to check the CI status of a repository
3436
3537 :param Repository lib_repo: The repo as a github.Repository.Repository object
3638 :param str|None user: The user that triggered the run; if `None` is
3739 provided, any user is acceptable
40+ :param str|None branch: The branch name to specifically check; if `None` is
41+ provided, all branches are allowed; this is the default
3842 :param str|None workflow_filename: The filename of the workflow; if `None` is
39- provided, any workflow name is acceptable; the default is `"build.yml"`
43+ provided, any workflow name is acceptable; the default is `` "build.yml"` `
4044 :return: The requested runs conclusion
4145 :rtype: str
4246 """
4347
4448 arg_dict = {}
4549 if user is not None :
4650 arg_dict ["actor" ] = user
51+ if branch is not None :
52+ arg_dict ["branch" ] = branch
4753
4854 workflow : Workflow = lib_repo .get_workflow (workflow_filename )
4955 workflow_runs = workflow .get_runs (** arg_dict )
@@ -53,6 +59,7 @@ def run_gh_rest_check(
5359def check_build_status (
5460 lib_repo : Repository ,
5561 user : Optional [str ] = None ,
62+ branch : Optional [str ] = None ,
5663 workflow_filename : Optional [str ] = "build.yml" ,
5764 debug : bool = False ,
5865) -> Optional [str ]:
@@ -62,6 +69,8 @@ def check_build_status(
6269 :param Repository lib_repo: The repo as a github.Repository.Repository object
6370 :param str|None user: The user that triggered the run; if `None` is
6471 provided, any user is acceptable
72+ :param str|None branch: The branch name to specifically check; if `None` is
73+ provided, all branches are allowed; this is the default
6574 :param str|None workflow_filename: The filename of the workflow; if `None`
6675 is provided, any workflow name is acceptable; the defail is `"build.yml"`
6776 :param bool debug: Whether debug statements should be printed to the standard
@@ -74,8 +83,13 @@ def check_build_status(
7483 if debug :
7584 print ("Checking" , lib_repo .name )
7685
86+ if lib_repo .archived :
87+ return True
88+
7789 try :
78- result = run_gh_rest_check (lib_repo , user , workflow_filename ) == "success"
90+ result = (
91+ run_gh_rest_check (lib_repo , user , branch , workflow_filename ) == "success"
92+ )
7993 if debug and not result :
8094 print ("***" , "Library" , lib_repo .name , "failed the patch!" , "***" )
8195 return result
@@ -94,6 +108,7 @@ def check_build_status(
94108def check_build_statuses (
95109 gh_token : str ,
96110 user : Optional [str ] = None ,
111+ branch : Optional [str ] = "main" ,
97112 workflow_filename : Optional [str ] = "build.yml" ,
98113 * ,
99114 debug : bool = False ,
@@ -104,6 +119,8 @@ def check_build_statuses(
104119 :param str gh_token: The Github token to be used for with the Github API
105120 :param str|None user: The user that triggered the run; if `None` is
106121 provided, any user is acceptable
122+ :param str|None branch: The branch name to specifically check; if `None` is
123+ provided, all branches are allowed; this is the default
107124 :param str|None workflow_filename: The filename of the workflow; if `None` is
108125 provided, any workflow name is acceptable; the defail is `"build.yml"`
109126 :param bool debug: Whether debug statements should be printed to
@@ -113,9 +130,10 @@ def check_build_statuses(
113130 :rtype: list
114131 """
115132
116- args = (user , workflow_filename )
117- kwargs = {"debug" : debug }
118- return iter_remote_bundle_with_func (gh_token , [(check_build_status , args , kwargs )])
133+ return iter_remote_bundle_with_func (
134+ gh_token ,
135+ [(check_build_status , (user , branch , workflow_filename ), {"debug" : debug })],
136+ )
119137
120138
121139def save_build_statuses (
@@ -138,3 +156,60 @@ def save_build_statuses(
138156 with open (failures_filepath , mode = "w" , encoding = "utf-8" ) as outputfile :
139157 for build in bad_builds :
140158 outputfile .write (build + "\n " )
159+
160+
161+ if __name__ == "__main__" :
162+
163+ parser = argparse .ArgumentParser (
164+ description = "Check the CI status of the Bundle libraries"
165+ )
166+ parser .add_argument (
167+ "gh_token" , metavar = "GH_TOKEN" , type = str , help = "GitHub token with proper scopes"
168+ )
169+ parser .add_argument (
170+ "--user" ,
171+ metavar = "U" ,
172+ type = str ,
173+ dest = "user" ,
174+ default = None ,
175+ help = "Select a specific user that triggered the workflow" ,
176+ )
177+ parser .add_argument (
178+ "--branch" ,
179+ metavar = "B" ,
180+ type = str ,
181+ dest = "branch" ,
182+ default = None ,
183+ help = 'Branch name; default is "main"' ,
184+ )
185+ parser .add_argument (
186+ "--workflow" ,
187+ metavar = "W" ,
188+ type = str ,
189+ dest = "workflow" ,
190+ default = "build.yml" ,
191+ help = 'Workflow name; default is "build.yml"' ,
192+ )
193+ parser .add_argument (
194+ "--debug" , action = "store_true" , help = "Print debug text during execution"
195+ )
196+
197+ args = parser .parse_args ()
198+
199+ results = check_build_statuses (
200+ args .gh_token , args .user , args .branch , args .workflow , debug = args .debug
201+ )
202+ fail_list = [
203+ repo_name .name for repo_name , repo_results in results if not repo_results [0 ]
204+ ]
205+
206+ if fail_list :
207+ print (f'Failures for CI workflow "{ args .workflow } ":' )
208+ for failure in fail_list :
209+ print (failure )
210+ RETURN_CODE = 1
211+ else :
212+ print (f"No failures for CI workflow: { args .workflow } !" )
213+ RETURN_CODE = 0
214+
215+ raise SystemExit (RETURN_CODE )
0 commit comments