From 82130f0241b281846076afbf96b2eb67959d5779 Mon Sep 17 00:00:00 2001 From: ckilpatrick-figma Date: Thu, 2 May 2024 22:35:04 -0700 Subject: [PATCH 1/3] Return partial sets of 'deletable_branches' and use exponential backoff for rate limiting --- src/github.py | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) diff --git a/src/github.py b/src/github.py index d11383b..02c0a90 100644 --- a/src/github.py +++ b/src/github.py @@ -133,6 +133,8 @@ def get_deletable_branches_from_closed_pull_requests( default_branch = self.get_default_branch() response = self.fetch_pull_requests() + if response[0] is None: + raise RuntimeError("Could not get any pull request info from GraphQL.") closed_pull_requests = response[0] after_cursor = response[1] has_next_page = response[2] @@ -216,6 +218,13 @@ def get_deletable_branches_from_closed_pull_requests( if has_next_page is True: response = self.fetch_pull_requests(after_cursor=after_cursor) + if response[0] is None: + # If we can't get any more pull requests, return whatever we have + if len(deletable_branches) > 0: + print(f'Could not get any more pull requests. Returning {len(deletable_branches)} branches') + return deletable_branches + else: + raise RuntimeError("Could not get any pull request info from GraphQL.") closed_pull_requests = response[0] after_cursor = response[1] has_next_page = response[2] @@ -375,25 +384,28 @@ def make_pull_request_query(self, count: int, after_cursor: str = None): def fetch_pull_requests(self, after_cursor: str = None): pull_requests = [] - counts = [30, 20, 10] data = {} + attempts = 4 - for count in counts: + while attempts > 0: try: data = self.client.execute( - query=self.make_pull_request_query(count, after_cursor), + query=self.make_pull_request_query(20, after_cursor), headers={"Authorization": "Bearer {}".format(self.token)}, ) if "data" in data: break except Exception as e: - print(e, data) - print(f"Could not get GraphQL result, retrying with count: {count}") - sleep(60/count) + print(f"Error fetching GraphQL result:\n{e}\ndata: {data}") + attempts -= 1 + if attempts > 0: + exponential_backoff_seconds = 3 ** (5 - attempts) + print(f"Retrying in {exponential_backoff_seconds} seconds (attempt {5 - attempts})\n") + sleep(exponential_backoff_seconds) if "data" not in data: - raise RuntimeError("Could not get pull request info from GraphQL after three tries") + return ([], None, False) for pull_request in data["data"]["repository"]["pullRequests"]["nodes"]: pull_requests.append(pull_request) From 411a29223817ac98223f834ccec75e4015a32893 Mon Sep 17 00:00:00 2001 From: ckilpatrick-figma Date: Thu, 2 May 2024 22:38:18 -0700 Subject: [PATCH 2/3] fix failure condition check --- src/github.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/github.py b/src/github.py index 02c0a90..fa967d0 100644 --- a/src/github.py +++ b/src/github.py @@ -133,7 +133,7 @@ def get_deletable_branches_from_closed_pull_requests( default_branch = self.get_default_branch() response = self.fetch_pull_requests() - if response[0] is None: + if response[1] is None: raise RuntimeError("Could not get any pull request info from GraphQL.") closed_pull_requests = response[0] after_cursor = response[1] @@ -218,7 +218,7 @@ def get_deletable_branches_from_closed_pull_requests( if has_next_page is True: response = self.fetch_pull_requests(after_cursor=after_cursor) - if response[0] is None: + if response[1] is None: # If we can't get any more pull requests, return whatever we have if len(deletable_branches) > 0: print(f'Could not get any more pull requests. Returning {len(deletable_branches)} branches') From eeabd4a79319d09ce0df3b120b13758610d1e442 Mon Sep 17 00:00:00 2001 From: ckilpatrick-figma Date: Thu, 2 May 2024 22:40:46 -0700 Subject: [PATCH 3/3] 4 attempts instead of 5 --- src/github.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/github.py b/src/github.py index fa967d0..bd00f5c 100644 --- a/src/github.py +++ b/src/github.py @@ -400,8 +400,8 @@ def fetch_pull_requests(self, after_cursor: str = None): print(f"Error fetching GraphQL result:\n{e}\ndata: {data}") attempts -= 1 if attempts > 0: - exponential_backoff_seconds = 3 ** (5 - attempts) - print(f"Retrying in {exponential_backoff_seconds} seconds (attempt {5 - attempts})\n") + exponential_backoff_seconds = 3 ** (4 - attempts) + print(f"Retrying in {exponential_backoff_seconds} seconds (attempt {4 - attempts})\n") sleep(exponential_backoff_seconds) if "data" not in data: