Skip to content

Commit 6e0fdc1

Browse files
committed
CI: fix seaching existed Update muted_ya PR (#17997)
1 parent da6e72a commit 6e0fdc1

File tree

1 file changed

+103
-0
lines changed

1 file changed

+103
-0
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
#!/usr/bin/env python3
2+
import os
3+
import argparse
4+
from github import Github
5+
6+
7+
def read_body_from_file(file_path):
8+
with open(file_path, 'r') as file:
9+
return file.read()
10+
11+
12+
def get_body_content(body_input):
13+
"""Determines if the body content is a file path or direct text."""
14+
if os.path.isfile(body_input):
15+
print(f"Body content will be read from file: {body_input}.")
16+
return read_body_from_file(body_input)
17+
else:
18+
print(f"Body content will be taken directly: '{body_input}.'")
19+
return body_input
20+
21+
22+
def create_or_update_pr(args, repo):
23+
current_pr = None
24+
pr_number = None
25+
body = get_body_content(args.body)
26+
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+
38+
if current_pr:
39+
print(f"Updating existing PR #{current_pr.number}.")
40+
current_pr.edit(title=args.title, body=body)
41+
print(f"PR #{current_pr.number} updated successfully.")
42+
else:
43+
print(f"No existing PR found. Creating a new PR from '{args.branch_for_pr}' to '{args.base_branch}'.")
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.")
46+
47+
pr_number = current_pr.number
48+
github_output = os.environ.get('GITHUB_OUTPUT')
49+
if github_output:
50+
with open(github_output, 'a') as gh_out:
51+
print(f"pr_number={pr_number}", file=gh_out)
52+
53+
print(f"PR operation completed. PR number: {pr_number}")
54+
return pr_number
55+
56+
57+
def append_to_pr_body(args, repo):
58+
body_to_append = get_body_content(args.body)
59+
60+
print(f"Looking for PR by number: {args.pr_number}")
61+
pr = repo.get_pull(args.pr_number)
62+
63+
if pr:
64+
print(f"Appending to PR #{pr.number}.")
65+
current_body = pr.body or ""
66+
new_body = current_body + "\n\n" + body_to_append
67+
pr.edit(body=new_body)
68+
print(f"PR #{pr.number} body updated successfully.")
69+
else:
70+
print("No matching pull request found to append body.")
71+
72+
73+
if __name__ == '__main__':
74+
parser = argparse.ArgumentParser(description='Operate on a GitHub Pull Request')
75+
subparsers = parser.add_subparsers(dest='mode', required=True, help='Mode of operation')
76+
77+
# Subparser for create or update PR mode
78+
create_parser = subparsers.add_parser('create_or_update', help='Create or update a pull request')
79+
create_parser.add_argument('--base_branch', type=str, required=True, help='Base branch for the PR')
80+
create_parser.add_argument('--branch_for_pr', type=str, required=True, help='Branch from which to create the PR')
81+
create_parser.add_argument('--title', type=str, required=True, help='Title of the PR')
82+
create_parser.add_argument('--body', type=str, default='', required=False, help='Body content of the PR, or path to a file with the content')
83+
84+
# Subparser for append PR body mode
85+
append_parser = subparsers.add_parser('append_pr_body', help='Append text to the body of an existing pull request')
86+
group = append_parser.add_mutually_exclusive_group(required=True)
87+
group.add_argument('--pr_number', type=int, help='Pull request number')
88+
append_parser.add_argument('--body', type=str, required=True, help='Text to append to the PR body')
89+
90+
args = parser.parse_args()
91+
92+
GITHUB_TOKEN = os.getenv('GITHUB_TOKEN')
93+
if not GITHUB_TOKEN:
94+
raise ValueError("GITHUB_TOKEN environment variable is not set")
95+
96+
g = Github(GITHUB_TOKEN)
97+
repo_name = os.getenv('GITHUB_REPOSITORY', 'ydb-platform/ydb')
98+
repo = g.get_repo(repo_name)
99+
100+
if args.mode == "create_or_update":
101+
create_or_update_pr(args, repo)
102+
elif args.mode == "append_pr_body":
103+
append_to_pr_body(args, repo)

0 commit comments

Comments
 (0)