I’m working with the Python JIRA library to generate new issues programmatically. My current implementation successfully creates tickets with all the basic fields like summary, description, assignee, and due date.
The issue creation works perfectly, but now I need to automatically add specific users as watchers to these newly created tickets. What’s the proper way to include watchers in this workflow? Should I add them during creation or after the ticket is made?
You can’t add watchers during ticket creation - the JIRA API doesn’t support it in the create_issue call. I’ve hit this before. Just add them right after creating the ticket using add_watcher.
created_ticket = jira_client.create_issue(fields=ticket_data)
# Add watchers after creation
watcher_list = ['user1', 'user2', 'user3']
for watcher in watcher_list:
try:
jira_client.add_watcher(created_ticket.key, watcher)
except Exception as e:
print(f"Failed to add watcher {watcher}: {e}")
Don’t forget error handling - bad usernames or permission issues will break the watcher addition. Your ticket still gets created though, even if some watchers fail. You’ll also need the right permissions to modify watchers on the project.
yep, after creating the ticket is the way to go for adding watchers. I just use add_watcher right after the ticket is made. But be careful with exceptions! Usernames can be a pain sometimes, and you might run into perm issues. Still, the ticket will be fine even if some watchers don’t get added.
I handle this with a helper function that wraps both operations - you’ve got to do it after creating the ticket. The add_watcher method is pretty straightforward, but I’d batch the operations if you’re creating multiple tickets.
def create_ticket_with_watchers(jira_client, title, details, priority, watchers):
created_ticket = jira_client.create_issue(fields=ticket_data)
for username in watchers:
jira_client.add_watcher(created_ticket, username)
return created_ticket
Watch out though - if you’re adding the same people as watchers over and over, check if they’re already watching first with get_watchers(). Also make sure your service account has the right project permissions or you’ll get auth errors even with valid usernames.