diff --git a/scheduler.py b/scheduler.py index 3d96466c..c7b13cb4 100644 --- a/scheduler.py +++ b/scheduler.py @@ -1,6 +1,7 @@ """This is scheduled to run by AWS Lambda""" import logging +import time from config import PRODUCT_ID, SUPABASE_SERVICE_ROLE_KEY, SUPABASE_URL from services.github.github_manager import ( add_label_to_issue, @@ -22,6 +23,10 @@ def schedule_handler(_event, _context) -> dict[str, int]: # Get all owners and repositories from GitHub. for installation_id in installation_ids: + # Pause for 1+ second to avoid secondary rate limits. https://docs.github.com/en/rest/using-the-rest-api/best-practices-for-using-the-rest-api?apiVersion=2022-11-28#pause-between-mutative-requests + time.sleep(1) + + # Start processing this loop. token: str = get_installation_access_token(installation_id=installation_id) owners_repos: list[dict[str, str]] = get_installed_owners_and_repos( installation_id=installation_id, token=token @@ -51,6 +56,7 @@ def schedule_handler(_event, _context) -> dict[str, int]: issue_number = issue["number"] # Label the issue with the product ID to trigger GitAuto. + time.sleep(1) add_label_to_issue( owner=owner, repo=repo, diff --git a/services/github/github_manager.py b/services/github/github_manager.py index 9a37c031..c918652f 100644 --- a/services/github/github_manager.py +++ b/services/github/github_manager.py @@ -332,10 +332,10 @@ def initialize_repo(repo_path: str, remote_url: str) -> None: def get_installation_access_token(installation_id: int) -> str: """https://docs.github.com/en/rest/apps/apps?apiVersion=2022-11-28#create-an-installation-access-token-for-an-app""" jwt_token: str = create_jwt() - headers: dict[str, str] = create_headers(token=jwt_token) - url = f"{GITHUB_API_URL}/app/installations/{installation_id}/access_tokens" response: requests.Response = requests.post( - url=url, headers=headers, timeout=TIMEOUT_IN_SECONDS + url=f"{GITHUB_API_URL}/app/installations/{installation_id}/access_tokens", + headers=create_headers(token=jwt_token), + timeout=TIMEOUT_IN_SECONDS, ) response.raise_for_status() return response.json()["token"]