Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/common/app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,17 @@ class VersionInfo(TypedDict):
package_versions: NotRequired[dict[str, str]]


@lru_cache()
def is_enterprise() -> bool:
return pathlib.Path("./ENTERPRISE_VERSION").exists()


@lru_cache()
def is_saas() -> bool:
return pathlib.Path("./SAAS_DEPLOYMENT").exists()


@lru_cache()
def has_email_provider() -> bool:
match settings.EMAIL_BACKEND:
case "django.core.mail.backends.smtp.EmailBackend":
Expand All @@ -47,22 +50,21 @@ def has_email_provider() -> bool:
return False


@lru_cache
def get_version_info() -> VersionInfo:
"""Reads the version info baked into src folder of the docker container"""
_is_saas = is_saas()
version_json: VersionInfo = {
"ci_commit_sha": _get_file_contents("./CI_COMMIT_SHA"),
"ci_commit_sha": get_file_contents("./CI_COMMIT_SHA") or UNKNOWN,
"image_tag": UNKNOWN,
"has_email_provider": has_email_provider(),
"is_enterprise": is_enterprise(),
"is_saas": _is_saas,
"self_hosted_data": None,
}

manifest_versions_content: str = _get_file_contents(VERSIONS_INFO_FILE_LOCATION)
manifest_versions_content = get_file_contents(VERSIONS_INFO_FILE_LOCATION)

if manifest_versions_content != UNKNOWN:
if manifest_versions_content:
manifest_versions = json.loads(manifest_versions_content)
version_json["package_versions"] = manifest_versions
version_json["image_tag"] = manifest_versions["."]
Expand All @@ -78,10 +80,11 @@ def get_version_info() -> VersionInfo:
return version_json


def _get_file_contents(file_path: str) -> str:
@lru_cache()
def get_file_contents(file_path: str) -> str | None:
"""Attempts to read a file from the filesystem and return the contents"""
try:
with open(file_path) as f:
return f.read().replace("\n", "")
except FileNotFoundError:
return UNKNOWN
return None
16 changes: 12 additions & 4 deletions tests/unit/common/app/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,24 @@
from pyfakefs.fake_filesystem import FakeFilesystem
from pytest_django.fixtures import SettingsWrapper

from common.app.utils import get_version_info

from common.app.utils import (
get_file_contents,
get_version_info,
has_email_provider,
is_enterprise,
is_saas,
)

pytestmark = pytest.mark.django_db


@pytest.fixture(autouse=True)
def clear_get_version_info_cache() -> Generator[None, None, None]:
def clear_lru_caches() -> Generator[None, None, None]:
yield
get_version_info.cache_clear()
get_file_contents.cache_clear()
has_email_provider.cache_clear()
is_enterprise.cache_clear()
is_saas.cache_clear()


def test_get_version_info(fs: FakeFilesystem) -> None:
Expand Down