I’m building a JIRA automation script using the python-jira package and running into an issue with accessing ticket keys.
def get_issue_by_summary(query_text):
global jira_client
issues = jira_client.search_issues('project=DEV and status != Closed and summary ~ "API BACKEND-SVC:MODULE1-INSTANCENUM" order by created', maxResults=1)
return issues
This function works fine and gives me back a jira result:
[<JIRA Issue: key=u'DEV-25', id=u'228401'>]
But when I try to access the key property directly:
issues.key
I’m getting this error message:
AttributeError: 'ResultList' object has no attribute 'key'
What’s the correct way to get the actual issue key from the search results? I can see it’s there in the output but I can’t figure out how to extract it properly.
The search_issues method always returns a ResultList object - basically a specialized list with Issue objects inside. Even with maxResults=1, you still need to iterate or index into this list to grab individual issues. Since you’re expecting just one result, use issues[0].key to access it directly. But I’d add a safety check to avoid index errors when nothing’s found: python if issues: first_issue_key = issues[0].key else: # handle no results case first_issue_key = None You could also iterate through all results if you expect multiple matches. The key property lives on individual Issue objects, not on the ResultList container.
yeah, just take the first item from the list. try issues[0].key to get the actual key. even if you set maxResults=1, it still returns a list. so remember to index it right!
ResultList works like a normal Python list, so you need to grab individual elements first. Since you’re using maxResults=1, just use issues[0].key to get the first one. But definitely wrap it in a try-catch or check the length first - if your search doesn’t match anything, you’ll hit an IndexError trying to access index 0. I usually do if len(issues) > 0: key = issues[0].key. I’ve been using python-jira for two years and this has saved me from crashes tons of times, especially with dynamic searches that don’t always return results.