From f4a6d797f025a17687855ecf42066448858e7c8c Mon Sep 17 00:00:00 2001 From: Valerie Becker Date: Thu, 15 Aug 2024 21:25:03 -0700 Subject: [PATCH] Switched approach based on available JIRA API --- manager/manager/utils.py | 51 +++++++++++++++++++++++++++------------- 1 file changed, 35 insertions(+), 16 deletions(-) diff --git a/manager/manager/utils.py b/manager/manager/utils.py index ecdc45d9..1661bde7 100644 --- a/manager/manager/utils.py +++ b/manager/manager/utils.py @@ -505,9 +505,9 @@ def jira_ticket(request_data): ) -def get_time_lost_from_jira(jira_id): +def update_time_loss(jira_id: int, add_time_loss: float = 0.0) -> Response: """Connect to the Rubin Observatory JIRA Cloud REST API to - retreive a jira ticket's time_lost attribute + update a jira ticket's specific field Params ------ @@ -516,19 +516,40 @@ def get_time_lost_from_jira(jira_id): Returns ------- - Float - The value of the time lost on the existing jira ticket, or 0.0 + Response + define response here """ 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}" - response = requests.get(url, headers=headers) - if response.status_code == 200: - return response.get("customfield_10106", 0.0) - else: - return 0.0 + 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) if response.status_code == 200 else 0.0 + jira_payload = { + "fields": { + "customfield_10106": float(existent_time_loss + add_time_loss), + }, + } + 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: + return Response( + { + "ack": "Jira field updated", + "url": f"https://{os.environ.get('JIRA_API_HOSTNAME')}/browse/{jira_id}", + }, + status=200, + ) + return Response( + { + "ack": "Jira field could not be updated", + }, + status=400, + ) def jira_comment(request_data): @@ -564,12 +585,6 @@ def jira_comment(request_data): jira_payload = { "body": get_jira_description(request_data), } - if 'time_lost' in request_data: - jira_payload["feilds"]["customfield_10106"] = float( - get_time_lost_from_jira(jira_id=jira_id) - + request_data.get("time_lost", 0.0) - ) - except Exception as e: return Response({"ack": f"Error creating jira payload: {e}"}, status=400) @@ -579,6 +594,10 @@ def jira_comment(request_data): } 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: + update_time_loss(jira_id=jira_id, add_time_loss=request_data.get("time_lost", 0.0)) + if response.status_code == 201: return Response( {