From 39f8841421f9b460a8c34702b1cc2907a638feff Mon Sep 17 00:00:00 2001 From: Aidan Feldman Date: Wed, 15 Feb 2023 03:37:29 +0000 Subject: [PATCH] fix(Sentry): handle cases where Git is available but we're not in a repository --- benefits/sentry.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/benefits/sentry.py b/benefits/sentry.py index 560a38e54..1cc1fc9f1 100644 --- a/benefits/sentry.py +++ b/benefits/sentry.py @@ -13,6 +13,12 @@ def git_available(): return bool(shutil.which("git")) +# https://stackoverflow.com/a/24584384/358804 +def is_git_directory(path="."): + dev_null = open(os.devnull, "w") + return subprocess.call(["git", "-C", path, "status"], stderr=dev_null, stdout=dev_null) == 0 + + # https://stackoverflow.com/a/21901260/358804 def get_git_revision_hash(): return subprocess.check_output(["git", "rev-parse", "HEAD"]).decode("ascii").strip() @@ -26,7 +32,7 @@ def get_sha_path(): def get_release() -> str: """Returns the first available: the SHA from Git, the value from sha.txt, or the VERSION.""" - if git_available(): + if git_available() and is_git_directory(): return get_git_revision_hash() else: sha_path = get_sha_path()