Пример #1
0
def test_copr_and_koji_build_for_one_trigger(clean_before_and_after):
    pr1 = PullRequestModel.get_or_create(
        pr_id=1, namespace="the-namespace", repo_name="the-repo-name"
    )
    pr1_trigger = JobTriggerModel.get_or_create(
        type=JobTriggerModelType.pull_request, trigger_id=pr1.id
    )
    srpm_build = SRPMBuildModel.create("asd\nqwe\n")
    copr_build = CoprBuildModel.get_or_create(
        build_id="123456",
        commit_sha="687abc76d67d",
        project_name="SomeUser-hello-world-9",
        owner="packit",
        web_url="https://copr.something.somewhere/123456",
        target=TARGET,
        status="pending",
        srpm_build=srpm_build,
        trigger_model=pr1,
    )
    koji_build = KojiBuildModel.get_or_create(
        build_id="987654",
        commit_sha="687abc76d67d",
        web_url="https://copr.something.somewhere/123456",
        target=TARGET,
        status="pending",
        srpm_build=srpm_build,
        trigger_model=pr1,
    )

    assert copr_build in pr1_trigger.copr_builds
    assert koji_build in pr1_trigger.koji_builds

    assert copr_build.job_trigger.get_trigger_object() == pr1
    assert koji_build.job_trigger.get_trigger_object() == pr1
Пример #2
0
    def run(self) -> TaskResults:
        logger.debug(f"Testing farm {self.pipeline_id} result:\n{self.result}")

        test_run_model = TFTTestRunModel.get_by_pipeline_id(
            pipeline_id=self.pipeline_id)
        if not test_run_model:
            logger.warning(
                f"Unknown pipeline_id received from the testing-farm: "
                f"{self.pipeline_id}")

        if test_run_model:
            test_run_model.set_status(self.result, created=self.created)

        if self.result == TestingFarmResult.running:
            status = BaseCommitStatus.running
            summary = self.summary or "Tests are running ..."
        elif self.result == TestingFarmResult.passed:
            status = BaseCommitStatus.success
            summary = self.summary or "Tests passed ..."
        elif self.result == TestingFarmResult.error:
            status = BaseCommitStatus.error
            summary = self.summary or "Error ..."
        else:
            status = BaseCommitStatus.failure
            summary = self.summary or "Tests failed ..."

        if self.result == TestingFarmResult.running:
            self.pushgateway.test_runs_started.inc()
        else:
            self.pushgateway.test_runs_finished.inc()
            test_run_time = measure_time(end=datetime.now(timezone.utc),
                                         begin=test_run_model.submitted_time)
            self.pushgateway.test_run_finished_time.observe(test_run_time)

        if test_run_model:
            test_run_model.set_web_url(self.log_url)

        trigger = JobTriggerModel.get_or_create(
            type=self.db_trigger.job_trigger_model_type,
            trigger_id=self.db_trigger.id,
        )
        status_reporter = StatusReporter.get_instance(
            project=self.project,
            commit_sha=self.data.commit_sha,
            trigger_id=trigger.id if trigger else None,
            pr_id=self.data.pr_id,
        )
        status_reporter.report(
            state=status,
            description=summary,
            url=get_testing_farm_info_url(test_run_model.id)
            if test_run_model else self.log_url,
            links_to_external_services={"Testing Farm": self.log_url},
            check_names=TestingFarmJobHelper.get_test_check(self.copr_chroot),
        )

        return TaskResults(success=True, details={})
Пример #3
0
 def status_reporter(self) -> StatusReporter:
     if not self._status_reporter:
         trigger = JobTriggerModel.get_or_create(
             type=self.db_trigger.job_trigger_model_type,
             trigger_id=self.db_trigger.id,
         )
         self._status_reporter = StatusReporter.get_instance(
             project=self.project,
             commit_sha=self.metadata.commit_sha,
             trigger_id=trigger.id if trigger else None,
             pr_id=self.metadata.pr_id,
         )
     return self._status_reporter
Пример #4
0
def mock_pr_functionality(request):
    packit_yaml = (
        "{'specfile_path': 'the-specfile.spec', 'synced_files': [], 'jobs':"
        + str(request.param)
        + "}"
    )
    flexmock(
        GithubProject,
        full_repo_name="packit/hello-world",
        get_file_content=lambda path, ref: packit_yaml,
        get_files=lambda ref, filter_regex: ["the-specfile.spec"],
        get_web_url=lambda: "https://github.com/the-namespace/the-repo",
        get_pr=lambda pr_id: flexmock(head_commit="12345"),
    )
    flexmock(Github, get_repo=lambda full_name_or_id: None)

    config = ServiceConfig()
    config.command_handler_work_dir = SANDCASTLE_WORK_DIR
    flexmock(ServiceConfig).should_receive("get_service_config").and_return(config)
    trigger = JobTriggerModel(type=JobConfigTriggerType.pull_request, id=123)
    flexmock(AddPullRequestDbTrigger).should_receive("db_trigger").and_return(trigger)
    flexmock(PullRequestModel).should_receive("get_by_id").with_args(123).and_return(
        trigger
    )
    flexmock(LocalProject, refresh_the_arguments=lambda: None)
    flexmock(JobTriggerModel).should_receive("get_by_id").with_args(123456).and_return(
        trigger
    )
    flexmock(trigger).should_receive("get_trigger_object").and_return(
        PullRequestModel(pr_id=123)
    )
    flexmock(PullRequestModel).should_receive("get_or_create").with_args(
        pr_id=123,
        namespace="packit",
        repo_name="hello-world",
        project_url="https://github.com/packit/hello-world",
    ).and_return(
        flexmock(id=12, job_config_trigger_type=JobConfigTriggerType.pull_request)
    )
    flexmock(JobTriggerModel).should_receive("get_or_create").and_return(
        flexmock(id=123456)
    )
