Skip to content

Commit

Permalink
feat: Bake enterprise version info into private cloud image (#2420)
Browse files Browse the repository at this point in the history
* Bake enterprise version info into private cloud image

* formatting
  • Loading branch information
matthewelwell authored Jul 13, 2023
1 parent bee68de commit acebf93
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ jobs:
cd api
echo ${{ github.sha }} > CI_COMMIT_SHA
echo '${{ steps.meta.outputs.tags }}' > IMAGE_TAG
echo '' > ENTERPRISE_VERSION
- name: Docker metadata
id: meta
Expand Down
2 changes: 1 addition & 1 deletion api/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
url(r"^api/v1/", include("api.urls.v1", namespace="api-v1")),
url(r"^admin/", admin.site.urls),
url(r"^health", include("health_check.urls", namespace="health")),
url(r"^version", views.version_info),
url(r"^version", views.version_info, name="version-info"),
url(
r"^sales-dashboard/",
include("sales_dashboard.urls", namespace="sales_dashboard"),
Expand Down
18 changes: 10 additions & 8 deletions api/app/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,21 @@ def create_hash():
return shortuuid.uuid()


def get_version_info():
def get_version_info() -> dict:
"""Reads the version info baked into src folder of the docker container"""
version_json = {
"ci_commit_sha": get_file("./CI_COMMIT_SHA"),
"image_tag": get_file("./IMAGE_TAG"),
"ci_commit_sha": _get_file_contents("./CI_COMMIT_SHA"),
"image_tag": _get_file_contents("./IMAGE_TAG"),
"is_enterprise": pathlib.Path("./ENTERPRISE_VERSION").exists(),
}

return version_json


def get_file(file_path):
def _get_file_contents(file_path: str) -> str:
"""Attempts to read a file from the filesystem and return the contents"""
if pathlib.Path(file_path).is_file():
return open(file_path).read().replace("\n", "")

return "unknown"
try:
with open(file_path) as f:
return f.read().replace("\n", "")
except FileNotFoundError:
return "unknown"
3 changes: 2 additions & 1 deletion api/app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
from django.http import HttpResponse, JsonResponse
from django.template import loader
from django.views.decorators.csrf import csrf_exempt
from rest_framework.request import Request

from . import utils

logger = logging.getLogger(__name__)


def version_info(request):
def version_info(request: Request) -> JsonResponse:
return JsonResponse(utils.get_version_info())


Expand Down
Empty file added api/tests/unit/app/__init__.py
Empty file.
33 changes: 33 additions & 0 deletions api/tests/unit/app/test_unit_app_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import pathlib

from pytest_mock import MockerFixture

from app.utils import get_version_info


def test_get_version_info(mocker: MockerFixture) -> None:
# Given
mocked_pathlib = mocker.patch("app.utils.pathlib")

def path_side_effect(file_path: str) -> mocker.MagicMock:
mocked_path_object = mocker.MagicMock(spec=pathlib.Path)

if file_path == "./ENTERPRISE_VERSION":
mocked_path_object.exists.return_value = True

return mocked_path_object

mocked_pathlib.Path.side_effect = path_side_effect

mock_get_file_contents = mocker.patch("app.utils._get_file_contents")
mock_get_file_contents.side_effect = ("some_sha", "v1.0.0")

# When
result = get_version_info()

# Then
assert result == {
"ci_commit_sha": "some_sha",
"image_tag": "v1.0.0",
"is_enterprise": True,
}
19 changes: 19 additions & 0 deletions api/tests/unit/app/test_unit_app_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
from django.urls import reverse
from rest_framework import status
from rest_framework.test import APIClient


def test_get_version_info(api_client: APIClient) -> None:
# Given
url = reverse("version-info")

# When
response = api_client.get(url)

# Then
assert response.status_code == status.HTTP_200_OK
assert response.json() == {
"ci_commit_sha": "unknown",
"image_tag": "unknown",
"is_enterprise": False,
}

3 comments on commit acebf93

@vercel
Copy link

@vercel vercel bot commented on acebf93 Jul 13, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on acebf93 Jul 13, 2023

Choose a reason for hiding this comment

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

Successfully deployed to the following URLs:

docs – ./docs

docs-git-main-flagsmith.vercel.app
docs-flagsmith.vercel.app
docs.flagsmith.com
docs.bullet-train.io

@vercel
Copy link

@vercel vercel bot commented on acebf93 Jul 13, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.