예제 #1
0
    def process_message(self,
                        event: dict,
                        topic: str = None,
                        source: str = None) -> Optional[dict]:
        """
        Entrypoint for message processing.

        :param event:  dict with webhook/fed-mes payload
        :param topic:  meant to be a topic provided by messaging subsystem (fedmsg, mqqt)
        :param source: source of message
        """

        if topic:
            # let's pre-filter messages: we don't need to get debug logs from processing
            # messages when we know beforehand that we are not interested in messages for such topic
            topics = [
                getattr(h, "topic", None)
                for h in MAP_HANDLER_TO_JOB_TYPES.keys()
            ]

            if topic not in topics:
                logger.debug(f"{topic} not in {topics}")
                return None

        event_object: Any
        if source == "centosmsg":
            event_object = CentosEventParser().parse_event(event)
        else:
            event_object = Parser.parse_event(event)

        if not (event_object and event_object.pre_check()):
            return None

        # CoprBuildEvent.get_project returns None when the build id is not known
        if not event_object.project:
            logger.warning("Cannot obtain project from this event! "
                           "Skipping private repository check!")
        elif event_object.project.is_private():
            logger.info("We do not interact with private repositories!")
            return None

        handler: Union[GithubAppInstallationHandler, TestingFarmResultsHandler,
                       CoprBuildStartHandler, CoprBuildEndHandler,
                       PagurePullRequestLabelHandler, ]
        processing_results = None

        # installation is handled differently b/c app is installed to GitHub account
        # not repository, so package config with jobs is missing
        if event_object.trigger == TheJobTriggerType.installation:
            GithubAppInstallationHandler.get_signature(event=event_object,
                                                       job=None).apply_async()
        # Label/Tag added event handler is run even when the job is not configured in package
        elif event_object.trigger == TheJobTriggerType.pr_label:
            PagurePullRequestLabelHandler.get_signature(
                event=event_object,
                job=None,
            ).apply_async()
        elif event_object.trigger in {
                TheJobTriggerType.issue_comment,
                TheJobTriggerType.pr_comment,
        } and (isinstance(
                event_object,
            (
                PullRequestCommentGithubEvent,
                PullRequestCommentPagureEvent,
                IssueCommentEvent,
                MergeRequestCommentGitlabEvent,
                IssueCommentGitlabEvent,
            ),
        )):
            processing_results = self.process_comment_jobs(event_object)
        else:
            # Processing the jobs from the config.
            processing_results = self.process_jobs(event_object)

        return processing_results or get_processing_results(event=event_object,
                                                            jobs=[])
