Repository: packit-service/packit
Pull request #743 (2020-03-04 13:11:38)

introduce package config commenting config
related packit-service/packit-service#168


Patch:
diff --git a/packit/config/package_config.py b/packit/config/package_config.py
index 535d3f29..7b116e33 100644
--- a/packit/config/package_config.py
+++ b/packit/config/package_config.py
@@ -39,6 +39,20 @@ from packit.exceptions import PackitConfigException
 logger = logging.getLogger(__name__)
 
 
+class PullRequestNotificationsConfig:
+    """ Configuration of commenting on pull requests. """
+
+    def __init__(self, successful_build: bool = True):
+        self.successful_build = successful_build
+
+
+class NotificationsConfig:
+    """ Configuration of notifications. """
+
+    def __init__(self, pull_request: PullRequestNotificationsConfig):
+        self.pull_request = pull_request
+
+
 class PackageConfig:
     """
     Config class for upstream/downstream packages;
@@ -66,6 +80,7 @@ class PackageConfig:
         spec_source_id: str = "Source0",
         upstream_tag_template: str = "{version}",
         patch_generation_ignore_paths: List[str] = None,
+        notifications: Optional[NotificationsConfig] = None,
         **kwargs,
     ):
         self.config_file_path: Optional[str] = config_file_path
@@ -87,6 +102,9 @@ class PackageConfig:
         self.allowed_gpg_keys = allowed_gpg_keys
         self.create_pr: bool = create_pr
         self.spec_source_id: str = spec_source_id
+        self.notifications = notifications or NotificationsConfig(
+            pull_request=PullRequestNotificationsConfig()
+        )
 
         # command to generate a tarball from the upstream repo
         # uncommitted changes will not be present in the archive
diff --git a/packit/schema.py b/packit/schema.py
index efbaec14..5d5ce14e 100644
--- a/packit/schema.py
+++ b/packit/schema.py
@@ -1,12 +1,35 @@
+# MIT License
+#
+# Copyright (c) 2018-2019 Red Hat, Inc.
+
+# Permission is hereby granted, free of charge, to any person obtaining a copy
+# of this software and associated documentation files (the "Software"), to deal
+# in the Software without restriction, including without limitation the rights
+# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+# copies of the Software, and to permit persons to whom the Software is
+# furnished to do so, subject to the following conditions:
+#
+# The above copyright notice and this permission notice shall be included in all
+# copies or substantial portions of the Software.
+#
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+# SOFTWARE.
+
 import logging
 import typing
 
 from marshmallow import Schema, fields, post_load, pre_load, ValidationError
 from marshmallow_enum import EnumField
-
 from packit.actions import ActionName
 from packit.config import PackageConfig, Config, SyncFilesConfig
 from packit.config.job_config import JobType, JobTriggerType, JobConfig, default_jobs
+from packit.config.package_config import NotificationsConfig
+from packit.config.package_config import PullRequestNotificationsConfig
 from packit.sync import SyncFilesItem
 
 logger = logging.getLogger(__name__)
@@ -85,8 +108,6 @@ class NotProcessedField(fields.Field):
     """
     Field class to mark fields which wil not be processed, only generates warning.
     Can be passed additional message via additional_message parameter.
-
-    :param str additional_message: additional warning message to be displayed
     """
 
     def _serialize(self, value: typing.Any, attr: str, obj: typing.Any, **kwargs):
@@ -146,6 +167,26 @@ class JobConfigSchema(Schema):
         return JobConfig(**data)
 
 
+class PullRequestNotificationsSchema(Schema):
+    """ Configuration of commenting on pull requests. """
+
+    successful_build = fields.Bool(default=True)
+
+    @post_load
+    def make_instance(self, data, **kwargs):
+        return PullRequestNotificationsConfig(**data)
+
+
+class NotificationsSchema(Schema):
+    """ Configuration of notifications. """
+
+    pull_request = fields.Nested(PullRequestNotificationsSchema)
+
+    @post_load
+    def make_instance(self, data, **kwargs):
+        return NotificationsConfig(**data)
+
+
 class PackageConfigSchema(Schema):
     """
     Schema for processing PackageConfig config data.
@@ -172,6 +213,7 @@ class PackageConfigSchema(Schema):
     actions = ActionField(default={})
     create_pr = fields.Bool(default=True)
     patch_generation_ignore_paths = fields.List(fields.String())
+    notifications = fields.Nested(NotificationsSchema)
 
     # list of deprecated keys and their replacement (new,old)
     deprecated_keys = (("upstream_package_name", "upstream_project_name"),)


Test patch:
diff --git a/tests/unit/test_config.py b/tests/unit/test_config.py
index f227a768..498eb3a4 100644
--- a/tests/unit/test_config.py
+++ b/tests/unit/test_config.py
@@ -287,6 +287,27 @@ def test_package_config_not_equal(not_equal_package_config):
             },
             False,
         ),
+        (
+            {
+                "specfile_path": "fedora/package.spec",
+                "notifications": {"pull_request": {"successful_build": False}},
+            },
+            True,
+        ),
+        (
+            {
+                "specfile_path": "fedora/package.spec",
+                "notifications": {"pull_request": {"successful_build": "nie"}},
+            },
+            False,
+        ),
+        (
+            {
+                "specfile_path": "fedora/package.spec",
+                "notifications": {"pull_request": False},
+            },
+            False,
+        ),
     ],
 )
 def test_package_config_validate(raw, is_valid):
@@ -722,3 +743,8 @@ def test_get_all_files_to_sync(package_config, all_synced_files):
 def test_get_local_specfile_path():
     assert get_local_specfile_path([UP_OSBUILD]) == "osbuild.spec"
     assert not get_local_specfile_path([SYNC_FILES])
+
+
+def test_notifications_section():
+    pc = PackageConfig.get_from_dict({"specfile_path": "package.spec"})
+    assert pc.notifications.pull_request.successful_build