3
3
import argparse
4
4
from github import Github
5
5
6
+
6
7
def read_body_from_file (file_path ):
7
8
with open (file_path , 'r' ) as file :
8
9
return file .read ()
9
10
11
+
10
12
def get_body_content (body_input ):
11
13
"""Determines if the body content is a file path or direct text."""
12
14
if os .path .isfile (body_input ):
@@ -16,52 +18,58 @@ def get_body_content(body_input):
16
18
print (f"Body content will be taken directly: '{ body_input } .'" )
17
19
return body_input
18
20
21
+
19
22
def create_or_update_pr (args , repo ):
20
23
current_pr = None
21
24
pr_number = None
22
25
body = get_body_content (args .body )
23
26
24
- # Check for an existing PR
25
- existing_prs = repo .get_pulls (head = args .branch_for_pr , base = args .base_branch , state = 'open' )
26
- for pr in existing_prs :
27
- if pr .base .ref == args .base_branch and pr .head .ref == args .branch_for_pr :
28
- current_pr = pr
29
- break
27
+ owner = repo .owner .login
28
+ head_format = f"{ owner } :{ args .branch_for_pr } "
29
+
30
+ print (f"Searching for PR with head branch '{ head_format } ' and base branch '{ args .base_branch } '" )
31
+
32
+ existing_prs = repo .get_pulls (head = head_format , base = args .base_branch , state = 'open' )
33
+
34
+ if existing_prs .totalCount > 0 :
35
+ current_pr = existing_prs [0 ]
36
+ print (f"Found existing PR #{ current_pr .number } : { current_pr .title } " )
37
+
30
38
if current_pr :
31
- print (f"Existing PR found. Updating PR #{ current_pr .number } ." )
39
+ print (f"Updating existing PR #{ current_pr .number } ." )
32
40
current_pr .edit (title = args .title , body = body )
41
+ print (f"PR #{ current_pr .number } updated successfully." )
33
42
else :
34
- print ("No existing PR found. Creating a new PR." )
43
+ print (f "No existing PR found. Creating a new PR from ' { args . branch_for_pr } ' to ' { args . base_branch } ' ." )
35
44
current_pr = repo .create_pull (title = args .title , body = body , head = args .branch_for_pr , base = args .base_branch )
45
+ print (f"New PR #{ current_pr .number } created successfully." )
36
46
37
47
pr_number = current_pr .number
38
- if os .environ ['GITHUB_OUTPUT' ]:
39
- with open (os .environ ['GITHUB_OUTPUT' ], 'a' ) as gh_out :
48
+ github_output = os .environ .get ('GITHUB_OUTPUT' )
49
+ if github_output :
50
+ with open (github_output , 'a' ) as gh_out :
40
51
print (f"pr_number={ pr_number } " , file = gh_out )
41
52
42
- print (f"PR created or updated successfully. PR number: { pr_number } " )
53
+ print (f"PR operation completed. PR number: { pr_number } " )
54
+ return pr_number
55
+
43
56
44
57
def append_to_pr_body (args , repo ):
45
58
body_to_append = get_body_content (args .body )
46
-
47
- pr = None
48
- if args .pr_number :
49
- pr = repo .get_pull (args .pr_number )
50
- else :
51
- existing_prs = repo .get_pulls (head = args .branch_for_pr , base = args .base_branch , state = 'open' )
52
- for p in existing_prs :
53
- if p .base .ref == args .base_branch and p .head .ref == args .branch_for_pr :
54
- pr = p
55
- break
59
+
60
+ print (f"Looking for PR by number: { args .pr_number } " )
61
+ pr = repo .get_pull (args .pr_number )
56
62
57
63
if pr :
58
64
print (f"Appending to PR #{ pr .number } ." )
59
65
current_body = pr .body or ""
60
66
new_body = current_body + "\n \n " + body_to_append
61
67
pr .edit (body = new_body )
68
+ print (f"PR #{ pr .number } body updated successfully." )
62
69
else :
63
70
print ("No matching pull request found to append body." )
64
71
72
+
65
73
if __name__ == '__main__' :
66
74
parser = argparse .ArgumentParser (description = 'Operate on a GitHub Pull Request' )
67
75
subparsers = parser .add_subparsers (dest = 'mode' , required = True , help = 'Mode of operation' )
0 commit comments