diff --git a/tests/ci/cherry_pick_utils/cherrypick.py b/tests/ci/cherry_pick_utils/cherrypick.py index a6e4b9eec5b..92c87800828 100644 --- a/tests/ci/cherry_pick_utils/cherrypick.py +++ b/tests/ci/cherry_pick_utils/cherrypick.py @@ -119,17 +119,16 @@ class CherryPick: ) # Create pull-request like a local cherry-pick + title = self._pr["title"].replace('"', r"\"") pr = self._gh.create_pull_request( source=self.cherrypick_branch, target=self.backport_branch, - title="Cherry pick #{number} to {target}: {title}".format( - number=self._pr["number"], - target=self.target_branch, - title=self._pr["title"].replace('"', '\\"'), - ), - description="Original pull-request #{}\n\n{}".format( - self._pr["number"], DESCRIPTION + title=( + f'Cherry pick #{self._pr["number"]} ' + f"to {self.target_branch}: " + f"{title}" ), + description=f'Original pull-request #{self._pr["number"]}\n\n{DESCRIPTION}', ) # FIXME: use `team` to leave a single eligible assignee. @@ -166,11 +165,8 @@ class CherryPick: "user.name=robot-clickhouse", ] - pr_title = "Backport #{number} to {target}: {title}".format( - number=self._pr["number"], - target=self.target_branch, - title=self._pr["title"].replace('"', '\\"'), - ) + title = (self._pr["title"].replace('"', r"\""),) + pr_title = f"Backport #{self._pr['number']} to {self.target_branch}: {title}" self._run(git_prefix + ["checkout", "-f", self.backport_branch]) self._run(git_prefix + ["pull", "--ff-only", "origin", self.backport_branch]) @@ -204,9 +200,8 @@ class CherryPick: source=self.backport_branch, target=self.target_branch, title=pr_title, - description="Original pull-request #{}\nCherry-pick pull-request #{}\n\n{}".format( - self._pr["number"], cherrypick_pr["number"], DESCRIPTION - ), + description=f"Original pull-request #{self._pr['number']}\n" + f"Cherry-pick pull-request #{cherrypick_pr['number']}\n\n{DESCRIPTION}", ) # FIXME: use `team` to leave a single eligible assignee. diff --git a/tests/ci/cherry_pick_utils/query.py b/tests/ci/cherry_pick_utils/query.py index 577a7185142..ef1ad5ad0fb 100644 --- a/tests/ci/cherry_pick_utils/query.py +++ b/tests/ci/cherry_pick_utils/query.py @@ -16,43 +16,43 @@ class Query: """ _PULL_REQUEST = """ - author {{ - ... on User {{ - id - login - }} - }} - - baseRepository {{ - nameWithOwner - }} - - mergeCommit {{ - oid - parents(first: {min_page_size}) {{ - totalCount - nodes {{ - oid - }} - }} - }} - - mergedBy {{ - ... on User {{ - id - login - }} - }} - - baseRefName - closed - headRefName +author {{ + ... on User {{ id - mergeable - merged - number - title - url + login + }} +}} + +baseRepository {{ + nameWithOwner +}} + +mergeCommit {{ + oid + parents(first: {min_page_size}) {{ + totalCount + nodes {{ + oid + }} + }} +}} + +mergedBy {{ + ... on User {{ + id + login + }} +}} + +baseRefName +closed +headRefName +id +mergeable +merged +number +title +url """ def __init__(self, token, owner, name, team, max_page_size=100, min_page_size=10): @@ -78,13 +78,13 @@ class Query: def get_repository(self): _QUERY = """ - repository(owner: "{owner}" name: "{name}") {{ - defaultBranchRef {{ - name - }} - id - sshUrl - }} +repository(owner: "{owner}" name: "{name}") {{ + defaultBranchRef {{ + name + }} + id + sshUrl +}} """ query = _QUERY.format(owner=self._owner, name=self._name) @@ -98,20 +98,20 @@ class Query: """ _QUERY = """ - organization(login: "{organization}") {{ - team(slug: "{team}") {{ - members(first: {max_page_size} {next}) {{ - pageInfo {{ - hasNextPage - endCursor - }} - nodes {{ - id - login - }} - }} - }} +organization(login: "{organization}") {{ + team(slug: "{team}") {{ + members(first: {max_page_size} {next}) {{ + pageInfo {{ + hasNextPage + endCursor }} + nodes {{ + id + login + }} + }} + }} +}} """ members = {} @@ -133,7 +133,7 @@ class Query: organization=self._owner, team=self._team, max_page_size=self._max_page_size, - next='after: "{}"'.format(result["pageInfo"]["endCursor"]), + next=f'after: "{result["pageInfo"]["endCursor"]}"', ) # Update members with new nodes compatible with py3.8-py3.10 @@ -146,11 +146,11 @@ class Query: def get_pull_request(self, number): _QUERY = """ - repository(owner: "{owner}" name: "{name}") {{ - pullRequest(number: {number}) {{ - {pull_request_data} - }} - }} +repository(owner: "{owner}" name: "{name}") {{ + pullRequest(number: {number}) {{ + {pull_request_data} + }} +}} """ query = _QUERY.format( @@ -164,14 +164,16 @@ class Query: def find_pull_request(self, base, head): _QUERY = """ - repository(owner: "{owner}" name: "{name}") {{ - pullRequests(first: {min_page_size} baseRefName: "{base}" headRefName: "{head}") {{ - nodes {{ - {pull_request_data} - }} - totalCount - }} - }} +repository(owner: "{owner}" name: "{name}") {{ + pullRequests( + first: {min_page_size} baseRefName: "{base}" headRefName: "{head}" + ) {{ + nodes {{ + {pull_request_data} + }} + totalCount + }} +}} """ query = _QUERY.format( @@ -193,13 +195,13 @@ class Query: Get all pull-requests filtered by label name """ _QUERY = """ - repository(owner: "{owner}" name: "{name}") {{ - pullRequests(first: {min_page_size} labels: "{label_name}" states: OPEN) {{ - nodes {{ - {pull_request_data} - }} - }} - }} +repository(owner: "{owner}" name: "{name}") {{ + pullRequests(first: {min_page_size} labels: "{label_name}" states: OPEN) {{ + nodes {{ + {pull_request_data} + }} + }} +}} """ query = _QUERY.format( @@ -217,35 +219,32 @@ class Query: """ _QUERY = """ - repository(owner: "{owner}" name: "{name}") {{ - defaultBranchRef {{ - target {{ - ... on Commit {{ - history(first: {max_page_size} {next}) {{ - pageInfo {{ - hasNextPage - endCursor - }} - nodes {{ - oid - associatedPullRequests(first: {min_page_size}) {{ - totalCount - nodes {{ - ... on PullRequest {{ - {pull_request_data} +repository(owner: "{owner}" name: "{name}") {{ + defaultBranchRef {{ + target {{ + ... on Commit {{ + history(first: {max_page_size} {next}) {{ + pageInfo {{ + hasNextPage + endCursor + }} + nodes {{ + oid + associatedPullRequests(first: {min_page_size}) {{ + totalCount + nodes {{ + ... on PullRequest {{ + {pull_request_data} - labels(first: {min_page_size}) {{ - totalCount - pageInfo {{ - hasNextPage - endCursor - }} - nodes {{ - name - color - }} - }} - }} + labels(first: {min_page_size}) {{ + totalCount + pageInfo {{ + hasNextPage + endCursor + }} + nodes {{ + name + color }} }} }} @@ -254,6 +253,9 @@ class Query: }} }} }} + }} + }} +}} """ pull_requests = [] @@ -278,7 +280,7 @@ class Query: max_page_size=self._max_page_size, min_page_size=self._min_page_size, pull_request_data=self._PULL_REQUEST, - next='after: "{}"'.format(result["pageInfo"]["endCursor"]), + next=f'after: "{result["pageInfo"]["endCursor"]}"', ) for commit in result["nodes"]: @@ -296,7 +298,7 @@ class Query: for pull_request in commit["associatedPullRequests"]["nodes"]: if ( pull_request["baseRepository"]["nameWithOwner"] - == "{}/{}".format(self._owner, self._name) + == f"{self._owner}/{self._name}" and pull_request["baseRefName"] == self.default_branch and pull_request["mergeCommit"]["oid"] == commit["oid"] ): @@ -308,19 +310,19 @@ class Query: self, source, target, title, description="", draft=False, can_modify=True ): _QUERY = """ - createPullRequest(input: {{ - baseRefName: "{target}", - headRefName: "{source}", - repositoryId: "{id}", - title: "{title}", - body: "{body}", - draft: {draft}, - maintainerCanModify: {modify} - }}) {{ - pullRequest {{ - {pull_request_data} - }} - }} +createPullRequest(input: {{ + baseRefName: "{target}", + headRefName: "{source}", + repositoryId: "{id}", + title: "{title}", + body: "{body}", + draft: {draft}, + maintainerCanModify: {modify} +}}) {{ + pullRequest {{ + {pull_request_data} + }} +}} """ query = _QUERY.format( @@ -335,29 +337,29 @@ class Query: ) return self._run(query, is_mutation=True)["createPullRequest"]["pullRequest"] - def merge_pull_request(self, id): + def merge_pull_request(self, pr_id): _QUERY = """ - mergePullRequest(input: {{ - pullRequestId: "{id}" - }}) {{ - pullRequest {{ - {pull_request_data} - }} - }} +mergePullRequest(input: {{ + pullRequestId: "{pr_id}" +}}) {{ + pullRequest {{ + {pull_request_data} + }} +}} """ - query = _QUERY.format(id=id, pull_request_data=self._PULL_REQUEST) + query = _QUERY.format(pr_id=pr_id, pull_request_data=self._PULL_REQUEST) return self._run(query, is_mutation=True)["mergePullRequest"]["pullRequest"] # FIXME: figure out how to add more assignees at once def add_assignee(self, pr, assignee): _QUERY = """ - addAssigneesToAssignable(input: {{ - assignableId: "{id1}", - assigneeIds: "{id2}" - }}) {{ - clientMutationId - }} +addAssigneesToAssignable(input: {{ + assignableId: "{id1}", + assigneeIds: "{id2}" +}}) {{ + clientMutationId +}} """ query = _QUERY.format(id1=pr["id"], id2=assignee["id"]) @@ -373,28 +375,28 @@ class Query: """ _GET_LABEL = """ - repository(owner: "{owner}" name: "{name}") {{ - labels(first: {max_page_size} {next} query: "{label_name}") {{ - pageInfo {{ - hasNextPage - endCursor - }} - nodes {{ - id - name - color - }} - }} - }} +repository(owner: "{owner}" name: "{name}") {{ + labels(first: {max_page_size} {next} query: "{label_name}") {{ + pageInfo {{ + hasNextPage + endCursor + }} + nodes {{ + id + name + color + }} + }} +}} """ _SET_LABEL = """ - addLabelsToLabelable(input: {{ - labelableId: "{pr_id}", - labelIds: "{label_id}" - }}) {{ - clientMutationId - }} +addLabelsToLabelable(input: {{ + labelableId: "{pr_id}", + labelIds: "{label_id}" +}}) {{ + clientMutationId +}} """ labels = [] @@ -415,10 +417,10 @@ class Query: name=self._name, label_name=label_name, max_page_size=self._max_page_size, - next='after: "{}"'.format(result["pageInfo"]["endCursor"]), + next=f'after: "{result["pageInfo"]["endCursor"]}"', ) - labels += [label for label in result["nodes"]] + labels += list(result["nodes"]) if not labels: return @@ -458,27 +460,23 @@ class Query: # sleep a little, because we querying github too often time.sleep(0.1) - headers = {"Authorization": "bearer {}".format(self._token)} + headers = {"Authorization": f"bearer {self._token}"} if is_mutation: - query = """ - mutation {{ - {query} - }} - """.format( - query=query - ) + query = f""" +mutation {{ + {query} +}} + """ else: - query = """ - query {{ - {query} - rateLimit {{ - cost - remaining - }} - }} - """.format( - query=query - ) + query = f""" +query {{ + {query} + rateLimit {{ + cost + remaining + }} +}} + """ response = self.session.post( "https://api.github.com/graphql", json={"query": query}, headers=headers @@ -487,21 +485,15 @@ class Query: result = response.json() if "errors" in result: raise Exception( - "Errors occurred: {}\nOriginal query: {}".format( - result["errors"], query - ) + f"Errors occurred: {result['errors']}\nOriginal query: {query}" ) if not is_mutation: - if caller not in list(self.api_costs.keys()): + if caller not in self.api_costs: self.api_costs[caller] = 0 self.api_costs[caller] += result["data"]["rateLimit"]["cost"] return result["data"] else: - raise Exception( - "Query failed with code {code}:\n{json}".format( - code=response.status_code, - json=json.dumps(response.json(), indent=4), - ) - ) + data = json.dumps(response.json(), indent=4) + raise Exception(f"Query failed with code {response.status_code}:\n{data}")