Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
osoukup committed Dec 16, 2024
1 parent 92411e4 commit e15dcc6
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 5 deletions.
3 changes: 3 additions & 0 deletions apps/taskman/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
28 changes: 23 additions & 5 deletions osidb/models/flaw/flaw.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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
"""
Expand All @@ -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
"""
Expand All @@ -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
)
53 changes: 53 additions & 0 deletions osidb/sync_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit e15dcc6

Please sign in to comment.