예제 #2
0
    def process_message(
        self, event: dict, topic: str = None, source: str = None
    ) -> Optional[dict]:
        """
        Entrypoint for message processing.

        :param event:  dict with webhook/fed-mes payload
        :param topic:  meant to be a topic provided by messaging subsystem (fedmsg, mqqt)
        :param source: source of message
        """

        if topic:
            # let's pre-filter messages: we don't need to get debug logs from processing
            # messages when we know beforehand that we are not interested in messages for such topic
            topics = [
                getattr(h, "topic", None) for h in MAP_HANDLER_TO_JOB_TYPES.keys()
            ]

            if topic not in topics:
                logger.debug(f"{topic} not in {topics}")
                return None

        event_object: Any
        if source == "centosmsg":
            event_object = CentosEventParser().parse_event(event)
        else:
            event_object = Parser.parse_event(event)

        if not event_object or not event_object.pre_check():
            return None

        # CoprBuildEvent.get_project returns None when the build id is not known
        if not event_object.project:
            logger.warning(
                "Cannot obtain project from this event! "
                "Skipping private repository check!"
            )
        elif event_object.project.is_private():
            logger.info("We do not interact with private repositories!")
            return None

        handler: Union[
            GithubAppInstallationHandler,
            TestingFarmResultsHandler,
            CoprBuildStartHandler,
            CoprBuildEndHandler,
            PagurePullRequestLabelHandler,
        ]
        jobs_results: Dict[str, HandlerResults] = {}
        # installation is handled differently b/c app is installed to GitHub account
        # not repository, so package config with jobs is missing
        if event_object.trigger == TheJobTriggerType.installation:
            handler = GithubAppInstallationHandler(
                self.config, job_config=None, event=event_object
            )
            job_type = JobType.add_to_whitelist.value
            jobs_results[job_type] = handler.run_n_clean()
        # Label/Tag added event handler is run even when the job is not configured in package
        elif event_object.trigger == TheJobTriggerType.pr_label:
            handler = PagurePullRequestLabelHandler(
                self.config, job_config=None, event=event_object
            )
            job_type = JobType.create_bugzilla.value
            jobs_results[job_type] = handler.run_n_clean()
        elif event_object.trigger in {
            TheJobTriggerType.issue_comment,
            TheJobTriggerType.pr_comment,
        } and (
            isinstance(
                event_object,
                (
                    PullRequestCommentGithubEvent,
                    PullRequestCommentPagureEvent,
                    IssueCommentEvent,
                ),
            )
        ):
            jobs_results = self.process_comment_jobs(event_object)
        else:
            # Processing the jobs from the config.
            jobs_results = self.process_jobs(event_object)

        logger.debug("All jobs finished!")

        task_results = {"jobs": jobs_results, "event": event_object.get_dict()}

        for v in jobs_results.values():
            if not (v and v["success"]):
                logger.warning(task_results)
                logger.error(v["details"]["msg"])
        return task_results
예제 #3
0
    def process_message(self,
                        event: dict,
                        topic: str = None,
                        source: str = None) -> List[TaskResults]:
        """
        Entrypoint for message processing.

        :param event:  dict with webhook/fed-mes payload
        :param topic:  meant to be a topic provided by messaging subsystem (fedmsg, mqqt)
        :param source: source of message
        """

        if topic:
            # let's pre-filter messages: we don't need to get debug logs from processing
            # messages when we know beforehand that we are not interested in messages for such topic
            topics = [
                getattr(handler, "topic", None)
                for handler in JobHandler.get_all_subclasses()
            ]

            if topic not in topics:
                logger.debug(f"{topic} not in {topics}")
                return []

        event_object: Any
        if source == "centosmsg":
            event_object = CentosEventParser().parse_event(event)
        else:
            event_object = Parser.parse_event(event)

        if not (event_object and event_object.pre_check()):
            return []

        # CoprBuildEvent.get_project returns None when the build id is not known
        if not event_object.project:
            logger.warning("Cannot obtain project from this event! "
                           "Skipping private repository check!")
        elif event_object.project.is_private():
            service_with_namespace = (
                f"{event_object.project.service.hostname}/"
                f"{event_object.project.namespace}")
            if (service_with_namespace
                    not in self.service_config.enabled_private_namespaces):
                logger.info(
                    f"We do not interact with private repositories by default. "
                    f"Add `{service_with_namespace}` to the `enabled_private_namespaces` "
                    f"in the service configuration.")
                return []
            logger.debug(f"Working in `{service_with_namespace}` namespace "
                         f"which is private but enabled via configuration.")

        handler: Union[GithubAppInstallationHandler, TestingFarmResultsHandler,
                       CoprBuildStartHandler, CoprBuildEndHandler,
                       PagurePullRequestLabelHandler, ]
        processing_results = None

        # installation is handled differently b/c app is installed to GitHub account
        # not repository, so package config with jobs is missing
        if isinstance(event, InstallationEvent):
            GithubAppInstallationHandler.get_signature(event=event_object,
                                                       job=None).apply_async()
        # Label/Tag added event handler is run even when the job is not configured in package
        elif isinstance(event, PullRequestLabelPagureEvent):
            PagurePullRequestLabelHandler.get_signature(
                event=event_object,
                job=None,
            ).apply_async()
        else:
            # Processing the jobs from the config.
            processing_results = self.process_jobs(event_object)

        return processing_results