Saltar al contenido

~ / archivo / stackoverflow / 44384897

How to download JIRA attachment files with Python

StackOverflow · python-3.4, python-jira · ayudé
  1. Surendranath Reddy pregunta +1 2017-06-06 08:22

    I want to download attachment files of an issue in JIRA Python.

  2. user9833400 respuesta +6 2018-05-23 09:05

    I use jira python lib ,you can use pip install JIRA

     # -- coding: UTF-8 --
        from jira import JIRA
        import requests
        url = 'https://jira.1234.com'
        jira = JIRA(server=url, basic_auth=('admin', 'password'))
        attachment=jira.attachment(12345) #12345 is attachment_key
        image = attachment.get()
        with open("Image.png", 'wb') as f:
            f.write(image)
    
  3. Miguel Ortiz yo respuesta +2 2017-06-22 17:37

    JIRA exposes its REST services and through that and some python you can download any attachment.

    It worked for me like this (you'll need to adjust the variables):

    #!/usr/bin/python
    # miguel ortiz
    # Requests module: http://docs.python-requests.org/en/latest/
    # Documentation: <url>
    
    #----------------------------------------------------------------Modules
    import sys
    import csv, json
    import requests
    
    #----------------------------------------------------------------Variables   
    myTicket= sys.argv[1] # Your ticket: ABC-123
    user = 'miguel'     # JIRA user
    pasw = 'password' # JIRA password
    jiraURL = 'https://yourinstance.jira.com/rest/api/latest/issue/'
    fileName = 'my_attached_file' # In this case we'll be looking for a specific file in the attachments
    attachment_final_url="" # To validate if there are or not attachments
    
    
    def main() :
        print '\n\n [ You are checking ticket: ' + myTicket+ ' ]\n'
        # Request Json from JIRA API
        r = requests.get(jiraURL+myTicket, auth=(user, pasw),timeout=5)
    
        # status of the request
        rstatus = r.status_code
    
        # If the status isn't 200 we leave
        if not rstatus == 200 :
            print 'Error accesing JIRA:' + str(rstatus)
            exit()
        else:
            data = r.json()
    
        if not data['fields']['attachment'] :            
          status_attachment = 'ERROR: Nothing attached, attach a file named: ' + fileName
          attachment_final_url=""
        else:
            for i in data['fields']['attachment'] :
              if i['filename'] == fileName :
                 attachment_final_url = i['content']
                 status_attachment_name = 'OK: The desired attachment exists: ' + fileName 
                 attachment_name = False
                 attachment_amount = False
                 attachment_files = False
                 break
              else :
                attachment_files = False
                status_attachment_name = + 'ERROR: None of the files has the desired name ' 
                attachment_final_url=""
                attachment_name = True
                attachment_amount = True
                continue
    
        if attachment_final_url != "" :
            r = requests.get(attachment_final_url, auth=(user, pasw), stream=True)
            with open(fileName, "wb") as f: 
                f.write(r.content.decode('iso-8859-1').encode('utf8'))
            f.close()
        else: 
            print status_attachment
    
    if __name__ == "__main__" :
     main()
    

    If you do not understand the code I've detailed it better in my blog.

    EDIT: Be careful, in JIRA you can add many files with the same name.

← todos los hilos de StackOverflow