From b80fc3bd14021d5e2208ef4bf0da72674bdd8f7c Mon Sep 17 00:00:00 2001 From: Chris Seto Date: Tue, 7 Feb 2017 10:15:36 -0500 Subject: [PATCH] [SHARE-497][Task] Add a status/version endpoint to the API * In DEBUG mode, if VERSION is not set it will be pulled from a git repo * The Dockerfile now accepts GIT_TAG which will be passed in as VERSION * [SHARE-526] Also removes "Notify" from browsable API page title --- Dockerfile | 2 ++ api/urls.py | 1 + api/views/share.py | 13 +++++++++++++ project/settings.py | 9 +++++++++ templates/rest_framework/api.html | 2 +- tests/api/test_status.py | 19 +++++++++++++++++++ 6 files changed, 45 insertions(+), 1 deletion(-) create mode 100644 tests/api/test_status.py diff --git a/Dockerfile b/Dockerfile index ebb591f89..34eee2205 100644 --- a/Dockerfile +++ b/Dockerfile @@ -52,7 +52,9 @@ COPY ./ /code/ RUN python manage.py collectstatic --noinput +ARG GIT_TAG= ARG GIT_COMMIT= +ENV VERSION ${GIT_TAG} ENV GIT_COMMIT ${GIT_COMMIT} CMD ["python", "manage.py", "--help"] diff --git a/api/urls.py b/api/urls.py index 890455c3f..16709d93f 100644 --- a/api/urls.py +++ b/api/urls.py @@ -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)), diff --git a/api/views/share.py b/api/views/share.py index 032a59492..f4815c1bd 100644 --- a/api/views/share.py +++ b/api/views/share.py @@ -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 @@ -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, + } + }) diff --git a/project/settings.py b/project/settings.py index dd1313f4b..7ac43758f 100644 --- a/project/settings.py +++ b/project/settings.py @@ -11,6 +11,7 @@ """ import os +import subprocess from django.utils.log import DEFAULT_LOGGING @@ -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' diff --git a/templates/rest_framework/api.html b/templates/rest_framework/api.html index 38cc64a76..5c1c3ddf5 100644 --- a/templates/rest_framework/api.html +++ b/templates/rest_framework/api.html @@ -2,7 +2,7 @@ {% load rest_framework %} {% load staticfiles %} - {% block title %}SHARE Notify{% endblock %} + {% block title %}SHARE{% endblock %} {% block style %} {% block bootstrap_theme %} diff --git a/tests/api/test_status.py b/tests/api/test_status.py new file mode 100644 index 000000000..1c2ba1b72 --- /dev/null +++ b/tests/api/test_status.py @@ -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', + } + } + }