From 907b1c862e68f5f2d46f45eaf0cad1945ef91ee2 Mon Sep 17 00:00:00 2001 From: Valerie Becker Date: Thu, 22 Aug 2024 13:23:12 -0700 Subject: [PATCH] Updating comments and using constant for custom fields --- manager/api/tests/test_jira.py | 24 +++++++++--------- manager/manager/utils.py | 45 ++++++++++++++++++---------------- 2 files changed, 37 insertions(+), 32 deletions(-) diff --git a/manager/api/tests/test_jira.py b/manager/api/tests/test_jira.py index 0f3fd9aa..2021ff51 100644 --- a/manager/api/tests/test_jira.py +++ b/manager/api/tests/test_jira.py @@ -27,10 +27,11 @@ from django.test import TestCase, override_settings from manager.utils import ( + TIME_LOST_FIELD, handle_jira_payload, jira_comment, jira_ticket, - update_time_loss, + update_time_lost, ) OLE_JIRA_OBS_COMPONENTS_FIELDS = [ @@ -318,24 +319,25 @@ def test_needed_parameters(self): @patch("requests.get") @patch("requests.put") - def test_update_time_loss(self, mock_get, mock_put): - """Test call to update_time_loss and verify field was updated""" + def test_update_time_lost(self, mock_get, mock_put): + """Test call to update_time_lost and verify field was updated""" # patch both requests.get, requests.put response = requests.Response() response.status_code = 200 - mock_put.return_value = response - - response.json = lambda: {"customfield_10106": 13.6} + response.json = lambda: {TIME_LOST_FIELD: 13.6} mock_get.return_value = response + response.status_code = 204 + mock_put.return_value = response + # call update time lost - jira_response = update_time_loss(1, 3.4) + jira_response = update_time_lost(1, 3.4) assert jira_response.status_code == 200 - assert jira_response.data["ack"] == "Jira field updated" + assert jira_response.data["ack"] == "Jira time_lost field updated" - jira_response = update_time_loss(93827, 1.23) + jira_response = update_time_lost(93827, 1.23) assert jira_response.status_code == 200 - assert jira_response.data["ack"] == "Jira field updated" + assert jira_response.data["ack"] == "Jira time_lost field updated" def test_add_comment(self): """Test call to jira_comment function with all needed parameters""" @@ -345,7 +347,7 @@ def test_add_comment(self): response.status_code = 201 mock_jira_client.return_value = response - mock_timeloss_patcher = patch("manager.utils.update_time_loss") + mock_timeloss_patcher = patch("manager.utils.update_time_lost") mock_timeloss_client = mock_timeloss_patcher.start() mock_timeloss_client.return_value = response diff --git a/manager/manager/utils.py b/manager/manager/utils.py index ada3a8d7..2b9b33d9 100644 --- a/manager/manager/utils.py +++ b/manager/manager/utils.py @@ -40,6 +40,9 @@ # Constants JSON_RESPONSE_LOCAL_STORAGE_NOT_ALLOWED = {"error": "Local storage not allowed."} JSON_RESPONSE_ERROR_NOT_VALID_JSON = {"error": "Not a valid JSON response."} +TIME_LOST_FIELD = "customfield_10106" +PRIMARY_SOFTWARE_COMPONENTS_IDS = "customfield_10107" +PRIMARY_HARDWARE_COMPONENTS_IDS = "customfield_10196" class LocationPermission(BasePermission): @@ -449,16 +452,16 @@ def jira_ticket(request_data): # "on" if int(request_data.get("level", 0)) >= 100 # else "off" # ), - "customfield_10106": float(request_data.get("time_lost", 0)), + TIME_LOST_FIELD: float(request_data.get("time_lost", 0)), # Default values of the following fields are set to -1 - "customfield_10107": { + PRIMARY_SOFTWARE_COMPONENTS_IDS: { "id": ( str(primary_software_components_ids[0]) if primary_software_components_ids else "-1" ) }, - "customfield_10196": { + PRIMARY_HARDWARE_COMPONENTS_IDS: { "id": ( str(primary_hardware_components_ids[0]) if primary_hardware_components_ids @@ -505,45 +508,45 @@ def jira_ticket(request_data): ) -def update_time_loss(jira_id: int, add_time_loss: float = 0.0) -> Response: +def update_time_lost(jira_id: int, add_time_lost: float = 0.0) -> Response: """Connect to the Rubin Observatory JIRA Cloud REST API to - update a jira ticket's specific field + update time_lost field in a given jira ticket Params ------ jira_id: int Jira ID + add_time_lost: float + time value given from comment Returns ------- Response The response and status code of the request to the JIRA API + 200 if the time_lost field was successfully updated + 400 if the time_lost field was not updated """ headers = { "Authorization": f"Basic {os.environ.get('JIRA_API_TOKEN')}", "content-type": "application/json", } - get_url = ( - f"https://{os.environ.get('JIRA_API_HOSTNAME')}/rest/api/latest/issue/{jira_id}" - ) - response = requests.get(get_url, headers=headers) - existent_time_loss = ( - response.json().get("customfield_10106", 0.0) + url = f"https://{os.environ.get('JIRA_API_HOSTNAME')}/rest/api/latest/issue/{jira_id}/" + response = requests.get(url, headers=headers) + existent_time_lost = ( + response.json().get(TIME_LOST_FIELD, 0.0) if response.status_code == 200 else 0.0 ) jira_payload = { "fields": { - "customfield_10106": float(existent_time_loss + add_time_loss), + TIME_LOST_FIELD: float(existent_time_lost + add_time_lost), }, } - put_url = f"https://{os.environ.get('JIRA_API_HOSTNAME')}/rest/api/latest/issue/{jira_id}/" - response = requests.put(put_url, json=jira_payload, headers=headers) - - if response.status_code == 200 or response.status_code == 204: + response = requests.put(url, json=jira_payload, headers=headers) + if response.status_code == 204: return Response( { - "ack": "Jira field updated", + "ack": "Jira time_lost field updated", "url": f"https://{os.environ.get('JIRA_API_HOSTNAME')}/browse/{jira_id}", }, status=200, @@ -600,8 +603,8 @@ def jira_comment(request_data): response = requests.post(url, json=jira_payload, headers=headers) if "time_lost" in request_data: - response = update_time_loss( - jira_id=jira_id, add_time_loss=request_data.get("time_lost", 0.0) + response = update_time_lost( + jira_id=jira_id, add_time_lost=request_data.get("time_lost", 0.0) ) if response.status_code == 400: return response @@ -677,8 +680,8 @@ def get_jira_obs_report(request_data): "key": issue["key"], "summary": issue["fields"]["summary"], "time_lost": ( - issue["fields"]["customfield_10106"] - if issue["fields"]["customfield_10106"] is not None + issue["fields"][TIME_LOST_FIELD] + if issue["fields"][TIME_LOST_FIELD] is not None else 0.0 ), "reporter": issue["fields"]["creator"]["displayName"],