Skip to content

Commit

Permalink
Add --env to comment trigger phrase
Browse files Browse the repository at this point in the history
Signed-off-by: Jakub Stejskal <[email protected]>

Remove leftover log

Signed-off-by: Jakub Stejskal <[email protected]>
  • Loading branch information
Frawless committed Oct 14, 2024
1 parent 755a694 commit 2a7b77b
Show file tree
Hide file tree
Showing 3 changed files with 213 additions and 49 deletions.
91 changes: 67 additions & 24 deletions packit_service/worker/helpers/testing_farm.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
# Copyright Contributors to the Packit project.
# SPDX-License-Identifier: MIT

import argparse
import logging
import re
from re import Pattern
import shlex
from typing import Dict, Any, Optional, Set, List, Union, Tuple, Callable

import requests
Expand Down Expand Up @@ -60,40 +61,72 @@ class CommentArguments:
Parse arguments from trigger comment and provide the attributes to Testing Farm helper.
"""

packit_command: str = None
identifier: str = None
labels: List[str] = None
pr_argument: str = None

def __init__(self, command_prefix: str, comment: str):
self.packit_command: str = None
self.identifier: str = None
self.labels: List[str] = None
self.pr_argument: str = None
self.envs: Dict[str, str] = None

if comment is None:
return

# Try to parse identifier argument from comment
# Remove the command prefix from the comment
logger.debug(f"Parsing comment -> {comment}")
logger.debug(f"Used command prefix -> {command_prefix}")

match = re.search(
r"^" + re.escape(command_prefix) + r"\s(?P<packit_command>\S+)", comment
)
# Match the command prefix and extract the rest of the comment
match = re.match(r"^" + re.escape(command_prefix) + r"\s+(.*)", comment)
if match:
self.packit_command = match.group("packit_command")
logger.debug(f"Parsed packit_command: {self.packit_command}")
arguments_str = match.group(1)
else:
# If the command prefix is not found, nothing to parse
logger.debug("Command prefix not found in the comment.")
return

match = re.search(r"(--identifier|--id|-i)[\s=](?P<identifier>\S+)", comment)
if match:
self.identifier = match.group("identifier")
logger.debug(f"Parsed test argument -> identifier: {self.identifier}")
# Use shlex to split the arguments string into a list
args_list = shlex.split(arguments_str)
logger.debug(f"Arguments list after shlex splitting: {args_list}")

match = re.search(r"--labels[\s=](?P<labels>\S+)", comment)
if match:
self.labels = match.group("labels").split(",")
logger.debug(f"Parsed test argument -> labels: {self.labels}")
# Set up argparse
parser = argparse.ArgumentParser()
parser.add_argument("packit_command")
parser.add_argument("--identifier", "--id", "-i")
parser.add_argument("--labels")
parser.add_argument("--env", action="append") # Allows multiple --env arguments

match = re.search(r"(?P<pr_arg>[^/\s]+/[^#]+#\d+)", comment)
if match:
self.pr_argument = match.group("pr_arg")
logger.debug(f"Parsed test argument -> pr_argument: {self.pr_argument}")
# Parse known arguments
try:
args, unknown_args = parser.parse_known_args(args_list)
logger.debug(f"Parsed known args: {args}")
logger.debug(f"Unknown args: {unknown_args}")
except argparse.ArgumentError as e:
logger.error(f"Argument parsing error: {e}")
return

# Assign the parsed arguments to the class attributes
self.packit_command = args.packit_command
logger.debug(f"Parsed packit_command: {self.packit_command}")

self.identifier = args.identifier
logger.debug(f"Parsed identifier: {self.identifier}")

if args.labels:
self.labels = args.labels.split(",")
logger.debug(f"Parsed labels: {self.labels}")

if args.env:
# Parse envs into dictionary
self.envs = dict(env.split("=") for env in args.env)
logger.debug(f"Parsed envs: {self.envs}")

# Process unknown_args to find pr_argument
pr_argument_pattern = re.compile(r"^[^/\s]+/[^#\s]+#\d+$")
for arg in unknown_args:
if pr_argument_pattern.match(arg):
self.pr_argument = arg
logger.debug(f"Parsed pr_argument: {self.pr_argument}")
break


class TestingFarmJobHelper(CoprBuildJobHelper):
Expand Down Expand Up @@ -523,6 +556,16 @@ def _payload(
env_variables = self.job_config.env if hasattr(self.job_config, "env") else {}
predefined_environment.update(env_variables)

# User-defined variables from comments have priority
if self.is_comment_event:
if self.comment_arguments.envs is not None:
env_variables = {
k: v
for k, v in self.comment_arguments.envs.items()
if v is not None
}
predefined_environment.update(env_variables)

environment: Dict[str, Any] = {
"arch": arch,
"os": {"compose": compose},
Expand Down
40 changes: 20 additions & 20 deletions tests/unit/test_checkers.py
Original file line number Diff line number Diff line change
Expand Up @@ -507,27 +507,27 @@ def test_koji_branch_merge_queue():
"comment, result",
(
pytest.param(
"/packit-dev test --identifier my-id-1",
"/packit test --identifier my-id-1",
True,
id="Matching identifier specified",
),
pytest.param(
"/packit-dev test --id my-id-1",
"/packit test --id my-id-1",
True,
id="Matching identifier specified",
),
pytest.param(
"/packit-dev test -i my-id-1",
"/packit test -i my-id-1",
True,
id="Matching identifier specified",
),
pytest.param(
"/packit-dev test",
"/packit test",
True,
id="No identifier specified",
),
pytest.param(
"/packit-dev test --identifier my-id-2",
"/packit test --identifier my-id-2",
False,
id="Non-matching identifier specified",
),
Expand Down Expand Up @@ -582,35 +582,35 @@ def test_tf_comment_identifier(comment, result):
"comment, default_identifier, job_identifier, result",
(
pytest.param(
"/packit-dev test --identifier my-id2",
"/packit test --identifier my-id2",
"id1",
"id1",
False,
id="Identifier specified in comment",
),
pytest.param(
"/packit-dev test",
"/packit test",
None,
"id1",
True,
id="No identifier specified, no default identifier",
),
pytest.param(
"/packit-dev test",
"/packit test",
"id1",
"id1",
True,
id="No identifier specified, default identifier matching",
),
pytest.param(
"/packit-dev test",
"/packit test",
"id1",
"id2",
False,
id="No identifier specified, default identifier not matching",
),
pytest.param(
"/packit-dev test",
"/packit test",
"id1",
None,
False,
Expand Down Expand Up @@ -670,17 +670,17 @@ def test_tf_comment_default_identifier(
"comment, result",
(
pytest.param(
"/packit-dev test --labels label1,label2",
"/packit test --labels label1,label2",
True,
id="Matching label specified",
),
pytest.param(
"/packit-dev test",
"/packit test",
True,
id="No labels specified",
),
pytest.param(
"/packit-dev test --labels random-label1,random-label2",
"/packit test --labels random-label1,random-label2",
False,
id="Non-matching label specified",
),
Expand Down Expand Up @@ -736,35 +736,35 @@ def test_tf_comment_labels(comment, result):
"comment, default_labels, job_labels, result",
(
pytest.param(
"/packit-dev test --labels label1,label2",
"/packit test --labels label1,label2",
["label3"],
["label3"],
False,
id="Labels specified in comment",
),
pytest.param(
"/packit-dev test",
"/packit test",
None,
["label1"],
True,
id="No labels specified, no default labels",
),
pytest.param(
"/packit-dev test",
"/packit test",
["label2"],
["label1", "label2"],
True,
id="No labels specified, default labels matching",
),
pytest.param(
"/packit-dev test",
"/packit test",
["label3"],
["label1", "label2"],
False,
id="No labels specified, default labels not matching",
),
pytest.param(
"/packit-dev test",
"/packit test",
["label3"],
[],
False,
Expand Down Expand Up @@ -829,12 +829,12 @@ def test_tf_comment_default_labels(comment, default_labels, job_labels, result):
"comment, result",
(
pytest.param(
"/packit-dev test",
"/packit test",
True,
id="No labels specified, none in config: should pass",
),
pytest.param(
"/packit-dev test --labels should_fail,should_fail_hard",
"/packit test --labels should_fail,should_fail_hard",
False,
id="Labels specified, none in config: should fail",
),
Expand Down
Loading

0 comments on commit 2a7b77b

Please sign in to comment.