Repository: packit/packit-service
Issue: Invalid config still not handled properly

Sentry: https://sentry.io/organizations/red-hat-0p/issues/1476350151/
PR example: https://github.com/OpenSCAP/openscap/pull/1447

Changed files:
packit_service/service/events.py, packit_service/config.py, packit_service/worker/testing_farm_handlers.py, packit_service/worker/github_handlers.py

diff --git a/packit_service/config.py b/packit_service/config.py
index 97001ee4..464d4ebd 100644
--- a/packit_service/config.py
+++ b/packit_service/config.py
@@ -24,10 +24,16 @@ import logging
 from pathlib import Path
 from typing import Set, Optional
 
+from ogr.abstract import GitProject
 from yaml import safe_load
 
-from packit.config import RunCommandType, Config
-from packit.exceptions import PackitException
+from packit.config import (
+    RunCommandType,
+    Config,
+    get_package_config_from_repo,
+    PackageConfig,
+)
+from packit.exceptions import PackitException, PackitConfigException
 from packit_service.constants import (
     SANDCASTLE_WORK_DIR,
     SANDCASTLE_PVC,
@@ -121,3 +127,49 @@ class ServiceConfig(Config):
 
             cls.service_config = ServiceConfig.get_from_dict(raw_dict=loaded_config)
         return cls.service_config
+
+
+class GithubPackageConfigGetter:
+    def get_package_config_from_repo(
+        self,
+        project: GitProject,
+        reference: str,
+        pr_id: int = None,
+        fail_when_missing: bool = True,
+    ):
+        """
+        Get the package config and catch the invalid config scenario and possibly no-config scenario
+        Static because of the easier mocking.
+        """
+        try:
+            package_config: PackageConfig = get_package_config_from_repo(
+                project, reference
+            )
+            if not package_config and fail_when_missing:
+                raise PackitConfigException(
+                    f"No config file found in {project.full_repo_name}"
+                )
+        except PackitConfigException as ex:
+            if pr_id:
+                project.pr_comment(
+                    pr_id, f"Failed to load packit config file:\\n```\\n{str(ex)}\\n```"
+                )
+            else:
+                # TODO: filter when https://github.com/packit-service/ogr/issues/308 fixed
+                issues = project.get_issue_list()
+                if "Invalid packit config" not in [x.title for x in issues]:
+                    # TODO: store in DB
+                    message = (
+                        f"Failed to load packit config file:\\n```\\n{str(ex)}\\n```\\n"
+                        "For more info, please check out the documentation: "
+                        "http://packit.dev/packit-as-a-service/ or contact us - "
+                        "[Packit team]"
+                        "(https://github.com/orgs/packit-service/teams/the-packit-team)"
+                    )
+
+                    i = project.create_issue(
+                        title="[packit] Invalid config", body=message
+                    )
+                    logger.debug(f"Created issue for invalid packit config: {i.url}")
+            raise ex
+        return package_config
diff --git a/packit_service/service/events.py b/packit_service/service/events.py
index b4bc9810..1ec4e44c 100644
--- a/packit_service/service/events.py
+++ b/packit_service/service/events.py
@@ -32,7 +32,7 @@ from typing import Optional, List, Union
 from ogr.abstract import GitProject
 from packit.config import JobTriggerType, get_package_config_from_repo, PackageConfig
 
-from packit_service.config import ServiceConfig
+from packit_service.config import ServiceConfig, GithubPackageConfigGetter
 from packit_service.worker.copr_db import CoprBuildDB
 
 logger = logging.getLogger(__name__)
@@ -158,7 +158,7 @@ class Event:
         return f"{self.__class__.__name__}({self.get_dict()})"
 
 
-class AbstractGithubEvent(Event):
+class AbstractGithubEvent(Event, GithubPackageConfigGetter):
     def __init__(self, trigger: JobTriggerType, project_url: str):
         super().__init__(trigger)
         self.project_url: str = project_url
@@ -177,8 +177,8 @@ class ReleaseEvent(AbstractGithubEvent):
         self.tag_name = tag_name
 
     def get_package_config(self) -> Optional[PackageConfig]:
-        package_config: PackageConfig = get_package_config_from_repo(
-            self.get_project(), self.tag_name
+        package_config: PackageConfig = self.get_package_config_from_repo(
+            project=self.get_project(), reference=self.tag_name, fail_when_missing=False
         )
         if not package_config:
             return None
@@ -215,8 +215,11 @@ class PullRequestEvent(AbstractGithubEvent):
         return result
 
     def get_package_config(self) -> Optional[PackageConfig]:
-        package_config: PackageConfig = get_package_config_from_repo(
-            self.get_project(), self.base_ref
+        package_config: PackageConfig = self.get_package_config_from_repo(
+            project=self.get_project(),
+            reference=self.base_ref,
+            pr_id=self.pr_id,
+            fail_when_missing=False,
         )
         if not package_config:
             return None
@@ -257,8 +260,11 @@ class PullRequestCommentEvent(AbstractGithubEvent):
     def get_package_config(self) -> Optional[PackageConfig]:
         if not self.base_ref:
             self.base_ref = self.get_project().get_pr_info(self.pr_id).source_branch
-        package_config: PackageConfig = get_package_config_from_repo(
-            self.get_project(), self.base_ref
+        package_config: PackageConfig = self.get_package_config_from_repo(
+            project=self.get_project(),
+            reference=self.base_ref,
+            pr_id=self.pr_id,
+            fail_when_missing=False,
         )
         if not package_config:
             return None
@@ -303,8 +309,8 @@ class IssueCommentEvent(AbstractGithubEvent):
 
         if releases:
             self.tag_name = releases[0].tag_name
-        package_config: PackageConfig = get_package_config_from_repo(
-            self.get_project(), self.tag_name
+        package_config: PackageConfig = self.get_package_config_from_repo(
+            project=self.get_project(), reference=self.tag_name, fail_when_missing=False
         )
         if not package_config:
             return None
@@ -417,8 +423,8 @@ class TestingFarmResultsEvent(AbstractGithubEvent):
         return result
 
     def get_package_config(self):
-        package_config: PackageConfig = get_package_config_from_repo(
-            self.get_project(), self.ref
+        package_config: PackageConfig = self.get_package_config_from_repo(
+            project=self.get_project(), reference=self.ref, fail_when_missing=False
         )
         if not package_config:
             return None
@@ -494,7 +500,9 @@ class CoprBuildEvent(AbstractGithubEvent):
         if not project:
             return None
 
-        package_config: PackageConfig = get_package_config_from_repo(project, self.ref)
+        package_config: PackageConfig = self.get_package_config_from_repo(
+            project=project, reference=self.ref, fail_when_missing=False
+        )
         if not package_config:
             return None
 
diff --git a/packit_service/worker/github_handlers.py b/packit_service/worker/github_handlers.py
index 035f76fd..d64540fd 100644
--- a/packit_service/worker/github_handlers.py
+++ b/packit_service/worker/github_handlers.py
@@ -33,13 +33,12 @@ from packit.config import (
     JobTriggerType,
     JobType,
     PackageConfig,
-    get_package_config_from_repo,
 )
 from packit.config.aliases import get_branches
-from packit.exceptions import PackitException, PackitConfigException
+from packit.exceptions import PackitException
 from packit.local_project import LocalProject
 
-from packit_service.config import ServiceConfig
+from packit_service.config import ServiceConfig, GithubPackageConfigGetter
 from packit_service.service.events import (
     PullRequestEvent,
     InstallationEvent,
@@ -71,48 +70,6 @@ from packit_service.worker.whitelist import Whitelist
 logger = logging.getLogger(__name__)
 
 
-class GithubPackageConfigGetter:
-    def get_package_config_from_repo(
-        self, project: GitProject, reference: str, pr_id: int = None
-    ):
-        """
-        Get the package config and catch the no-config scenario.
-        Static because of the easier mocking.
-        """
-        try:
-            package_config: PackageConfig = get_package_config_from_repo(
-                project, reference
-            )
-            if not package_config:
-                raise PackitConfigException(
-                    f"No config file found in {project.full_repo_name}"
-                )
-        except PackitConfigException as ex:
-            if pr_id:
-                project.pr_comment(
-                    pr_id, f"Failed to load packit config file:\\n```\\n{str(ex)}\\n```"
-                )
-            else:
-                # TODO: filter when https://github.com/packit-service/ogr/issues/308 fixed
-                issues = project.get_issue_list()
-                if "Invalid packit config" not in [x.title for x in issues]:
-                    # TODO: store in DB
-                    message = (
-                        f"Failed to load packit config file:\\n```\\n{str(ex)}\\n```\\n"
-                        "For more info, please check out the documentation: "
-                        "http://packit.dev/packit-as-a-service/ or contact us - "
-                        "[Packit team]"
-                        "(https://github.com/orgs/packit-service/teams/the-packit-team)"
-                    )
-
-                    i = project.create_issue(
-                        title="[packit] Invalid config", body=message
-                    )
-                    logger.debug(f"Created issue for invalid packit config: {i.url}")
-            raise ex
-        return package_config
-
-
 class AbstractGithubJobHandler(JobHandler, GithubPackageConfigGetter):
     pass
 
diff --git a/packit_service/worker/testing_farm_handlers.py b/packit_service/worker/testing_farm_handlers.py
index 4ae63ee6..a913514e 100644
--- a/packit_service/worker/testing_farm_handlers.py
+++ b/packit_service/worker/testing_farm_handlers.py
@@ -69,7 +69,11 @@ class TestingFarmResultsHandler(AbstractGithubJobHandler):
         self.package_config.upstream_project_url = test_results_event.project_url
 
     def get_package_config_from_repo(
-        self, project: GitProject, reference: str, pr_id: int = None
+        self,
+        project: GitProject,
+        reference: str,
+        pr_id: int = None,
+        fail_when_missing: bool = False,
     ):
         return get_package_config_from_repo(self.project, self.event.ref)