Skip to content
20 changes: 18 additions & 2 deletions packit_service/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,28 @@ def get_comment_parser_fedora_ci(
)
test_parser = subparsers.add_parser("test", help="Run tests in Testing Farm")
test_parser.add_argument(
"target",
"test_identifier",
nargs="?",
choices=["installability", "rpmlint", "rpminspect", "custom"],
help="Specific type of tests to run",
)
subparsers.add_parser("scratch-build", help="Build package in Koji")

test_parser.add_argument(
"--target",
dest="check_target",
nargs="?",
choices=["eln", "rawhide"],
help="Target for which to trigger tests",
)

scratch_build_parser = subparsers.add_parser("scratch-build", help="Build package in Koji")
scratch_build_parser.add_argument(
"--target",
dest="check_target",
nargs="?",
choices=["eln", "rawhide"],
help="Target for which to trigger a scratch build in Koji",
)

return parser

Expand Down
31 changes: 31 additions & 0 deletions packit_service/worker/handlers/abstract.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,37 @@ class RateLimitRequeueException(Exception):
MAP_COMMENT_TO_HANDLER_FEDORA_CI: dict[str, set[type["FedoraCIJobHandler"]]] = defaultdict(set)
MAP_CHECK_PREFIX_TO_HANDLER: dict[str, set[type["JobHandler"]]] = defaultdict(set)

MAP_TARGET_TO_HANDLER: dict[type["FedoraCIJobHandler"], str] = defaultdict(str)


def corresponds_to_check_target(check_target: str):
"""
[class decorator]
Specify which target the handler corresponds to.

Normally, when retriggering jobs on ELN rawhide PRs with no
existing eln branch, jobs would be run for both the rawhide
and eln targets. When the check target is specified like this,

/packit-ci test installability rawhide

then only the handler corresponding to the specified target
(rawhide in this example) will be run, resulting in jobs being run
only for the desired target.

Example:
```
@corresponds_to_check_target(check_target="rawhide")
class DownstreamKojiScratchBuildHandler(
```
"""

def _add_to_mapping(kls: type["FedoraCIJobHandler"]):
MAP_TARGET_TO_HANDLER[kls] = check_target
return kls

return _add_to_mapping


def configured_as(job_type: JobType):
"""
Expand Down
3 changes: 3 additions & 0 deletions packit_service/worker/handlers/distgit.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@
RetriableJobHandler,
TaskName,
configured_as,
corresponds_to_check_target,
reacts_to,
reacts_to_as_fedora_ci,
run_for_check_rerun,
Expand Down Expand Up @@ -776,6 +777,7 @@ def _run(self) -> TaskResults:


@run_for_comment_as_fedora_ci(command="scratch-build")
@corresponds_to_check_target(check_target="rawhide")
@reacts_to_as_fedora_ci(event=pagure.pr.Action)
@reacts_to_as_fedora_ci(event=pagure.pr.Comment)
class DownstreamKojiScratchBuildHandler(
Expand Down Expand Up @@ -1010,6 +1012,7 @@ def run_koji_build(


@run_for_comment_as_fedora_ci(command="scratch-build")
@corresponds_to_check_target(check_target="eln")
@reacts_to_as_fedora_ci(event=pagure.pr.Action)
@reacts_to_as_fedora_ci(event=pagure.pr.Comment)
class DownstreamKojiELNScratchBuildHandler(DownstreamKojiScratchBuildHandler):
Expand Down
3 changes: 3 additions & 0 deletions packit_service/worker/handlers/testing_farm.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
RetriableJobHandler,
TaskName,
configured_as,
corresponds_to_check_target,
reacts_to,
reacts_to_as_fedora_ci,
run_for_check_rerun,
Expand Down Expand Up @@ -358,6 +359,7 @@ def _run(self) -> TaskResults:


@run_for_comment_as_fedora_ci(command="test")
@corresponds_to_check_target(check_target="rawhide")
@reacts_to_as_fedora_ci(event=koji.result.Task)
@reacts_to_as_fedora_ci(event=pagure.pr.Comment)
class DownstreamTestingFarmHandler(
Expand Down Expand Up @@ -518,6 +520,7 @@ def _run(self) -> TaskResults:


@run_for_comment_as_fedora_ci(command="test")
@corresponds_to_check_target(check_target="eln")
@reacts_to_as_fedora_ci(event=pagure.pr.Comment)
class DownstreamTestingFarmELNHandler(DownstreamTestingFarmHandler):
"""
Expand Down
35 changes: 28 additions & 7 deletions packit_service/worker/jobs.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@
MAP_COMMENT_TO_HANDLER_FEDORA_CI,
MAP_JOB_TYPE_TO_HANDLER,
MAP_REQUIRED_JOB_TYPE_TO_HANDLER,
MAP_TARGET_TO_HANDLER,
SUPPORTED_EVENTS_FOR_HANDLER,
SUPPORTED_EVENTS_FOR_HANDLER_FEDORA_CI,
FedoraCIJobHandler,
Expand Down Expand Up @@ -112,6 +113,7 @@
class ParsedComment:
command: Optional[str] = None
package: Optional[str] = None
check_target: Optional[str] = None


