@@ -225,13 +225,6 @@ def make(self, directory: Path) -> Path:
225225 return Path (shutil .make_archive (str (directory / self .name .with_suffix ("" )), ext_format_map [extension ], root_dir = temp_dir_path ))
226226
227227def main (args : 'argparse.Namespace' ) -> int :
228-
229- try :
230- semantic_version .Version .parse (args .version ) # type: ignore
231- except ValueError as e :
232- print (f"Error: invalid version: { e } " , file = sys .stderr )
233- return 1
234-
235228 monkey_patch_github ()
236229
237230 import github
@@ -246,27 +239,40 @@ def main(args: 'argparse.Namespace') -> int:
246239 repos [nwo ] = github .Github (auth = github .Auth .Token (token )).get_repo (nwo )
247240
248241 repo = repos [args .repo ]
249- releases = [release for release in repo .get_releases () if release .title == f"v{ args .version } " ]
250- if len (releases ) != 1 :
251- print (f"Error: expected exactly one release with title { args .version } , but found { len (releases )} " , file = sys .stderr )
252- return 1
253- release = releases [0 ]
254242
255- pull_candidates = [pr for pr in repo .get_pulls (state = "open" ) if pr .title == f"Release v { args .version } " ]
243+ pull_candidates = [pr for pr in repo .get_pulls (state = "open" ) if pr .head . sha == args .head_sha ]
256244 if len (pull_candidates ) != 1 :
257- print (f"Error: expected exactly one PR for version { args .version } , but found { len (pull_candidates )} " , file = sys .stderr )
245+ print (f"Error: expected exactly one PR for SHA { args .head_sha } , but found { len (pull_candidates )} " , file = sys .stderr )
258246 return 1
259247
260248 pull_request = pull_candidates [0 ]
261249
262250 if pull_request .state != "open" :
263251 print (f"Error: PR for version { args .version } is not open" , file = sys .stderr )
264252 return 1
253+
254+ rc_branch_regex = r"^rc/(?P<version>.*)$"
255+ rc_branch_match = re .match (rc_branch_regex , pull_request .base .ref )
256+ if not rc_branch_match :
257+ print (f"Error: PR { pull_request .url } is not based on a release candidate branch" , file = sys .stderr )
258+ return 1
259+
260+ release_version = rc_branch_match .group ("version" )
261+
262+ try :
263+ semantic_version .Version .parse (release_version ) # type: ignore
264+ except ValueError as e :
265+ print (f"Error: invalid version { release_version } use by release branch. Reason { e } " , file = sys .stderr )
266+ return 1
265267
266- head_sha = pull_request .head .sha
268+ releases = [release for release in repo .get_releases () if release .title == f"v{ release_version } " ]
269+ if len (releases ) != 1 :
270+ print (f"Error: expected exactly one release with title { args .version } , but found { len (releases )} " , file = sys .stderr )
271+ return 1
272+ release = releases [0 ]
267273
268- print (f"Collecting workflow runs for ref { head_sha } " )
269- check_runs : List [CheckRun .CheckRun ] = repo .get_check_runs (head_sha ) # type: ignore
274+ print (f"Collecting workflow runs for ref { args . head_sha } " )
275+ check_runs : List [CheckRun .CheckRun ] = repo .get_check_runs (args . head_sha ) # type: ignore
270276
271277 action_workflow_run_url_regex = r"^https://(?P<github_url>[^/]+)/(?P<owner>[^/]+)/(?P<repo>[^/]+)/actions/runs/(?P<run_id>\d+)$"
272278 action_workflow_job_run_url_regex = r"^https://(?P<github_url>[^/]+)/(?P<owner>[^/]+)/(?P<repo>[^/]+)/actions/runs/(?P<run_id>\d+)/job/(?P<job_id>\d+)$"
@@ -302,7 +308,7 @@ def main(args: 'argparse.Namespace') -> int:
302308 latests_workflow_runs = list (workflow_runs_per_id .values ())
303309
304310 if not args .skip_checks :
305- print (f"Checking that all workflow runs for ref { head_sha } succeeded" )
311+ print (f"Checking that all workflow runs for ref { args . head_sha } succeeded" )
306312 for workflow_run in latests_workflow_runs :
307313 if workflow_run .status != "completed" :
308314 print (f"Error: workflow run { workflow_run .name } with id { workflow_run .id } is not completed" , file = sys .stderr )
@@ -336,7 +342,7 @@ def main(args: 'argparse.Namespace') -> int:
336342 from sys import exit
337343
338344 parser = argparse .ArgumentParser ()
339- parser .add_argument ('--version ' , help = "The version to release (MUST be a valid semantic version) " , required = True )
345+ parser .add_argument ('--head-sha ' , help = "The head SHA of the release PR for which we update it's corresponding release " , required = True )
340346 parser .add_argument ('--repo' , help = "The owner and repository name. For example, 'octocat/Hello-World'. Used when testing this script on a fork" , required = True , default = "github/codeql-coding-standards" )
341347 parser .add_argument ('--github-token' , help = "The github token to use for the release PR" , required = True , nargs = "+" )
342348 parser .add_argument ('--layout' , help = "The layout to use for the release" , required = True )
0 commit comments