Validating targets will instantly cache their profile data since we're hitting the profile api to validate

This commit is contained in:
Ritchie Mwewa
2026-01-03 23:02:58 +02:00
parent 77efa0982f
commit b5ee95db2b
2 changed files with 23 additions and 24 deletions

View File

@@ -45,7 +45,7 @@ class GitHub:
return response
if response.status_code == 200:
sanitised = self._sanitise_response(response=response.json())
sanitised = self.sanitise_response(response=response.json())
# Cache the successful response
if use_cache:
@@ -77,7 +77,7 @@ class GitHub:
# Only cache if entity exists (status 200)
if response.status_code == 200:
sanitised = self._sanitise_response(response.json())
sanitised = self.sanitise_response(response.json())
self.cache.set(url, sanitised)
return True
@@ -85,11 +85,11 @@ class GitHub:
except requests.RequestException:
return False
def _sanitise_response(self, response: t.Union[dict, list]) -> t.Union[dict, list]:
def sanitise_response(self, response: t.Union[dict, list]) -> t.Union[dict, list]:
pattern = re.compile(r"https://api\.github\.com")
if isinstance(response, list):
return [self._sanitise_response(response=item) for item in response]
return [self.sanitise_response(response=item) for item in response]
if isinstance(response, dict):
keys_to_remove = [
@@ -103,6 +103,6 @@ class GitHub:
# Recursively clean nested dicts/lists
for key, value in response.items():
if isinstance(value, (dict, list)):
response[key] = self._sanitise_response(response=value)
response[key] = self.sanitise_response(response=value)
return response

View File

@@ -1,3 +1,5 @@
from requests import exceptions
from .github import GitHub, BASE_URL
github = GitHub()
@@ -14,28 +16,25 @@ class GitHubEntity:
def exists(self) -> tuple[bool, dict]:
"""Check if the entity exists on GitHub."""
_type = None
kwargs = {}
# Check cache first
cached = github.cache.get(self.endpoint)
if cached is not None:
return True, cached
if isinstance(self, User):
_type = "user"
kwargs = {"username": self.name}
elif isinstance(self, Org):
_type = "org"
kwargs = {"username": self.name}
elif isinstance(self, Repo):
_type = "repo"
kwargs = {"repo_owner": self.owner, "repo_name": self.name}
try:
response = github.get(url=self.endpoint, return_response=True)
# Use is_valid_entity which handles caching and sanitization
exists = github.is_valid_entity(_type=_type, **kwargs)
if response.status_code == 200:
data = response.json()
# Sanitise the data
sanitised = github.sanitise_response(data)
# Cache the sanitised response
github.cache.set(self.endpoint, sanitised)
return True, sanitised
if exists:
# Get the cached sanitized data
cached = github.cache.get(self.endpoint)
return True, cached if cached else {}
return False, {}
return False, response.json()
except exceptions.RequestException:
return False, {}
class User(GitHubEntity):