Skip to content

Commit 5153380

Browse files
committed
Introduce ParsedComment class
As per mfocko's suggestion, the parse_comment function now returns an instance of ParsedComment rather than a dictionary to increase the amount of code clarity. The parse_comment function now also features a simple example in docstring.
1 parent 30ee03b commit 5153380

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

packit_service/worker/jobs.py

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
"""
77

88
import logging
9+
from dataclasses import dataclass
910
from datetime import datetime
1011
from functools import cached_property
1112
from re import match
@@ -94,10 +95,16 @@
9495
MANUAL_OR_RESULT_EVENTS = [abstract.comment.CommentEvent, abstract.base.Result, github.check.Rerun]
9596

9697

98+
@dataclass
99+
class ParsedComment:
100+
command: str
101+
package: str
102+
103+
97104
def parse_comment(
98105
comment: str,
99106
packit_comment_command_prefix: str,
100-
) -> dict[str, str]:
107+
) -> Optional[ParsedComment]:
101108
"""
102109
Get arguments from the given comment respecting `packit_comment_command_prefix`.
103110
@@ -106,11 +113,16 @@ def parse_comment(
106113
packit_comment_command_prefix: `/packit` for packit-prod or `/packit-stg` for stg
107114
108115
Returns:
109-
Dictionary storing command inside comment and monorepo package if specified
116+
ParsedComment storing command inside comment and monorepo package if specified
117+
If comment isn't recognized, ParsedComment containing empty strings is returned instead
118+
119+
For example: If the comment is `/packit build --commit 123 --package best-package-ever`,
120+
it would return ParsedComment(command="build", package="best-package-ever")
121+
Other aguments are ignored because they are handled separately by job handlers
110122
"""
111123
commands = get_packit_commands_from_comment(comment, packit_comment_command_prefix)
112124
if not commands:
113-
return {}
125+
return None
114126

115127
if comment.startswith("/packit-ci"):
116128
parser = get_pr_comment_parser_fedora_ci(
@@ -127,11 +139,11 @@ def parse_comment(
127139

128140
try:
129141
args = parser.parse_args(commands)
130-
return {"command": args.command, "monorepo_package": args.package}
142+
return ParsedComment(command=args.command, package=args.package)
131143
except SystemExit:
132144
# tests expect invalid syntax comments be ignored
133145
logger.debug(f"Command {commands} uses unexpected syntax.")
134-
return {"command": ""}
146+
return None
135147

136148

137149
def get_handlers_for_command(
@@ -543,7 +555,7 @@ def is_packit_config_present(self) -> bool:
543555
self.event.comment,
544556
self.service_config.comment_command_prefix,
545557
)
546-
command = arguments["command"]
558+
command = arguments.command if arguments else ""
547559

548560
if handlers := get_handlers_for_command(command):
549561
# we require packit config file when event is triggered by /packit command
@@ -587,8 +599,8 @@ def process_fedora_ci_jobs(self) -> list[TaskResults]:
587599
)
588600

589601
# [XXX] if there are ever monorepos in Fedora CI…
590-
# monorepo_package = arguments.get("monorepo_package", None)
591-
command = arguments["command"]
602+
# monorepo_package = arguments.package if arguments else ""
603+
command = arguments.command if arguments else ""
592604
handlers_triggered_by_job = get_handlers_for_command_fedora_ci(command)
593605

594606
matching_handlers = {
@@ -662,8 +674,8 @@ def process_jobs(self) -> list[TaskResults]:
662674
self.service_config.comment_command_prefix,
663675
)
664676

665-
monorepo_package = arguments.get("monorepo_package", None)
666-
command = arguments.get("command", "")
677+
monorepo_package = arguments.package if arguments else ""
678+
command = arguments.command if arguments else ""
667679

668680
if not get_handlers_for_command(command):
669681
return [
@@ -1006,7 +1018,7 @@ def get_handlers_for_comment_and_rerun_event(self) -> set[type[JobHandler]]:
10061018
self.service_config.comment_command_prefix,
10071019
)
10081020

1009-
command = arguments.get("command", "")
1021+
command = arguments.command if arguments else ""
10101022
handlers_triggered_by_job = get_handlers_for_command(command)
10111023

10121024
if handlers_triggered_by_job and not isinstance(

0 commit comments

Comments
 (0)