From b994d1e0f8433d88efb9deed3a2b85ef071dac91 Mon Sep 17 00:00:00 2001 From: atarutin Date: Wed, 8 May 2024 18:30:54 +0300 Subject: [PATCH 1/3] Add function to define latest released version on Jira project #68 --- integration/jira.py | 24 ++++++++++++++++++++++++ jira_utils.py | 21 +++++++++++++++++++++ tests/test_jira_hotfix_version.py | 24 ++++++++++++++++++++++++ tests/test_jira_released_version.py | 19 +++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 tests/test_jira_hotfix_version.py create mode 100644 tests/test_jira_released_version.py diff --git a/integration/jira.py b/integration/jira.py index 391a178..fce8c87 100644 --- a/integration/jira.py +++ b/integration/jira.py @@ -7,6 +7,7 @@ from jira import JIRA, JIRAError from jira.resources import Component, Issue from jira.client import ResultList +from jira.resources import Version import jira_utils as ju @@ -98,6 +99,29 @@ def can_release_version(self, project_code: str, version_name: str) -> bool: return False return True + def get_latest_released_version(self, project_code: str) -> Version: + """ + Get the latest version of a project been released. + Hotfixes are excluded. + + :param project_code: project code + :return: latest version + """ + versions = sorted( + filter( + lambda v: not ju.is_jira_hotfix_version(v) + and ju.is_jira_released_version(v), + self.__j.project_versions(project_code), + ), + key=lambda v: v.releaseDate, + reverse=True, + ) + + if len(versions) == 0: + raise ValueError("No versions found") + + return versions[0] + def transition_issue( self, task_name: str, status: str, comment: str = "" ) -> str: diff --git a/jira_utils.py b/jira_utils.py index b989c42..149d97d 100644 --- a/jira_utils.py +++ b/jira_utils.py @@ -7,6 +7,7 @@ from validators.url import url from jira.resources import Issue +from jira.resources import Version from core.cvs import CodeRepository, GitCloudService from core.nova_component import NovaComponent, NovaEmptyComponent @@ -147,3 +148,23 @@ def filter_jira_issue(jira_issue, component_name) -> bool: nova_name = component_name.strip().lower() return jira_name == nova_name + + +def is_jira_released_version(version: Version) -> bool: + """ + Filter Jira version by release status. + + :param version: Jira version. + :return: True if version was released, False otherwise. + """ + return version.released + + +def is_jira_hotfix_version(version: Version) -> bool: + """ + Filter Jira version by hotfix status. + + :param version: Jira version. + :return: True if version is a hotfix, False otherwise. + """ + return "hotfix" in version.name.lower() diff --git a/tests/test_jira_hotfix_version.py b/tests/test_jira_hotfix_version.py new file mode 100644 index 0000000..749f90c --- /dev/null +++ b/tests/test_jira_hotfix_version.py @@ -0,0 +1,24 @@ +""" +""" + +from unittest.mock import Mock +import pytest +from jira_utils import is_jira_hotfix_version + + +@pytest.mark.parametrize( + "version_name", + [ + "hotfix-1.0.0", + "1.0.0-hotfix", + "1.0.0-hotfix-1", + "1.0.0-hotfix-2", + "1.0.0-hotfix-3", + "1.0.0-HoTfIx-4", + "hotfiX", + ], +) +def test_when_version_name_contains_hotfix(version_name): + version = Mock() + version.name = version_name + assert is_jira_hotfix_version(version) diff --git a/tests/test_jira_released_version.py b/tests/test_jira_released_version.py new file mode 100644 index 0000000..dee4667 --- /dev/null +++ b/tests/test_jira_released_version.py @@ -0,0 +1,19 @@ +""" +This module contains tests for the jira_utils module functions +which are used to work with versions. +""" + +from unittest.mock import Mock +from jira_utils import is_jira_released_version + + +def test_when_version_is_not_released(): + version = Mock() + version.released = False + assert not is_jira_released_version(version) + + +def test_when_version_is_released(): + version = Mock() + version.released = True + assert is_jira_released_version(version) From 97e6556ca99a0f894b460b4868e812a89a0d650a Mon Sep 17 00:00:00 2001 From: atarutin Date: Wed, 8 May 2024 19:22:44 +0300 Subject: [PATCH 2/3] Make "since" argument optional If not specified the latest delivery release date will be used fixes #68 --- main.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/main.py b/main.py index 8413496..e31c4c1 100644 --- a/main.py +++ b/main.py @@ -164,6 +164,22 @@ def choose_component_from_release(rel: NovaRelease) -> Optional[NovaComponent]: packages = release_repository.get_packages( config.data["jira"]["project"] ) + + if args.since: + print("Since date is specified as an argument.") + since = args.since + else: + print( + f"Since date is not specified, using latest released version date." + ) + print(f"If you want to specify since date, use --since argument.") + latest_version = ji.get_latest_released_version( + config.data["jira"]["project"] + ) + print(f"Latest released version detected: {latest_version.name}.") + since = latest_version.releaseDate + print(f"'Since' date to be used: {since}") + gi = GitIntegration() all_tags_info: list[dict[str, str]] = [] counter = 0 @@ -173,7 +189,7 @@ def choose_component_from_release(rel: NovaRelease) -> Optional[NovaComponent]: if package.repo is None: continue - repo_all_tags = gi.list_tags(package.repo.url, args.since) + repo_all_tags = gi.list_tags(package.repo.url, since) # filter out tags which are not related to packages, common rules package_tags = list( From 11586da4d6cb0a1ecf705e6a54e840e3fc2f721e Mon Sep 17 00:00:00 2001 From: atarutin Date: Wed, 8 May 2024 19:28:40 +0300 Subject: [PATCH 3/3] add module docstring --- tests/test_jira_hotfix_version.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_jira_hotfix_version.py b/tests/test_jira_hotfix_version.py index 749f90c..7832621 100644 --- a/tests/test_jira_hotfix_version.py +++ b/tests/test_jira_hotfix_version.py @@ -1,4 +1,6 @@ """ +This module contains tests for the is_jira_hotfix_version +function in the jira_utils module. """ from unittest.mock import Mock