Search code examples
jenkins-pipelinejenkins-pluginsjira-xrayx-ray

X-Ray import json result from Jenkins to new test execution under a test plan


i would like to import the results of the execution on an existing test plan but each time in a new test execution i have the below on jenkings

  stage("Publish XRay Results to JIRA") {
            // publish results to Jira only when parameter results is set to true
            when {
                expression { return params.PUBLISH_TO_JIRA == true }
            }
            steps {
                script {
                    catchError(buildResult: 'UNSTABLE', stageResult: 'UNSTABLE') {
                        //SEARCH ALL cucumber.json files and publish them one by one
                        archiveArtifacts artifacts: '**/target/cucumber*.json', fingerprint: true
                        for (file in findFiles(glob: '**/target/cucumber*.json')) {
                            if ( file.getLength() < 6 ) {
                                echo "[ INFO ] File ${file} is too small, will not be uploaded."
                            }
                            else {
                                echo "[ INFO ] Uploading file ${file}."
                                step([$class: 'XrayImportBuilder',
                                    serverInstance: "${pipelineConfig.jira_server_id}",
                                    endpointName: '/cucumber',
                                    importFilePath: "${file}"])
                            }
                        }
                    }
                }
            }
        }
    }

and in the feature file, I specify the id of the existing test execution it works correctly adding the result to the existing test execution.

if I specify the ids of the test plan I take the error :

23:19:54 ERROR: Unable to confirm Result of the upload..... Upload Failed! Status:400 Response:{"error":"description: description"}

I have tried without TPL-XXX and with TPL-XXX but the results it the same.

so my question is :

how I can import test execution results using endpointName: '/cucumber', on a new Test Execution under an existing test plan?


Solution

  • Whenever you import test results from a Cucumber JSON report, Xray will process the tags on the scenarios to look for the corresponding Test issues in Jira. It will also look at a the Feature section, and see if there's a tag there referring to a Test Execution issue; if that's the case, Xray will update the results on that Test Execution issue instead of creating a new one. If you add a reference to a Test Plan issue anywhere on the .feature file, it won't be processed in any way; there's currently no such logic.

    If you want to link the Test Execution to a Test Plan then you have to use the [Cucumber JSON multipart endpoint][1] instead. The ["standard" endpoint][2] is simpler, and doesn't allow you to pass parameters whenever importing results; it only has that special handling that I've mentioned earlier, based on some tags on the .feature file itself (which will be then part of the cucumber json report).

    How does the cucumber multipart endpoint works then? Well, besides having a different REST API endpoint, you have to upload the cucumber json report along with an auxiliary JSON file to customize fields on the Test Execution issue that will be created. Whenever using the cucumber multipart endpoint it will always create new Test Executions.

    The auxiliary JSON syntax is based on Jira issue update syntax. In order to refer to the Test Plan, you need to know the custom field id; then it should be something like this, assuming 11807 is the id of that field in Jira (find yours going to Jira admin > custom fields > Test Plan):

    
    {
        "fields": {
            "project": {
                "key": "XRAY"
            },
            "summary": "Test Execution for cucumber Execution",
            "issuetype": {
                "name": "Test Execution"
            },
            "customfield_11807" : "CALC-123"
        }
    }```
      [1]: https://docs.getxray.app/display/XRAY/Import+Execution+Results+-+REST#ImportExecutionResultsREST-CucumberJSONresultsMultipart
      [2]: https://docs.getxray.app/display/XRAY/Import+Execution+Results+-+REST#ImportExecutionResultsREST-CucumberJSONresults