pyredminews
pyredminews copied to clipboard
Create issue with custom fields
It would be great to be able to create an issue with custom fields in one shot. Currently, we must create an issue then update it to fill the custom fields.
You can right now. Unfortunately, you need to know the ID of the custom field (not the name) that you want to create as Redmine needs that ID. It's not a clean interface, but it should work:
issue = project.issues.new(subject="Test from Python", description="That rabbit is dynamite!", custom_fields=[ {"id":2, "value":"carrot"}, {"id":4, "value":"Run away!"} ] )
You should be able to get the ID for the custom fields by retrieving an issue from that project (to ensure it has the correct fields) then printing the custom_fields object:
print issue.custom_fields
<Custom Fields: [{u'name': u'Steps to reproduce', u'value': u'Touch X and observe Y...', u'id': 4}, {u'name': u'Show Issue', u'value': u'1', u'id': 6}]>
OK, it works but you forgot to mention that the custom_fields is a list so :
issue = project.issues.new(subject="Test from Python", description="That rabbit is dynamite!", custom_fields=[{"id":2, "value":"carrot"},{ "id":4, "value":"Run away!"}])
Thanks for pointing that out, I updated my comment.
I haven't written anything recently that requires creating a new item. This syntax seems a bit wonky - you've got to look up any valid fields. On the other had, it's fairly direct and if any new fields are added it doesn't require library support.
I wonder if it's worth it to clean up the interface. For instance, maybe allow a library user to create a new object (Issue, in this case) then add that object to the appropriate location (Project instance to tie it to a project, or to the Redmine instance directly).
from redmine import Issue
my_new_issue = Issue()
my_new_issue.subject = "Test from Python"
my_new_issue[2] = "carrot" #much nicer way to define the custom field, IMO.
my_project.issues.add(my_new_issue)
Another approach may be to ask the Item_Manager for the new object, then you can edit and save that object. The benefit is that the Item_Manager can set the appropriate references to allow the library user to use the object's .save() method, the library could raise an error earlier if the operation isn't supported and it would require less importing. However, it feels a bit less "Pythonic" to me - but maybe I'm just being too squeamish about object factories (I really shouldn't, there's several in this code already!)
my_new_issue = my_project.issues.new()
my_new_issue.subject = "Test from Python"
my_new_issue[2] = "carrot"
my_new_issue.save()
The .new() method would check to see if any data was passed to it, and if there isn't any it would just spit back an empty object without trying to send anything to Redmine.
Both seem to be clean in my opinion
Related to this issue; two of the custom fields in an instance I'm working on are list-type. We have 'Customer' and 'Billing code' as custom fields. I'm not seeing a way to set these through the API, as I'm not sure how to get a list of valid options when creating a new issue.