Adding an external file to JIRA
Tags: automation, jira, python, restful
Published: 2018-09-03 14:29:25

---
title: Adding an external file to JIRA
published: true
tags: automation,JIRA,python,RESTful
canonical_url: 
---
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  
> urllib3.disable\_warnings()  
> 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"  
>     try:  
>         os.remove(outputFile)  
>     except OSError:  
>         pass  
>     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'  
>             exit()  
>     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 check\_file.search(file):  
>             for i,line in enumerate(open(file)):  
>                 #if re.finditer(key,line):  
>                 match = re.search(key,line)  
>                 if match:  
>                     print \>\> f, "Found in %s \n %s" % (file, line)  
>                 else:  
>                     print \>\> f, "No entries that matched keyword %s" % fileKey  
>         else:  
>             print \>\> f, "No files that matched %s" % fileKey  
>     f.close()  
>     return outputFile  
>   
> def addSqlTraceToJira(location,ticket):  
>     """  
>     Add a SQL Trace file to the Jira Ticket  
>     :param location:  
>     :param ticket:  
>     :return:  
>     """  
>     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 check\_file.search(file) and check\_ticket.search(file):  
>             attachFileToJira(file,ticket)  
>         else:  
>             print "No files found with %s" % ticket  
> def attachFileToJira(logfile,ticket):  
>     """  
>     Attach the output file to the Jira ticket  
>     :param logfile:  
>     :param ticket:  
>     :return:  
>     """  
>     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 = requests.post(url,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:  
>     :return:  
>     """  
>     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 = requests.post(url,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\_\_))  
>     else:  
>         # 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  
>         #TIMESTART = sys.argv[3]  
>         #timeend  
>         #TIMEEND = sys.argv[4]  
>     # logfile = openFile(LOCATION,SEARCHKEY,FILETYPE,FILEKEY)  
>     # attachFileToJira(logfile,TICKET)  
>     # addCommentToJira(SEARCHKEY,TICKET)  
>     addSqlTraceToJira(LOCATION,TICKET)  
> if \_\_name\_\_ == '\_\_main\_\_':  
>     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.