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

Fix issue with update_time_lost method #277

Merged
merged 3 commits into from
Aug 27, 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
7 changes: 6 additions & 1 deletion CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,15 @@
Version History
===============

v7.0.3
------

* Fix issue with update_time_lost method `<https://github.com/lsst-ts/LOVE-manager/pull/277>`_

v7.0.2
------

* Add accumulation of time lost in Jira comments
* Add accumulation of time lost in Jira comments `<https://github.com/lsst-ts/LOVE-manager/pull/275>`_

v7.0.1
------
Expand Down
21 changes: 13 additions & 8 deletions manager/api/tests/test_jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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"
Expand Down
48 changes: 24 additions & 24 deletions manager/manager/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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(
Expand Down
Loading