Skip to content
Advertisement

How To Dynamically Use User Input for Jira Python

So I am trying to make an interactive method of pulling out Jira information, based on a Jira Key.

Full Code:

import os
from atlassian import Jira

import json
with open('secrets.json','r') as f:
      config = json.load(f)

jira_instance = Jira(
    url = "https://mirantis.jira.com",
    username = (config['user']['username']),
    password = (config['user']['password'])
)

projects = jira_instance.get_all_projects(included_archived=None)

value = input("Please enter your Jira Key and the Issue ID:n")
jira_key = (value)

issue = jira_instance.issue('(jira_key)', fields='summary,history,created,updated')
#issue = jira_instance.issue('DESDI-212', fields='summary,history,created,updated')
print(issue)

The main thing that is breaking is this:

issue = jira_instance.issue('(jira_key)', fields='summary,history,created,updated')

For some odd reason, it doesn’t like the way I am using user input for jira_key even though it will print out what I want if I use print(jira_key)

Am I invoking it wrong?

I basically need this:

issue = jira_instance.issue('DESDI-212', fields='summary,history,created,updated')

Whereby which, DESDI-212 will be user input. When I try it using '(jira_key)' it responds back with this error:

 rbarrett@MacBook-Pro-2  ~/Projects/Mirantis/Dataeng/Python  python test_single.py                                                             ✔  10422  22:03:34
Please enter your Jira Key and the Issue ID:
DESDI-212
Traceback (most recent call last):
  File "/Users/rbarrett/Projects/Mirantis/Dataeng/Python/test_single.py", line 19, in <module>
    issue = jira_instance.issue('(jira_key)', fields='summary,history,created,updated')
  File "/usr/local/lib/python3.9/site-packages/atlassian/jira.py", line 676, in issue
    return self.get("rest/api/2/issue/{0}?fields={1}".format(key, fields), params=params)
  File "/usr/local/lib/python3.9/site-packages/atlassian/rest_client.py", line 264, in get
    response = self.request(
  File "/usr/local/lib/python3.9/site-packages/atlassian/rest_client.py", line 236, in request
    self.raise_for_status(response)
  File "/usr/local/lib/python3.9/site-packages/atlassian/jira.py", line 3715, in raise_for_status
    raise HTTPError(error_msg, response=response)
requests.exceptions.HTTPError: Issue does not exist or you do not have permission to see it.

I expect to see this, which if I use 'DESDI-212' instead of '(jira_key)' it actually works:

{'expand': 'renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations', 'id': '372744', 'self': 'https://mirantis.jira.com/rest/api/2/issue/372744', 'key': 'DESDI-212', 'fields': {'summary': 'Add the MSR version to be parsed into Loadstone', 'updated': '2021-06-23T17:33:21.206-0700', 'created': '2021-06-01T12:54:06.136-0700'}}

Advertisement

Answer

So it turns out I was invoking it wrong. I needed to drop the '' around the '(jira_key)' and just invoke it as follows with (jira_key) instead:

import os
from atlassian import Jira

import json
with open('secrets.json','r') as f:
      config = json.load(f)

jira_instance = Jira(
    url = "https://mirantis.jira.com",
    username = (config['user']['username']),
    password = (config['user']['password'])
)

projects = jira_instance.get_all_projects(included_archived=None)

value = input("Please enter your Jira Key and the Issue ID:n")
jira_key = (value)

issue = jira_instance.issue((jira_key), fields='summary,history,created,updated')
#issue = jira_instance.issue('DESDI-212', fields='summary,history,created,updated')
print(issue)

As such I got the expected output I needed, not it’s working as expected:

 rbarrett@MacBook-Pro-2  ~/Projects/Mirantis/Dataeng/Python  python test_single.py                                                             ✔  10428  22:22:17
Please enter your Jira Key and the Issue ID:
DESDI-212
{'expand': 'renderedFields,names,schema,operations,editmeta,changelog,versionedRepresentations', 'id': '372744', 'self': 'https://mirantis.jira.com/rest/api/2/issue/372744', 'key': 'DESDI-212', 'fields': {'summary': 'Add the MSR version to be parsed into Loadstone', 'updated': '2021-06-23T17:33:21.206-0700', 'created': '2021-06-01T12:54:06.136-0700'}}
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement