Search code examples
jirajira-rest-api

query on jira data rest api jql


I need to report on a monthly base on number of tickets by priority in jira. I know I can do that in the jira web portal by using filters/advanced search. But it gives me all the tickets details, not exactly what I want. I am trying to do reporting on this. So what's the best way to do this? I tried using curl and jira rest api using jql, but that gives me a json file with all the details. I only need number of tickets by priority. Can jql do that or do I have to load that json file into some application and parse it?


Solution

  • There isn't a way to specifically select the counts using JQL or the REST API.

    However, a solution that would limit returning a large result set from JIRA is to use the POST /rest/api/2/search API method which allows you to supply a JQL query along with the fields that you want to return in the result set and the number of results you want to return.

    Relevent JIRA docs here: https://docs.atlassian.com/jira/REST/latest/#api/2/search

    For example:

    {
      "jql": "project = PROJECTKEY and priority = \"Must Have\"",
      "maxResults": 1,
      "fields": ["status"]
    }
    

    This will return a JSON object that contains the number of results. Supplying maxResults: 1 will mean that only one result is returned in the issue list.

    {
      "expand": "names,schema",
      "startAt": 0,
      "maxResults": 1,
      "total": 354,
      "issues": [
        {...}
       ]
    }
    

    You can then use the total property for your count.

    Specific example using cURL:

    curl -i -u username:password  -H "Content-Type: application/json" -H "Accept: application/json" -X POST -d '{"jql": "project = PROJECTKEY and priority = \"Must Have\"","fields": ["status"], "maxResults": 1}' https://jira.url/rest/api/2/search
    

    Unfortunately, there are limitations of this solution;

    1. You'd need to provide the priority as an input (rather than simply having JIRA aggregate the results)
    2. You'll need to make one request per priority in order to retrieve the counts of all known priorities

    If these limitations aren't acceptable, then you could select all issues for a project and only return the priority field. You'd then have to parse the JSON and aggregate the results yourself. Note however that the results from the API are paged, so you will not necessarily get all issues returned from a single result.

    From JIRA REST API Docs;

    maxResults (int) the maximum number of issues to return (defaults to 50). The maximum allowable value is dictated by the JIRA property 'jira.search.views.default.max'. If you specify a value that is higher than this number, your search results will be truncated.