Search code examples
pythonjira-rest-java-api

JIRA Python use a string to access class


I need help with getting the following function working.

The issue I can't solve how to reference the function with a string.

  • This line works

    field_value = results.fields.customfield_10000
    
  • This line does not, as the string value of custom_field is customfield_10000

    field_value = results.fields.custom_field
    

This is the full function

def get_customfield_value(results, custom_field):
    field_value = results.fields.custom_field
    return field_value

from jira.client import JIRA
jira_options={'server': 'http://localhost:8080'}
jira=JIRA(options=jira_options,basic_auth=('usrname','pwd'))

results = jira.search_issues(' some jql retuning issues ')
fieldValue = get_customfield_value(results, "customfield_10000")

I've looked at locals() and globals() but not sure if this is the right thing.


Solution

  • You can use __getattribute__.

    from jira import JIRA
    
    jira = JIRA('https://jira.atlassian.com')
    issue = jira.issue('JRA-9')
    issue.fields.__getattribute__(issue.fields,'customfield_11437')
    

    So in your case, this should work:

    def get_customfield_value(results, custom_field):
        field_value = results.fields.__getattribute__(results.fields,custom_field)
        return field_value