Skip to content

Commit

Permalink
Switched approach based on available JIRA API
Browse files Browse the repository at this point in the history
  • Loading branch information
Vebop committed Aug 16, 2024
1 parent ab848da commit f4a6d79
Showing 1 changed file with 35 additions and 16 deletions.
51 changes: 35 additions & 16 deletions manager/manager/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
------
Expand All @@ -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):
Expand Down Expand Up @@ -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)

Expand All @@ -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(
{
Expand Down

0 comments on commit f4a6d79

Please sign in to comment.