Skip to content

Commit 188d06f

Browse files
authored
CI: fix seaching existed Update muted_ya PR (#17997)
1 parent 71218f4 commit 188d06f

File tree

1 file changed

+29
-21
lines changed

1 file changed

+29
-21
lines changed

.github/scripts/create_or_update_pr.py

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,12 @@
33
import argparse
44
from github import Github
55

6+
67
def read_body_from_file(file_path):
78
with open(file_path, 'r') as file:
89
return file.read()
910

11+
1012
def get_body_content(body_input):
1113
"""Determines if the body content is a file path or direct text."""
1214
if os.path.isfile(body_input):
@@ -16,52 +18,58 @@ def get_body_content(body_input):
1618
print(f"Body content will be taken directly: '{body_input}.'")
1719
return body_input
1820

21+
1922
def create_or_update_pr(args, repo):
2023
current_pr = None
2124
pr_number = None
2225
body = get_body_content(args.body)
2326

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+
3038
if current_pr:
31-
print(f"Existing PR found. Updating PR #{current_pr.number}.")
39+
print(f"Updating existing PR #{current_pr.number}.")
3240
current_pr.edit(title=args.title, body=body)
41+
print(f"PR #{current_pr.number} updated successfully.")
3342
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}'.")
3544
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.")
3646

3747
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:
4051
print(f"pr_number={pr_number}", file=gh_out)
4152

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+
4356

4457
def append_to_pr_body(args, repo):
4558
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)
5662

5763
if pr:
5864
print(f"Appending to PR #{pr.number}.")
5965
current_body = pr.body or ""
6066
new_body = current_body + "\n\n" + body_to_append
6167
pr.edit(body=new_body)
68+
print(f"PR #{pr.number} body updated successfully.")
6269
else:
6370
print("No matching pull request found to append body.")
6471

72+
6573
if __name__ == '__main__':
6674
parser = argparse.ArgumentParser(description='Operate on a GitHub Pull Request')
6775
subparsers = parser.add_subparsers(dest='mode', required=True, help='Mode of operation')

0 commit comments

Comments
 (0)