2

I am trying to access jira from python at my work place and the basic operation I intend to do is to fetch/create/update jira issues. I looked at the template code online and am trying to use that, but no luck. I have already installed jira api using pip.

pip install jira

#!/usr/bin/python
from jira import JIRA
options = {'server' : 'https://jira.mycompany.com/rest/api/2'}
jira = JIRA(options)
projects = jira.projects()
print (projects)

And this is its output:

Traceback (most recent call last):
  File "JiraTest.py", line 7, in <module>
    jira = JIRA(options)
  File "C:\Anaconda3\lib\site-packages\jira\client.py", line 317, in __init__
    si = self.server_info()
  File "C:\Anaconda3\lib\site-packages\jira\client.py", line 1771, in server_info
    j = self._get_json('serverInfo')
  File "C:\Anaconda3\lib\site-packages\jira\client.py", line 2172, in _get_json
    r = self._session.get(url, params=params)
  File "C:\Anaconda3\lib\site-packages\jira\resilientsession.py", line 150, in get
   return self.__verb('GET', url, **kwargs)
  File "C:\Anaconda3\lib\site-packages\jira\resilientsession.py", line 146, in __verb
    raise_on_error(response, verb=verb, **kwargs)
  File "C:\Anaconda3\lib\site-packages\jira\resilientsession.py", line 56, in raise_on_error
    r.status_code, error, r.url, request=request, response=r, **kwargs)
jira.exceptions.JIRAError: JiraError HTTP 404 url:https://jira.mycompany.com/rest/api/2/rest/api/2/serverInfo

        response headers = {'Date': 'Sat, 29 Jul 2017 22:42:31 GMT', 'Content-Length': '0', 'Server': 'Apache-Coyote/1.1'}
        response text = 

` I know I am doing something wrong here and hence this are the things I want to ask:

  • How to determine jira server at your work place.
  • Do the jira administrator need to enable rest api calls or something else from admin login? Is there a way to determine if it is disabled from our code?
  • Is there anything else I have to install apart from just installing jira through pip.
  • How to deal with login credentials. I am sure there is a better way than specifying username/password in your .py file. Can someone point me on where to find that info.

Thanks.

4
  • What do you mean "no luck"? Are you getting exceptions? If so, show us the traceback. Currently it's unclear what's happening with the code you've shown (and obviously we can't test your company's JIRA setup). Commented Jul 29, 2017 at 22:20
  • @Blckknght - I have updated my post with its output. I feel I am totally doing something wrong or missing some step in trying to access jira. Commented Jul 29, 2017 at 22:54
  • 2
    I'm not a JIRA expert by any means (I've used it to report bugs, but never programmed with its APIs), but judging by the duplicated part of the URL in the error message, I'd guess you should leave the /rest/api/2 part out of the value you're putting in your options dict. Commented Jul 29, 2017 at 23:47
  • @Blckknght - Yes i tried that too, removing the duplicate portion but still the same error (minus the duplicate string). Commented Jul 31, 2017 at 17:55

3 Answers 3

3

I'm not sure what version of the jira-python client you're using but to instantiate a JIRA object, you don't pass the server inside the "options" parameter (and you definitely don't put the path to the REST api). According to the docs:

class jira.JIRA(server=None, options=None, basic_auth=None, oauth=None, jwt=None, kerberos=False, validate=False, get_server_info=True, async=False, logging=True, max_retries=3, proxies=None, timeout=None)

So your instantiation should look like:

from jira.client import JIRA

jira = JIRA('https://jira.mycompany.com')

If you need to auth, then it would be:

jira = JIRA('https://jira.mycompany.com', basic_auth=(username, password))
Sign up to request clarification or add additional context in comments.

4 Comments

Using the options is totally fine. It's not needed in this case, since he's simply passing the server name, but it's valid, and allows to add other option (like certs and cookies) if needed. My guess is, that he's using some of these in the production code, and removed them for the exmple.
How would you use a PAT token for this?
@not2qubit had as well a research journey to figure that out. You have to use basic_auth(username, api-token) instead of basic_auth=(username, password). See: jira.readthedocs.io/examples.html#http-basic
For PAT on self hosted you need to use auth_jira = JIRA(token_auth='API token')
0

The server URL should not contain the REST endpoint, this is added automatically by python-jira.

If you check the error you're getting, you'll see that the rest path is listed twice, this is why you're getting the 404.

So, changing your code to:

#!/usr/bin/python
from jira import JIRA

options = {'server' : 'https://jira.mycompany.com'}
jira = JIRA(options)
projects = jira.projects()
print (projects)

should do the trick.

Please let me know if there are other problems.

Comments

-1

To confirm you have the right JIRA server, browse to the root URL https://jira.mycompany.com and ensure you can see the login screen and log in (this confirms your username and password work too).

Access to the JIRA REST API must be enabled before you can use it. To test it, try communicating with it using curl, e.g.:

curl -u "username:password" -X GET -H "Content-Type: application/json" https://jira.mycompany.com/rest/api/2/issue/KEY-666

As long as Python can communicate with servers using GET and POST, you shouldn't need anything else.

For username and password I just have the script ask at the command line with an instruction that doesn't show the password. I use node.js but I am sure Python has something similar. Or keep them in a separate file the script reads in, but make sure you never check the file in!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.