2024-04-10 13:17:39 +00:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
"""Script for automatic sync PRs handling in private repos"""
|
|
|
|
|
2024-04-11 11:58:44 +00:00
|
|
|
import argparse
|
2024-04-10 13:17:39 +00:00
|
|
|
import sys
|
|
|
|
|
|
|
|
from get_robot_token import get_best_robot_token
|
|
|
|
from pr_info import PRInfo
|
|
|
|
from github_helper import GitHub
|
2024-04-11 11:58:44 +00:00
|
|
|
from commit_status_helper import get_commit, post_commit_status
|
|
|
|
from report import FAILURE, SUCCESS
|
|
|
|
|
|
|
|
|
|
|
|
def parse_args() -> argparse.Namespace:
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
|
|
|
|
description="Script for handling sync PRs",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--merge",
|
|
|
|
action="store_true",
|
|
|
|
help="merge sync pr",
|
|
|
|
)
|
|
|
|
parser.add_argument(
|
|
|
|
"--status",
|
|
|
|
action="store_true",
|
|
|
|
help="check and set sync pr status",
|
|
|
|
)
|
|
|
|
args = parser.parse_args()
|
|
|
|
return args
|
|
|
|
|
|
|
|
|
|
|
|
def merge_sync_pr(gh, sync_pr):
|
|
|
|
if not sync_pr:
|
|
|
|
print("Sync PR not found - exiting")
|
|
|
|
return
|
|
|
|
|
|
|
|
if sync_pr.state == "closed":
|
|
|
|
print(f"Sync PR [{sync_pr.number}] already closed - exiting")
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
if sync_pr.state != "open":
|
|
|
|
print(
|
|
|
|
f"WARNING: Unknown Sync PR [{sync_pr.number}] state [{sync_pr.state}] - exiting"
|
|
|
|
)
|
|
|
|
sys.exit(0)
|
|
|
|
|
|
|
|
print(f"Trying to merge Sync PR [{sync_pr.number}]")
|
|
|
|
if sync_pr.draft:
|
|
|
|
gh.toggle_pr_draft(sync_pr)
|
|
|
|
sync_pr.merge()
|
|
|
|
|
|
|
|
|
|
|
|
def set_sync_status(gh, pr_info, sync_pr):
|
2024-04-11 15:55:25 +00:00
|
|
|
if not sync_pr or not sync_pr.mergeable:
|
2024-04-11 14:33:25 +00:00
|
|
|
post_commit_status(
|
|
|
|
get_commit(gh, pr_info.sha), FAILURE, "", "Sync PR failure", "A Sync"
|
|
|
|
)
|
2024-04-11 11:58:44 +00:00
|
|
|
else:
|
|
|
|
post_commit_status(get_commit(gh, pr_info.sha), SUCCESS, "", "", "A Sync")
|
2024-04-10 13:17:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
2024-04-11 11:58:44 +00:00
|
|
|
args = parse_args()
|
|
|
|
|
|
|
|
assert args.merge ^ args.status
|
|
|
|
|
2024-04-10 13:17:39 +00:00
|
|
|
gh = GitHub(get_best_robot_token())
|
|
|
|
|
|
|
|
pr_info = PRInfo()
|
|
|
|
assert pr_info.merged_pr, "BUG. merged PR number could not been determined"
|
|
|
|
|
|
|
|
prs = gh.get_pulls_from_search(
|
2024-04-10 18:07:05 +00:00
|
|
|
query=f"head:sync-upstream/pr/{pr_info.merged_pr} org:ClickHouse type:pr",
|
2024-04-10 13:17:39 +00:00
|
|
|
repo="ClickHouse/clickhouse-private",
|
|
|
|
)
|
2024-04-11 11:58:44 +00:00
|
|
|
|
|
|
|
sync_pr = None
|
|
|
|
|
2024-04-10 13:17:39 +00:00
|
|
|
if len(prs) > 1:
|
2024-04-10 18:07:05 +00:00
|
|
|
print(f"WARNING: More than one PR found [{prs}] - exiting")
|
2024-04-11 11:58:44 +00:00
|
|
|
elif len(prs) == 0:
|
2024-04-10 13:17:39 +00:00
|
|
|
print("WARNING: No Sync PR found")
|
2024-04-11 11:58:44 +00:00
|
|
|
else:
|
|
|
|
sync_pr = prs[0]
|
2024-04-10 13:17:39 +00:00
|
|
|
|
2024-04-11 11:58:44 +00:00
|
|
|
if args.merge:
|
|
|
|
merge_sync_pr(gh, sync_pr)
|
|
|
|
elif args.status:
|
|
|
|
set_sync_status(gh, pr_info, sync_pr)
|
2024-04-10 13:17:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
main()
|