Skip to content

Commit

Permalink
Cache github api responses
Browse files Browse the repository at this point in the history
but always revalidate
  • Loading branch information
dflook committed Feb 11, 2024
1 parent 7f5594b commit 5544c09
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 4 deletions.
1 change: 1 addition & 0 deletions image/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
install_requires=[
'requests',
'requests-cache',
'python-hcl2',
'canonicaljson'
]
Expand Down
19 changes: 16 additions & 3 deletions image/src/github_actions/api.py
Original file line number Diff line number Diff line change
@@ -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)
Expand All @@ -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}'
Expand Down Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion image/src/github_pr_comment/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')

Expand Down

0 comments on commit 5544c09

Please sign in to comment.