Skip to content

Instantly share code, notes, and snippets.

@michaelkuty
Created October 5, 2013 20:23
Show Gist options
  • Save michaelkuty/6845607 to your computer and use it in GitHub Desktop.
Save michaelkuty/6845607 to your computer and use it in GitHub Desktop.
simple add file to exist issue in redmine
import requests
import json
url = 'http://jachym-redmine.samsung.majklk.cz'
auth = {'key':'008e507cf5bffac1f5df822d8cb3465170813b09'}
#filename relative path
#issue=issue_id = String !!
#item {"name":"test.jpeg", "description":"","content-Type":"image/png"path:"/srv/jachym/bin/"}
#project_id = String!!
def add_file(item, issue_id, project_id, notes=None):
if item != None and issue_id != None and project_id != None:
try:
files = {'file': open("%s%s"% (item.get("path", None), item.get("name", None)))}
except Exception, e:
print e
else:
headers = {"Content-Type": "application/octet-stream" }
file_upload = requests.post("%s%s"% (url, "/uploads.json"), files=files, params=auth, headers=headers)
print "File upload status: %s"% file_upload.status_code
upload = file_upload.json()
if upload != None:
upload["upload"]["filename"] = item.get("name",None)
upload["upload"]["description"] = item.get("description",None)
upload["upload"]["content-Type"] = item.get("content-Type",None)
headers_json = {'content-type': 'application/json'}
if file_upload.status_code == 201:
if issue_id.isdigit() and project_id.isdigit():
if notes == None:
issue = {"issue": {"project_id":project_id,"id":issue_id, "uploads": upload}}
else:
issue = {"issue": {"project_id":project_id,"id":issue_id, "uploads": upload, "notes":notes}}
update_url = "%s%s%s%s"% (url, "/issues/",issue_id,".json")
issue_post = requests.put(update_url, data=json.dumps(issue), params=auth, headers=headers_json)
else: print "missing issue id"
if issue_post.status_code == 200:
print "issue UPDATE OK"
else:
issue_post.status_code
else: print "file upload crash"
#finally: print "Goodbye"
add_file({"name":"test.txt", "description":"test","content-Type":"text/plan", "path": "/srv/jachym/bin/"},"2","1")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment