def __init__(
        self,
        config: ServiceConfig,
        package_config: PackageConfig,
        project: GitProject,
        event: Union[PullRequestEvent, PullRequestCommentEvent, CoprBuildEvent,
                     PushGitHubEvent, ReleaseEvent, ],
        job: Optional[JobConfig] = None,
    ):
        self.config: ServiceConfig = config
        self.package_config: PackageConfig = package_config
        self.project: GitProject = project
        self.event: Union[PullRequestEvent, PullRequestCommentEvent,
                          CoprBuildEvent, PushGitHubEvent,
                          ReleaseEvent, ] = event
        self.pr_id = (self.event.pr_id if isinstance(
            self.event, (PullRequestEvent, PullRequestCommentEvent)) else None)

        # lazy properties
        self._api = None
        self._local_project = None
        self._status_reporter = None
        self._test_check_names: Optional[List[str]] = None
        self._build_check_names: Optional[List[str]] = None

        # lazy properties, current job by default
        self._job_build = (job if job and are_job_types_same(
            job.type, self.job_type_build) else None)
        self._job_tests = (job if job and are_job_types_same(
            job.type, self.job_type_test) else None)
Exemple #2
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
Exemple #3
0
    def __init__(
        self,
        config: ServiceConfig,
        package_config: PackageConfig,
        project: GitProject,
        event: Union[
            PullRequestGithubEvent,
            PullRequestPagureEvent,
            PullRequestCommentGithubEvent,
            PullRequestCommentPagureEvent,
            CoprBuildEvent,
            PushGitHubEvent,
            ReleaseEvent,
        ],
        job: Optional[JobConfig] = None,
    ):
        self.config: ServiceConfig = config
        self.package_config: PackageConfig = package_config
        self.project: GitProject = project
        self.event: Union[
            PullRequestGithubEvent,
            PullRequestPagureEvent,
            PullRequestCommentGithubEvent,
            PullRequestCommentPagureEvent,
            CoprBuildEvent,
            PushGitHubEvent,
            ReleaseEvent,
        ] = event
        self.msg_retrigger: Optional[str] = ""

        # lazy properties
        self._api = None
        self._local_project = None
        self._status_reporter: Optional[StatusReporter] = None
        self._test_check_names: Optional[List[str]] = None
        self._build_check_names: Optional[List[str]] = None
        self._srpm_model: Optional[SRPMBuildModel] = None
        self._srpm_path: Optional[Path] = None

        # lazy properties, current job by default
        self._job_build = (
            job if job and are_job_types_same(job.type, self.job_type_build) else None
        )
        self._job_tests = (
            job if job and are_job_types_same(job.type, self.job_type_test) else None
        )
Exemple #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
             ):
                 self._job_build = job
                 break
     return self._job_build
    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
Exemple #6
0
    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.job_config] + self.package_config.jobs:
                if are_job_types_same(job.type, self.job_type_test) 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_tests = job
                    break
        return self._job_tests