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