From a4fd92f679a997b319fb05eb387c005292e78bdd Mon Sep 17 00:00:00 2001 From: Sebastian Aranda Sanchez Date: Tue, 27 Aug 2024 14:36:06 -0400 Subject: [PATCH 1/3] Fix issue with adding float and str on `update_time_lost` method Also adjust method to read time lost parameter from the fields attribute in the Jira response --- manager/manager/utils.py | 48 ++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/manager/manager/utils.py b/manager/manager/utils.py index 3d0147c6..0d47a4d8 100644 --- a/manager/manager/utils.py +++ b/manager/manager/utils.py @@ -532,25 +532,24 @@ def update_time_lost(jira_id: int, add_time_lost: float = 0.0) -> Response: } 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": { - TIME_LOST_FIELD: float(existent_time_lost + add_time_lost), - }, - } - response = requests.put(url, json=jira_payload, headers=headers) - if response.status_code == 204: - return Response( - { - "ack": "Jira time_lost field updated", - "url": f"https://{os.environ.get('JIRA_API_HOSTNAME')}/browse/{jira_id}", + + if response.status_code == 200: + jira_ticket_fields = response.json().get("fields", {}) + existent_time_lost = float(jira_ticket_fields.get(TIME_LOST_FIELD, 0.0)) + jira_payload = { + "fields": { + TIME_LOST_FIELD: existent_time_lost + add_time_lost, }, - status=200, - ) + } + response = requests.put(url, json=jira_payload, headers=headers) + if response.status_code == 204: + return Response( + { + "ack": "Jira time_lost field updated", + "url": f"https://{os.environ.get('JIRA_API_HOSTNAME')}/browse/{jira_id}", + }, + status=200, + ) return Response( { "ack": "Jira time_lost field could not be updated", @@ -595,18 +594,19 @@ def jira_comment(request_data): except Exception as e: return Response({"ack": f"Error creating jira payload: {e}"}, status=400) + if "time_lost" in request_data: + timelost_response = update_time_lost( + jira_id=jira_id, add_time_lost=float(request_data.get("time_lost", 0.0)) + ) + if timelost_response.status_code != 200: + return timelost_response + headers = { "Authorization": f"Basic {os.environ.get('JIRA_API_TOKEN')}", "content-type": "application/json", } url = f"https://{os.environ.get('JIRA_API_HOSTNAME')}/rest/api/latest/issue/{jira_id}/comment" response = requests.post(url, json=jira_payload, headers=headers) - if "time_lost" in request_data: - timelost_response = update_time_lost( - jira_id=jira_id, add_time_lost=request_data.get("time_lost", 0.0) - ) - if timelost_response.status_code != 200: - return timelost_response if response.status_code == 201: return Response( From d3fe2386eb03888aefb44783732dfc31f7655bf9 Mon Sep 17 00:00:00 2001 From: Sebastian Aranda Sanchez Date: Tue, 27 Aug 2024 14:57:48 -0400 Subject: [PATCH 2/3] Extend and make `update_time_lost` tests more robust --- manager/api/tests/test_jira.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/manager/api/tests/test_jira.py b/manager/api/tests/test_jira.py index 51d827d1..503830e1 100644 --- a/manager/api/tests/test_jira.py +++ b/manager/api/tests/test_jira.py @@ -23,6 +23,7 @@ import random from unittest.mock import patch +import pytest import requests import rest_framework from django.test import TestCase, override_settings @@ -322,15 +323,16 @@ def test_update_time_lost(self): """Test call to update_time_lost and verify field was updated""" mock_jira_patcher = patch("requests.get") mock_jira_get = mock_jira_patcher.start() - response = requests.Response() - response.status_code = 200 - response.json = lambda: {TIME_LOST_FIELD: 13.6} - mock_jira_get.return_value = response + response_get = requests.Response() + response_get.status_code = 200 + response_get.json = lambda: {"fields": {TIME_LOST_FIELD: 13.6}} + mock_jira_get.return_value = response_get put_patcher = patch("requests.put") mock_jira_put = put_patcher.start() - response.status_code = 204 - mock_jira_put.return_value = response + response_put = requests.Response() + response_put.status_code = 204 + mock_jira_put.return_value = response_put # call update time lost jira_response = update_time_lost(1, 3.4) @@ -341,9 +343,12 @@ def test_update_time_lost(self): assert jira_response.status_code == 200 assert jira_response.data["ack"] == "Jira time_lost field updated" - response.status_code = 400 - mock_jira_put.return_value = response + # call update time lost with invalid time_lost + with pytest.raises(TypeError): + update_time_lost(93827, "5.25") + # call update time lost with fail put response + response_put.status_code = 400 jira_response = update_time_lost(12, 97.01) assert jira_response.status_code == 400 assert jira_response.data["ack"] == "Jira time_lost field could not be updated" From bb6564da8f5fe224ad46b74bf199b452b9070a50 Mon Sep 17 00:00:00 2001 From: Sebastian Aranda Sanchez Date: Tue, 27 Aug 2024 14:59:35 -0400 Subject: [PATCH 3/3] Update CHANGELOG --- CHANGELOG.rst | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index cc9a9f88..d145199f 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -2,10 +2,15 @@ Version History =============== +v7.0.3 +------ + +* Fix issue with update_time_lost method ``_ + v7.0.2 ------ -* Add accumulation of time lost in Jira comments +* Add accumulation of time lost in Jira comments ``_ v7.0.1 ------