@@ -38,6 +38,7 @@ def init_parser() -> ArgumentParser:
3838 parser .add_argument ("--token" , "-t" , required = True , help = "GitHub Access Token" )
3939 parser .add_argument ("--repo_path" , "-r" , help = "Cloned repository path" )
4040 parser .add_argument ("--pull" , "-p" , type = int , required = True , help = "Pull Request number" )
41+ parser .add_argument ("--branch" , "-b" , help = "target branch (manual selection)" )
4142 parser .add_argument ("--dry_run" , "-d" , action = 'store_true' , help = "Dry Run mode" )
4243 parser .add_argument ("--verbose" , "-v" , action = 'store_true' , help = "Verbose mode" )
4344 return parser
@@ -58,6 +59,21 @@ def set_logging(verbose: bool = False) -> None:
5859 logger .addHandler (handler )
5960
6061
62+ def set_gh_summary (value : str ) -> None :
63+ """Sets a summary status for a GitHub Actions workflow.
64+ This is used to write the summary of the workflow run.
65+
66+ Args:
67+ value: Summary content
68+ """
69+
70+ # Check if the GITHUB_STEP_SUMMARY environment variable is set
71+ gh = os .environ .get ("GITHUB_STEP_SUMMARY" )
72+ if gh :
73+ with open (gh , "a" , encoding = "utf-8" ) as outfile :
74+ outfile .write (f"{ value } \n " )
75+
76+
6177# ===============================================================================
6278# Sub functions to retrieve objects
6379# ===============================================================================
@@ -116,6 +132,7 @@ def get_commits_to_cherry_pick(pull: PullRequest) -> List[str]:
116132 logger .error ("No commits found. Exiting." )
117133 sys .exit (1 )
118134 logger .info ("Found commits: %s" , commits )
135+ set_gh_summary (f":loudspeaker: Found { len (commits )} commits" )
119136 return commits
120137
121138
@@ -181,7 +198,7 @@ def cherry_pick_commits(local_repo: Repo,
181198
182199 for sha in commits :
183200 try :
184- logger .info ("Cherry-picking %s onto %s " , sha , auto_branch )
201+ logger .info ("Cherry-picking %s" , sha )
185202 local_repo .git .cherry_pick ('-x' , sha )
186203 except GitCommandError as err :
187204 logger .error ("Failed to apply cherry-pick of %s:\n %s" , sha , err )
@@ -208,7 +225,7 @@ def cherry_pick_commits(local_repo: Repo,
208225def create_pull_request_if_needed (git_repo : Repository ,
209226 auto_branch : str ,
210227 target_br : str ,
211- pull_number : int ) -> None :
228+ pull_number : int ) -> bool :
212229 """Create a pull request if one does not already exist."""
213230 pr_auto_title = f"[AUTO_UPDATE] Branch { target_br } "
214231 prs = git_repo .get_pulls (state = "open" , base = target_br )
@@ -240,6 +257,7 @@ def create_pull_request_if_needed(git_repo: Repository,
240257 existing_pr .number , pull_number )
241258 except Exception as e :
242259 logger .error ("Failed to update existing PR: %s" , e )
260+ return False
243261 else :
244262 try :
245263 # Create a new pull request with the auto_update branch as
@@ -253,7 +271,7 @@ def create_pull_request_if_needed(git_repo: Repository,
253271 )
254272 except :
255273 logger .error ("Failed to create PR!" )
256- sys . exit ( 1 )
274+ return False
257275
258276 try :
259277 # Assign the PR to the original author
@@ -263,6 +281,7 @@ def create_pull_request_if_needed(git_repo: Repository,
263281 except :
264282 logger .error ("Failed to assign PR to original author: %s" , original_author )
265283 logger .info ("Created PR for branch '%s': %s" , auto_branch , new_pr .html_url )
284+ return True
266285
267286
268287# ===============================================================================
@@ -294,8 +313,15 @@ def main():
294313 # Retrieve PR object
295314 pull = get_pull_request (git_repo , args .pull )
296315
297- # Search target branches from PR description
298- target_branches = get_target_branches (pull )
316+ if args .branch :
317+ # check branch naming
318+ if not args .branch .startswith ("API_LEVEL_" ):
319+ logger .error ("Invalid branch: %s" , args .branch )
320+ sys .exit (1 )
321+ target_branches = [args .branch ]
322+ else :
323+ # Search target branches from PR description
324+ target_branches = get_target_branches (pull )
299325
300326 # Retrieve all available branches
301327 branches = get_all_branches (git_repo )
@@ -341,30 +367,37 @@ def main():
341367
342368 # Check if dedicated auto_update branch exists, else create it
343369 auto_branch , branch_created = create_or_get_branch (local_repo ,
344- target_br ,
345- branches ,
346- args .dry_run )
370+ target_br ,
371+ branches ,
372+ args .dry_run )
347373
348374 # Cherry-pick the commits onto the auto_update branch
349375 if not cherry_pick_commits (local_repo ,
350- commits ,
351- auto_branch ,
352- default_branch ,
353- branch_created ,
354- args .dry_run ):
376+ commits ,
377+ auto_branch ,
378+ default_branch ,
379+ branch_created ,
380+ args .dry_run ):
355381 result = 1
356- continue
357382
358- if not args .dry_run :
383+ if result == 0 and not args .dry_run :
359384 # Create a pull request if needed
360- create_pull_request_if_needed (git_repo , auto_branch , target_br , args .pull )
385+ if not create_pull_request_if_needed (git_repo , auto_branch , target_br , args .pull ):
386+ result = 1
361387
362388 try :
363389 # Return to default branch
364390 local_repo .git .checkout (default_branch )
365391 except GitCommandError as e :
366392 logger .warning ("Failed to checkout to default branch (%s): %s" , default_branch , e )
367393
394+ # Set result in summary
395+ if result == 0 :
396+ set_gh_summary (f":white_check_mark: { target_br } " )
397+ else :
398+ set_gh_summary (f":x: { target_br } " )
399+
400+
368401 if result != 0 :
369402 logger .error ("One or more operations failed." )
370403 sys .exit (1 )
0 commit comments