I’m working on a Python script to create JIRA tickets. I’ve got the basic creation part working, but I’m stuck on how to add watchers to the newly created issues. Here’s a simplified version of my current code:
Can anyone help me figure out how to add a watcher to the ticket after it’s created? I’ve looked through the JIRA Python API docs but couldn’t find a clear example. Thanks for any help!
I’ve dealt with this exact situation in my work. The JIRA Python API can be a bit tricky when it comes to watchers. Here’s what worked for me:
After creating the ticket, you can use the add_watcher() method, but be aware it only adds one watcher at a time. I found it helpful to create a separate function for adding multiple watchers:
def add_watchers(jira_client, issue, watcher_list):
for watcher in watcher_list:
try:
jira_client.add_watcher(issue, watcher)
except Exception as e:
print(f'Failed to add watcher {watcher}: {str(e)}')
Then you can call this function after creating the ticket:
I’ve encountered this issue before when working with the JIRA Python API. To add watchers, you can use the add_watcher() method as mentioned, but it’s worth noting that you need appropriate permissions to add watchers to issues. Here’s how you could modify your existing function to include watcher addition:
def make_jira_ticket(jira_client, title, content, ticket_type, watchers=None):
# Your existing code here
new_ticket = jira_client.create_issue(fields=ticket_info)
if watchers:
for watcher in watchers:
jira_client.add_watcher(new_ticket, watcher)
return new_ticket
# Usage
watchers_list = ['user1', 'user2']
ticket = make_jira_ticket(jira, 'Test Issue', 'This is a test', 'Task', watchers=watchers_list)
This approach allows you to add multiple watchers efficiently. Remember to handle potential exceptions, as adding watchers can sometimes fail due to permission issues or invalid usernames.