|
| 1 | +from django.http import JsonResponse |
| 2 | +from django import forms |
| 3 | +import requests |
| 4 | +import json |
| 5 | +import os |
| 6 | + |
| 7 | +# XXX: remove csrf_exempt usage before production |
| 8 | +from django.views.decorators.csrf import csrf_exempt |
| 9 | + |
| 10 | + |
| 11 | +@csrf_exempt |
| 12 | +def create_new_feedback(request: str) -> JsonResponse: |
| 13 | + """ |
| 14 | + Create a new feedback request in Jira Service Desk. |
| 15 | + """ |
| 16 | + token: str = os.environ.get("JIRA_API_KEY") |
| 17 | + |
| 18 | + data: dict[str, str] = json.loads(request.body) |
| 19 | + name: str = data["name"] |
| 20 | + email: str = data["email"] |
| 21 | + message: str = data["message"] |
| 22 | + |
| 23 | + url: str = "https://balancer.atlassian.net/rest/servicedeskapi/request" |
| 24 | + |
| 25 | + headers: dict[str, str] = { |
| 26 | + "Accept": "application/json", |
| 27 | + "Content-Type": "application/json", |
| 28 | + "Authorization": f"Basic {token}", |
| 29 | + } |
| 30 | + |
| 31 | + payload: str = json.dumps( |
| 32 | + { |
| 33 | + "requestFieldValues": { |
| 34 | + "summary": f"{name} - Feedback", |
| 35 | + "customfield_10061": email, |
| 36 | + "description": message, |
| 37 | + }, |
| 38 | + "requestTypeId": "33", |
| 39 | + "serviceDeskId": "2", |
| 40 | + } |
| 41 | + ) |
| 42 | + |
| 43 | + response: requests.Response = requests.request( |
| 44 | + "POST", url, data=payload, headers=headers |
| 45 | + ) |
| 46 | + match response.status_code: |
| 47 | + case 201: |
| 48 | + response_body: dict[str, str] = json.loads(response.text) |
| 49 | + issue_key: str = response_body["issueKey"] |
| 50 | + return JsonResponse( |
| 51 | + {"status": 201, "message": "Feedback submitted", "issueKey": issue_key} |
| 52 | + ) |
| 53 | + case 400: |
| 54 | + return JsonResponse({"status": 400, "message": "Invalid request"}) |
| 55 | + case 401 | 403: |
| 56 | + return JsonResponse({"status": 401, "message": "Unauthorized request"}) |
| 57 | + case _: |
| 58 | + return JsonResponse({"status": 500, "message": "Internal server error"}) |
| 59 | + |
| 60 | + |
| 61 | +class UploadAttachmentForm(forms.Form): |
| 62 | + issueKey: forms.CharField = forms.CharField(max_length=50) |
| 63 | + attachment = forms.FileField() |
| 64 | + |
| 65 | + |
| 66 | +@csrf_exempt |
| 67 | +def upload_servicedesk_attachment(request: str) -> JsonResponse: |
| 68 | + """ |
| 69 | + Upload file to temporary files in Jira Service Desk. |
| 70 | + """ |
| 71 | + token: str = os.environ.get("JIRA_API_KEY") |
| 72 | + form: UploadAttachmentForm = UploadAttachmentForm(request.POST, request.FILES) |
| 73 | + if form.is_valid(): |
| 74 | + url: str = f"https://balancer.atlassian.net/rest/servicedeskapi/servicedesk/2/attachTemporaryFile" |
| 75 | + |
| 76 | + headers: dict[str, str] = { |
| 77 | + "Accept": "application/json", |
| 78 | + "X-Atlassian-Token": "no-check", |
| 79 | + "Authorization": f"Basic {token}", |
| 80 | + } |
| 81 | + |
| 82 | + response: requests.Response = requests.request( |
| 83 | + "POST", url, files={"file": request.FILES["attachment"]}, headers=headers |
| 84 | + ) |
| 85 | + match response.status_code: |
| 86 | + case 201: |
| 87 | + response_body: dict[str, str] = json.loads(response.text) |
| 88 | + temp_attachment_id: str = response_body["temporaryAttachments"][0][ |
| 89 | + "temporaryAttachmentId" |
| 90 | + ] |
| 91 | + issue_key: str = request.POST.get("issueKey") |
| 92 | + return JsonResponse( |
| 93 | + { |
| 94 | + "status": 200, |
| 95 | + "message": "Attachment uploaded to temporary files", |
| 96 | + "tempAttachmentId": temp_attachment_id, |
| 97 | + "issueKey": issue_key, |
| 98 | + } |
| 99 | + ) |
| 100 | + case 400: |
| 101 | + return JsonResponse({"status": 400, "message": "Invalid request"}) |
| 102 | + case 401 | 403: |
| 103 | + return JsonResponse({"status": 401, "message": "Unauthorized request"}) |
| 104 | + case _: |
| 105 | + return JsonResponse({"status": 500, "message": "Internal server error"}) |
| 106 | + return JsonResponse({"status": 400, "message": "Invalid form object"}) |
| 107 | + |
| 108 | + |
| 109 | +@csrf_exempt |
| 110 | +def attach_feedback_attachment(request: str) -> JsonResponse: |
| 111 | + """ |
| 112 | + Attach a temporary file to a Jira Service Desk issue. |
| 113 | + """ |
| 114 | + token: str = os.environ.get("JIRA_API_KEY") |
| 115 | + data: dict[str, str] = json.loads(request.body) |
| 116 | + issue_key: str = data["issueKey"] |
| 117 | + temp_attachment_id: str = data["tempAttachmentId"] |
| 118 | + print(issue_key) |
| 119 | + |
| 120 | + url: str = f"https://balancer.atlassian.net/rest/servicedeskapi/request/{issue_key}/attachment" |
| 121 | + |
| 122 | + headers = { |
| 123 | + "Accept": "application/json", |
| 124 | + "Content-Type": "application/json", |
| 125 | + "Authorization": f"Basic {token}", |
| 126 | + } |
| 127 | + |
| 128 | + payload: str = json.dumps( |
| 129 | + {"public": True, "temporaryAttachmentIds": [temp_attachment_id]} |
| 130 | + ) |
| 131 | + |
| 132 | + response: requests.Response = requests.request( |
| 133 | + "POST", url, data=payload, headers=headers |
| 134 | + ) |
| 135 | + match response.status_code: |
| 136 | + case 201: |
| 137 | + return JsonResponse({"status": 201, "message": f"File attached to issue {issue_key}"}) |
| 138 | + case 400: |
| 139 | + return JsonResponse({"status": 400, "message": "Invalid request"}) |
| 140 | + case 401 | 403: |
| 141 | + return JsonResponse({"status": 401, "message": "Unauthorized request"}) |
| 142 | + case _: |
| 143 | + return JsonResponse({"status": 500, "message": "Internal server error"}) |
| 144 | + |
| 145 | + |
| 146 | +# These functions are used to get Jira data, but shouldn't be enabled in production. |
| 147 | +# Keep these commented out unless in use. |
| 148 | + |
| 149 | +# @csrf_exempt |
| 150 | +# def get_jira_servicedesk_list(request: str) -> JsonResponse: |
| 151 | +# """Get jira service desk list.""" |
| 152 | +# url = "https://balancer.atlassian.net/rest/servicedeskapi/servicedesk" |
| 153 | +# headers = { |
| 154 | +# "Accept": "application/json", |
| 155 | +# "Authorization": "Basic ", |
| 156 | +# } |
| 157 | + |
| 158 | +# response = requests.request("GET", url, headers=headers) |
| 159 | +# print( |
| 160 | +# json.dumps( |
| 161 | +# json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ") |
| 162 | +# ) |
| 163 | +# ) |
| 164 | +# return JsonResponse({"message": "complete"}) |
| 165 | + |
| 166 | + |
| 167 | +# @csrf_exempt |
| 168 | +# def get_jira_request_types(request: str) -> JsonResponse: |
| 169 | +# url = "https://balancer.atlassian.net/rest/servicedeskapi/servicedesk/2/requesttype" |
| 170 | + |
| 171 | +# headers = {"Accept": "application/json", "Authorization": "Basic "} |
| 172 | + |
| 173 | +# response = requests.request("GET", url, headers=headers) |
| 174 | + |
| 175 | +# print( |
| 176 | +# json.dumps( |
| 177 | +# json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": ") |
| 178 | +# ) |
| 179 | +# ) |
| 180 | +# return JsonResponse({"message": "complete"}) |
| 181 | + |
| 182 | +# @csrf_exempt |
| 183 | +# def get_required_request_type_fields(request: str) -> JsonResponse: |
| 184 | +# url = "https://balancer.atlassian.net/rest/servicedeskapi/servicedesk/2/requesttype/33/field" |
| 185 | + |
| 186 | +# headers = { |
| 187 | +# "Accept": "application/json", |
| 188 | +# "Content-Type": "application/json", |
| 189 | +# "Authorization": "Basic " |
| 190 | +# } |
| 191 | + |
| 192 | +# response = requests.request("GET", url, headers=headers) |
| 193 | +# print(json.dumps(json.loads(response.text), sort_keys=True, indent=4, separators=(",", ": "))) |
| 194 | +# return JsonResponse({"message": "complete"}) |
0 commit comments