4.0.0 • Published 7 months ago

@qytera/xray-client v4.0.0

Weekly downloads
-
License
MIT
Repository
github
Last release
7 months ago

npm version npm downloads open GitHub issues unaddressed GitHub issues

Node.js Xray Client

This project provides TypeScript clients to users that want to interact with the API of Xray. Supports Xray server and Xray cloud.

!WARNING The client is at a very early stage of development and may not cover all Xray interactions. Please refer to the roadmaps for full lists of supported endpoints.


Based on: https://docs.getxray.app/display/XRAY/REST+API

  • v1
    • Tests
      • GET /rest/raven/1.0/api/test
      • GET /rest/raven/1.0/api/settings/teststatuses
      • GET /rest/raven/1.0/api/test/{key}/testruns
      • GET /rest/raven/1.0/api/test/{key}/preconditions
      • GET /rest/raven/1.0/api/test/{key}/testsets
      • GET /rest/raven/1.0/api/test/{key}/testexecutions
      • GET /rest/raven/1.0/api/test/{key}/testplans
      • Test Steps
        • GET /rest/raven/1.0/api/settings/teststepstatuses
        • GET /rest/raven/1.0/api/test/{testKey}/step
        • GET /rest/raven/1.0/api/test/{testKey}/step/{id}
        • PUT /rest/raven/1.0/api/test/{testKey}/step
        • POST /rest/raven/1.0/api/test/{testKey}/step/{id}
        • DELETE /rest/raven/1.0/api/test/{testKey}/step/{id}
        • GET /rest/raven/1.0/api/test/{testKey}/step/{id}/attachment
        • DELETE /rest/raven/1.0/api/test/{testKey}/step/{id}/attachment/{attachmentID}
    • Pre-Conditions
      • GET /rest/raven/1.0/api/precondition/{preConditionKey}/test
      • POST /rest/raven/1.0/api/precondition/{preConditionKey}/test
      • DELETE /rest/raven/1.0/api/precondition/{preConditionKey}/test/{testKey}
    • Test Sets
      • GET /rest/raven/1.0/api/testset/{testSetKey}/test
      • POST /rest/raven/1.0/api/testset/{testSetKey}/test
      • DELETE /rest/raven/1.0/api/testset/{testSetKey}/test/{testKey}
    • Test Plans
      • GET /rest/raven/1.0/api/testplan/{testPlanKey}/test
      • POST /rest/raven/1.0/api/testplan/{testPlanKey}/test
      • DELETE /rest/raven/1.0/api/testplan/{testPlanKey}/test/{testKey}
      • GET /rest/raven/1.0/api/testplan/{testPlanKey}/testexecution
      • POST /rest/raven/1.0/api/testplan/{testPlanKey}/testexecution
      • DELETE /rest/raven/1.0/api/testplan/{testPlanKey}/testexecution/{testExecKey}
    • Test Executions
      • GET /rest/raven/1.0/api/testexec/{testExecKey}/test
      • POST /rest/raven/1.0/api/testexec/{testExecKey}/test
      • DELETE /rest/raven/1.0/api/testexec/{testExecKey}/test/{testKey}
    • Test Runs
      • GET /rest/raven/1.0/api/testrun/{id}/
      • GET /rest/raven/1.0/api/testrun/
      • PUT /rest/raven/1.0/api/testrun/{id}/
      • GET /rest/raven/1.0/api/testrun/{id}/status
      • PUT /rest/raven/1.0/api/testrun/{id}/status
      • GET /rest/raven/1.0/api/testrun/{id}/defect
      • POST /rest/raven/1.0/api/testrun/{id}/defect
      • DELETE /rest/raven/1.0/api/testrun/{id}/defect/{issueIdOrKey}
      • GET /rest/raven/1.0/api/testrun/{id}/attachment
      • POST /rest/raven/1.0/api/testrun/{id}/attachment
      • DELETE /rest/raven/1.0/api/testrun/{id}/attachment
      • DELETE /rest/raven/1.0/api/testrun/{id}/attachment/{attachmentid}
      • GET /rest/raven/1.0/api/testrun/{id}/comment
      • PUT /rest/raven/1.0/api/testrun/{id}/comment
      • GET /rest/raven/1.0/api/testrun/{id}/example
      • GET /rest/raven/1.0/api/testrun/{id}/step
      • GET /rest/raven/1.0/api/testrun/{id}/assignee
      • PUT /rest/raven/1.0/api/testrun/{id}/assignee
      • Test Examples
        • GET /rest/raven/1.0/api/testrun/{id}/example/{exampleIndex}
        • PUT /rest/raven/1.0/api/testrun/{id}/example/{exampleIndex}
      • Test Step Results
        • GET /rest/raven/1.0/api/testrun/{id}/step/{stepResultId}/
        • PUT /rest/raven/1.0/api/testrun/{id}/step/{stepResultId}/
        • GET /rest/raven/1.0/api/testrun/{id}/step/{stepResultId}/status
        • PUT /rest/raven/1.0/api/testrun/{id}/step/{stepResultId}/status
        • GET /rest/raven/1.0/api/testrun/{id}/step/{stepResultId}/attachment
        • POST /rest/raven/1.0/api/testrun/{id}/step/{stepResultId}/attachment
        • DELETE /rest/raven/1.0/api/testrun/{id}/step/{stepResultId}/attachment
        • DELETE /rest/raven/1.0/api/testrun/{id}/step/{stepResultId}/attachment/{attachmentid}
    • Test Repository
      • GET /rest/raven/1.0/api/testrepository/{projectKey}/folders
      • GET /rest/raven/1.0/api/testrepository/{projectKey}/folders/{folderId}/tests
      • PUT /rest/raven/1.0/api/testrepository/{projectKey}/folders/{folderId}/tests
      • GET /rest/raven/1.0/api/testrepository/{projectKey}/folders/{folderId}
      • POST /rest/raven/1.0/api/testrepository/{projectKey}/folders/{folderId}
      • PUT /rest/raven/1.0/api/testrepository/{projectKey}/folders/{folderId}
      • DELETE /rest/raven/1.0/api/testrepository/{projectKey}/folders/{folderId}
    • Import Execution Results
      • POST /rest/raven/1.0/api/import/execution
      • POST /rest/raven/1.0/api/import/execution/multipart
      • POST /rest/raven/1.0/api/import/execution/cucumber
      • POST /rest/raven/1.0/api/import/execution/cucumber/multipart
      • POST /rest/raven/1.0/api/import/execution/behave
      • POST /rest/raven/1.0/api/import/execution/behave/multipart
      • POST /rest/raven/1.0/api/import/execution/junit
      • POST /rest/raven/1.0/api/import/execution/junit/multipart
      • POST /rest/raven/1.0/api/import/execution/testng
      • POST /rest/raven/1.0/api/import/execution/testng/multipart
      • POST /rest/raven/1.0/api/import/execution/nunit
      • POST /rest/raven/1.0/api/import/execution/nunit/multipart
      • POST /rest/raven/1.0/api/import/execution/xunit
      • POST /rest/raven/1.0/api/import/execution/xunit/multipart
      • POST /rest/raven/1.0/api/import/execution/robot
      • POST /rest/raven/1.0/api/import/execution/robot/multipart
      • POST /rest/raven/1.0/api/import/execution/bundle
    • Export Execution Results
      • GET /rest/raven/1.0/api/testruns
      • GET /rest/raven/1.0/api/execution/result (deprecated)
    • Importing Cucumber Tests
      • POST /rest/raven/1.0/api/import/feature
    • Exporting Cucumber Tests
      • GET /rest/raven/1.0/api/export/test
    • Settings
      • GET /rest/raven/1.0/api/settings/teststatuses
      • GET /rest/raven/1.0/api/settings/teststepstatuses
  • v2
    • Test Step
      • GET /rest/raven/2.0/api/test/{testKey}/steps
      • POST /rest/raven/2.0/api/test/{testKey}/steps
      • GET /rest/raven/2.0/api/test/{testKey}/steps/{stepId}
      • PUT /rest/raven/2.0/api/test/{testKey}/steps/{stepId}
      • DELETE /rest/raven/2.0/api/test/{testKey}/steps/{stepId}
      • GET /rest/raven/2.0/api/test/{testKey}/steps/{stepId}/attachments
      • DELETE /rest/raven/2.0/api/test/{testKey}/steps/{stepId}/attachment/{attachmentId}
    • Test Run
      • GET /rest/raven/2.0/api/testrun
      • GET /rest/raven/2.0/api/testrun/{id}
      • PUT /rest/raven/2.0/api/testrun/{id}
      • GET /rest/raven/2.0/api/testrun/{id}/customfield/{customFieldId}
      • PUT /rest/raven/2.0/api/testrun/{id}/customfield/{customFieldId}
      • GET /rest/raven/2.0/api/testrun/{id}/iteration/{iterationId}
      • PUT /rest/raven/2.0/api/testrun/{id}/iteration/{iterationId}
      • GET /rest/raven/2.0/api/testrun/{id}/iteration/{iterationId}/step
      • GET /rest/raven/2.0/api/testrun/{id}/iteration/{iterationId}/step/{stepResultId}
      • PUT /rest/raven/2.0/api/testrun/{id}/iteration/{iterationId}/step/{stepResultId}
      • GET /rest/raven/2.0/api/testrun/{id}/iteration/{iterationId}/step/{stepResultId}/status
      • PUT /rest/raven/2.0/api/testrun/{id}/iteration/{iterationId}/step/{stepResultId}/status
      • GET /rest/raven/2.0/api/testrun/{id}/iteration/{iterationId}/step/{stepResultId}/attachment
      • POST /rest/raven/2.0/api/testrun/{id}/iteration/{iterationId}/step/{stepResultId}/attachment
      • DELETE /rest/raven/2.0/api/testrun/{id}/iteration/{iterationId}/step/{stepResultId}/attachment
      • DELETE /rest/raven/2.0/api/testrun/{id}/iteration/{iterationId}/step/{stepResultId}/attachment/{attachmentId}
      • GET /testruns
    • Test Run Status
      • PUT /rest/raven/2.0/api/testrunstatus/reset
    • Requirement Status
      • PUT /rest/raven/2.0/api/requirementstatus/reset
    • Project
      • GET /rest/raven/2.0/api/project/{id}/settings/customfields/testruns
      • GET /rest/raven/2.0/api/project/{id}/settings/customfields/teststeps
    • External Apps
      • GET /rest/raven/2.0/api/xraylicense
    • Import
      • POST /rest/raven/2.0/api/import/execution
      • POST /rest/raven/2.0/api/import/execution/multipart
    • Dataset
      • GET /rest/raven/2.0/api/dataset/export
      • POST /rest/raven/2.0/api/dataset/import
    • Test Repository
      • GET /rest/raven/2.0/api/testrepository/{projectKey}/folders/{folderId}

