Skip to content

Commit

Permalink
Implement conditions on labels for downstream Koji builds
Browse files Browse the repository at this point in the history
Fixes #2186
Since the event triggering Koji builds is not a PR event (but
a PushPagureEvent), checking the conditions on labels on corresponding
PR from push is not covered by the generic check done when getting jobs matching
event and needs to be done additionally in a new checker.
  • Loading branch information
lbarcziova committed Feb 6, 2024
1 parent 1aabf24 commit 3c53979
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
16 changes: 16 additions & 0 deletions packit_service/worker/checker/distgit.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from ogr.abstract import AccessLevel
from packit.config.aliases import get_branches
from packit_service.constants import MSG_GET_IN_TOUCH, KojiAllowedAccountsAlias
from packit_service.utils import pr_labels_match_configuration
from packit_service.worker.checker.abstract import Checker, ActorChecker
from packit_service.worker.events import (
PushPagureEvent,
Expand All @@ -24,6 +25,21 @@
logger = logging.getLogger(__name__)


class LabelsOnDistgitPR(Checker, GetPagurePullRequestMixin):
def pre_check(self) -> bool:
if self.data.event_type not in (PushPagureEvent.__name__,) or not (
self.job_config.require.label.present
or self.job_config.require.label.absent
):
return True

return pr_labels_match_configuration(
self.pull_request,
self.job_config.require.label.present,
self.job_config.require.label.absent,
)


class PermissionOnDistgit(Checker, GetPagurePullRequestMixin):
def contains_specfile_change(self):
"""
Expand Down
7 changes: 6 additions & 1 deletion packit_service/worker/handlers/distgit.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
ValidInformationForPullFromUpstream,
HasIssueCommenterRetriggeringPermissions,
IsUpstreamTagMatchingConfig,
LabelsOnDistgitPR,
)
from packit_service.worker.events import (
PushPagureEvent,
Expand Down Expand Up @@ -678,7 +679,11 @@ def __init__(

@staticmethod
def get_checkers() -> Tuple[Type[Checker], ...]:
return (PermissionOnDistgit, HasIssueCommenterRetriggeringPermissions)
return (
LabelsOnDistgitPR,
PermissionOnDistgit,
HasIssueCommenterRetriggeringPermissions,
)

def _get_or_create_koji_group_model(self) -> KojiBuildGroupModel:
if self._koji_group_model_id is not None:
Expand Down
62 changes: 61 additions & 1 deletion tests/unit/test_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from flexmock import flexmock

from ogr import PagureService
from ogr.abstract import AccessLevel
from ogr.abstract import AccessLevel, PRStatus
from ogr.services.pagure import PagureProject
from packit.config import (
CommonPackageConfig,
Expand All @@ -18,6 +18,7 @@
)

from packit.config.commands import TestCommandConfig
from packit.config.requirements import RequirementsConfig, LabelRequirementsConfig
from packit_service.config import ServiceConfig
from packit_service.models import CoprBuildTargetModel
from packit_service.worker.checker.copr import (
Expand All @@ -27,6 +28,7 @@
from packit_service.worker.checker.distgit import (
IsUpstreamTagMatchingConfig,
PermissionOnDistgit,
LabelsOnDistgitPR,
)
from packit_service.worker.checker.koji import (
IsJobConfigTriggerMatching as IsJobConfigTriggerMatchingKoji,
Expand Down Expand Up @@ -992,3 +994,61 @@ def test_koji_check_allowed_accounts(
package_config, job_config, distgit_push_event.get_dict()
)
assert checker.check_allowed_accounts(allowed_pr_authors, account) == should_pass


@pytest.mark.parametrize(
"pr_labels,labels_present,labels_absent,should_pass",
(
([], [], [], True),
(["allowed-1"], [], ["skip-ci"], True),
(["allowed-1"], ["allowed-1"], ["skip-ci"], True),
(["allowed-1"], ["allowed-1"], ["skip-ci"], True),
(["allowed-1", "skip-ci"], ["allowed-1"], ["skip-ci"], False),
),
)
def test_labels_on_distgit_pr(
distgit_push_event,
pr_labels,
labels_present,
labels_absent,
should_pass,
):
jobs = [
JobConfig(
type=JobType.koji_build,
trigger=JobConfigTriggerType.commit,
packages={
"package": CommonPackageConfig(
dist_git_branches=["f36"],
require=RequirementsConfig(
LabelRequirementsConfig(
absent=labels_absent,
present=labels_present,
)
),
)
},
),
]

package_config = PackageConfig(
jobs=jobs,
packages={"package": CommonPackageConfig()},
)
job_config = jobs[0]

flexmock(PagureProject).should_receive("get_pr_list").and_return(
[
flexmock(
id=5,
head_commit="ad0c308af91da45cf40b253cd82f07f63ea9cbbf",
status=PRStatus.open,
labels=pr_labels,
)
]
)

checker = LabelsOnDistgitPR(
package_config, job_config, distgit_push_event.get_dict()
)
assert checker.pre_check() == should_pass

0 comments on commit 3c53979

Please sign in to comment.