I’m working on a Python script that creates JIRA tickets automatically. Everything is running smoothly except for one thing. I can’t figure out how to add a watcher to a ticket once it’s created.
Here’s a simplified version of my current code:
from jira import JIRA
def make_jira_ticket(jira_connection, title, details, ticket_type):
ticket_info = {
'project': {'key': 'MYPROJECT'},
'summary': title,
'description': details,
'issuetype': {'name': ticket_type},
'assignee': {'name': 'john.doe'},
}
new_ticket = jira_connection.create_issue(fields=ticket_info)
return new_ticket
# Need help adding a watcher here!
I’ve combed through the documentation but haven’t found a clear example. Can anyone share tips or code on how to add a watcher to a ticket right after it’s created? Thanks a lot!
I’ve used the JIRA Python library in several projects, and adding watchers can be a bit finicky. One approach that’s worked well for me is using the add_watcher() method right after creating the issue. Here’s a snippet that might help:
Just replace ‘watcher_username’ with the actual username you want to add. A word of caution though - make sure the JIRA account you’re using has the necessary permissions to add watchers. I’ve run into issues before where the script would fail silently because of permission problems.
Also, if you need to add multiple watchers, you could use a list and a simple loop:
watchers = [‘user1’, ‘user2’, ‘user3’]
for watcher in watchers:
jira_connection.add_watcher(new_ticket, watcher)
Hope this helps! Let me know if you run into any other issues.
I’ve worked extensively with the JIRA Python library, and adding watchers is indeed a bit tricky. To accomplish this, you’ll need to use the add_watcher() method after creating the issue. Here’s how you can modify your code:
Replace ‘watcher_username’ with the actual username of the person you want to add as a watcher. You can also add multiple watchers by calling the method multiple times with different usernames.
One caveat: ensure the user you’re adding as a watcher has the appropriate permissions in JIRA. Otherwise, you might encounter errors. If you need to add multiple watchers programmatically, consider using a loop or list comprehension for efficiency.