Based on: https://docs.getxray.app/display/XRAYCLOUD/REST+API

Installation

Xray Server

npm install @qytera/xray-client

Xray Cloud

If you don't intend to use the GraphQL endpoints:

npm install @qytera/xray-client

With GraphQL support:

npm install @qytera/xray-client graphql graphql-tag

Usage

Simply create a client instance for your Xray setup and start using the fully typed endpoints available.

Xray Server

import { XrayClientServer } from "@qytera/xray-client";

// PAT authentication.
export const serverClient = new XrayClientServer({
  credentials: {
    token: "jira-token",
  },
  url: "https://jira.company.com",
});

// Basic authentication.
export const serverClient = new XrayClientServer({
  credentials: {
    username: "jira-username",
    password: "jira-password",
  },
  url: "https://jira.company.com",
});

const csvData = await serverClient.dataset.export({ testIssueKey: "PRJ-123" });
const testRun = await serverClient.testRun.getTestRun({
  testExecIssueKey: "PRJ-456",
  testIssueKey: "PRJ-123",
});
// ... other endpoints

Xray Cloud

import { XrayClientCloud } from "@qytera/xray-client";

// Xray cloud authentication.
export const cloudClient = new XrayClientCloud({
  credentials: {
    clientId: "xray-client-id",
    clientSecret: "xray-client-secret",
    path: "/api/v2/authenticate",
  },
  url: "https://xray.cloud.getxray.app",
});

