Merge pull request #62488 from ClickHouse/ci_auto_sync_pr_merge

CI: merge sync pr on push to master
This commit is contained in:
Max K 2024-04-10 18:17:36 +00:00 committed by GitHub
commit 4a7450ed2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 54 additions and 1 deletions

View File

@ -23,6 +23,10 @@ jobs:
clear-repository: true # to ensure correct digests
fetch-depth: 0 # to get version
filter: tree:0
- name: Check sync PR
run: |
cd "$GITHUB_WORKSPACE/tests/ci"
python3 sync_pr.py || :
- name: Python unit tests
run: |
cd "$GITHUB_WORKSPACE/tests/ci"

View File

@ -199,7 +199,6 @@ class PRInfo:
EventType.MERGE_QUEUE in github_event
): # pull request and other similar events
self.event_type = EventType.MERGE_QUEUE
# FIXME: need pr? we can parse it from ["head_ref": "refs/heads/gh-readonly-queue/test-merge-queue/pr-6751-4690229995a155e771c52e95fbd446d219c069bf"]
self.number = 0
self.sha = github_event[EventType.MERGE_QUEUE]["head_sha"]
self.base_ref = github_event[EventType.MERGE_QUEUE]["base_ref"]
@ -208,6 +207,8 @@ class PRInfo:
self.base_name = github_event["repository"]["full_name"]
# any_branch-name - the name of working branch name
self.head_ref = github_event[EventType.MERGE_QUEUE]["head_ref"]
# parse underlying pr from ["head_ref": "refs/heads/gh-readonly-queue/test-merge-queue/pr-6751-4690229995a155e771c52e95fbd446d219c069bf"]
self.merged_pr = int(self.head_ref.split("/pr-")[-1].split("-")[0])
# UserName/ClickHouse or ClickHouse/ClickHouse
self.head_name = self.base_name
self.user_login = github_event["sender"]["login"]
@ -235,6 +236,8 @@ class PRInfo:
if pull_request is None or pull_request["state"] == "closed":
# it's merged PR to master
self.number = 0
if pull_request:
self.merged_pr = pull_request["number"]
self.labels = set()
self.pr_html_url = f"{repo_prefix}/commits/{ref}"
self.base_ref = ref

46
tests/ci/sync_pr.py Normal file
View File

@ -0,0 +1,46 @@
#!/usr/bin/env python
"""Script for automatic sync PRs handling in private repos"""
import sys
from get_robot_token import get_best_robot_token
from pr_info import PRInfo
from github_helper import GitHub
def main():
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(
query=f"head:sync-upstream/pr/{pr_info.merged_pr} org:ClickHouse type:pr",
repo="ClickHouse/clickhouse-private",
)
if len(prs) > 1:
print(f"WARNING: More than one PR found [{prs}] - exiting")
sys.exit(0)
if len(prs) == 0:
print("WARNING: No Sync PR found")
sys.exit(0)
pr = prs[0]
if pr.state == "closed":
print(f"Sync PR [{pr.number}] already closed - exiting")
sys.exit(0)
if pr.state != "open":
print(f"WARNING: Unknown Sync PR [{pr.number}] state [{pr.state}] - exiting")
sys.exit(0)
print(f"Trying to merge Sync PR [{pr.number}]")
if pr.draft:
gh.toggle_pr_draft(pr)
pr.merge()
if __name__ == "__main__":
main()