DEV Community


Posted on • Originally published at on

Adding an external file to JIRA

This is something I worked on a few years ago. As a part of my companies personal project time, a yearly occurrence they called "Off The Grid", I was part of a team project where the goal was to add external files to a JIRA ticket.

JIRA is used to track EVERYTHING in the company. It was the first time I used it, and I have used it in a few other places since.

This is the python script I hobbled together.

!/usr/bin/env python

from stat import S_ISREG, ST_CTIME, ST_MODE

import os,sys,re,requests

from requests.auth import HTTPBasicAuth

import urllib3


def openFile(location,key,filetype,fileKey):


Tests that the file location exists and searches through each file for a specific keyword

:param location:

:param key:

:param filetype:

:param fileKey

:return: outputFile:


outputFile = filetype + "output.txt"



except OSError:


f = open(outputFile,'w')

print >> f, "Looking in %s" % location

for dirpath, dirnames, files in os.walk(location):

if not files:

print dirpath, 'is empty'


entriesf = (os.path.join(location, fn) for fn in os.listdir(location))

entries = ((os.stat(path), path) for path in entriesf)

entries = ((stat[ST_CTIME], path)

for stat, path in entries if S_ISREG(stat[ST_MODE]))

# for cdate, path in sorted(entries):

# print time.ctime(cdate), os.path.basename(path)

check_file = re.compile(fileKey)

for file in entriesf:

if os.path.isfile(file): # and

for i,line in enumerate(open(file)):

#if re.finditer(key,line):

match =,line)

if match:

print >> f, "Found in %s \n %s" % (file, line)


print >> f, "No entries that matched keyword %s" % fileKey


print >> f, "No files that matched %s" % fileKey


return outputFile

def addSqlTraceToJira(location,ticket):


Add a SQL Trace file to the Jira Ticket

:param location:

:param ticket:



entriesf = (os.path.join(location, fn) for fn in os.listdir(location))

check_file = re.compile('trc$')

check_ticket = re.compile(ticket)

for file in entriesf:

if os.path.isfile(file) and and



print "No files found with %s" % ticket

def attachFileToJira(logfile,ticket):


Attach the output file to the Jira ticket

:param logfile:

:param ticket:



url = "https://jira-test.fakedomain.comnet/rest/api/2/issue/%s/attachments" % ticket

headers = {'X-Atlassian-Token':'no-check'}

files = {'file': open(logfile, 'r')}

auth = HTTPBasicAuth('username', 'password')

# print "Sending %s to ticket %s" % (logfile,url)

r =,headers=headers,files=files,auth=auth,verify=False)

print 'Jira file attach sent me a %s with \n %s' % (r.status_code,r.text)

def addCommentToJira(keyword,ticket):


This attaches a comment about the search parameter that was added to the ticket

:param keyword:

:param tickiet:



url = "https://jira-test.fakedomain.comnet/rest/api/2/issue/%s/comment" % ticket

headers = {'X-Atlassian-Token': 'no-check'}

auth = HTTPBasicAuth('username', 'password')

comment = "Just added an attached log file about the results for the %s search." % keyword

json = {"body": comment}

r =,headers=headers,json=json,auth=auth,verify=False)

print 'Jira adding a comment sent me a %s with \n %s' % (r.status_code, r.text)

def main():

if len(sys.argv) < 5:

from os.path import basename

print ('Usage: %s location key type ticket' % basename(__file__))


# fileLocation

LOCATION = sys.argv[1]

# searchKey

SEARCHKEY = sys.argv[2]

# type

FILETYPE = sys.argv[3]

# Jira Ticket

TICKET = sys.argv[4]

# File Keyword

FILEKEY = sys.argv[5]


#TIMESTART = sys.argv[3]


#TIMEEND = sys.argv[4]


# attachFileToJira(logfile,TICKET)

# addCommentToJira(SEARCHKEY,TICKET)


if __name__ == '__main__':


I claim some responsibility for it, mostly by hacking together a lot of python and doing a lot of TDD as I built out the functions.

Top comments (0)