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
2 changes: 2 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ COPY ./ /code/

RUN python manage.py collectstatic --noinput

ARG GIT_TAG=
ARG GIT_COMMIT=
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason for the separate GIT_TAG and GIT_COMMIT? It looks like GIT_COMMIT is only used for release in sentry issues, and it looks like it's null. Seems like they could be the same thing?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Mostly readability:

# git rev-parse HEAD
aeb92b8c98b9b2165b66f4ba2f866636743fec26

# git describe
2.3.1-16-gaeb92b8

The purpose of the endpoint is mostly to check that the latest release is actually whats released.

I don't really have strong feelings one way or the other. Do you have a preference?

(And I copied it from fakecas.)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

git describe is definitely better than the full commit hash. I don't have any feelings about GIT_COMMIT, it just seemed potentially redundant, and if it's only used for display (or not actually given a value?) could be replaced with GIT_TAG. 🤷‍♂️

ENV VERSION ${GIT_TAG}
ENV GIT_COMMIT ${GIT_COMMIT}

CMD ["python", "manage.py", "--help"]
1 change: 1 addition & 0 deletions api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ def register_url(self, subclass, viewset):
]

urlpatterns = [
url(r'status/?', views.ServerStatusView.as_view(), name='status'),
url(r'rss/?', views.CreativeWorksRSS(), name='rss'),
url(r'atom/?', views.CreativeWorksAtom(), name='atom'),
url(r'graph/?', GraphQLView.as_view(graphiql=True)),
Expand Down
13 changes: 13 additions & 0 deletions api/views/share.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from rest_framework_json_api import serializers

from django import http
from django.conf import settings
from django.views.decorators.http import require_GET
from django.views.generic.base import RedirectView
from django.shortcuts import get_object_or_404
Expand Down Expand Up @@ -139,3 +140,15 @@ def get(self, request, *args, **kwargs):
return HttpSmartResponsePermanentRedirect(url)
return HttpSmartResponseRedirect(url)
return http.HttpResponseGone()


class ServerStatusView(views.APIView):
def get(self, request):
return Response({
'id': '1',
'type': 'Status',
'attributes': {
'status': 'up',
'version': settings.VERSION,
}
})
9 changes: 9 additions & 0 deletions project/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"""

import os
import subprocess

from django.utils.log import DEFAULT_LOGGING

Expand All @@ -35,6 +36,14 @@
# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = bool(os.environ.get('DEBUG', True))

if 'VERSION' not in os.environ and DEBUG:
try:
VERSION = subprocess.check_output(['git', 'describe']).decode().strip()
except subprocess.CalledProcessError:
VERSION = 'UNKNOWN'
else:
VERSION = os.environ.get('VERSION') or 'UNKNOWN'

ALLOWED_HOSTS = os.environ.get('ALLOWED_HOSTS', '').split(' ')

AUTH_USER_MODEL = 'share.ShareUser'
Expand Down
2 changes: 1 addition & 1 deletion templates/rest_framework/api.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
{% load rest_framework %}
{% load staticfiles %}

{% block title %}SHARE Notify{% endblock %}
{% block title %}SHARE{% endblock %}

{% block style %}
{% block bootstrap_theme %}
Expand Down
19 changes: 19 additions & 0 deletions tests/api/test_status.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.test import override_settings


class TestAPIStatusView:

@override_settings(VERSION='TESTCASE')
def test_works(self, client):
resp = client.get('/api/v2/status/')
assert resp.status_code == 200
assert resp.json() == {
'data': {
'id': '1',
'type': 'Status',
'attributes': {
'status': 'up',
'version': 'TESTCASE',
}
}
}