From 11e281efffdc9b2501161cf1200d06d5fe7dca0a Mon Sep 17 00:00:00 2001 From: enaysaa Date: Mon, 13 May 2024 16:12:00 +0100 Subject: [PATCH] Improved error resilience and clarity in handling Jira search operations by introducing structured TypeScript type definitions and enhancing the searchJira function to return both search results and HTTP status codes Signed-off-by: enaysaa saachi.nayyer@ericsson.com Introduced TypeScript type definitions SearchJiraResponse and JiraQueryResults to represent Jira search responses and pagination details. Updated the searchJira function to return search results as a SearchJiraResponse, incorporating the new types. Enhanced error handling in the searchJira function by handling HTTP response errors and logging them appropriately. The JiraQueryResults type outlines the structure of a paginated Jira search response, facilitating better data handling. These changes streamline the Jira Dashboard plugin's codebase, improving error resilience and clarity in handling search operations. --- .changeset/witty-jokes-hammer.md | 10 ++++++++ plugins/jira-dashboard-backend/api-report.md | 4 ++-- plugins/jira-dashboard-backend/src/api.ts | 15 ++++++++---- plugins/jira-dashboard-common/api-report.md | 19 +++++++++++++++ plugins/jira-dashboard-common/src/types.ts | 25 ++++++++++++++++++++ 5 files changed, 67 insertions(+), 6 deletions(-) create mode 100644 .changeset/witty-jokes-hammer.md diff --git a/.changeset/witty-jokes-hammer.md b/.changeset/witty-jokes-hammer.md new file mode 100644 index 00000000..e4bb5c15 --- /dev/null +++ b/.changeset/witty-jokes-hammer.md @@ -0,0 +1,10 @@ +--- +'@axis-backstage/plugin-jira-dashboard-backend': major +'@axis-backstage/plugin-jira-dashboard-common': minor +--- + +Introduced TypeScript type definitions SearchJiraResponse and JiraQueryResults to represent Jira search responses and pagination details. +Updated the searchJira function to return search results as a SearchJiraResponse, incorporating the new types. +The searchJira function now returns an object containing both the search results and the HTTP status code, improving error resilience and clarity in handling search operations. +The JiraQueryResults type outlines the structure of a paginated Jira search response, facilitating better data handling. +These changes streamline the Jira Dashboard plugin's codebase, improving error resilience and clarity in handling search operations. diff --git a/plugins/jira-dashboard-backend/api-report.md b/plugins/jira-dashboard-backend/api-report.md index 9e66591d..7eb13be1 100644 --- a/plugins/jira-dashboard-backend/api-report.md +++ b/plugins/jira-dashboard-backend/api-report.md @@ -10,8 +10,8 @@ import { DiscoveryService } from '@backstage/backend-plugin-api'; import express from 'express'; import { HttpAuthService } from '@backstage/backend-plugin-api'; import { IdentityApi } from '@backstage/plugin-auth-node'; -import { Issue } from '@axis-backstage/plugin-jira-dashboard-common'; import { Logger } from 'winston'; +import { SearchJiraResponse } from '@axis-backstage/plugin-jira-dashboard-common'; import { TokenManager } from '@backstage/backend-common'; // @public @@ -51,7 +51,7 @@ export const searchJira: ( config: Config, jqlQuery: string, options: SearchOptions, -) => Promise; +) => Promise; // @public export type SearchOptions = { diff --git a/plugins/jira-dashboard-backend/src/api.ts b/plugins/jira-dashboard-backend/src/api.ts index 4ebf5377..6f1a5d85 100644 --- a/plugins/jira-dashboard-backend/src/api.ts +++ b/plugins/jira-dashboard-backend/src/api.ts @@ -4,6 +4,7 @@ import { Filter, Issue, Project, + SearchJiraResponse, } from '@axis-backstage/plugin-jira-dashboard-common'; import { resolveJiraBaseUrl, resolveJiraToken } from './config'; import { jqlQueryBuilder } from './queries'; @@ -93,14 +94,15 @@ export type SearchOptions = { * @param config - A Backstage config * @param jqlQuery - A string containing the jql query. * @param options - Query options that will be passed on to the POST request. - * + * @returns A promise that resolves with the search results and status code. + * @throws If an error occurs during the search process. * @public */ export const searchJira = async ( config: Config, jqlQuery: string, options: SearchOptions, -): Promise => { +): Promise => { const response = await fetch(`${resolveJiraBaseUrl(config)}search`, { method: 'POST', body: JSON.stringify({ jql: jqlQuery, ...options }), @@ -109,8 +111,13 @@ export const searchJira = async ( Accept: 'application/json', 'Content-Type': 'application/json', }, - }).then(resp => resp.json()); - return response.issues; + }); + const jsonResponse = await response.json(); + + return { + results: jsonResponse, + statusCode: response.status, + } as SearchJiraResponse; }; export const getIssuesByComponent = async ( diff --git a/plugins/jira-dashboard-common/api-report.md b/plugins/jira-dashboard-common/api-report.md index 55271abe..c09c50df 100644 --- a/plugins/jira-dashboard-common/api-report.md +++ b/plugins/jira-dashboard-common/api-report.md @@ -51,6 +51,19 @@ export type JiraDataResponse = { issues: Issue[]; }; +// @public +export type JiraQueryResults = { + expand: string; + names: object; + schema: object; + issues: Issue[]; + total: number; + startAt: number; + maxResults: number; + warningMessages?: string[]; + errorMessages?: string[]; +}; + // @public export type JiraResponse = { project: Project; @@ -78,4 +91,10 @@ export type Project = { // @public export const PROJECT_KEY_NAME = 'project-key'; + +// @public +export type SearchJiraResponse = { + results: JiraQueryResults; + statusCode: number; +}; ``` diff --git a/plugins/jira-dashboard-common/src/types.ts b/plugins/jira-dashboard-common/src/types.ts index 3237fd11..705029bb 100644 --- a/plugins/jira-dashboard-common/src/types.ts +++ b/plugins/jira-dashboard-common/src/types.ts @@ -72,3 +72,28 @@ export type JiraResponse = { project: Project; data: JiraDataResponse[]; }; + +/** + * Type definition for a Jira search result & the recieved HTTP status code + * @public + */ +export type SearchJiraResponse = { + results: JiraQueryResults; + statusCode: number; +}; + +/** + * Type definition for a paginated Jira search response + * @public + */ +export type JiraQueryResults = { + expand: string; + names: object; + schema: object; + issues: Issue[]; + total: number; + startAt: number; + maxResults: number; + warningMessages?: string[]; + errorMessages?: string[]; +};