Adding watchers to JIRA issues using Python API

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:

from jira import JIRA

def make_jira_ticket(jira_client, title, content, ticket_type):
    ticket_info = {
        'project': {'key': 'MYPROJECT'},
        'summary': title,
        'description': content,
        'issuetype': {'name': ticket_type},
        'assignee': {'name': 'default_assignee'},
    }

    new_ticket = jira_client.create_issue(fields=ticket_info)
    return new_ticket

# Usage
jira = JIRA(server='https://myjira.example.com', basic_auth=('username', 'password'))
ticket = make_jira_ticket(jira, 'Test Issue', 'This is a test', 'Task')

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!

hey elizabeths, i think i can help ya out. after creating the ticket, you can use the add_watcher() method. it’d look somethin like this:

jira.add_watcher(ticket, ‘username_to_watch’)

just replace ‘username_to_watch’ with the actual username you want to add as a watcher. hope this helps!

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:

new_ticket = jira_client.create_issue(fields=ticket_info)
add_watchers(jira_client, new_ticket, ['user1', 'user2', 'user3'])

This approach has been reliable for me and handles errors gracefully. Just remember to import the necessary exception classes from the JIRA library.

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.