Search code examples
pythonjirapython-jira

Jira-Python : How can we fetch the commentor name by passing object?


I'm trying fetch the data of commentor name in the recently updated issue. I'm saving the updated issues in a list, when I'm passing the list in the query it gives me : AttributeError: type object 'PropertyHolder' has no attribute 'comment', even though there is comment present on the issue.

Can anyone please help?

Below is the code

def issue_tested(project_name, updated_from, updated_to):
    print project_name, updated_from, updated_to
    total_update=[]
    total_update = jira.search_issues('project = %s AND updated >= %s and updated <= %s' % (project_name, updated_from, updated_to))
    total_updated_length = len(total_update)

print total_update
print total_updated_length
print total_update[2]
issue = jira.issue(total_update[2])
print issue
print issue.fields.comment.comments[0].author.name

I'm getting the issue , but it is not getting passed in the print issue.fields.comment.comments[0].author.name. However when I directly assign the issue like : issue = jira.issue('Issue_name'), it gives me the author name. But while passing it as an object shows Attribute error.

PS: I don't want to use Script-Runner as it as paid now, can you please give me any solution within python?

Thanks!


Solution

  • Explanation

    The reason why you get this error is that by default search method returns issues with navigable fields:

    By default, only navigable (*navigable) fields are returned in this search resource. Note: the default is different in the get-issue resource -- the default there all fields (*all).

    When you execute issue method:

    issue = jira.issue(total_update[2])
    

    you pass issue object, not string. Inside that there is a condition:

    if type(id) == Issue:
        return id
    

    i.e. it just returns the original method from search execution, which was missing the comment field.

    Solution

    1. You can ask search method to include comment field to the result:

      total_update = jira.search_issues(jql_str='project = %s AND updated >= %s and updated <= %s' % (project_name, updated_from, updated_to), fields='comment')
      
    2. You can query an issue by it's key:

      issue = jira.issue(total_update[2].key)

    Method #1 is preferable, unless you need to get comments for some specific issue.