From 8ee3cf8df3112aac6f1ec3c0053d0218a4740ac6 Mon Sep 17 00:00:00 2001
From: Qixiang Wan <qwan@redhat.com>
Date: Jun 05 2017 09:22:56 +0000
Subject: Remove event from whitelist and blacklist


As we have one handler for each event, so just having handler name in
whiltelist and blacklist is enough.

---

diff --git a/conf/config.py b/conf/config.py
index 26e85bc..c1445af 100644
--- a/conf/config.py
+++ b/conf/config.py
@@ -80,45 +80,39 @@ class BaseConfiguration(object):
     SSL_ENABLED = False
 
     # whitelist and blacklist for handlers to decide whether an artifact
-    # can be built on some events.
+    # can be built.
     #
     # In format of:
     #
     # { <handler_name> :
-    #     { <event_name> :
-    #         { <artifact_type>: <list_of_name_branch_dict> }
-    #     }
+    #     { <artifact_type>: <list_of_name_branch_dict> }
     # }
     #
-    # Here is an example of allowing MBS handler to build any module on
-    # "RPMSpecUpdated" event that module name matches 'base-.*' but not:
+    # Here is an example of allowing MBSModuleStateChangeHandler to build
+    # any module that module name matches 'base-.*' but not:
     #   1. module name matches 'base-test-module'
     # or:
     #   2. module from branch 'rawhide'
     #
     # HANDLER_BUILD_WHITELIST = {
-    #     "MBS": {
-    #         "RPMSpecUpdated": {
-    #             "module": [
-    #                 {
-    #                     'name': 'base-.*',
-    #                 },
-    #             ],
-    #         },
+    #     "MBSModuleStateChangeHandler": {
+    #         "module": [
+    #             {
+    #                 'name': 'base-.*',
+    #             },
+    #         ],
     #     },
     # }
     # HANDLER_BUILD_BLACKLIST = {
-    #     "MBS": {
-    #         "RPMSpecUpdated": {
-    #             "module": [
-    #                 {
-    #                     'name': 'base-test-module',
-    #                 },
-    #                 {
-    #                     'branch': 'rawhide',
-    #                 },
-    #             ],
-    #         },
+    #     "MBSModuleStateChangeHandler": {
+    #         "module": [
+    #             {
+    #                 'name': 'base-test-module',
+    #             },
+    #             {
+    #                 'branch': 'rawhide',
+    #             },
+    #         ],
     #     },
     # }
 
diff --git a/freshmaker/handlers/__init__.py b/freshmaker/handlers/__init__.py
index 8724d0d..383fa70 100644
--- a/freshmaker/handlers/__init__.py
+++ b/freshmaker/handlers/__init__.py
@@ -129,18 +129,17 @@ class BaseHandler(object):
         models.ArtifactBuild.create(db.session, ev, name, type, build_id, dep_of)
         db.session.commit()
 
-    def allow_build(self, event, artifact_type, name, branch):
+    def allow_build(self, artifact_type, name, branch):
         """
         Check whether the artifact is allowed to be built by checking
         HANDLER_BUILD_WHITELIST and HANDLER_BUILD_BLACKLIST in config.
 
-        :param event: event instance.
         :param artifact_type: 'module' or 'image'.
         :param name: name of the artifact.
         :param branch: branch name of the artifact.
         :return: True or False.
         """
-        # If there is a whitelist specified for the (handler, event, artifact_type),
+        # If there is a whitelist specified for the (handler, artifact_type),
         # the build target of (name, branch) need to be in that whitelist first.
         # After that (if the build target is in whitelist), check the build target
         # is not in the specified blacklist.
@@ -150,9 +149,8 @@ class BaseHandler(object):
         in_blacklist = False
 
         handler_name = self.name
-        event_name = type(event).__name__
-        whitelist_rules = conf.handler_build_whitelist.get(handler_name, {}).get(event_name, {})
-        blacklist_rules = conf.handler_build_blacklist.get(handler_name, {}).get(event_name, {})
+        whitelist_rules = conf.handler_build_whitelist.get(handler_name, {})
+        blacklist_rules = conf.handler_build_blacklist.get(handler_name, {})
 
         def match_rule(name, branch, rule):
             name_rule = rule.get('name', None)
@@ -179,8 +177,8 @@ class BaseHandler(object):
                     in_blacklist = True
 
         except re.error as exc:
-            log.error("Error while compiling blacklist/whilelist rule for <handler(%s) event(%s) artifact(%s)>:\n"
+            log.error("Error while compiling blacklist/whilelist rule for <handler(%s) artifact(%s)>:\n"
                       "Incorrect regular expression: %s\nBlacklist and Whitelist will not take effect",
-                      handler_name, event_name, artifact_type, str(exc))
+                      handler_name, artifact_type, str(exc))
             return True
         return in_whitelist and not in_blacklist
