示例#1
0
def get_config_for_handler_kls(
        handler_kls: Type[Handler], event: Event,
        package_config: PackageConfig) -> List[JobConfig]:
    """
    Get a list of JobConfigs relevant to event and the handler class.

    We need to find all job configurations that:
    - can be run by the given handler class AND
    - that matches the trigger of the event

    If there is no matching job-config found, we will pick the ones that are required.
    e.g.: For build handler, you can pick the test config since tests require the build.

    Examples of the matching can be found in the tests:
    ./tests/unit/test_jobs.py:test_get_config_for_handler_kls

    :param handler_kls: class that will use the JobConfig
    :param event: which we are reacting to
    :param package_config: we pick the JobConfig(s) from this package_config instance
    :return: list of JobConfigs relevant to the given handler and event
             preserving the order in the config
    """
    matching_jobs = []
    jobs_that_can_be_triggered = []

    for job in package_config.jobs:
        if (event.db_trigger and event.db_trigger.job_config_trigger_type
                == job.trigger) or is_trigger_matching_job_config(
                    trigger=event.trigger, job_config=job):
            jobs_that_can_be_triggered.append(job)

    matching_job_types = MAP_HANDLER_TO_JOB_TYPES[handler_kls]
    for job in jobs_that_can_be_triggered:
        if (
                # Check if the job matches any job supported by the handler.
                # The function `are_job_types_same` is used
                # because of the `build` x `copr_build` aliasing.
                any(
                    are_job_types_same(job.type, type)
                    for type in matching_job_types)
                and job not in matching_jobs):
            matching_jobs.append(job)

    if matching_jobs:
        return matching_jobs

    # The job was not configured but let's try required ones.
    # e.g. we can use `tests` configuration when running build
    for job in jobs_that_can_be_triggered:
        required_handlers = MAP_REQUIRED_JOB_TO_HANDLERS[job.type]
        if handler_kls in required_handlers:
            for trigger in handler_kls.triggers:
                if ((trigger == event.db_trigger.job_config_trigger_type)
                        or is_trigger_matching_job_config(trigger=trigger,
                                                          job_config=job)
                        and job not in matching_jobs):
                    matching_jobs.append(job)

    return matching_jobs
示例#2
0
def get_handlers_for_event(
    event: Event, package_config: PackageConfig
) -> Set[Type[JobHandler]]:
    """
    Get all handlers that we need to run for the given event.

    :param event: event which we are reacting to
    :param package_config: for checking configured jobs
    :return: set of handler instances
    """
    handlers: Set[Type[JobHandler]] = set()
    classes_for_trigger = MAP_EVENT_TRIGGER_TO_HANDLERS[event.trigger]

    for job in package_config.jobs:
        if is_trigger_matching_job_config(trigger=event.trigger, job_config=job):
            for pos_handler in classes_for_trigger:
                if job.type in MAP_HANDLER_TO_JOB_TYPES[pos_handler]:
                    handlers.add(pos_handler)
        required_handlers = MAP_REQUIRED_JOB_TO_HANDLERS[job.type]
        for pos_handler in required_handlers:
            for trigger in pos_handler.triggers:
                if trigger == event.trigger:
                    handlers.add(pos_handler)

    return handlers
    def job_tests(self) -> Optional[JobConfig]:
        """
        Check if there is JobConfig for tests defined
        :return: JobConfig or None
        """
        if not self.job_type_test:
            return None

        if not self._job_tests:
            for job in self.package_config.jobs:
                if are_job_types_same(
                        job.type,
                        self.job_type_test) and is_trigger_matching_job_config(
                            trigger=self.event.trigger, job_config=job):
                    self._job_tests = job
                    break
        return self._job_tests
示例#4
0
 def job_build(self) -> Optional[JobConfig]:
     """
     Check if there is JobConfig for builds defined
     :return: JobConfig or None
     """
     if not self.job_type_build:
         return None
     if not self._job_build:
         for job in [self.job_config] + self.package_config.jobs:
             if are_job_types_same(job.type, self.job_type_build) and (
                 (self.db_trigger and
                  self.db_trigger.job_config_trigger_type == job.trigger)
                     or is_trigger_matching_job_config(
                         trigger=self.metadata.trigger, job_config=job)):
                 self._job_build = job
                 break
     return self._job_build
示例#5
0
def get_config_for_handler_kls(
    handler_kls: Type[JobHandler], event: Event, package_config: PackageConfig
) -> Optional[JobConfig]:
    """
    Get a JobConfig relevant to event and the handler class.

    :param handler_kls: class that will use the JobConfig
    :param event: which we are reacting to
    :param package_config: we pick the JobConfig from this package_config instance
    :return: JobConfig
    """
    for job in package_config.jobs:
        if job.type in MAP_HANDLER_TO_JOB_TYPES[
            handler_kls
        ] and is_trigger_matching_job_config(trigger=event.trigger, job_config=job):
            return job

        required_handlers = MAP_REQUIRED_JOB_TO_HANDLERS[job.type]
        for pos_handler in required_handlers:
            for trigger in pos_handler.triggers:
                if trigger == event.trigger:
                    return job
    return None
示例#6
0
def get_handlers_for_event(
    event: Event, package_config: PackageConfig
) -> Set[Type[JobHandler]]:
    """
    Get all handlers that we need to run for the given event.

    We need to return all handler classes that:
    - can react to the given event AND
    - are configured in the package_config (either directly or as a required job)

    Examples of the matching can be found in the tests:
    ./tests/unit/test_jobs.py:test_get_handlers_for_event

    :param event: event which we are reacting to
    :param package_config: for checking configured jobs
    :return: set of handler instances that we need to run for given event and user configuration
    """
    handlers: Set[Type[JobHandler]] = set()
    classes_for_trigger = MAP_EVENT_TRIGGER_TO_HANDLERS[event.trigger]

    for job in package_config.jobs:
        if (
            event.db_trigger and event.db_trigger.job_config_trigger_type == job.trigger
        ) or is_trigger_matching_job_config(trigger=event.trigger, job_config=job):
            for pos_handler in classes_for_trigger:
                if job.type in MAP_HANDLER_TO_JOB_TYPES[pos_handler]:
                    handlers.add(pos_handler)

        # We need to return also handlers that are required for the configured jobs.
        # e.g. we need to run `build` when only `test` is configured
        required_handlers = MAP_REQUIRED_JOB_TO_HANDLERS[job.type]
        for pos_handler in required_handlers:
            for trigger in pos_handler.triggers:
                if trigger == event.trigger:
                    handlers.add(pos_handler)

    return handlers