Alen Joses
Alen Joses

Reputation: 1

How to upload a file in Jira Issue using python?

I need to upload a file to jira issue using python and attach the file in a particular comment.

jira.add_attachment(issue=issue, attachment='inputFile.xlsx')

# read and upload a file (note binary mode for opening, it's important):
with open('inputFile.xlsx', 'rb') as f:
    jira.add_attachment(issue=issue, attachment=f)

# attach file from memory (you can skip IO operations). In this case you MUST provide `filename`.

attachment = StringIO()
attachment.write(data)
jira.add_attachment(issue=issue, attachment=attachment, filename='content.txt')

I took this code from the jira documentation but I am not sure how the attachment.write(data) statement is working.

How is the data variable used here?

Upvotes: 0

Views: 462

Answers (1)

dskrypa
dskrypa

Reputation: 1108

The attachment.write(data) part of the example assumes you have a variable called data that points to a string. That part of the example is intended to show how you would upload an attachment when you don't already have a file containing the data that you want to attach to the issue. It would be given the name content.txt in the issue, but you could pick any value for the attachment file name.

StringIO is a file-like object that you would interact with as if you were interacting with a file that you opened like this:

file_name = 'content.txt'
data = 'foo'

with open(file_name, 'w') as f:  # open for writing in text mode
    f.write(data)

StringIO (and BytesIO, the binary equivalent) objects exist only in memory, but many functions that accept file-like objects will accept them as if they were files that you opened via open(...).

Each of those jira.add_attachment(...) calls are independent of each other - you only need to use one of those approaches at a time.

Upvotes: 1

Related Questions