Skip to content

Commit 20aa8b5

Browse files
committed
Refine PR comment parser
Comment parser has been split to two separate parsers: regular and for Fedora CI. Each has its own defined arguments as per Packit documentation.
1 parent 85e528e commit 20aa8b5

File tree

2 files changed

+85
-11
lines changed

2 files changed

+85
-11
lines changed

packit_service/utils.py

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Copyright Contributors to the Packit project.
22
# SPDX-License-Identifier: MIT
33

4+
import argparse
45
import logging
56
import os
67
from datetime import datetime, timezone
@@ -217,6 +218,79 @@ def get_packit_commands_from_comment(
217218
return []
218219

219220

221+
def get_pr_comment_parser() -> argparse.ArgumentParser:
222+
parser = argparse.ArgumentParser(prog="/packit")
223+
parser.add_argument("--package", help="Specific package from monorepo to apply job to")
224+
225+
subparsers = parser.add_subparsers(
226+
dest="command",
227+
help="Jobs available",
228+
)
229+
230+
build_parser = subparsers.add_parser(
231+
"copr-build",
232+
aliases=["build"],
233+
help="Build package(s) in copr",
234+
)
235+
build_parser.add_argument(
236+
"--commit", help="Run copr build jobs configured with the commit trigger"
237+
)
238+
build_parser.add_argument(
239+
"--release", help="Run copr build jobs configured with the release trigger"
240+
)
241+
subparsers.add_parser("rebuild-failed", help="Re-build failed builds in copr")
242+
subparsers.add_parser(
243+
"upstream-koji-build",
244+
help="Build package(s) in Koji (the latest commit of this PR will be targeted, not HEAD)",
245+
)
246+
test_parser = subparsers.add_parser("test", help="Run tests in Testing Farm")
247+
test_parser.add_argument("--commit", help="Run tests configured with the commit trigger")
248+
test_parser.add_argument("--release", help="Run tests configured with the release trigger")
249+
test_parser.add_argument("target", nargs="*", help="Test target(s)")
250+
test_parser.add_argument("env", nargs="*", help="Environment variables")
251+
subparsers.add_parser("retest-failed", help="Re-run failed tests in Testing Farm")
252+
subparsers.add_parser("vm-image-build", help="Trigger VM image build")
253+
subparsers.add_parser("propose-downstream", help="Trigger propose-downstream job")
254+
pull_from_upstream_parser = subparsers.add_parser(
255+
"pull-from-upstream", help="Trigger pull-from-upstream job"
256+
)
257+
pull_from_upstream_parser.add_argument(
258+
"--resolve-bug",
259+
help="Override the referenced resolved bug set by Packit",
260+
)
261+
pull_from_upstream_parser.add_argument(
262+
"--with-pr-config",
263+
action="store_true",
264+
help="Use the configuration file from this dist-git pull request",
265+
)
266+
subparsers.add_parser(
267+
"koji-build",
268+
help="Build package(s) in Koji (the latest commit of this PR will be targeted, not HEAD)",
269+
)
270+
koji_tag_parser = subparsers.add_parser("koji-tag", help="Tag Koji build to the common sidetag")
271+
koji_tag_parser.add_argument("--all-branches", action="store_true", help="Target all branches")
272+
subparsers.add_parser("create-update", help="Trigger Bodhi update job")
273+
274+
return parser
275+
276+
277+
def get_pr_comment_parser_fedora_ci() -> argparse.ArgumentParser:
278+
parser = argparse.ArgumentParser(prog="/packit-ci")
279+
parser.add_argument("--package", help="Specific package from monorepo to apply job to")
280+
281+
subparsers = parser.add_subparsers(
282+
dest="command",
283+
help="Jobs available",
284+
)
285+
test_parser = subparsers.add_parser("test", help="Run tests in Testing Farm")
286+
test_parser.add_argument("target", nargs="*", help="Test target(s)")
287+
test_parser.add_argument("env", nargs="*", help="Environment variables")
288+
subparsers.add_parser("retest-failed", help="Re-run failed tests in Testing Farm")
289+
subparsers.add_parser("scratch-build", help="Build package in Scratch")
290+
291+
return parser
292+
293+
220294
def get_koji_task_id_and_url_from_stdout(stdout: str) -> tuple[Optional[int], Optional[str]]:
221295
task_id, task_url = None, None
222296

packit_service/worker/jobs.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
We love you, Steve Jobs.
66
"""
77

8-
import argparse
98
import logging
109
from datetime import datetime
1110
from functools import cached_property
@@ -36,6 +35,8 @@
3635
from packit_service.utils import (
3736
elapsed_seconds,
3837
get_packit_commands_from_comment,
38+
get_pr_comment_parser,
39+
get_pr_comment_parser_fedora_ci,
3940
pr_labels_match_configuration,
4041
)
4142
from packit_service.worker.allowlist import Allowlist
@@ -107,19 +108,18 @@ def parse_comment(
107108
if not commands:
108109
return {}
109110

110-
parser = argparse.ArgumentParser()
111-
parser.add_argument("--package", help="Specific package from monorepo to apply job to")
112-
parser.add_argument("command", nargs="*", help="Command to perform")
113-
parser.add_argument("--all-branches", action="store_true")
114-
parser.add_argument("--with-pr-config", action="store_true")
115-
parser.add_argument("--resolve-bug")
111+
if comment.startswith("/packit-ci"):
112+
parser = get_pr_comment_parser_fedora_ci()
113+
else:
114+
parser = get_pr_comment_parser()
116115

117116
try:
118117
args = parser.parse_args(commands)
119118
return {"command": args.command, "monorepo_package": args.package}
120-
except argparse.ArgumentError:
119+
except SystemExit:
120+
# tests expect invalid syntax comments be ignored
121121
logger.debug(f"Command {commands} uses unexpected syntax.")
122-
return {}
122+
return {"command": ""}
123123

124124

125125
def get_handlers_for_command(
@@ -137,7 +137,7 @@ def get_handlers_for_command(
137137
if not command:
138138
return set()
139139

140-
handlers = MAP_COMMENT_TO_HANDLER[command[0]]
140+
handlers = MAP_COMMENT_TO_HANDLER[command]
141141
if not handlers:
142142
logger.debug(f"Command {command} not supported by packit.")
143143
return handlers
@@ -158,7 +158,7 @@ def get_handlers_for_command_fedora_ci(
158158
if not command:
159159
return set()
160160

161-
handlers = MAP_COMMENT_TO_HANDLER_FEDORA_CI[command[0]]
161+
handlers = MAP_COMMENT_TO_HANDLER_FEDORA_CI[command]
162162
if not handlers:
163163
logger.debug(f"Command {command} not supported by packit.")
164164
return handlers

0 commit comments

Comments
 (0)