diff --git a/gdk/common/GithubUtils.py b/gdk/common/GithubUtils.py new file mode 100644 index 00000000..879e841c --- /dev/null +++ b/gdk/common/GithubUtils.py @@ -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"] diff --git a/gdk/common/config/TestConfiguration.py b/gdk/common/config/TestConfiguration.py index 7db65d26..0a51028c 100644 --- a/gdk/common/config/TestConfiguration.py +++ b/gdk/common/config/TestConfiguration.py @@ -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) @@ -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)) diff --git a/gdk/common/consts.py b/gdk/common/consts.py index 8565c81c..2c808664 100644 --- a/gdk/common/consts.py +++ b/gdk/common/consts.py @@ -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" diff --git a/integration_tests/gdk/common/config/test_GDKProject.py b/integration_tests/gdk/common/config/test_GDKProject.py index 9c97283d..348ab81e 100644 --- a/integration_tests/gdk/common/config/test_GDKProject.py +++ b/integration_tests/gdk/common/config/test_GDKProject.py @@ -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 @@ -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) diff --git a/integration_tests/gdk/test/test_integ_uat_InitCommand.py b/integration_tests/gdk/test/test_integ_uat_InitCommand.py index 810bab64..2192468e 100644 --- a/integration_tests/gdk/test/test_integ_uat_InitCommand.py +++ b/integration_tests/gdk/test/test_integ_uat_InitCommand.py @@ -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 @@ -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 diff --git a/tests/gdk/commands/test/config/test_InitConfiguration.py b/tests/gdk/commands/test/config/test_InitConfiguration.py index 810c83ed..201afcf3 100644 --- a/tests/gdk/commands/test/config/test_InitConfiguration.py +++ b/tests/gdk/commands/test/config/test_InitConfiguration.py @@ -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 @@ -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) diff --git a/tests/gdk/common/test_GithubUtils.py b/tests/gdk/common/test_GithubUtils.py new file mode 100644 index 00000000..2e49b933 --- /dev/null +++ b/tests/gdk/common/test_GithubUtils.py @@ -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"