Skip to content

Commit

Permalink
Check the labels also when processing copr build end (#2344)
Browse files Browse the repository at this point in the history
Check the labels also when processing copr build end

When scheduling follow-ups testing jobs, this needs to be additionally checked here as well. Followup of #2333

RELEASE NOTES BEGIN

RELEASE NOTES END

Reviewed-by: Matej Focko
  • Loading branch information
softwarefactory-project-zuul[bot] authored Feb 12, 2024
2 parents 38110e1 + c3c4c33 commit 2d6a08f
Show file tree
Hide file tree
Showing 3 changed files with 300 additions and 1 deletion.
15 changes: 15 additions & 0 deletions packit_service/worker/handlers/copr.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
dump_job_config,
dump_package_config,
elapsed_seconds,
pr_labels_match_configuration,
)
from packit_service.worker.checker.abstract import Checker
from packit_service.worker.checker.copr import (
Expand Down Expand Up @@ -415,6 +416,20 @@ def handle_testing_farm(self):
if (
not job_config.skip_build
and not job_config.manual_trigger
# we need to check the labels here
# the same way as when scheduling jobs for event
and (
job_config.trigger != JobConfigTriggerType.pull_request
or not (
job_config.require.label.present
or job_config.require.label.absent
)
or pr_labels_match_configuration(
pull_request=self.copr_build_helper.pull_request_object,
configured_labels_absent=job_config.require.label.absent,
configured_labels_present=job_config.require.label.present,
)
)
and self.copr_event.chroot
in self.copr_build_helper.build_targets_for_test_job(job_config)
):
Expand Down
9 changes: 8 additions & 1 deletion packit_service/worker/helpers/job_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from typing import Optional, Union, Dict, Callable

from lazy_object_proxy import Proxy
from ogr.abstract import GitProject
from ogr.abstract import GitProject, PullRequest
from ogr.exceptions import GitlabAPIException
from ogr.services.gitlab import GitlabProject
from packit.api import PackitAPI
Expand Down Expand Up @@ -58,6 +58,7 @@ def __init__(
self._pr_id: Optional[int] = None
self._is_reporting_allowed: Optional[bool] = None
self._is_gitlab_instance: Optional[bool] = None
self._pull_request_object: Optional[PullRequest] = None

def get_package_name(self) -> Optional[str]:
"""If the package_config is just for one package,
Expand Down Expand Up @@ -105,6 +106,12 @@ def pr_id(self) -> Optional[int]:
self._pr_id = self.metadata.pr_id
return self._pr_id

@property
def pull_request_object(self) -> Optional[PullRequest]:
if not self._pull_request_object and self.pr_id:
self._pull_request_object = self.project.get_pr(self.pr_id)
return self._pull_request_object

@property
def is_reporting_allowed(self) -> bool:
username = self.project.service.user.get_username()
Expand Down
277 changes: 277 additions & 0 deletions tests/integration/test_listen_to_fedmsg.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
JobType,
PackageConfig,
)
from packit.config.requirements import RequirementsConfig, LabelRequirementsConfig
from packit.copr_helper import CoprHelper
from packit.local_project import LocalProject
from packit_service.config import PackageConfigGetter, ServiceConfig
Expand Down Expand Up @@ -1151,6 +1152,282 @@ def test_copr_build_end_testing_farm_manual_trigger(copr_build_end, copr_build_p
)


def test_copr_build_end_testing_farm_labels_matching(copr_build_end, copr_build_pr):
ServiceConfig.get_service_config().testing_farm_api_url = (
"https://api.dev.testing-farm.io/v0.1/"
)
ServiceConfig.get_service_config().testing_farm_secret = "secret token"

flexmock(GithubProject).should_receive("is_private").and_return(False)
flexmock(GithubProject).should_receive("get_pr").and_return(
flexmock(
source_project=flexmock(
get_web_url=lambda: "https://github.com/source/bar"
),
target_project=flexmock(
get_web_url=lambda: "https://github.com/target/bar"
),
head_commit="0011223344",
target_branch_head_commit="deadbeef",
source_branch="the-source-branch",
target_branch="the-target-branch",
id=123,
labels=[flexmock(name="a-label")],
)
.should_receive("comment")
.mock()
)
urls.DASHBOARD_URL = "https://dashboard.localhost"

config = PackageConfig(
packages={
"package": CommonPackageConfig(
specfile_path="test.spec",
)
},
jobs=[
JobConfig(
type=JobType.copr_build,
trigger=JobConfigTriggerType.pull_request,
packages={
"package": CommonPackageConfig(
_targets=["fedora-rawhide"],
specfile_path="test.spec",
)
},
),
JobConfig(
type=JobType.tests,
trigger=JobConfigTriggerType.pull_request,
packages={
"package": CommonPackageConfig(
_targets=["fedora-rawhide"],
specfile_path="test.spec",
require=RequirementsConfig(
label=LabelRequirementsConfig(present=["a-label"])
),
)
},
),
],
)

flexmock(AbstractCoprBuildEvent).should_receive("get_packages_config").and_return(
config
)
flexmock(CoprHelper).should_receive("get_copr_client").and_return(
Client(config={"username": "packit", "copr_url": "https://dummy.url"})
)
flexmock(PackageConfigGetter).should_receive(
"get_package_config_from_repo"
).and_return(config)

flexmock(LocalProject).should_receive("refresh_the_arguments").and_return(None)

flexmock(CoprBuildTargetModel).should_receive("get_by_build_id").and_return(
copr_build_pr
)
flexmock(CoprBuildTargetModel).should_receive("get_by_id").and_return(copr_build_pr)
copr_build_pr.should_call("set_status").with_args(BuildStatus.success).once()
copr_build_pr.should_receive("set_end_time").once()
copr_build_pr.should_receive("get_package_name").and_return(None)
flexmock(requests).should_receive("get").and_return(requests.Response())
flexmock(requests.Response).should_receive("raise_for_status").and_return(None)
# check if packit-service set correct PR status
url = get_copr_build_info_url(1)
flexmock(StatusReporter).should_receive("report").with_args(
state=BaseCommitStatus.success,
description="RPMs were built successfully.",
url=url,
check_names=EXPECTED_BUILD_CHECK_NAME,
markdown_content=None,
links_to_external_services=None,
update_feedback_time=object,
).once()

flexmock(StatusReporter).should_receive("report").with_args(
state=BaseCommitStatus.pending,
description="RPMs were built successfully.",
url=url,
check_names=EXPECTED_TESTING_FARM_CHECK_NAME,
markdown_content=None,
links_to_external_services=None,
update_feedback_time=object,
).once()

flexmock(GithubProject).should_receive("get_web_url").and_return(
"https://github.com/foo/bar"
)

flexmock(Signature).should_receive("apply_async").twice()

# skip SRPM url since it touches multiple classes
flexmock(CoprBuildEndHandler).should_receive("set_srpm_url").and_return(None)

(
flexmock(CoprBuildJobHelper)
.should_receive("get_build_chroot")
.with_args(1, "some-target")
.and_return(flexmock(ended_on=1666889710))
.at_least()
.once()
)

flexmock(Pushgateway).should_receive("push").times(2).and_return()

processing_results = SteveJobs().process_message(copr_build_end)
event_dict, job, job_config, package_config = get_parameters_from_results(
processing_results
)
assert json.dumps(event_dict)

flexmock(CoprBuildJobHelper).should_receive("get_built_packages").and_return([])

run_copr_build_end_handler(
package_config=package_config,
event=event_dict,
job_config=job_config,
)


def test_copr_build_end_testing_farm_labels_not_matching(copr_build_end, copr_build_pr):
ServiceConfig.get_service_config().testing_farm_api_url = (
"https://api.dev.testing-farm.io/v0.1/"
)
ServiceConfig.get_service_config().testing_farm_secret = "secret token"

flexmock(GithubProject).should_receive("is_private").and_return(False)
flexmock(GithubProject).should_receive("get_pr").and_return(
flexmock(
source_project=flexmock(
get_web_url=lambda: "https://github.com/source/bar"
),
target_project=flexmock(
get_web_url=lambda: "https://github.com/target/bar"
),
head_commit="0011223344",
target_branch_head_commit="deadbeef",
source_branch="the-source-branch",
target_branch="the-target-branch",
id=123,
labels=[flexmock(name="another-label")],
)
.should_receive("comment")
.mock()
)
urls.DASHBOARD_URL = "https://dashboard.localhost"

config = PackageConfig(
packages={
"package": CommonPackageConfig(
specfile_path="test.spec",
)
},
jobs=[
JobConfig(
type=JobType.copr_build,
trigger=JobConfigTriggerType.pull_request,
packages={
"package": CommonPackageConfig(
_targets=["fedora-rawhide"],
specfile_path="test.spec",
)
},
),
JobConfig(
type=JobType.tests,
trigger=JobConfigTriggerType.pull_request,
packages={
"package": CommonPackageConfig(
_targets=["fedora-rawhide"],
specfile_path="test.spec",
require=RequirementsConfig(
label=LabelRequirementsConfig(present=["a-label"])
),
)
},
),
],
)

flexmock(AbstractCoprBuildEvent).should_receive("get_packages_config").and_return(
config
)
flexmock(CoprHelper).should_receive("get_copr_client").and_return(
Client(config={"username": "packit", "copr_url": "https://dummy.url"})
)
flexmock(PackageConfigGetter).should_receive(
"get_package_config_from_repo"
).and_return(config)

flexmock(LocalProject).should_receive("refresh_the_arguments").and_return(None)

flexmock(CoprBuildTargetModel).should_receive("get_by_build_id").and_return(
copr_build_pr
)
flexmock(CoprBuildTargetModel).should_receive("get_by_id").and_return(copr_build_pr)
copr_build_pr.should_call("set_status").with_args(BuildStatus.success).once()
copr_build_pr.should_receive("set_end_time").once()
copr_build_pr.should_receive("get_package_name").and_return(None)
flexmock(requests).should_receive("get").and_return(requests.Response())
flexmock(requests.Response).should_receive("raise_for_status").and_return(None)
# check if packit-service set correct PR status
url = get_copr_build_info_url(1)
flexmock(StatusReporter).should_receive("report").with_args(
state=BaseCommitStatus.success,
description="RPMs were built successfully.",
url=url,
check_names=EXPECTED_BUILD_CHECK_NAME,
markdown_content=None,
links_to_external_services=None,
update_feedback_time=object,
).once()

flexmock(StatusReporter).should_receive("report").with_args(
state=BaseCommitStatus.pending,
description="RPMs were built successfully.",
url=url,
check_names=EXPECTED_TESTING_FARM_CHECK_NAME,
markdown_content=None,
links_to_external_services=None,
update_feedback_time=object,
).once()

flexmock(GithubProject).should_receive("get_web_url").and_return(
"https://github.com/foo/bar"
)

flexmock(Signature).should_receive("apply_async").once()

# skip SRPM url since it touches multiple classes
flexmock(CoprBuildEndHandler).should_receive("set_srpm_url").and_return(None)

(
flexmock(CoprBuildJobHelper)
.should_receive("get_build_chroot")
.with_args(1, "some-target")
.and_return(flexmock(ended_on=1666889710))
.at_least()
.once()
)

flexmock(Pushgateway).should_receive("push").times(2).and_return()

processing_results = SteveJobs().process_message(copr_build_end)
event_dict, job, job_config, package_config = get_parameters_from_results(
processing_results
)
assert json.dumps(event_dict)

flexmock(CoprBuildJobHelper).should_receive("get_built_packages").and_return([])

run_copr_build_end_handler(
package_config=package_config,
event=event_dict,
job_config=job_config,
)


def test_copr_build_end_push_testing_farm(copr_build_end_push, copr_build_branch_push):
config = PackageConfig(
packages={
Expand Down

0 comments on commit 2d6a08f

Please sign in to comment.