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

Refactor jira_ticket method to comply with new OBS systems hierarchy. #300

Merged
merged 6 commits into from
Jan 10, 2025
Merged
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@
Version History
===============

v7.2.0
------

* Refactor jira_ticket method to comply with new OBS systems hierarchy. `<https://github.com/lsst-ts/LOVE-manager/pull/300>`_

v7.1.7
------

Expand Down
148 changes: 74 additions & 74 deletions manager/api/tests/test_jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
# this program. If not, see <http://www.gnu.org/licenses/>.


import math
import os
import random
from unittest.mock import patch
Expand All @@ -30,49 +29,56 @@
from django.test import TestCase, override_settings

from manager.utils import (
TIME_LOST_FIELD,
OBS_TIME_LOST_FIELD,
get_jira_obs_report,
handle_jira_payload,
jira_comment,
jira_ticket,
update_time_lost,
)

OLE_JIRA_OBS_COMPONENTS_FIELDS = [
"AuxTel",
"Calibrations",
"Environmental Monitoring Systems",
"Facilities",
"IT Infrastricture",
"MainTel",
"Observer Remark",
"Other",
"Unknown",
]

OLE_JIRA_OBS_PRIMARY_SOFTWARE_COMPONENT_FIELDS = [
"None",
"CSC level",
"Component Level (EUI)",
"Visualization",
"Analysis",
"Other",
"Camera Control Software",
]

OLE_JIRA_OBS_PRIMARY_HARDWARE_COMPONENT_FIELDS = [
"None",
"Mount",
"Rotator",
"Hexapod",
"M2",
"Science Cameras",
"M1M3",
"Dome",
"Utilities",
"Calibration",
"Other",
]
JIRA_OBS_SYSTEMS_SELECTION_EXAMPLE = """
{
"selection": [
[
{
"name": "AuxTel",
"id": "1",
"children": [
"60",
"61",
"62",
"63",
"64",
"430",
"128"
]
}
],
[
{
"name": "AT: OCS",
"id": "430",
"children": [
"432",
"433",
"434",
"435",
"436",
"437",
"438"
]
}
],
[
{
"name": "ATScheduler CSC",
"id": "437"
}
]
]
}
"""


@override_settings(DEBUG=True)
Expand Down Expand Up @@ -109,40 +115,7 @@ def setUp(self):
}

request_narrative = {
"components": ",".join(
list(
OLE_JIRA_OBS_COMPONENTS_FIELDS[
: math.ceil(
random.random() * (len(OLE_JIRA_OBS_COMPONENTS_FIELDS) - 1)
)
]
)
),
"components_ids": ",".join(
[str(n) for n in range(1, math.ceil(random.random() * 100))]
),
"primary_software_components": ",".join(
OLE_JIRA_OBS_PRIMARY_SOFTWARE_COMPONENT_FIELDS[
math.ceil(
random.random()
* (len(OLE_JIRA_OBS_PRIMARY_SOFTWARE_COMPONENT_FIELDS) - 1)
)
]
),
"primary_software_components_ids": ",".join(
[str(math.ceil(random.random() * 100))]
),
"primary_hardware_components": ",".join(
OLE_JIRA_OBS_PRIMARY_HARDWARE_COMPONENT_FIELDS[
math.ceil(
random.random()
* (len(OLE_JIRA_OBS_PRIMARY_HARDWARE_COMPONENT_FIELDS) - 1)
)
]
),
"primary_hardware_components_ids": ",".join(
[str(math.ceil(random.random() * 100))]
),
"jira_obs_selection": JIRA_OBS_SYSTEMS_SELECTION_EXAMPLE,
"date_begin": "2022-07-03T19:58:13.00000",
"date_end": "2022-07-04T19:25:13.00000",
"time_lost": 10,
Expand Down Expand Up @@ -231,6 +204,13 @@ def setUp(self):
data=data_narrative_without_param
)

# narrative with not valid jira_obs_selection json
data_narrative_invalid_jira_obs_selection = {**request_full_narrative}
data_narrative_invalid_jira_obs_selection["jira_obs_selection"] = "invalid_json"
self.jira_request_narrative_invalid_jira_obs_selection = requests.Request(
data=data_narrative_invalid_jira_obs_selection
)

