ClickHouse/tests/ci/sync_pr.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

122 lines
3.3 KiB
Python
Raw Normal View History

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 SUCCESS
2024-04-11 11:58:44 +00:00
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):
# FIXME: uncomment posting red Sync status to prohibit merge in MQ if PR state fetching works good
2024-04-12 11:11:49 +00:00
if not sync_pr:
# post_commit_status(
# get_commit(gh, pr_info.sha), FAILURE, "", "Sync PR not found", "A Sync"
# )
2024-04-12 11:11:49 +00:00
return
# FIXME: fetch sync pr in a proper way
# retries = 0
# while sync_pr.mergeable_state == "unknown" and retries < 3:
# retries += 1
# print(f"Unknown status. Trying to fetch again [{retries}/3]")
# time.sleep(5)
# sync_pr = gh.get_pulls_from_search(
# query=f"head:sync-upstream/pr/{sync_pr.number} org:ClickHouse type:pr",
# repo="ClickHouse/clickhouse-private",
# )
2024-04-12 11:11:49 +00:00
if sync_pr.mergeable_state == "clean":
print(f"Sync PR [{sync_pr.number}] is clean")
2024-04-11 11:58:44 +00:00
post_commit_status(get_commit(gh, pr_info.sha), SUCCESS, "", "", "A Sync")
2024-04-12 11:11:49 +00:00
else:
print(
f"Sync PR [{sync_pr}] is not mergeable, state [{sync_pr.mergeable_state}]"
)
# post_commit_status(
# get_commit(gh, pr_info.sha),
# FAILURE,
# "",
# f"state: {sync_pr.mergeable_state}",
# "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()