Skip to content

Commit 8095734

Browse files
committed
Revert "Ref #520: add typing for action parameters (#523)"
This reverts commit c178e30.
1 parent 394f39f commit 8095734

File tree

5 files changed

+20
-35
lines changed

5 files changed

+20
-35
lines changed

jbi/models.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import warnings
1010
from inspect import signature
1111
from types import ModuleType
12-
from typing import Callable, Literal, Mapping, Optional, TypedDict
12+
from typing import Any, Callable, Literal, Mapping, Optional, TypedDict
1313
from urllib.parse import ParseResult, urlparse
1414

1515
from pydantic import BaseModel, Extra, Field, PrivateAttr, root_validator, validator
@@ -23,19 +23,6 @@
2323
JIRA_HOSTNAMES = ("jira", "atlassian")
2424

2525

26-
class ActionParameters(BaseModel):
27-
"""Action parameters"""
28-
29-
# For runner
30-
jira_project_key: str
31-
steps: Optional[dict[str, list[str]]] = None
32-
# For steps
33-
status_map: Optional[dict[str, str]] = None
34-
resolution_map: Optional[dict[str, str]] = None
35-
jira_components: Optional[list[str]] = None
36-
sync_whiteboard_labels: bool = True
37-
38-
3926
class Action(YamlModel):
4027
"""
4128
Action is the inner model for each action in the configuration file"""
@@ -46,21 +33,21 @@ class Action(YamlModel):
4633
description: str
4734
enabled: bool = True
4835
allow_private: bool = False
49-
parameters: ActionParameters
36+
parameters: dict = {}
5037
_caller: Optional[Callable] = PrivateAttr(default=None)
5138
_required_jira_permissions: set[str] = PrivateAttr(default=None)
5239

5340
@property
5441
def jira_project_key(self):
5542
"""Return the configured project key."""
56-
return self.parameters.jira_project_key
43+
return self.parameters["jira_project_key"]
5744

5845
@property
5946
def caller(self) -> Callable:
6047
"""Return the initialized callable for this action."""
6148
if self._caller is None:
6249
action_module: ModuleType = importlib.import_module(self.module)
63-
initialized: Callable = action_module.init(**self.parameters.dict()) # type: ignore
50+
initialized: Callable = action_module.init(**self.parameters) # type: ignore
6451
self._caller = initialized
6552
return self._caller
6653

@@ -78,23 +65,18 @@ def validate_action_config(cls, values): # pylint: disable=no-self-argument
7865
"""Validate action: exists, has init function, and has expected params"""
7966
try:
8067
module: str = values["module"] # type: ignore
81-
try:
82-
action_parameters = values["parameters"].dict()
83-
except KeyError:
84-
action_parameters = {}
68+
action_parameters: Optional[dict[str, Any]] = values["parameters"]
8569
action_module: ModuleType = importlib.import_module(module)
8670
if not action_module:
8771
raise TypeError("Module is not found.")
8872
if not hasattr(action_module, "init"):
8973
raise TypeError("Module is missing `init` method.")
9074

91-
signature(action_module.init).bind(**action_parameters)
75+
signature(action_module.init).bind(**action_parameters) # type: ignore
9276
except ImportError as exception:
9377
raise ValueError(f"unknown Python module `{module}`.") from exception
9478
except (TypeError, AttributeError) as exception:
95-
raise ValueError(
96-
f"action '{module}' is not properly setup. {exception}"
97-
) from exception
79+
raise ValueError(f"action is not properly setup.{exception}") from exception
9880
return values
9981

10082

@@ -126,7 +108,11 @@ def get(self, tag: Optional[str]) -> Optional[Action]:
126108
@functools.cached_property
127109
def configured_jira_projects_keys(self) -> set[str]:
128110
"""Return the list of Jira project keys from all configured actions"""
129-
return {action.jira_project_key for action in self.__root__}
111+
return {
112+
action.jira_project_key
113+
for action in self.__root__
114+
if "jira_project_key" in action.parameters
115+
}
130116

131117
@validator("__root__")
132118
def validate_actions( # pylint: disable=no-self-argument

jbi/runner.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ def execute_action(
8181
event=event,
8282
operation=Operation.IGNORE,
8383
jira=JiraContext(project=action.jira_project_key, issue=linked_issue_key),
84-
extra={k: str(v) for k, v in action.parameters.dict().items()},
84+
extra={k: str(v) for k, v in action.parameters.items()},
8585
)
8686

8787
if action_context.jira.issue is None:

jbi/services/jira.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@ def _all_projects_permissions(actions: Actions):
129129
def _fetch_project_permissions(actions):
130130
"""Fetches permissions for the configured projects"""
131131
required_perms_by_project = {
132-
action.parameters.jira_project_key: action.required_jira_permissions
132+
action.parameters["jira_project_key"]: action.required_jira_permissions
133133
for action in actions
134+
if "jira_project_key" in action.parameters
134135
}
135136
client = get_client()
136137
all_projects_perms = {}
@@ -183,8 +184,9 @@ def _validate_permissions(all_projects_perms):
183184

184185
def _all_projects_components_exist(actions: Actions):
185186
components_by_project = {
186-
action.parameters.jira_project_key: action.parameters.jira_components
187+
action.parameters["jira_project_key"]: action.parameters["jira_components"]
187188
for action in actions
189+
if "jira_components" in action.parameters
188190
}
189191
success = True
190192
for project, specified_components in components_by_project.items():

tests/fixtures/bugzilla_action.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@ def __call__(self, bug, event):
33
return {"bug": bug, "event": event}
44

55

6-
def init(**kwargs):
6+
def init():
77
return FakeBugzillaAction()

tests/unit/test_models.py

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import pytest
22
from fastapi.encoders import jsonable_encoder
33

4-
from jbi.models import Action, ActionParameters, Actions
4+
from jbi.models import Action, Actions
55
from tests.fixtures.factories import action_factory
66

77

@@ -13,7 +13,6 @@ def test_model_serializes():
1313
"bugzilla_user_id": 123456,
1414
"description": "test config",
1515
"module": "tests.fixtures.bugzilla_action",
16-
"parameters": ActionParameters(jira_project_key="fooo"),
1716
}
1817
)
1918
action.caller(bug=None, event=None)
@@ -30,7 +29,6 @@ def test_model_with_user_list_serializes():
3029
"bugzilla_user_id": [123456, 654321, 000000, 111111],
3130
"description": "test config",
3231
"module": "tests.fixtures.bugzilla_action",
33-
"parameters": ActionParameters(jira_project_key="fooo"),
3432
}
3533
)
3634
action.caller(bug=None, event=None)
@@ -46,7 +44,6 @@ def test_model_with_user_list_of_one_serializes():
4644
"bugzilla_user_id": [123456],
4745
"description": "test config",
4846
"module": "tests.fixtures.bugzilla_action",
49-
"parameters": ActionParameters(jira_project_key="fooo"),
5047
}
5148
)
5249
action.caller(bug=None, event=None)
@@ -70,7 +67,7 @@ def test_unknown_module_fails():
7067
def test_bad_module_fails():
7168
with pytest.raises(ValueError) as exc_info:
7269
Actions.parse_obj([{"whiteboard_tag": "x", "module": "jbi.runner"}])
73-
assert "action 'jbi.runner' is not properly setup" in str(exc_info.value)
70+
assert "action is not properly setup" in str(exc_info.value)
7471

7572

7673
def test_duplicated_whiteboard_tag_fails():

0 commit comments

Comments
 (0)