@@ -367,6 +367,43 @@ def create_issue(self, repo_owner: str, repo_name: str, title: str, body: str, l
367367 traceback .print_exc ()
368368 return None
369369
370+ def merge_pr (self , repo_owner : str , repo_name : str , pr_number : int , commit_title : Optional [str ] = None , commit_message : Optional [str ] = None , merge_method : Literal ['merge' , 'squash' , 'rebase' ] = 'squash' ) -> Dict [str , Any ]:
371+ """
372+ Merges a pull request in the specified GitHub repository.
373+ Args:
374+ repo_owner (str): The owner of the repository.
375+ repo_name (str): The name of the repository.
376+ pr_number (int): The pull request number to merge.
377+ commit_title (str, optional): The title for the merge commit. Defaults to None.
378+ commit_message (str, optional): The message for the merge commit. Defaults to None.
379+ merge_method (Literal['merge', 'squash', 'rebase'], optional): The merge method to use ('merge', 'squash', or 'rebase'). Defaults to 'squash'.
380+ Returns:
381+ Dict[str, Any]: The JSON response from the GitHub API containing merge information if successful.
382+ Error Handling:
383+ Logs errors and prints the traceback if the merge fails, returning None.
384+ """
385+ logging .info (f"Merging PR { repo_owner } /{ repo_name } #{ pr_number } " )
386+
387+ # Construct the merge URL
388+ merge_url = f"https://api.github.com/repos/{ repo_owner } /{ repo_name } /pulls/{ pr_number } /merge"
389+
390+ try :
391+ response = requests .put (merge_url , headers = self ._get_headers (), json = {
392+ 'commit_title' : commit_title ,
393+ 'commit_message' : commit_message ,
394+ 'merge_method' : merge_method
395+ })
396+ response .raise_for_status ()
397+ merge_data = response .json ()
398+
399+ logging .info (f"PR merged successfully" )
400+ return merge_data
401+
402+ except Exception as e :
403+ logging .error ({"status" : "error" , "message" : str (e )})
404+ traceback .print_exc ()
405+ return {"status" : "error" , "message" : str (e )}
406+
370407 def update_issue (self , repo_owner : str , repo_name : str , issue_number : int , title : str , body : str , labels : list [str ] = [], state : Literal ['open' , 'closed' ] = 'open' ) -> Dict [str , Any ]:
371408 """
372409 Updates an existing GitHub issue with the specified parameters.
0 commit comments