From e15dcc63ceb2ae1d6be3c1228972174efd164118 Mon Sep 17 00:00:00 2001 From: osoukup Date: Mon, 16 Dec 2024 17:35:26 +0100 Subject: [PATCH] tmp --- apps/taskman/constants.py | 3 +++ osidb/models/flaw/flaw.py | 28 +++++++++++++++++---- osidb/sync_manager.py | 53 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 5 deletions(-) diff --git a/apps/taskman/constants.py b/apps/taskman/constants.py index 39bb85256..24ecc63ac 100644 --- a/apps/taskman/constants.py +++ b/apps/taskman/constants.py @@ -11,6 +11,9 @@ JIRA_TASKMAN_AUTO_SYNC_FLAW = get_env( "JIRA_TASKMAN_AUTO_SYNC_FLAW", default="0", is_bool=True ) +JIRA_TASKMAN_ASYNCHRONOUS_SYNC = get_env( + "JIRA_TASKMAN_ASYNCHRONOUS_SYNC", default="False", is_bool=True +) SYNC_REQUIRED_FIELDS = [ "cve_id", diff --git a/osidb/models/flaw/flaw.py b/osidb/models/flaw/flaw.py index 6f658be94..be06da44a 100644 --- a/osidb/models/flaw/flaw.py +++ b/osidb/models/flaw/flaw.py @@ -16,6 +16,7 @@ from apps.bbsync.constants import SYNC_FLAWS_TO_BZ, SYNC_FLAWS_TO_BZ_ASYNCHRONOUSLY from apps.bbsync.mixins import BugzillaSyncMixin from apps.taskman.constants import ( + JIRA_TASKMAN_ASYNCHRONOUS_SYNC, JIRA_TASKMAN_AUTO_SYNC_FLAW, SYNC_REQUIRED_FIELDS, TRANSITION_REQUIRED_FIELDS, @@ -1037,19 +1038,33 @@ def tasksync( based on the task existence it is either created or updated and/or transitioned old pre-OSIDB flaws without tasks are ignored unless force_creation is set """ + update_task = False + transition_task = False + if not self.task_key: # old pre-OSIDB flaws without tasks are ignored by default if force_creation or not self.meta_attr.get("bz_id"): - self._create_or_update_task(jira_token) + update_task = True elif diff is not None: if any(field in diff.keys() for field in SYNC_REQUIRED_FIELDS): - self._create_or_update_task(jira_token) + update_task = True if any(field in diff.keys() for field in TRANSITION_REQUIRED_FIELDS): - self._transition_task(jira_token) + transition_task = True + + if not update_task and not transition_task: + return - def _create_or_update_task(self, jira_token): + # switch of sync/async processing + if JIRA_TASKMAN_ASYNCHRONOUS_SYNC: + JiraTaskSyncManager.check_for_reschedules() + JiraTaskSyncManager.schedule(str(self.uuid), update_task, transition_task) + else: + self._create_or_update_task(jira_token) + self._transition_task(jira_token) + + def _create_or_update_task(self, jira_token=None): """ create or update the Jira task of this flaw based on its existence """ @@ -1076,7 +1091,7 @@ def _create_or_update_task(self, jira_token): task_updated_dt=self.task_updated_dt ) - def _transition_task(self, jira_token): + def _transition_task(self, jira_token=None): """ transition the Jira task of this flaw """ @@ -1103,6 +1118,9 @@ def _transition_task(self, jira_token): task_download_manager = models.ForeignKey( JiraTaskDownloadManager, null=True, blank=True, on_delete=models.CASCADE ) + task_sync_manager = models.ForeignKey( + JiraTaskSyncManager, null=True, blank=True, on_delete=models.CASCADE + ) bzsync_manager = models.ForeignKey( BZSyncManager, null=True, blank=True, on_delete=models.CASCADE ) diff --git a/osidb/sync_manager.py b/osidb/sync_manager.py index baa150b00..c1130759d 100644 --- a/osidb/sync_manager.py +++ b/osidb/sync_manager.py @@ -679,6 +679,59 @@ def __str__(self): return result +class JiraTaskSyncManager(SyncManager): + """ + Sync manager class for OSIDB => Jira Task synchronization. + """ + + @staticmethod + @app.task(name="sync_manager.jira_task_sync", bind=True) + def sync_task(self, flaw_id, update_task=False, transition_task=False): + """ + perform the sync of the task of the given flaw to Jira + + the task may not be exising yet when performing the first + sync therefore we use the flaw UUID as the identifier + + the task update and task transition use two different Jira + endpoints so the call parameters specify what to perform + """ + from osidb.models import Flaw + + JiraTaskSyncManager.started(flaw_id, self) + + set_user_acls(settings.ALL_GROUPS) + + try: + flaw = Flaw.objects.get(uuid=flaw_id) + + if update_task: + flaw._create_or_update_task() + + if transition_task: + flaw._transition_task() + + except Exception as e: + JiraTaskSyncManager.failed(flaw_id, e) + else: + JiraTaskSyncManager.finished(flaw_id) + + def update_synced_links(self): + from osidb.models import Flaw + + Flaw.objects.filter(uuid=self.sync_id).update(bzsync_manager=self) + + def __str__(self): + from osidb.models import Flaw + + result = super().__str__() + flaws = Flaw.objects.filter(uuid=self.sync_id) + cves = [f.cve_id or f.uuid for f in flaws] + result += f"Flaws: {cves}\n" + + return result + + class JiraTrackerDownloadManager(SyncManager): """ Sync manager class for Jira => OSIDB Tracker synchronization.