diff --git a/image/setup.py b/image/setup.py index 01f4c9e7..319d03d1 100644 --- a/image/setup.py +++ b/image/setup.py @@ -21,6 +21,7 @@ }, install_requires=[ 'requests', + 'requests-cache', 'python-hcl2', 'canonicaljson' ] diff --git a/image/src/github_actions/api.py b/image/src/github_actions/api.py index c893190f..3f8c0652 100644 --- a/image/src/github_actions/api.py +++ b/image/src/github_actions/api.py @@ -1,10 +1,11 @@ import datetime +import re import sys from typing import NewType, Iterable, Any, Optional import requests from requests import Response - +from requests_cache import CachedSession, EXPIRE_IMMEDIATELY from github_actions.debug import debug GitHubUrl = NewType('GitHubUrl', str) @@ -15,11 +16,20 @@ class GithubApi: - def __init__(self, host: str, token: Optional[str]): + def __init__(self, host: str, token: Optional[str], cache_path: Optional[str] = None): self._host = host self._token = token - self._session = requests.Session() + if cache_path is not None: + urls_expire_after = { + re.compile(r'/repos/.*/.*/issues/\d+/comments'): 60 * 60 * 24 * 3, + '*': EXPIRE_IMMEDIATELY + } + + self._session = CachedSession(backend='sqlite', cache_name=f'{cache_path}/github_api_cache', urls_expire_after=urls_expire_after, always_revalidate=True) + self._session.cache.delete(expired=True) + else: + self._session = requests.Session() if token is not None: self._session.headers['authorization'] = f'token {token}' @@ -67,6 +77,9 @@ def paged_get(self, url: GitHubUrl, *args, **kwargs) -> Iterable[dict[str, Any]] response = self.api_request('GET', url, *args, **kwargs) response.raise_for_status() + if hasattr(response, 'from_cache'): + debug(f'Cache hit: {response.from_cache}') + yield from response.json() if 'next' in response.links: diff --git a/image/src/github_pr_comment/__main__.py b/image/src/github_pr_comment/__main__.py index 9f67791a..2caccbac 100644 --- a/image/src/github_pr_comment/__main__.py +++ b/image/src/github_pr_comment/__main__.py @@ -35,7 +35,7 @@ env = cast(GithubEnv, os.environ) github_token = env['TERRAFORM_ACTIONS_GITHUB_TOKEN'] -github = GithubApi(env.get('GITHUB_API_URL', 'https://api.github.com'), github_token) +github = GithubApi(env.get('GITHUB_API_URL', 'https://api.github.com'), github_token, os.environ.get('JOB_TMP_DIR', '.')) ToolProductName = os.environ.get('TOOL_PRODUCT_NAME', 'Terraform')