Created
November 5, 2012 10:10
-
-
Save jerrykan/4016451 to your computer and use it in GitHub Desktop.
Example script to create a roundup issue
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
""" | |
This script will create a new issue with a file "attachment" in a demo roundup | |
instance. This should provide a basic example of how to programmatically create | |
an issue in your own tracker. | |
""" | |
import roundup.instance | |
from roundup.exceptions import Reject | |
from roundup.date import Date | |
TRACKER_HOME = '<path to roundup tracker instance>' | |
def create_issue(tracker, username, issue, attachment=None): | |
db = tracker.open('admin') # NOTE: you may want to change this | |
# Lookup the client | |
try: | |
user_id = db.user.lookup(username) | |
except KeyError, m: | |
db.rollback() | |
db.close() | |
return "Could not find user '%s'" % username | |
# Create a file (if there is an "attachment") | |
file_id = None | |
if attachment is not None: | |
props = { | |
'name': attachment['name'], | |
'type': attachment['type'], | |
'content': attachment['content'], | |
} | |
try: | |
file_id = db.file.create(**props) | |
except (Reject, TypeError) as e: | |
db.rollback() | |
db.close() | |
return "Could not create file: %s" % str(e) | |
# Create the issue message | |
try: | |
props = { | |
'author': user_id, | |
'date': Date('.'), | |
'content': issue['content'] | |
} | |
if file_id: | |
props['files'] = [file_id] | |
msg_id = db.msg.create(**props) | |
except (Reject, TypeError) as e: | |
db.rollback() | |
db.close() | |
return "Could not create msg: %s" % str(e) | |
# Create the issue | |
props = { | |
'title': issue['title'], | |
'messages': [msg_id], | |
} | |
if file_id: | |
props['files'] = [file_id] | |
try: | |
msg_id = db.issue.create(**props) | |
except (Reject, TypeError) as e: | |
db.rollback() | |
db.close() | |
return "Could not create issue: %s" % str(e) | |
db.commit() | |
db.close() | |
return None | |
issue = { | |
'title': 'New Issue', | |
'content': """\ | |
This is a new issue that has a file attached to it. | |
Have fun. | |
""" | |
} | |
attachment = { | |
'name': 'testfile.txt', | |
'type': 'text/plain', | |
'content': """\ | |
This is the data contents of the attached file. | |
""" | |
} | |
# Create the issue | |
tracker = roundup.instance.open(TRACKER_HOME) | |
create_issue(tracker, 'demo', issue, attachment) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment