App page
Requirements and Test Management for Jira
Cloud Server/Data Center
Requirements and Test Management for Jira

Cloud

Server/Data Center

Documentation
FAQ
Release notes
Migration to Cloud
Last updated Feb 7, 2025

Bitbucket integration

Set up and run a script in your bitbucket-pipelines.yml file to import test results into the RTM system using its REST API.

  1. Obtain the URL address to RTM REST API. Follow authentication documentation to learn how to authenticate and get the API URL for your RTM instance.
  2. Add a secret repository variable RTM_API_TOKEN in Bitbucket. It’s used to authenticate with the RTM REST API. To learn how to set secured variables, see Atlassian documentation.
  3. Update PROJECT_KEY with your target project for import.
  4. Create an archive with test results using tar.
Example: bitbucket-pipelines.yml Configuration
image: maven:3.6.3-openjdk-17

pipelines:
   default:
      - step:
           name: run tests
           caches:
              - maven
           script:
              - |
                 echo "execute tests"
                 mvn test

                 # use tar to create archive with test results
                 RESULTS_ARCHIVE=test_results.tar.gz
                 find ./target/surefire-reports -type f -name "*.xml" -exec tar czvf $RESULTS_ARCHIVE {} +      

                 RTM_URL=https://your-rtm-instance.com
                 JOB_URL="https://bitbucket.org/${BITBUCKET_REPO_FULL_NAME}/pipelines/results/${BITBUCKET_BUILD_NUMBER}"            
                 PROJECT_KEY=EXAMPLE
                 TE_SUMMARY="Automated tests report build: ${BITBUCKET_BUILD_NUMBER}"            

                 printf "\n[RTM] Importing test results...\n"
                 IMPORT_RESULT=$(curl -s -S -X POST ${RTM_URL}/api/v2/automation/import-test-results \
                   -H "Authorization: Bearer ${RTM_API_TOKEN}" \
                   -F "projectKey=${PROJECT_KEY}" \
                   -F "name=${TE_SUMMARY}" \
                   -F "file=@${RESULTS_ARCHIVE}" \
                   -F 'reportType=JUNIT' \
                   -F "jobUrl=${JOB_URL}")
                 # checks in loop import status
                 if [[ $IMPORT_RESULT =~ ^.{8}-.{4}-.{4}-.{4}-.{12}$ ]]; then
                   printf "[RTM] Import created with taskId: $IMPORT_RESULT, checking import status"
                   TASK_STATUS='IMPORTING'
                   TRIES=0
                   while [[ "$TASK_STATUS" == *"IMPORTING"* && $TRIES != 30 ]]; do
                     TASK_STATUS=$(curl -s -S ${RTM_URL}/api/v2/automation/import-status/$IMPORT_RESULT -H "Authorization: Bearer ${RTM_API_TOKEN}")
                     if [[ "$TASK_STATUS" == *"IMPORTING"* ]]; then
                       printf "\n[RTM] Importing, retrying in 10 second"
                       sleep 10s
                     fi
                     ((TRIES = TRIES + 1))
                   done
                   if [ $TRIES -gt 30 ]; then
                     printf "\n[RTM] Import timeout with last status: ${TASK_STATUS}\n"
                   else
                     printf "\n[RTM] Import completed with status: ${TASK_STATUS}\n"
                   fi
                 else
                   printf "\n[RTM] Import failed with response: ${IMPORT_RESULT}\n"
                 fi

Explanation of steps

  1. Defining variables:

    • RTM_URL: The base URL for your RTM instance.
    • JOB_URL: A URL pointing to the current pipeline build in Bitbucket.
    • PROJECT_KEY: Jira project key where results will be imported.
    • TE_SUMMARY: A summary for created Test Execution.
  2. Creating archive of test results:

    • Uses the tar command to compress all XML files from the ./target/surefire-reports directory into a .tar.gz archive.
  3. Importing test results:

    • The script uses curl to issue a POST request to the RTM API endpoint /api/v2/automation/import-test-results. It includes the test results archive and metadata such as project key, build summary, and job URL.
  4. Monitoring Import Status:

    • After submitting the test results, the script monitors the import status by repeatedly querying /api/v2/automation/import-status/{taskId}.
    • If the status is IMPORTING, it retries up to 30 times, with a 10-second delay between retries.
    • The script will log the final status or timeout if the import doesn’t complete in time.

Required changes

  1. Replace https://your-rtm-instance.com with the URL of your RTM instance.
  2. Replace EXAMPLE in PROJECT_KEY with your RTM project key.
  3. Ensure RTM_API_TOKEN is properly added as secret repository variable.
  4. Update directory for tar command with path to XML files with test results.

Best practices:

  • The RTM_API_TOKEN must remain secret as it provides authentication to the RTM system.
  • Make sure the generated test results are compatible with the specified reportType (in the example, JUnit is used).
  • Logs and responses will help debug any issues during the import process. If an error occurs, check the response from the curl commands for further insights.