Search code examples
graphqljirajira-xray

Locating defects from Jira Xray using Graphql


I am trying to use Graphql to export test cases and their execution results for any test case in a specific Test Plan. I was able to write the Graphql script to get all the details from the test execution I need, but the one problem is that the defect value returned is a string of numbers rather than the actual defect key. I tried using jira fields in the code for "links" and "linked issues", but that didn't work. Does anyone know how to just get the specific defect key associated with the test case? Here is my code:

{
    getTestExecutions(jql: "key = Demo-123", limit: 100 start:0) {
        results{
          jira(fields: ["key", "summary"])
    
          testRuns(limit: 100 start: 0){
              total
            results{
                test{
                  jira(fields: ["key", "summary", "description"])  
                }
              status{
                name
              }
              comment
              defects
              

              steps {
                  status{
                    name
                  }
                  action
                  result
                  actualResult     
                  customFields {
                   value
                    }
                    defects
                                  
              }
              
            }
        }

    }
  }
}

Solution

  • To obtain the issue keys of the defects, currently you need to invoke Jira's REST API for that.

    GET /rest/api/3/issue/{issueIdOrKey}

    You need to do this for every issueId that is returned on the defects object of the test run object returned in the GraphQL request.

    Here's a code snippet for achieving that, in Python:

    import requests
    import json
    import os
    from requests.auth import HTTPBasicAuth
    
    jira_base_url = "https://yoursite.atlassian.net/rest/api/3"
    jira_username = client_id = os.getenv('JIRA_USERNAME', "xxxx")
    jira_password = os.getenv('JIRA_PASSWORD',"xxx") # password is actually an API token obtained from https://id.atlassian.com/manage-profile/security/api-tokens
    
    
    
    
    def getIssueKeys(issueIds):
        issueKeys = []
        for issueId in issueIds:
            # endpoint doc https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-issues/#api-rest-api-3-issue-issueidorkey-get
            response = requests.get(f'{jira_base_url}/issue/{issueId}', params={}, auth=HTTPBasicAuth(jira_username, jira_password))
            res = response.json()
            issueKeys.append(res["key"])
        return issueKeys
    
    
    issueIds = ["12933", "12933"]
    
    print(getIssueKeys(issueIds))
    
    

    Note: Xray's GraphQL API doesn't yet (as of Oct2022) provide a primitive/function for that.