# all parameters requests
self.jira_request_exposure_full = requests.Request(data=request_full_exposure)
self.jira_request_narrative_full = requests.Request(data=request_full_narrative)
Expand Down Expand Up @@ -298,6 +278,13 @@ def test_missing_parameters(self):
jira_response = jira_ticket(self.jira_request_narrative_without_param.data)
assert "Error creating jira payload" in jira_response.data["ack"]

def test_not_valid_obs_systems_json(self):
"""Test call to jira_ticket function with invalid jira_obs_selection"""
jira_response = jira_ticket(
self.jira_request_narrative_invalid_jira_obs_selection.data
)
assert "Error creating jira payload" in jira_response.data["ack"]

@patch.dict(os.environ, {"JIRA_API_HOSTNAME": "jira.lsstcorp.org"})
def test_needed_parameters(self):
"""Test call to jira_ticket function with all needed parameters"""
Expand Down Expand Up @@ -332,7 +319,7 @@ def test_update_time_lost(self):
mock_jira_get = mock_jira_patcher.start()
response_get = requests.Response()
response_get.status_code = 200
response_get.json = lambda: {"fields": {TIME_LOST_FIELD: 13.6}}
response_get.json = lambda: {"fields": {OBS_TIME_LOST_FIELD: 13.6}}
mock_jira_get.return_value = response_get

put_patcher = patch("requests.put")
Expand Down Expand Up @@ -360,13 +347,16 @@ def test_update_time_lost(self):
assert jira_response.status_code == 400
assert jira_response.data["ack"] == "Jira time_lost field could not be updated"

mock_jira_patcher.stop()
put_patcher.stop()

def test_update_current_time_lost_none(self):
"""Test call to update_time_lost with None as current time_lost"""
mock_jira_patcher = patch("requests.get")
mock_jira_get = mock_jira_patcher.start()
response_get = requests.Response()
response_get.status_code = 200
response_get.json = lambda: {"fields": {TIME_LOST_FIELD: None}}
response_get.json = lambda: {"fields": {OBS_TIME_LOST_FIELD: None}}
mock_jira_get.return_value = response_get

put_patcher = patch("requests.put")
Expand All @@ -380,6 +370,9 @@ def test_update_current_time_lost_none(self):
assert jira_response.status_code == 200
assert jira_response.data["ack"] == "Jira time_lost field updated"

mock_jira_patcher.stop()
put_patcher.stop()

def test_add_comment(self):
"""Test call to jira_comment function with all needed parameters"""
mock_jira_patcher = patch("requests.post")
Expand All @@ -405,6 +398,9 @@ def test_add_comment(self):
assert jira_response.status_code == 200
assert jira_response.data["ack"] == "Jira comment created"

mock_jira_patcher.stop()
mock_time_lost_patcher.stop()

def test_add_comment_fail(self):
"""Test jira_comment() return value when update_time_lost()
fails during jira_comment()"""
Expand All @@ -427,6 +423,9 @@ def test_add_comment_fail(self):
assert resp.status_code == 400
assert resp.data["ack"] == "Jira time_lost field could not be updated"

mock_jira_patcher.stop()
mock_time_lost_patcher.stop()

@patch.dict(os.environ, {"JIRA_API_HOSTNAME": "jira.lsstcorp.org"})
def test_handle_narrative_jira_payload(self):
"""Test call to function handle_jira_payload with all needed parameters
Expand Down Expand Up @@ -464,6 +463,7 @@ def test_handle_narrative_jira_payload(self):
)

mock_jira_patcher.stop()
mock_time_lost_patcher.stop()

def test_handle_exposure_jira_payload(self):
"""Test call to function handle_jira_payload with all needed parameters
Expand Down Expand Up @@ -534,7 +534,7 @@ def test_get_jira_obs_report(self):
"key": "LOVE-XX",
"fields": {
"summary": "Issue title",
TIME_LOST_FIELD: 13.6,
OBS_TIME_LOST_FIELD: 13.6,
"creator": {"displayName": "user"},
"created": "2024-11-27T12:00:00.00000",
},
Expand Down
Loading
Loading