Skip to content

Commit

Permalink
feat: get latest GTF release name from Github API
Browse files Browse the repository at this point in the history
  • Loading branch information
patrzhan committed Dec 4, 2023
1 parent 14fee92 commit c1978b0
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 1 deletion.
10 changes: 10 additions & 0 deletions gdk/common/GithubUtils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import requests


class GithubUtils:
def get_latest_release_name(self, owner, repository):
latest_release_api_url = f"https://api.github.com/repos/{owner}/{repository}/releases/latest"
response = requests.get(latest_release_api_url)
if response.status_code != 200:
response.raise_for_status()
return response.json()["name"]
16 changes: 15 additions & 1 deletion gdk/common/config/TestConfiguration.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
import logging

from gdk.common.GithubUtils import GithubUtils
from gdk.common.consts import GTF_REPO_OWNER, GTF_REPO_NAME


class TestConfiguration:
def __init__(self, test_config):
self.test_build_system = "maven"
self.gtf_version = "1.2.0" # TODO: Default value should be the latest version of otf testing standalone jar.
self.gtf_version = "1.2.0" # Default value for when Github API call fails
self.gtf_options = {}

self._set_test_config(test_config)
Expand All @@ -14,6 +20,14 @@ def _set_build_config(self, test_build_config):
self.test_build_system = test_build_config.get("build_system", self.test_build_system)

def _set_gtf_config(self, test_config):
github_utils = GithubUtils()
try:
latest_gtf_version = github_utils.get_latest_release_name(GTF_REPO_OWNER, GTF_REPO_NAME)
self.gtf_version = latest_gtf_version
logging.info("Discovered %s as latest GTF release name.", self.gtf_version)
except Exception as e:
logging.info("Unable to get the latest GTF release name. Using %s as the default value.", self.gtf_version)
logging.debug("Exception information for GTF release name API call: %s", str(e))
self.gtf_version = (test_config.get("gtf_version")
if "gtf_version" in test_config
else test_config.get("otf_version", self.gtf_version))
Expand Down
2 changes: 2 additions & 0 deletions gdk/common/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@
GDK_CONFIG_DOCS_LINK = (
"https://docs.aws.amazon.com/greengrass/v2/developerguide/gdk-cli-configuration-file.html#gdk-config-format"
)
GTF_REPO_OWNER = "aws-greengrass"
GTF_REPO_NAME = "aws-greengrass-testing"

# DEFAULT LOGGING
log_format = "[%(asctime)s] %(levelname)s - %(message)s"
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/gdk/common/config/test_GDKProject.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import pytest
from unittest import TestCase
from gdk.common.config.GDKProject import GDKProject
from gdk.common.GithubUtils import GithubUtils

import gdk.common.exceptions.error_messages as error_messages
import os
Expand All @@ -13,6 +14,7 @@ class GDKProjectTest(TestCase):
@pytest.fixture(autouse=True)
def __inject_fixtures(self, mocker, tmpdir):
self.mocker = mocker
self.mocker.patch.object(GithubUtils, "get_latest_release_name", return_value="1.2.0")
self.tmpdir = Path(tmpdir).resolve()
self.c_dir = Path(".").resolve()
os.chdir(self.tmpdir)
Expand Down
2 changes: 2 additions & 0 deletions integration_tests/gdk/test/test_integ_uat_InitCommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
from urllib3.exceptions import HTTPError
import gdk.common.consts as consts
from gdk.common.config.GDKProject import GDKProject
from gdk.common.GithubUtils import GithubUtils
import requests


Expand All @@ -27,6 +28,7 @@ def __inject_fixtures(self, mocker, tmpdir):
+ "TestTemplateForCLI.zip"
)
self.mocker.patch.object(GDKProject, "_get_recipe_file", return_value=Path(".").joinpath("recipe.json").resolve())
self.mocker.patch.object(GithubUtils, "get_latest_release_name", return_value="1.2.0")

os.chdir(tmpdir)
yield
Expand Down
2 changes: 2 additions & 0 deletions tests/gdk/commands/test/config/test_InitConfiguration.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import os
from gdk.commands.test.config.InitConfiguration import InitConfiguration
from gdk.common.config.GDKProject import GDKProject
from gdk.common.GithubUtils import GithubUtils
import requests


Expand All @@ -12,6 +13,7 @@ class InitConfigurationUnitTest(TestCase):
def __inject_fixtures(self, mocker, tmpdir):
self.mocker = mocker
self.tmpdir = tmpdir
self.mocker.patch.object(GithubUtils, "get_latest_release_name", return_value="1.2.0")
self.c_dir = Path(".").resolve()
self.mocker.patch.object(GDKProject, "_get_recipe_file", return_value=Path(".").joinpath("recipe.json").resolve())
os.chdir(tmpdir)
Expand Down
26 changes: 26 additions & 0 deletions tests/gdk/common/test_GithubUtils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
from unittest import TestCase

import pytest

from gdk.common.GithubUtils import GithubUtils


class MockGetResponse:
def __init__(self, json_data, status_code):
self.json_data = json_data
self.status_code = status_code

def json(self):
return self.json_data


class GithubUtilsTest(TestCase):
@pytest.fixture(autouse=True)
def __inject_fixtures(self, mocker):
self.mocker = mocker

def test_GIVEN_latest_release_request_WHEN_request_successful_THEN_return_release_name(self):
self.mocker.patch("requests.get", return_value=MockGetResponse({"name": "1.0.0"}, 200))
github_utils = GithubUtils()
latest_release = github_utils.get_latest_release_name("author", "repo")
assert latest_release == "1.0.0"

0 comments on commit c1978b0

Please sign in to comment.