Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue 13 #29

Merged
merged 2 commits into from
Sep 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion joft/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ def create_ticket(

logging.info(f"New Jira ticket created: {new_issue.permalink()}")

reference_pool[action.object_id] = new_issue
if action.object_id:
reference_pool[action.object_id] = new_issue


# TODO jira_session is not needed here. Maybe remove?
Expand Down Expand Up @@ -53,6 +54,9 @@ def update_ticket(

logging.info(f"Ticket '{ticket_to.key}' updated.")

if action.object_id:
reference_pool[action.object_id] = ticket_to


def link_issues(
action: joft.models.LinkIssuesAction,
Expand Down Expand Up @@ -105,3 +109,6 @@ def transition_issue(
jira_session.transition_issue(
ticket_to, action.transition, action.fields, action.comment
)

if action.object_id:
reference_pool[action.object_id] = ticket_to
3 changes: 2 additions & 1 deletion joft/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,8 @@ def validate_uniqueness_of_object_ids(jira_template: joft.models.JiraTemplate) -
object_ids.append(jira_template.jira_search.object_id)

for action in jira_template.jira_actions:
object_ids.append(action.object_id)
if action.object_id:
object_ids.append(action.object_id)

# check if all the object ids are unique
if len(object_ids) != len(set(object_ids)):
Expand Down
8 changes: 6 additions & 2 deletions joft/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ class ReferenceData:
class Action:
# required fields
type: str
object_id: str
fields: typing.Dict[str, typing.Any]

def reuse_data_must_be_list(self, reuse_data):
Expand All @@ -34,6 +33,7 @@ def reuse_data_must_be_list(self, reuse_data):
@dataclasses.dataclass(kw_only=True)
class CreateTicketAction(Action):
reuse_data: dataclasses.InitVar[typing.List[ReferenceData] | None] = None
object_id: str | None = None

reference_data: typing.List[ReferenceData] = dataclasses.field(default_factory=list)

Expand All @@ -48,6 +48,7 @@ def __post_init__(self, reuse_data):
@dataclasses.dataclass(kw_only=True)
class UpdateTicketAction(Action):
reference_id: str
object_id: str | None = None
reuse_data: dataclasses.InitVar[typing.List[ReferenceData] | None] = None

reference_data: typing.List[ReferenceData] = dataclasses.field(default_factory=list)
Expand All @@ -63,6 +64,7 @@ def __post_init__(self, reuse_data):
@dataclasses.dataclass(kw_only=True)
class LinkIssuesAction(Action):
reuse_data: dataclasses.InitVar[typing.List[ReferenceData] | None] = None
object_id: str | None = None

reference_data: typing.List[ReferenceData] = dataclasses.field(default_factory=list)

Expand All @@ -79,6 +81,7 @@ class TransitionAction(Action):
reference_id: str
transition: str
comment: str
object_id: str | None = None
reuse_data: dataclasses.InitVar[typing.List[ReferenceData] | None] = None

reference_data: typing.List[ReferenceData] = dataclasses.field(default_factory=list)
Expand All @@ -95,7 +98,6 @@ def __post_init__(self, reuse_data):
class JiraTemplate:
api_version: int
kind: str
metadata: typing.Dict[str, str]

# initvars
actions: dataclasses.InitVar[typing.List[typing.Dict[str, typing.Any]]]
Expand All @@ -106,6 +108,8 @@ class JiraTemplate:
CreateTicketAction | UpdateTicketAction | LinkIssuesAction | TransitionAction
] = dataclasses.field(default_factory=list)

metadata: typing.Dict[str, str] | None = None

def __post_init__(self, actions, trigger) -> None:
if trigger:
self.jira_search: Trigger = Trigger(**trigger)
Expand Down
19 changes: 18 additions & 1 deletion tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@


def _setup_jira_template_yaml(
duplicate_id: bool = False, invalid_action: bool = False
duplicate_id: bool = False,
invalid_action: bool = False,
no_object_ids: bool = False,
) -> dict[str, typing.Any]:
"""Setup function that provides different types of yaml structures"""

Expand Down Expand Up @@ -63,6 +65,10 @@ def _setup_jira_template_yaml(
if invalid_action:
jira_template_yaml["actions"][0]["type"] = "invalid_action"

if no_object_ids:
for action in jira_template_yaml["actions"]:
action.pop("object_id", None)

return jira_template_yaml


Expand Down Expand Up @@ -350,3 +356,14 @@ def test_validate_template_success(
assert ret_code == 0
mock_load_and_parse_yaml.assert_called_once_with(yaml_file_path)
mock_validate_uniqueness_object_ids.assert_called_once_with(jira_template)


def test_object_id_not_present() -> None:
"""Test that Object IDs are optional, but still present in the dataclasses with
None value"""

jira_template_yaml = _setup_jira_template_yaml(no_object_ids=True)
jira_template = joft.models.JiraTemplate(**jira_template_yaml)

for action in jira_template.jira_actions:
assert not action.object_id
4 changes: 2 additions & 2 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def test_load_invalid_yaml_raise() -> None:

@unittest.mock.patch("joft.utils.pathlib.Path.cwd")
@unittest.mock.patch("joft.utils.platformdirs")
def test_load_toml_app_config(mock_platformdirs, mock_cwd):
def test_load_toml_app_config(mock_platformdirs, mock_cwd) -> None:
"""Test if we can find the app config file in one of the platform dirs"""
hostname = "test"
pat_token = "pat_token"
Expand Down Expand Up @@ -60,7 +60,7 @@ def test_load_toml_app_config(mock_platformdirs, mock_cwd):

@unittest.mock.patch("joft.utils.pathlib.Path.cwd")
@unittest.mock.patch("joft.utils.platformdirs")
def test_load_toml_app_config_no_config_found(mock_platformdirs, mock_cwd):
def test_load_toml_app_config_no_config_found(mock_platformdirs, mock_cwd) -> None:
"""
Test that we will end with a non-zero error code when there is no config present and
printing a message on the stdout.
Expand Down
Loading