Skip to content

Commit

Permalink
Signed-off-by: enaysaa [email protected]
Browse files Browse the repository at this point in the history
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 May 13, 2024
1 parent 5fd2a31 commit ea04f74
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 14 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': minor
'@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.
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.
5 changes: 3 additions & 2 deletions plugins/jira-dashboard-backend/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -51,7 +51,8 @@ export const searchJira: (
config: Config,
jqlQuery: string,
options: SearchOptions,
) => Promise<Issue[]>;
logger: Logger,
) => Promise<SearchJiraResponse>;

// @public
export type SearchOptions = {
Expand Down
54 changes: 42 additions & 12 deletions plugins/jira-dashboard-backend/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,13 @@ import fetch from 'node-fetch';
import {
Filter,
Issue,
JiraQueryResults,
Project,
SearchJiraResponse,
} from '@axis-backstage/plugin-jira-dashboard-common';
import { resolveJiraBaseUrl, resolveJiraToken } from './config';
import { jqlQueryBuilder } from './queries';
import { Logger } from 'winston';

export const getProjectInfo = async (
projectKey: string,
Expand Down Expand Up @@ -91,26 +94,53 @@ 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.
*
* @param logger - An optional logger for error handling and debugging purposes.
* @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<Issue[]> => {
const response = await fetch(`${resolveJiraBaseUrl(config)}search`, {
method: 'POST',
body: JSON.stringify({ jql: jqlQuery, ...options }),
headers: {
Authorization: resolveJiraToken(config),
Accept: 'application/json',
'Content-Type': 'application/json',
},
}).then(resp => resp.json());
return response.issues;
logger: Logger,
): Promise<SearchJiraResponse> => {
try {
const response = await fetch(`${resolveJiraBaseUrl(config)}search`, {
method: 'POST',
body: JSON.stringify({ jql: jqlQuery, ...options }),
headers: {
Authorization: resolveJiraToken(config),
Accept: 'application/json',
'Content-Type': 'application/json',
},
});
const jsonResponse = await response.json();

return {
results: toPaginatedResponse(jsonResponse),
statusCode: response.status,
} as SearchJiraResponse;
} catch (error) {
logger.error(error);
throw error;
}
};

function toPaginatedResponse(response: any): JiraQueryResults {
return {
expand: response.expand,
names: response.names,
schema: response.schema,
issues: response.issues,
total: response.total,
startAt: response.startAt,
maxResults: response.maxResults,
warningMessages: response.warningMessages,
errorMessages: response.errorMessages,
};
}

export const getIssuesByComponent = async (
projectKey: string,
componentKey: string,
Expand Down
19 changes: 19 additions & 0 deletions plugins/jira-dashboard-common/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,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 Expand Up @@ -75,4 +88,10 @@ export type Project = {

// @public
export const PROJECT_KEY_NAME = 'project-key';

// @public
export type SearchJiraResponse = {
results: JiraQueryResults;
statusCode: number;
};
```
25 changes: 25 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,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[];
};

0 comments on commit ea04f74

Please sign in to comment.