const csvData = await cloudClient.dataset.export({ testIssueKey: "PRJ-123" });
// ... other endpoints

The GraphQL query and mutation endpoints are also available. For example, consider the following getTestRuns query:

{
  getTestRuns(limit: 100, testExecIssueIds: ["XCN-2"]) {
    total
    limit
    start
    results {
      test {
        jira(fields: ["key"])
      }
    }
  }
}

It can easily be mapped to the following code snippet using the library:

const testRuns = await cloudClient.graphql.getTestRuns(
  { limit: 100, testExecIssueIds: ["XCN-2"] },
  (testRunResults) => [
    testRunResults.total,
    testRunResults.limit,
    testRunResults.start,
    testRunResults.results((testRun) => [
      testRun.test((test) => [
        test.jira({ fields: ["key"] })
      ])
    ]),
  ]
);

Version Specification

Xray server and Xray cloud both have versioned API endpoints with slightly different behaviour. By default, the clients access the latest documented version available for each endpoint.

You can switch between versions on a per-request basis:

// Xray server:
const evidence = await serverClient.testRun.evidence.getEvidence("12345"); // 2.0 endpoint
const evidence = await serverClient.testRun.evidence.v1.getEvidence("12345"); // 1.0 endpoint

// Xray cloud:
const attachment = await cloudClient.attachment.addAttachment("my-file.txt"); // v2 endpoint
const attachment = await cloudClient.attachment.v1.addAttachment("my-file.txt"); // v1 endpoint

