Search code examples
pythonjirajira-rest-apipython-jira

Jira api to search all issues in Jira using python


I want to list/search all the issues in Jira. I have a code like :

url = 'https://company.com/rest/api/2/search'


auth = HTTPBasicAuth("username", "password") // I tries token as well

headers = {
    'Accept': 'application/json'
}

query = {
    'jql': 'project=PRKJECTKEY',
    'startAt': 0
}
response = requests.request(
    "GET",
    url,
    headers=headers,
    auth=auth,
    params=query
) 

I am not sure if the password should be token or the actual password and the url should be should be starting from companyname.com. This gives me <Response [401]> but i have all the permissions with the account.

Can someone help me with the authentication is supposed to be used this way.


Solution

  • I can only describe my way of accessing the JIRA-API:

    1. I am using an API-key for this which one can easily create online, if one has the necessary permissions

    (https://developer.atlassian.com/cloud/jira/platform/basic-auth-for-rest-apis/)

    2. You need to set up the Jira-object to query issues first
    user = '[email protected]'
    
    apikey = 'xxxxxxxxxxxxxxxxx'
    server = 'https://companypage.atlassian.net'
    
    options = {
     'server': server
    }
    
    jira = JIRA(options, basic_auth=(user, apikey))
    
    3. Now, you can use the Jira-object to query with
    tickets = jira.search_issues('text ~ "my search text" ORDER BY updated DESC')
    

    Now, you can look what you got back and play with the results

    for ticket in tickets:
      print(ticket)
    

    For Packages, you only need to import JIRA at the top (obviously, need to install jira first):

    from jira import JIRA