From 672f7c53e114e43bb0c8177762bff57bac4e4c43 Mon Sep 17 00:00:00 2001
From: Jan Kaluza <jkaluza@redhat.com>
Date: Jan 23 2018 08:01:07 +0000
Subject: Add convenient method from_errata_advisory() for Errata related events.


---

diff --git a/freshmaker/events.py b/freshmaker/events.py
index 238920a..79be0ca 100644
--- a/freshmaker/events.py
+++ b/freshmaker/events.py
@@ -263,18 +263,49 @@ class ErrataAdvisoryStateChangedEvent(BaseEvent):
         self.state = state
         self.content_types = content_types
 
+    @classmethod
+    def from_errata_advisory(cls, msg_id, advisory):
+        """
+        Creates new ErrataAdvisoryStateChangedEvent from errata.ErrataAdvisory
+        instances.
+
+        :param ErrataAdvisory advisory: errata.ErrataAdvisory instance.
+        :rtype: ErrataAdvisoryStateChangedEvent
+        :return: New ErrataAdvisoryStateChangedEvent instance.
+        """
+
+        return ErrataAdvisoryStateChangedEvent(
+            msg_id, advisory.errata_id, advisory.state,
+            advisory.content_types)
+
 
 class ErrataAdvisoryRPMsSignedEvent(BaseEvent):
     """
     Event when all RPMs in Errata advisory are signed.
     """
-    def __init__(self, msg_id, errata_name, errata_id, security_impact, errata_state):
+    def __init__(self, msg_id, errata_name, errata_id, security_impact,
+                 errata_state):
         super(ErrataAdvisoryRPMsSignedEvent, self).__init__(msg_id)
         self.errata_name = errata_name
         self.errata_id = errata_id
         self.security_impact = security_impact
         self.errata_state = errata_state
 
+    @classmethod
+    def from_errata_advisory(cls, msg_id, advisory):
+        """
+        Creates new ErrataAdvisoryRPMsSignedEvent from errata.ErrataAdvisory
+        instances.
+
+        :param ErrataAdvisory advisory: errata.ErrataAdvisory instance.
+        :rtype: ErrataAdvisoryRPMsSignedEvent
+        :return: New ErrataAdvisoryRPMsSignedEvent instance.
+        """
+
+        return ErrataAdvisoryRPMsSignedEvent(
+            msg_id, advisory.name, advisory.errata_id,
+            advisory.security_impact, advisory.state)
+
     @property
     def search_key(self):
         return str(self.errata_id)
diff --git a/freshmaker/handlers/brew/sign_rpm.py b/freshmaker/handlers/brew/sign_rpm.py
index 1ce1493..4e7bdcf 100644
--- a/freshmaker/handlers/brew/sign_rpm.py
+++ b/freshmaker/handlers/brew/sign_rpm.py
@@ -96,10 +96,8 @@ class BrewSignRPMHandler(BaseHandler):
         # ErrataAdvisoryRPMsSignedEvent.
         new_events = []
         for advisory in advisories:
-            new_event = ErrataAdvisoryRPMsSignedEvent(
-                event.msg_id + "." + str(advisory.name), advisory.name,
-                advisory.errata_id, advisory.security_impact,
-                advisory.state)
+            new_event = ErrataAdvisoryRPMsSignedEvent.from_errata_advisory(
+                event.msg_id + "." + str(advisory.name), advisory)
             db_event = Event.create(
                 db.session, new_event.msg_id, new_event.search_key,
                 new_event.__class__, released=False)
diff --git a/freshmaker/handlers/errata/errata_advisory_state_changed.py b/freshmaker/handlers/errata/errata_advisory_state_changed.py
index fd4b8e3..7881923 100644
--- a/freshmaker/handlers/errata/errata_advisory_state_changed.py
+++ b/freshmaker/handlers/errata/errata_advisory_state_changed.py
@@ -100,9 +100,8 @@ class ErrataAdvisoryStateChangedHandler(BaseHandler):
                  "advisory %d, because its state changed to %s.", errata_id,
                  event.state)
         advisory = advisories[0]
-        db_event = ErrataAdvisoryRPMsSignedEvent(
-            event.msg_id + "." + str(advisory.name), advisory.name,
-            advisory.errata_id, advisory.security_impact, advisory.state)
+        db_event = ErrataAdvisoryRPMsSignedEvent.from_errata_advisory(
+            event.msg_id + "." + str(advisory.name), advisory)
         return [db_event]
 
     def handle(self, event):
diff --git a/freshmaker/handlers/internal/manual_rebuild.py b/freshmaker/handlers/internal/manual_rebuild.py
index d91afc3..99483b9 100644
--- a/freshmaker/handlers/internal/manual_rebuild.py
+++ b/freshmaker/handlers/internal/manual_rebuild.py
@@ -63,9 +63,8 @@ class FreshmakerManualRebuildHandler(ContainerBuildHandler):
                  "advisory %d - manually triggered rebuild.",
                  manual_rebuild_event.errata_id)
         advisory = advisories[0]
-        new_event = ErrataAdvisoryStateChangedEvent(
-            manual_rebuild_event.msg_id + "." + str(advisory.name),
-            advisory.errata_id, advisory.state, advisory.content_types)
+        new_event = ErrataAdvisoryStateChangedEvent.from_errata_advisory(
+            manual_rebuild_event.msg_id + "." + str(advisory.name), advisory)
         new_event.manual = True
         msg = ("Generated ErrataAdvisoryStateChangedEvent (%s) for errata: %s"
                % (manual_rebuild_event.msg_id, manual_rebuild_event.errata_id))

