From 2e4e3187d5d342fec7565b9db9652e90084f5e3d Mon Sep 17 00:00:00 2001 From: Max Kainov Date: Wed, 10 Apr 2024 13:17:39 +0000 Subject: [PATCH 1/2] merge sync pr on push to master --- .github/workflows/master.yml | 4 ++++ tests/ci/pr_info.py | 5 +++- tests/ci/sync_pr.py | 45 ++++++++++++++++++++++++++++++++++++ 3 files changed, 53 insertions(+), 1 deletion(-) create mode 100644 tests/ci/sync_pr.py diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml index 816bdfd4f31..64372a90613 100644 --- a/.github/workflows/master.yml +++ b/.github/workflows/master.yml @@ -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" diff --git a/tests/ci/pr_info.py b/tests/ci/pr_info.py index 204284785c9..293004fc4f3 100644 --- a/tests/ci/pr_info.py +++ b/tests/ci/pr_info.py @@ -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 diff --git a/tests/ci/sync_pr.py b/tests/ci/sync_pr.py new file mode 100644 index 00000000000..dfa7379ae6f --- /dev/null +++ b/tests/ci/sync_pr.py @@ -0,0 +1,45 @@ +#!/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"type:pr [Sync] ClickHouse/ClickHouse#{pr_info.merged_pr}", + repo="ClickHouse/clickhouse-private", + ) + if len(prs) > 1: + print(f"WARNING: More than one PR found [{prs}]") + elif 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"NOTE: Trying to merge Sync PR [{pr.number}]") + if pr.draft: + gh.toggle_pr_draft(pr) + pr.merge() + + +if __name__ == "__main__": + main() From 407f15465042382d1a38806c1e2947b25a4478bd Mon Sep 17 00:00:00 2001 From: Max Kainov Date: Wed, 10 Apr 2024 18:07:05 +0000 Subject: [PATCH 2/2] comments --- tests/ci/sync_pr.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/ci/sync_pr.py b/tests/ci/sync_pr.py index dfa7379ae6f..f33f6122f30 100644 --- a/tests/ci/sync_pr.py +++ b/tests/ci/sync_pr.py @@ -16,12 +16,13 @@ def main(): assert pr_info.merged_pr, "BUG. merged PR number could not been determined" prs = gh.get_pulls_from_search( - query=f"type:pr [Sync] ClickHouse/ClickHouse#{pr_info.merged_pr}", + 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}]") - elif len(prs) == 0: + 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) @@ -35,7 +36,7 @@ def main(): print(f"WARNING: Unknown Sync PR [{pr.number}] state [{pr.state}] - exiting") sys.exit(0) - print(f"NOTE: Trying to merge Sync PR [{pr.number}]") + print(f"Trying to merge Sync PR [{pr.number}]") if pr.draft: gh.toggle_pr_draft(pr) pr.merge()