Skip to content

Commit f562818

Browse files
SilviaAmAmJulian Roeland
authored and
Julian Roeland
committed
🔧 [#728] Update retrieving version info
1 parent e835ee0 commit f562818

File tree

2 files changed

+43
-17
lines changed

2 files changed

+43
-17
lines changed

backend/src/openarchiefbeheer/conf/base.py

+8-17
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from celery.schedules import crontab
99
from corsheaders.defaults import default_headers
1010

11-
from .utils import config, get_sentry_integrations
11+
from .utils import config, get_git_sha, get_release, get_sentry_integrations
1212

1313
# Build paths inside the project, so further paths can be defined relative to
1414
# the code root.
@@ -378,26 +378,17 @@
378378
# multiple login urls defined.
379379
LOGIN_URLS = [reverse_lazy("admin:login")]
380380

381+
381382
if "GIT_SHA" in os.environ:
382383
GIT_SHA = config("GIT_SHA", "")
383-
# in docker (build) context, there is no .git directory
384-
elif (BASE_DIR / ".git").exists():
385-
try:
386-
import git
387-
except ImportError:
388-
GIT_SHA = None
389-
else:
390-
repo = git.Repo(search_parent_directories=True)
391-
try:
392-
GIT_SHA = repo.head.object.hexsha
393-
except (
394-
ValueError
395-
): # on startproject initial runs before any git commits have been made
396-
GIT_SHA = repo.active_branch.name
397384
else:
398-
GIT_SHA = None
385+
GIT_SHA = get_git_sha()
386+
387+
if "RELEASE" in os.environ:
388+
RELEASE = config("RELEASE", "")
389+
else:
390+
RELEASE = get_release() or GIT_SHA
399391

400-
RELEASE = config("RELEASE", GIT_SHA)
401392

402393
REQUESTS_READ_TIMEOUT = config("REQUESTS_READ_TIMEOUT", 30)
403394
# Default (connection timeout, read timeout) for the requests library (in seconds)

backend/src/openarchiefbeheer/conf/utils.py

+35
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,38 @@ def get_sentry_integrations() -> list:
4848
extra.append(celery.CeleryIntegration())
4949

5050
return [*default, *extra]
51+
52+
53+
def get_git_sha() -> str:
54+
from git import InvalidGitRepositoryError, Repo
55+
56+
try:
57+
# in docker (build) context, there is no .git directory
58+
repo = Repo(search_parent_directories=True)
59+
except InvalidGitRepositoryError:
60+
return ""
61+
62+
try:
63+
return repo.head.object.hexsha
64+
except (
65+
ValueError
66+
): # on startproject initial runs before any git commits have been made
67+
return repo.active_branch.name
68+
69+
70+
def get_release() -> str:
71+
from git import InvalidGitRepositoryError, Repo
72+
73+
try:
74+
# in docker (build) context, there is no .git directory
75+
repo = Repo(search_parent_directories=True)
76+
except InvalidGitRepositoryError:
77+
return ""
78+
79+
if not len(repo.tags):
80+
return ""
81+
82+
current_tag = next(
83+
(tag for tag in repo.tags if tag.commit == repo.head.commit), None
84+
)
85+
return current_tag.name if current_tag else ""

0 commit comments

Comments
 (0)