22
33import argparse
44import os
5+ import sys
56import tempfile
67from urllib .parse import urlparse
78
@@ -61,11 +62,17 @@ def main():
6162 '--work-dir' ,
6263 help = 'Optional directory for temporary XML comparison files.' ,
6364 )
65+ parser .add_argument (
66+ '--upstream-revision' ,
67+ help = 'Optional upstream revision to record in the report. If omitted, '
68+ 'the script tries to resolve it from the GitHub API.' ,
69+ )
6470
6571 args = parser .parse_args ()
6672
6773 report = build_report (
6874 upstream_url = args .upstream_url ,
75+ upstream_revision = args .upstream_revision ,
6976 work_dir = args .work_dir ,
7077 )
7178 print_console_report (report )
@@ -80,10 +87,11 @@ def main():
8087 handle .write ('\n ' )
8188
8289
83- def build_report (* , upstream_url , work_dir = None ):
90+ def build_report (* , upstream_url , upstream_revision = None , work_dir = None ):
8491 """Download upstream config and return a structured drift report."""
8592
86- upstream_revision = _resolve_upstream_revision (upstream_url )
93+ if upstream_revision is None :
94+ upstream_revision = _resolve_upstream_revision (upstream_url )
8795
8896 if work_dir is not None :
8997 return _build_report_in_dir (
@@ -142,12 +150,16 @@ def _resolve_upstream_revision(upstream_url):
142150 return None
143151
144152 owner , repo , ref , path = github_source
145- return _get_latest_commit_sha (
146- owner = owner ,
147- repo = repo ,
148- ref = ref ,
149- path = path ,
150- )
153+ try :
154+ return _get_latest_commit_sha (
155+ owner = owner ,
156+ repo = repo ,
157+ ref = ref ,
158+ path = path ,
159+ )
160+ except requests .RequestException as exc :
161+ _warn_revision_resolution_failure (exc )
162+ return None
151163
152164
153165def _parse_github_raw_url (upstream_url ):
@@ -179,6 +191,7 @@ def _get_latest_commit_sha(*, owner, repo, ref, path):
179191 'path' : path ,
180192 'per_page' : 1 ,
181193 },
194+ headers = _build_github_api_headers (),
182195 timeout = 60 ,
183196 )
184197 response .raise_for_status ()
@@ -194,5 +207,31 @@ def _get_latest_commit_sha(*, owner, repo, ref, path):
194207 return sha
195208
196209
210+ def _build_github_api_headers ():
211+ headers = {
212+ 'Accept' : 'application/vnd.github+json' ,
213+ 'User-Agent' : 'mache/update_cime_machine_config' ,
214+ }
215+
216+ token = os .getenv ('GITHUB_TOKEN' ) or os .getenv ('GH_TOKEN' )
217+ if token :
218+ headers ['Authorization' ] = f'Bearer { token } '
219+
220+ return headers
221+
222+
223+ def _warn_revision_resolution_failure (error ):
224+ response = getattr (error , 'response' , None )
225+ status_code = getattr (response , 'status_code' , None )
226+ status = '' if status_code is None else f' (HTTP { status_code } )'
227+ print (
228+ 'Warning: could not resolve upstream revision from the GitHub API'
229+ f'{ status } ; continuing without revision metadata. Set '
230+ 'GITHUB_TOKEN or GH_TOKEN, or rerun with --upstream-revision <sha> '
231+ 'to record a specific upstream commit.' ,
232+ file = sys .stderr ,
233+ )
234+
235+
197236if __name__ == '__main__' :
198237 main ()
0 commit comments