Forums

Articles
Create
cancel
Showing results for 
Search instead for 
Did you mean: 

REST API - Add an attachment during transition

idanieri September 10, 2020

Hi. I was successfull to transition my issues from different states but I'm facing problems to transition them when it validates if I have uploaded an attachment.

I tried uploading the file before transition it but didn't work.

I did my research but couldn't figure out if is this possible to do, and if it is, how. Plese help!

from jira import JIRA
import requests
from requests.auth import HTTPBasicAuth

attachLocation = "C:\\Users\\ivan\\attachments\\"
attachFile = "text.txt"
attachmentURL = attachLocation+attachFile

parentCH = "CH-10101"

def
upload_attachment (ch_issue_key, attachment, fname):
with open(attachment, 'rb') as attachment:
url = "https://mycompany.jira.com/rest/api/2/issue/" + ch_issue_key + "/attachments"
file = {"file": (fname, attachment, "application/octet-stream")}
auth = HTTPBasicAuth('ivan', 'xxxxxxxxxxxxxxx')
headers = {
"content-type": None,
"X-Atlassian-Token": "no-check"
}
r = requests.post(
url,
auth=auth,
files=file,
headers=headers
)
print(" status_code:", r.status_code)
print("\n ",r.text)
jira = JIRA(
server="https://mycompany.jira.com/",
basic_auth=('ivan','xxxxxxxxxxxxxxx')
)

issue_parent_ch = jira.issue(parentCH)
print(issue_parent_ch.fields.status) # This prints the issue status before transitioning it
status = str(issue_parent_ch.fields.status)

if status == "Completed":
upload_attachment(issue_parent_ch.key, attachmentURL, attachFile)
evidence = { "customfield_24678": "Closing issue" }
transition = 111
jira.transition_issue(issue_parent_ch.key, transition, fields=evidence)
print(issue_parent_ch.key + ": Moving from \"Complete\" to \"Start review\"")

 

1 answer

0 votes
Prince Nyeche
Rising Star
Rising Star
Rising Stars are recognized for providing high-quality answers to other users. Rising Stars receive a certificate of achievement and are on the path to becoming Community Leaders.
September 10, 2020

Hi @idanieri 

Is this correct ...

fields=evidencia

shouldn't it be evidence? basically the flow should work like that, upload the image then transition or transition then upload; either way, it seems like a valid step to do. a user can upload an image then transition the issue or vice versa. What error messages are you getting or is there none? 

idanieri September 11, 2020

You're right, my code is in spanish and I forget to translate that part when I posted it here, thanks for the observation (I edited the post and is OK now).

As you said the flow looks good but when I run this script I get the following out:

"Field Attachment is required"

C:\>python cerrar_ch.py

Completed || This is the issue status before transitioning it
status_code: 200 || This is the status when I upload the file

[{"self":"https://mycompany.jira.com/rest/api/2/attachment/732032","id":"732032","filename":"new6.txt","author":{"self":"https://mycompany.jira.com/
rest/api/2/user?accountId=xxxxxxxxxxxxxxx","accountId":"xxxxxxxxxxxxxxx","emailAddress":"ivan","avatarUrls":{"48x4
8":"https://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/xxxxxxxxxxxxxxx/0fbc1977-422f-4e2b-86ec-4c1518a5801c/48","24x24":"http
s://avatar-management--avatars.us-west-2.prod.public.atl-paas.net/xxxxxxxxxxxxxxx/0fbc1977-422f-4e2b-86ec-4c1518a5801c/24","16x16":"https://avat
ar-management--avatars.us-west-2.prod.public.atl-paas.net/xxxxxxxxxxxxxxx/0fbc1977-422f-4e2b-86ec-4c1518a5801c/16","32x32":"https://avatar-manag
ement--avatars.us-west-2.prod.public.atl-paas.net/xxxxxxxxxxxxxxx/0fbc1977-422f-4e2b-86ec-4c1518a5801c/32"},"displayName":"Ivan Danieri","active
":true,"timeZone":"America/Argentina/Buenos_Aires","accountType":"atlassian"},"created":"2020-09-11T10:30:20.784-0300","size":1,"mimeType":"text/plain","
content":"https://mycompany.jira.com/secure/attachment/732032/new6.txt"}]
Traceback (most recent call last):
File "cerrar_ch.py", line 116, in <module>
jira.transition_issue(issue_parent_ch.key, transition, fields=evidencia)
File "C:\Users\ivan\AppData\Local\Programs\Python\Python38\lib\site-packages\jira\client.py", line 126, in wrapper
result = func(*arg_list, **kwargs)
File "C:\Users\ivan\AppData\Local\Programs\Python\Python38\lib\site-packages\jira\client.py", line 1577, in transition_issue
r = self._session.post(
File "C:\Users\ivan\AppData\Local\Programs\Python\Python38\lib\site-packages\jira\resilientsession.py", line 154, in post
return self.__verb('POST', url, **kwargs)
File "C:\Users\ivan\AppData\Local\Programs\Python\Python38\lib\site-packages\jira\resilientsession.py", line 147, in __verb
raise_on_error(response, verb=verb, **kwargs)
File "C:\Users\ivan\AppData\Local\Programs\Python\Python38\lib\site-packages\jira\resilientsession.py", line 56, in raise_on_error
raise JIRAError(
jira.exceptions.JIRAError: JiraError HTTP 400 url: https://mycompany.jira.com/rest/api/2/issue/CH-84978/transitions
text: Field Attachment is required.

response headers = {'Server': 'AtlassianProxy/1.15.8.1', 'Cache-Control': 'no-cache, no-store, no-transform', 'Content-Type': 'application/json;c
harset=UTF-8', 'Strict-Transport-Security': 'max-age=315360000; includeSubDomains; preload', 'Date': 'Fri, 11 Sep 2020 13:30:22 GMT', 'ATL-TraceId': 'f1f9c23ebedf8a53', 'X-AREQUESTID': '465b3cc7-3958-4daf-8555-c6e346420208', 'X-AACCOUNTID': 'xxxxxxxxxxxxxxx', 'X-XSS-Protection': '1; mode=block',
'Transfer-Encoding': 'chunked', 'Timing-Allow-Origin': '*', 'X-Content-Type-Options': 'nosniff', 'Micros-Issuer': 'micros/edge-authenticator', 'Connection': 'close', 'Expect-CT': 'report-uri="https://web-security-reports.services.atlassian.com/expect-ct-report/global-proxy", enforce, max-age=86400'}
response text = {"errorMessages":["Field Attachment is required."],"errors":{}}

Maybe there is a way to upload the file during transition and not before as I'm dooing it in my code.

Like Fabio Pepe likes this

Suggest an answer

Log in or Sign up to answer
DEPLOYMENT TYPE
CLOUD
PRODUCT PLAN
STANDARD
TAGS
AUG Leaders

Atlassian Community Events