Skip to content

Commit

Permalink
Updating comments and using constant for custom fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Vebop committed Aug 22, 2024
1 parent 6cd9b4a commit 907b1c8
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 32 deletions.
24 changes: 13 additions & 11 deletions manager/api/tests/test_jira.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
Expand Down Expand Up @@ -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"""
Expand All @@ -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

Expand Down
45 changes: 24 additions & 21 deletions manager/manager/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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"],
Expand Down

0 comments on commit 907b1c8

Please sign in to comment.