Пример #5
0
    def parse_check_rerun_event(
        event,
    ) -> Optional[Union[CheckRerunCommitEvent, CheckRerunPullRequestEvent,
                        CheckRerunReleaseEvent]]:
        """Look into the provided event and see if it is Github check rerun event."""
        if not (nested_get(event, "check_run")
                and nested_get(event, "action") == "rerequested"):
            return None

        check_name = nested_get(event, "check_run", "name")
        logger.info(f"Github check run {check_name} rerun event.")

        deployment = ServiceConfig.get_service_config().deployment
        app = nested_get(event, "check_run", "app", "slug")
        if (deployment == Deployment.prod and app != "packit-as-a-service"
            ) or (deployment == Deployment.stg
                  and app != "packit-as-a-service-stg"):
            logger.warning(f"Check run created by {app} and not us.")
            return None

        check_name_job, check_name_target = None, None
        if ":" in check_name:
            # e.g. "rpm-build:fedora-34-x86_64"
            check_name_job, _, check_name_target = check_name.partition(":")
            if check_name_job not in MAP_CHECK_PREFIX_TO_HANDLER:
                logger.warning(
                    f"{check_name_job} not in {list(MAP_CHECK_PREFIX_TO_HANDLER.keys())}"
                )
                check_name_job = None
        elif "/" in check_name:
            # for backward compatibility, e.g. packit/rpm-build-fedora-34-x86_64
            # TODO: remove this (whole elif) after some time
            _, _, check_name_job_info = check_name.partition("/")
            for job_name in MAP_CHECK_PREFIX_TO_HANDLER.keys():
                if check_name_job_info.startswith(job_name):
                    check_name_job = job_name
                    # e.g. [rpm-build-]fedora-34-x86_64
                    check_name_target = check_name_job_info[(len(job_name) +
                                                             1):  # noqa
                                                            ]
                    break

        if not (check_name_job and check_name_target):
            logger.warning(f"We were not able to parse the job and target "
                           f"from the check run name {check_name}.")
            return None

        repo_namespace = nested_get(event, "repository", "owner", "login")
        repo_name = nested_get(event, "repository", "name")
        actor = nested_get(event, "sender", "login")

        if not (repo_namespace and repo_name):
            logger.warning("No full name of the repository.")
            return None

        https_url = event["repository"]["html_url"]

        commit_sha = nested_get(event, "check_run", "head_sha")
        external_id = nested_get(event, "check_run", "external_id")

        if not external_id:
            logger.warning(
                "No external_id to identify the original trigger provided.")
            return None

        job_trigger = JobTriggerModel.get_by_id(int(external_id))
        if not job_trigger:
            logger.warning(f"Job trigger with ID {external_id} not found.")
            return None

        db_trigger = job_trigger.get_trigger_object()
        logger.info(f"Original trigger: {db_trigger}")

        event = None
        if isinstance(db_trigger, PullRequestModel):
            event = CheckRerunPullRequestEvent(
                repo_namespace=repo_namespace,
                repo_name=repo_name,
                project_url=https_url,
                commit_sha=commit_sha,
                pr_id=db_trigger.pr_id,
                check_name_job=check_name_job,
                check_name_target=check_name_target,
                db_trigger=db_trigger,
                actor=actor,
            )

        elif isinstance(db_trigger, ProjectReleaseModel):
            event = CheckRerunReleaseEvent(
                repo_namespace=repo_namespace,
                repo_name=repo_name,
                project_url=https_url,
                commit_sha=commit_sha,
                tag_name=db_trigger.tag_name,
                check_name_job=check_name_job,
                check_name_target=check_name_target,
                db_trigger=db_trigger,
                actor=actor,
            )

        elif isinstance(db_trigger, GitBranchModel):
            event = CheckRerunCommitEvent(
                repo_namespace=repo_namespace,
                repo_name=repo_name,
                project_url=https_url,
                commit_sha=commit_sha,
                git_ref=db_trigger.name,
                check_name_job=check_name_job,
                check_name_target=check_name_target,
                db_trigger=db_trigger,
                actor=actor,
            )

        return event
Пример #6
0
def branch_trigger_model(branch_model):
    yield JobTriggerModel.get_or_create(
        type=JobTriggerModelType.branch_push, trigger_id=branch_model.id
    )
Пример #7
0
def release_trigger_model(release_model):
    yield JobTriggerModel.get_or_create(
        type=JobTriggerModelType.release, trigger_id=release_model.id
    )
Пример #8
0
def different_pr_trigger_model(different_pr_model):
    yield JobTriggerModel.get_or_create(
        type=JobTriggerModelType.pull_request, trigger_id=different_pr_model.id
    )
Пример #9
0
def pr_trigger_model(pr_model):
    yield JobTriggerModel.get_or_create(
        type=JobTriggerModelType.pull_request, trigger_id=pr_model.id
    )