Skip to content

Commit 3c53979

Browse files
committed
Implement conditions on labels for downstream Koji builds
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.
1 parent 1aabf24 commit 3c53979

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

packit_service/worker/checker/distgit.py

+16
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from ogr.abstract import AccessLevel
88
from packit.config.aliases import get_branches
99
from packit_service.constants import MSG_GET_IN_TOUCH, KojiAllowedAccountsAlias
10+
from packit_service.utils import pr_labels_match_configuration
1011
from packit_service.worker.checker.abstract import Checker, ActorChecker
1112
from packit_service.worker.events import (
1213
PushPagureEvent,
@@ -24,6 +25,21 @@
2425
logger = logging.getLogger(__name__)
2526

2627

28+
class LabelsOnDistgitPR(Checker, GetPagurePullRequestMixin):
29+
def pre_check(self) -> bool:
30+
if self.data.event_type not in (PushPagureEvent.__name__,) or not (
31+
self.job_config.require.label.present
32+
or self.job_config.require.label.absent
33+
):
34+
return True
35+
36+
return pr_labels_match_configuration(
37+
self.pull_request,
38+
self.job_config.require.label.present,
39+
self.job_config.require.label.absent,
40+
)
41+
42+
2743
class PermissionOnDistgit(Checker, GetPagurePullRequestMixin):
2844
def contains_specfile_change(self):
2945
"""

packit_service/worker/handlers/distgit.py

+6-1
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@
6060
ValidInformationForPullFromUpstream,
6161
HasIssueCommenterRetriggeringPermissions,
6262
IsUpstreamTagMatchingConfig,
63+
LabelsOnDistgitPR,
6364
)
6465
from packit_service.worker.events import (
6566
PushPagureEvent,
@@ -678,7 +679,11 @@ def __init__(
678679

679680
@staticmethod
680681
def get_checkers() -> Tuple[Type[Checker], ...]:
681-
return (PermissionOnDistgit, HasIssueCommenterRetriggeringPermissions)
682+
return (
683+
LabelsOnDistgitPR,
684+
PermissionOnDistgit,
685+
HasIssueCommenterRetriggeringPermissions,
686+
)
682687

683688
def _get_or_create_koji_group_model(self) -> KojiBuildGroupModel:
684689
if self._koji_group_model_id is not None:

tests/unit/test_checkers.py

+61-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
from flexmock import flexmock
77

88
from ogr import PagureService
9-
from ogr.abstract import AccessLevel
9+
from ogr.abstract import AccessLevel, PRStatus
1010
from ogr.services.pagure import PagureProject
1111
from packit.config import (
1212
CommonPackageConfig,
@@ -18,6 +18,7 @@
1818
)
1919

2020
from packit.config.commands import TestCommandConfig
21+
from packit.config.requirements import RequirementsConfig, LabelRequirementsConfig
2122
from packit_service.config import ServiceConfig
2223
from packit_service.models import CoprBuildTargetModel
2324
from packit_service.worker.checker.copr import (
@@ -27,6 +28,7 @@
2728
from packit_service.worker.checker.distgit import (
2829
IsUpstreamTagMatchingConfig,
2930
PermissionOnDistgit,
31+
LabelsOnDistgitPR,
3032
)
3133
from packit_service.worker.checker.koji import (
3234
IsJobConfigTriggerMatching as IsJobConfigTriggerMatchingKoji,
@@ -992,3 +994,61 @@ def test_koji_check_allowed_accounts(
992994
package_config, job_config, distgit_push_event.get_dict()
993995
)
994996
assert checker.check_allowed_accounts(allowed_pr_authors, account) == should_pass
997+
998+
999+
@pytest.mark.parametrize(
1000+
"pr_labels,labels_present,labels_absent,should_pass",
1001+
(
1002+
([], [], [], True),
1003+
(["allowed-1"], [], ["skip-ci"], True),
1004+
(["allowed-1"], ["allowed-1"], ["skip-ci"], True),
1005+
(["allowed-1"], ["allowed-1"], ["skip-ci"], True),
1006+
(["allowed-1", "skip-ci"], ["allowed-1"], ["skip-ci"], False),
1007+
),
1008+
)
1009+
def test_labels_on_distgit_pr(
1010+
distgit_push_event,
1011+
pr_labels,
1012+
labels_present,
1013+
labels_absent,
1014+
should_pass,
1015+
):
1016+
jobs = [
1017+
JobConfig(
1018+
type=JobType.koji_build,
1019+
trigger=JobConfigTriggerType.commit,
1020+
packages={
1021+
"package": CommonPackageConfig(
1022+
dist_git_branches=["f36"],
1023+
require=RequirementsConfig(
1024+
LabelRequirementsConfig(
1025+
absent=labels_absent,
1026+
present=labels_present,
1027+
)
1028+
),
1029+
)
1030+
},
1031+
),
1032+
]
1033+
1034+
package_config = PackageConfig(
1035+
jobs=jobs,
1036+
packages={"package": CommonPackageConfig()},
1037+
)
1038+
job_config = jobs[0]
1039+
1040+
flexmock(PagureProject).should_receive("get_pr_list").and_return(
1041+
[
1042+
flexmock(
1043+
id=5,
1044+
head_commit="ad0c308af91da45cf40b253cd82f07f63ea9cbbf",
1045+
status=PRStatus.open,
1046+
labels=pr_labels,
1047+
)
1048+
]
1049+
)
1050+
1051+
checker = LabelsOnDistgitPR(
1052+
package_config, job_config, distgit_push_event.get_dict()
1053+
)
1054+
assert checker.pre_check() == should_pass

0 commit comments

Comments
 (0)