diff --git a/freshmaker/handlers/bodhi/update_complete_stable.py b/freshmaker/handlers/bodhi/update_complete_stable.py
index 24551af..a62aa82 100644
--- a/freshmaker/handlers/bodhi/update_complete_stable.py
+++ b/freshmaker/handlers/bodhi/update_complete_stable.py
@@ -49,7 +49,7 @@ class BodhiUpdateCompleteStableHandler(BaseHandler):
         log.info('Found docker images to rebuild: %s', containers)
 
         for container in containers:
-            if not self.allow_build(event, 'image', container['name'], container['branch']):
+            if not self.allow_build('image', container['name'], container['branch']):
                 log.info("Skip rebuild of image %s:%s as it's not allowed by configured whitelist/blacklist",
                          container['name'], container['branch'])
                 continue
diff --git a/freshmaker/handlers/git/dockerfile_change.py b/freshmaker/handlers/git/dockerfile_change.py
index e9e010f..bb0d667 100644
--- a/freshmaker/handlers/git/dockerfile_change.py
+++ b/freshmaker/handlers/git/dockerfile_change.py
@@ -38,7 +38,7 @@ class GitDockerfileChangeHandler(BaseHandler):
 
         log.info('Start to rebuild docker image %s.', event.container)
 
-        if not self.allow_build(event, 'image', event.container, event.branch):
+        if not self.allow_build('image', event.container, event.branch):
             log.info("Skip rebuild of %s:%s as it's not allowed by configured whitelist/blacklist",
                      event.container, event.branch)
             return []
diff --git a/freshmaker/handlers/git/module_metadata_change.py b/freshmaker/handlers/git/module_metadata_change.py
index 94967f0..8da9f9f 100644
--- a/freshmaker/handlers/git/module_metadata_change.py
+++ b/freshmaker/handlers/git/module_metadata_change.py
@@ -39,7 +39,7 @@ class GitModuleMetadataChangeHandler(BaseHandler):
     def handle(self, event):
         log.info("Triggering rebuild of module %s:%s, metadata updated (%s).",
                  event.module, event.branch, event.rev)
-        if not self.allow_build(event, 'module', event.module, event.branch):
+        if not self.allow_build('module', event.module, event.branch):
             log.info("Skip rebuild of %s:%s as it's not allowed by configured whitelist/blacklist",
                      event.module, event.branch)
             return []
diff --git a/freshmaker/handlers/git/rpm_spec_change.py b/freshmaker/handlers/git/rpm_spec_change.py
index 123074d..59f9a71 100644
--- a/freshmaker/handlers/git/rpm_spec_change.py
+++ b/freshmaker/handlers/git/rpm_spec_change.py
@@ -53,7 +53,7 @@ class GitRPMSpecChangeHandler(BaseHandler):
         for module in modules:
             name = module['variant_name']
             version = module['variant_version']
-            if not self.allow_build(event, 'module', name, version):
+            if not self.allow_build('module', name, version):
                 log.info("Skip rebuild of %s:%s as it's not allowed by configured whitelist/blacklist",
                          name, version)
                 continue
diff --git a/freshmaker/handlers/mbs/module_state_change.py b/freshmaker/handlers/mbs/module_state_change.py
index b333755..e37bd57 100644
--- a/freshmaker/handlers/mbs/module_state_change.py
+++ b/freshmaker/handlers/mbs/module_state_change.py
@@ -77,7 +77,7 @@ class MBSModuleStateChangeHandler(BaseHandler):
             for mod in modules:
                 name = mod['variant_name']
                 version = mod['variant_version']
-                if not self.allow_build(event, 'module', name, version):
+                if not self.allow_build('module', name, version):
                     log.info("Skip rebuild of %s:%s as it's not allowed by configured whitelist/blacklist",
                              name, version)
                     continue
diff --git a/tests/test_mbs_module_state_change_handler.py b/tests/test_mbs_module_state_change_handler.py
index 95a3a55..82cd04c 100644
--- a/tests/test_mbs_module_state_change_handler.py
+++ b/tests/test_mbs_module_state_change_handler.py
@@ -113,13 +113,11 @@ class MBSModuleStateChangeHandlerTest(helpers.FreshmakerTestCase):
     def test_module_is_not_allowed_in_whitelist(self, conf, utils, PDC):
         conf.handler_build_whitelist = {
             "MBSModuleStateChangeHandler": {
-                "MBSModuleStateChangeEvent": {
-                    "module": [
-                        {
-                            'name': 'base.*',
-                        },
-                    ],
-                },
+                "module": [
+                    {
+                        'name': 'base.*',
+                    },
+                ],
             },
         }
         conf.handler_build_blacklist = {}
@@ -150,13 +148,11 @@ class MBSModuleStateChangeHandlerTest(helpers.FreshmakerTestCase):
         conf.handler_build_whitelist = {}
         conf.handler_build_blacklist = {
             "MBSModuleStateChangeHandler": {
-                "MBSModuleStateChangeEvent": {
-                    "module": [
-                        {
-                            'name': 'test.*',
-                        },
-                    ],
-                },
+                "module": [
+                    {
+                        'name': 'test.*',
+                    },
+                ],
             },
         }
         msg = helpers.ModuleStateChangeMessage('testmodule', 'master', state='ready').produce()

