Skip to content

Commit

Permalink
Improved error resilience and clarity in handling Jira search operati…
Browse files Browse the repository at this point in the history
…ons 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.
  • Loading branch information
SaachiNayyer committed Oct 15, 2024
1 parent 4f1aed7 commit c47bede
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 13 deletions.
10 changes: 10 additions & 0 deletions .changeset/witty-jokes-hammer.md
Original file line number Diff line number Diff line change
@@ -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.
4 changes: 2 additions & 2 deletions plugins/jira-dashboard-backend/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ 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 { JiraQueryResults } from '@axis-backstage/plugin-jira-dashboard-common';
import { Logger } from 'winston';
import { TokenManager } from '@backstage/backend-common';

Expand Down Expand Up @@ -51,7 +51,7 @@ export const searchJira: (
config: Config,
jqlQuery: string,
options: SearchOptions,
) => Promise<Issue[]>;
) => Promise<JiraQueryResults>;

// @public
export type SearchOptions = {
Expand Down
27 changes: 16 additions & 11 deletions plugins/jira-dashboard-backend/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import {
Filter,
Issue,
Project,
JiraQueryResults,
} from '@axis-backstage/plugin-jira-dashboard-common';
import { resolveJiraBaseUrl, resolveJiraToken } from './config';
import { jqlQueryBuilder } from './queries';

import { ResponseError } from '@backstage/errors';
export const getProjectInfo = async (
projectKey: string,
config: Config,
Expand Down Expand Up @@ -84,23 +85,23 @@ export type SearchOptions = {
};

/**
* Search for Jira issues using JQL.
* Asynchronously searches for Jira issues using JQL.
*
* For more information about the available options see the API
* documentation at:
* For more information about the available options, see the API documentation at:
* https://developer.atlassian.com/cloud/jira/platform/rest/v2/api-group-issue-search/#api-rest-api-2-search-post
*
* @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.
*
* @param config - The Backstage configuration object containing Jira base URL and token.
* @param jqlQuery - The JQL query string to search for Jira issues.
* @param options - Additional search options such as expand, fields, startAt, etc.
* @returns A promise that resolves to the Jira query results, including issues and total count.
* @throws An error if the search process fails.
* @public
*/
export const searchJira = async (
config: Config,
jqlQuery: string,
options: SearchOptions,
): Promise<Issue[]> => {
): Promise<JiraQueryResults> => {
const response = await fetch(`${resolveJiraBaseUrl(config)}search`, {
method: 'POST',
body: JSON.stringify({ jql: jqlQuery, ...options }),
Expand All @@ -109,8 +110,12 @@ export const searchJira = async (
Accept: 'application/json',
'Content-Type': 'application/json',
},
}).then(resp => resp.json());
return response.issues;
});
if (!response.ok) {
throw await ResponseError.fromResponse(response);
}
const jsonResponse = await response.json();
return jsonResponse as JiraQueryResults;
};

export const getIssuesByComponent = async (
Expand Down
13 changes: 13 additions & 0 deletions plugins/jira-dashboard-common/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
16 changes: 16 additions & 0 deletions plugins/jira-dashboard-common/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,19 @@ export type JiraResponse = {
project: Project;
data: JiraDataResponse[];
};

/**
* 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[];
};

0 comments on commit c47bede

Please sign in to comment.