Skip to content

Commit

Permalink
feat: export jqlQueryBuilder, fix component query (AxisCommunications…
Browse files Browse the repository at this point in the history
…#126)

exported a function jqlQueryBuilder that will create jql query strings.

using components containing spaces should now return the correct
results.

Co-authored-by: Niklas Aronsson <[email protected]>
  • Loading branch information
anicke and Niklas Aronsson authored May 7, 2024
1 parent 64f9890 commit 5fd2a31
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 10 deletions.
10 changes: 10 additions & 0 deletions .changeset/stupid-ties-push.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
'@axis-backstage/plugin-jira-dashboard-backend': minor
---

Querying for components that contain spaces should now return the expected results. Component
names is now wrapped in single quotations.

Added the `jqlQueryBuilder` function that will create a JQL query based on the arguments. This is
exported from the backend plugin to be used outside the context of the plugin together with the
`searchJira` function.
14 changes: 14 additions & 0 deletions plugins/jira-dashboard-backend/api-report.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,20 @@ export function createRouter(options: RouterOptions): Promise<express.Router>;
const jiraDashboardPlugin: () => BackendFeature;
export default jiraDashboardPlugin;

// @public
export const jqlQueryBuilder: ({
project,
components,
query,
}: JqlQueryBuilderArgs) => string;

// @public
export type JqlQueryBuilderArgs = {
project: string;
components?: string[];
query?: string;
};

// @public
export interface RouterOptions {
auth?: AuthService;
Expand Down
18 changes: 8 additions & 10 deletions plugins/jira-dashboard-backend/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Project,
} from '@axis-backstage/plugin-jira-dashboard-common';
import { resolveJiraBaseUrl, resolveJiraToken } from './config';
import { jqlQueryBuilder } from './queries';

export const getProjectInfo = async (
projectKey: string,
Expand Down Expand Up @@ -51,14 +52,9 @@ export const getIssuesByFilter = async (
query: string,
config: Config,
): Promise<Issue[]> => {
let componentQuery = '';
if (components.length) {
componentQuery = `AND component in (${components})`;
}
const jql = jqlQueryBuilder({ project: projectKey, components, query });
const response = await fetch(
`${resolveJiraBaseUrl(
config,
)}search?jql=project in (${projectKey}) ${componentQuery} AND ${query}`,
`${resolveJiraBaseUrl(config)}search?jql=${jql}`,
{
method: 'GET',
headers: {
Expand Down Expand Up @@ -120,10 +116,12 @@ export const getIssuesByComponent = async (
componentKey: string,
config: Config,
): Promise<Issue[]> => {
const jql = jqlQueryBuilder({
project: projectKey,
components: [componentKey],
});
const response = await fetch(
`${resolveJiraBaseUrl(
config,
)}search?jql=project=${projectKey} AND component = "${componentKey}"`,
`${resolveJiraBaseUrl(config)}search?jql=${jql}`,
{
method: 'GET',
headers: {
Expand Down
2 changes: 2 additions & 0 deletions plugins/jira-dashboard-backend/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,5 @@ export * from './service/router';
export { jiraDashboardPlugin as default } from './plugin';
export { searchJira } from './api';
export type { SearchOptions } from './api';
export { jqlQueryBuilder } from './queries';
export type { JqlQueryBuilderArgs } from './queries';
20 changes: 20 additions & 0 deletions plugins/jira-dashboard-backend/src/queries.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { jqlQueryBuilder } from './queries';

describe('queries', () => {
it('can use all arguments build a query.', async () => {
const jql = jqlQueryBuilder({
project: 'BS',
components: ['comp 1', 'comp 2'],
query: 'filter=example',
});
expect(jql).toBe(
"project in (BS) AND component in ('comp 1','comp 2') AND filter=example",
);
});
it('can create a query using only a project as argument.', async () => {
const jql = jqlQueryBuilder({
project: 'BS',
});
expect(jql).toBe('project in (BS)');
});
});
41 changes: 41 additions & 0 deletions plugins/jira-dashboard-backend/src/queries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/**
* Types for the arguments to the jqlQueryBuilder function.
*
* @public
*/
export type JqlQueryBuilderArgs = {
project: string;
components?: string[];
query?: string;
};

/**
* Creates a jql query string.
*
* @public
*/
export const jqlQueryBuilder = ({
project,
components,
query,
}: JqlQueryBuilderArgs) => {
let jql = `project in (${project})`;
if (components && components.length > 0) {
let componentsInclude = '(';
for (let index = 0; index < components.length; index++) {
const component = components[index];
componentsInclude += `'${component}'`;
// Add either the "," separator or close the parentheses.
if (index === components.length - 1) {
componentsInclude += ')';
} else {
componentsInclude += ',';
}
}
jql += ` AND component in ${componentsInclude}`;
}
if (query) {
jql += ` AND ${query}`;
}
return jql;
};

0 comments on commit 5fd2a31

Please sign in to comment.