def parse_comment(
Expand Down Expand Up @@ -143,7 +145,8 @@ def parse_comment(

try:
args = parser.parse_args(commands)
return ParsedComment(command=args.command, package=args.package)
check_target = getattr(args, "check_target", None)
return ParsedComment(command=args.command, package=args.package, check_target=check_target)
except SystemExit:
# tests expect invalid syntax comments be ignored
logger.debug(
Expand Down Expand Up @@ -176,12 +179,16 @@ def get_handlers_for_command(

def get_handlers_for_command_fedora_ci(
command: str,
check_target: Optional[str],
) -> set[type[FedoraCIJobHandler]]:
"""
Get handlers for the given command.
Get handlers for the given command. If check_target is specified
(for example: eln), then only handlers relevant to this target
will be returned.

Args:
command: command to get handler to
check_target: target for which to run jobs

Returns:
Set of handlers for Fecora CI that are triggered by command.
Expand All @@ -192,6 +199,12 @@ def get_handlers_for_command_fedora_ci(
handlers = MAP_COMMENT_TO_HANDLER_FEDORA_CI[command]
if not handlers:
logger.debug(f"Command {command} not supported by packit.")

if check_target:
handlers = {
handler for handler in handlers if MAP_TARGET_TO_HANDLER[handler] == check_target
}

return handlers


Expand Down Expand Up @@ -550,7 +563,11 @@ def _post_fedora_ci_transition_comment(self) -> None:
# Don't fail the job if we can't post the comment
logger.warning(f"Failed to post CI transition comment: {ex}")

def report_task_accepted_for_fedora_ci(self, handler_kls: type[FedoraCIJobHandler]):
def report_task_accepted_for_fedora_ci(
self,
handler_kls: type[FedoraCIJobHandler],
user_specified_target_branch: Optional[str] = None,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one is a bit tricky, FedoraCIHelper has target_branch attribute that comes from a target_branch argument of StatusReporter.set_status() and was originally used for target branch, but now it's actually the build target (in eln and main cases). But I would probably keep this as it is.

):
"""
For CI-related dist-git PR comment events report the initial status
"Task was accepted" to inform user we are working on the request.
Expand All @@ -570,10 +587,12 @@ def report_task_accepted_for_fedora_ci(self, handler_kls: type[FedoraCIJobHandle
if (target_branch := self.event.pull_request_object.target_branch) == "main":
target_branch = "rawhide"

# target_branch determines the check's title such as:
# "Packit - installability - rawhide [beaf90b]"
helper = FedoraCIHelper(
project=self.event.project,
metadata=metadata,
target_branch=target_branch,
target_branch=user_specified_target_branch or target_branch,
)

first_status_reported = False
Expand Down Expand Up @@ -681,9 +700,10 @@ def process_fedora_ci_jobs(self) -> list[TaskResults]:
A list of task results for each task created.
"""
handlers_triggered_by_job = None
check_target = None

# [XXX] if there are ever monorepos in Fedora CI…
# monorepo_package = None

if isinstance(self.event, abstract.comment.CommentEvent):
arguments = parse_comment(
self.event.comment,
Expand All @@ -693,7 +713,8 @@ def process_fedora_ci_jobs(self) -> list[TaskResults]:
# [XXX] if there are ever monorepos in Fedora CI…
# monorepo_package = arguments.package
command = arguments.command
handlers_triggered_by_job = get_handlers_for_command_fedora_ci(command)
check_target = getattr(arguments, "check_target", None)
handlers_triggered_by_job = get_handlers_for_command_fedora_ci(command, check_target)

matching_handlers = {
handler
Expand Down Expand Up @@ -724,7 +745,7 @@ def process_fedora_ci_jobs(self) -> list[TaskResults]:
# if monorepo_package and handler_kls.job_config.package == monorepo_package:
# continue

self.report_task_accepted_for_fedora_ci(handler_kls)
self.report_task_accepted_for_fedora_ci(handler_kls, check_target)

celery_signature = celery.signature(
handler_kls.task_name.value,
Expand Down
Loading
Loading