From 51b5b6be9b3c983490efd1f515b8b6faf9b0af65 Mon Sep 17 00:00:00 2001 From: Victor Seva Date: Tue, 20 Sep 2022 16:04:54 +0200 Subject: [PATCH] TT#33006 repoapi: add notes to MANTIS using old TT# IDs using mapper info, try to push notes to migrated mantis tickets using old workfront ids * tracker: add support for get old WF ids via setting Change-Id: Ief8e5eb44c0d802d0de6e88dec165039a058cebe --- hotfix/test/test_utils.py | 4 +++- tracker/conf.py | 1 + tracker/models.py | 24 ++++++++++++++++++--- tracker/test/test_models.py | 42 +++++++++++++++++++++++++++++++++++++ 4 files changed, 67 insertions(+), 4 deletions(-) diff --git a/hotfix/test/test_utils.py b/hotfix/test/test_utils.py index 1490025..1f3c6f5 100644 --- a/hotfix/test/test_utils.py +++ b/hotfix/test/test_utils.py @@ -50,7 +50,9 @@ class TestUtils(SimpleTestCase): self.assertEqual(changelog.full_version, "3.8.7.4+0~mr3.8.7.4") self.assertEqual(changelog.package, "ngcp-fake") - @override_settings(TRACKER_PROVIDER=Tracker.MANTIS) + @override_settings( + TRACKER_PROVIDER=Tracker.MANTIS, TRACKER_WORKFRONT_MAPPER_IDS=False + ) @patch("builtins.open", mock_open(read_data=debian_changelog)) def test_parse_changelog_mantis(self): ids, changelog = utils.parse_changelog("/tmp/fake.txt") diff --git a/tracker/conf.py b/tracker/conf.py index bb68c08..8712354 100644 --- a/tracker/conf.py +++ b/tracker/conf.py @@ -41,6 +41,7 @@ class TrackerConf(AppConf): ] WORKFRONT_CREDENTIALS = "fake.txt" MANTIS_URL = "https://support.local/api/rest/{}" + WORKFRONT_MAPPER_IDS = True MANTIS_MAPPER_URL = "https://support.local/view.php?id={mantis_id}" MANTIS_TOKEN = "fake_mantis_token" MANTIS_TARGET_RELEASE = { diff --git a/tracker/models.py b/tracker/models.py index 70d3d8b..a068d50 100644 --- a/tracker/models.py +++ b/tracker/models.py @@ -37,11 +37,10 @@ class TrackerInfo(models.Model): parses text searching for tracker occurrences returns a list of IDs """ + res = () if change: res = cls.tracker_re.findall(change) - return set(res) - else: - return set() + return set(res) @property def field_id(self): @@ -71,6 +70,18 @@ class MantisInfo(TrackerInfo): class Meta: abstract = True + @classmethod + def getIds(cls, change): + from tracker.conf import settings + + res = super().getIds(change) + if change and settings.TRACKER_WORKFRONT_MAPPER_IDS: + old_ids = WorkfrontInfo.getIds(change) + qs = TrackerMapper.objects.get_wf_qs(old_ids) + for wf in qs: + res.add(wf.mantis_id) + return res + def send(self, msg: str): return utils.mantis_note_send(self.mantis_id, msg) @@ -79,6 +90,13 @@ class MantisInfo(TrackerInfo): class TrackerMapperManager(models.Manager): + def get_wf_qs(self, _ids): + return ( + self.get_queryset() + .filter(workfront_id__in=_ids) + .order_by("mantis_id") + ) + def get_workfront_issue_qs(self, _id): return self.get_queryset().filter( Q(workfront_id=_id) | Q(workfront_uuid=_id), diff --git a/tracker/test/test_models.py b/tracker/test/test_models.py index e758213..2b4ec37 100644 --- a/tracker/test/test_models.py +++ b/tracker/test/test_models.py @@ -12,7 +12,11 @@ # # You should have received a copy of the GNU General Public License along # with this program. If not, see . +from django.test import override_settings +from natsort import humansorted + from repoapi.test.base import BaseTest +from tracker.models import MantisInfo from tracker.models import TrackerMapper @@ -20,8 +24,10 @@ class TrackerMapperTest(BaseTest): fixtures = ["test_mapper"] ISSUE_id = "1022" ISSUE_uuid = "577a4dfb004111d28a015ed5a24512a4" + ISSUE_mantis_id = "33066" TASK_id = "190650" TASK_uuid = "631ee19a0283b8913a3ed6e6938bbd6d" + TASK_mantis_id = "55282" def test_get_workfront_issue_qs(self): qs_uuid = TrackerMapper.objects.get_workfront_issue_qs(self.ISSUE_uuid) @@ -32,3 +38,39 @@ class TrackerMapperTest(BaseTest): def test_get_workfront_issue_qs_ko(self): qs_uuid = TrackerMapper.objects.get_workfront_issue_qs("fake") self.assertEqual(qs_uuid.count(), 0) + + def test_get_wf_qs(self): + qs = TrackerMapper.objects.get_wf_qs([self.ISSUE_id, self.TASK_id]) + self.assertEqual(qs.count(), 2) + self.assertEqual(qs.first().mantis_id, self.ISSUE_mantis_id) + self.assertEqual(qs.last().mantis_id, self.TASK_mantis_id) + + def test_get_wf_qs_ko(self): + wf = TrackerMapper.objects.get_wf_qs(["0000"]) + self.assertEqual(wf.count(), 0) + + @override_settings(TRACKER_WORKFRONT_MAPPER_IDS=False) + def test_getIds(self): + ids = MantisInfo.getIds("whatever MT#1234 TT#1022 TT#190650 MT#33006") + self.assertListEqual( + humansorted(ids), + ["1234", "33006"], + ) + + @override_settings(TRACKER_WORKFRONT_MAPPER_IDS=True) + def test_getIds_mapper(self): + ids = MantisInfo.getIds( + "whatever MT#1234 TT#1022 TT#190650 MT#33006", + ) + self.assertListEqual( + humansorted(ids), + ["1234", "33006", self.ISSUE_mantis_id, self.TASK_mantis_id], + ) + + @override_settings(TRACKER_WORKFRONT_MAPPER_IDS=True) + def test_getIds_mapper_ko(self): + ids = MantisInfo.getIds("whatever MT#1234 TT#000 TT#0 MT#33006") + self.assertListEqual( + humansorted(ids), + ["1234", "33006"], + )