Skip to content

Commit

Permalink
fix(check_updates): do not fail with a strange error message
Browse files Browse the repository at this point in the history
Reading the date of the last check from update_check.yaml failed because
the name of the key is `check-at`, not `check_at`. This used to work
because `load_yaml_dict()` transparently turned `-` into `_`, but it
does not do this anymore now that we fixed #480.
  • Loading branch information
agateau-gg committed Mar 14, 2023
1 parent 7681136 commit a4857cd
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions ggshield/core/check_updates.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"update_check.yaml",
)

CHECK_AT_KEY = "check-at"


# Use a short timeout to prevent blocking
CHECK_TIMEOUT = 5
Expand All @@ -26,6 +28,11 @@ def _split_version(version: str) -> Tuple[int, ...]:
return tuple([int(x) for x in version.split(".")])


def _save_check_time(check_at: float) -> None:
"""Store `check_at` in our cache"""
save_yaml_dict({CHECK_AT_KEY: check_at}, CACHE_FILE)


def check_for_updates() -> Optional[str]:
"""
Check for ggshield updates on GitHub. Return the latest version if available.
Expand All @@ -40,7 +47,7 @@ def check_for_updates() -> Optional[str]:
cached_data = None
if cached_data is not None:
try:
check_at = cached_data["check_at"]
check_at = cached_data[CHECK_AT_KEY]
except Exception as e:
logger.warning("Could not load cached latest version: %s", repr(e))

Expand All @@ -53,7 +60,7 @@ def check_for_updates() -> Optional[str]:
# Save check time now so that it is saved even if the check fails. This ensures we
# don't try for every command if the user does not have network access.
try:
save_yaml_dict({"check_at": time.time()}, CACHE_FILE)
_save_check_time(time.time())
except Exception as e:
logger.warning("Could not save time of version check to cache: %s", repr(e))
# Do not continue if we can't save check time. If we continue we are going to
Expand Down Expand Up @@ -89,7 +96,7 @@ def check_for_updates() -> Optional[str]:
return None

try:
save_yaml_dict({"check_at": check_at}, CACHE_FILE)
_save_check_time(check_at)
except Exception as e:
logger.warning(
"Could not save time of version check to cache: %s", repr(e)
Expand Down

0 comments on commit a4857cd

Please sign in to comment.