Custom HTTP Agents

The clients can use custom HTTP agents in the form of custom fetch implementations. The recommended approach is to use undici, which is already included in Node.js but is not exposed.

!NOTE Make sure to install the version of undici that comes with the installed version of Node.js to avoid version conflicts and typing issues:

npm install undici@$(node -e "console.log(process.versions.undici)")
import { ProxyAgent, fetch } from "undici";

const proxyAgent = new ProxyAgent({
  token: Buffer.from("username:password").toString("base64"),
  uri: "http://1.2.3.4:8765",
});

const configuration: ClientConfiguration = {
  fetch: (url, init) => {
    return fetch(url, { ...init, dispatcher: proxyAgent });
  },
  credentials: { /* ... */ },
  url: ...
};

Error Handling

Requests may result in HTTP error responses (e.g. 400 Bad Request, 403 Forbidden, 500 Internal Server Error) depending on the request configuration. The clients always throw errors when the actual response status does not match the expected status defined internally for each endpoint.

!NOTE Unfortunately, Xray's official documentation is not always precise about error response formats. The error responses also vary between Xray server/cloud and based on the Jira/Xray versions present. It is therefore impossible for the clients to directly model all error responses.

To handle response errors gracefully, use isResponseError to adapt possible outcomes to your Xray environment:

import { isResponseError } from "@qytera/xray-client";

try {
  const data = await client.import.execution.xray(/* ... */);
  // ...
} catch (error: unknown) {
  if (isResponseError(error)) {
    // access request/response data
    console.error("request failed:", error.request.url);
    console.error("request failed:", error.response.json.details);
  } else {
    // ...
  }
}

You can also easily define your own expected response error details and distinguish between them:

import { isResponseError } from "@qytera/xray-client";

try {
  const data = await client.import.execution.xray(/* ... */);
  // ...
} catch (error: unknown) {
  if (isResponseError(error, 400)) {
    console.error("Bad Request:", error.response.text);
  } else if (isResponseError<{ missingPermission: string }>(error, 403)) {
    console.error("Forbidden:", error.response.json.missingPermission);
  } else {
    console.error("Unexpected error:", error);
  }
}

Credits

This project was heavily inspired by jira.js.

4.0.0

7 months ago

3.0.0

8 months ago

2.1.0

8 months ago

2.0.0

8 months ago

1.1.1

8 months ago

1.1.0

8 months ago

1.0.0

8 months ago

0.0.8

8 months ago

0.0.7

9 months ago

0.0.6

9 months ago

0.0.5

9 months ago

0.0.4

9 months ago

0.0.3

9 months ago

0.0.2

9 months ago

0.0.1

9 months ago