Skip to content

Commit

Permalink
fix(Jira Software Node): Get all users in dropdown/RLC (#7322)
Browse files Browse the repository at this point in the history
Github issue / Community forum post (link here to close automatically):

fixes #2670

---------

Co-authored-by: Michael Kret <[email protected]>
  • Loading branch information
2 people authored and netroy committed Oct 4, 2023
1 parent 77d15b1 commit 7abf13b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 48 deletions.
38 changes: 38 additions & 0 deletions packages/nodes-base/nodes/Jira/GenericFunctions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import type {
IHookFunctions,
ILoadOptionsFunctions,
INodeListSearchItems,
INodePropertyOptions,
JsonObject,
} from 'n8n-workflow';
import { NodeApiError } from 'n8n-workflow';
Expand Down Expand Up @@ -228,3 +229,40 @@ export function filterSortSearchListItems(items: INodeListSearchItems[], filter?
return 0;
});
}

export async function getUsers(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const jiraVersion = this.getCurrentNodeParameter('jiraVersion') as string;
const maxResults = 1000;
const query: IDataObject = { maxResults };
let endpoint = '/api/2/users/search';

if (jiraVersion === 'server') {
endpoint = '/api/2/user/search';
query.username = "'";
}

const users = [];
let hasNextPage: boolean;

do {
const usersPage = (await jiraSoftwareCloudApiRequest.call(
this,
endpoint,
'GET',
{},
{ ...query, startAt: users.length },
)) as IDataObject[];
users.push(...usersPage);
hasNextPage = usersPage.length === maxResults;
} while (hasNextPage);

return users
.filter((user) => user.active)
.map((user) => ({
name: user.displayName as string,
value: (user.accountId ?? user.name) as string,
}))
.sort((a: INodePropertyOptions, b: INodePropertyOptions) => {
return a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1;
});
}
53 changes: 5 additions & 48 deletions packages/nodes-base/nodes/Jira/Jira.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { BINARY_ENCODING, NodeOperationError } from 'n8n-workflow';

import {
filterSortSearchListItems,
getUsers,
jiraSoftwareCloudApiRequest,
jiraSoftwareCloudApiRequestAllItems,
simplifyIssueOutput,
Expand Down Expand Up @@ -206,30 +207,9 @@ export class Jira implements INodeType {
// Get all the users to display them to user so that they can
// select them easily
async getUsers(this: ILoadOptionsFunctions, filter?: string): Promise<INodeListSearchResult> {
const jiraVersion = this.getCurrentNodeParameter('jiraVersion') as string;
const query: IDataObject = {};
let endpoint = '/api/2/users/search';

if (jiraVersion === 'server') {
endpoint = '/api/2/user/search';
query.username = "'";
}

const users = await jiraSoftwareCloudApiRequest.call(this, endpoint, 'GET', {}, query);
const returnData: INodeListSearchItems[] = users.reduce(
(activeUsers: INodeListSearchItems[], user: IDataObject) => {
if (user.active) {
activeUsers.push({
name: user.displayName as string,
value: (user.accountId ?? user.name) as string,
});
}
return activeUsers;
},
[],
);
const users = await getUsers.call(this);

return { results: filterSortSearchListItems(returnData, filter) };
return { results: filterSortSearchListItems(users, filter) };
},

// Get all the priorities to display them to user so that they can
Expand Down Expand Up @@ -373,30 +353,7 @@ export class Jira implements INodeType {
// Get all the users to display them to user so that they can
// select them easily
async getUsers(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const jiraVersion = this.getCurrentNodeParameter('jiraVersion') as string;
const query: IDataObject = {};
let endpoint = '/api/2/users/search';

if (jiraVersion === 'server') {
endpoint = '/api/2/user/search';
query.username = "'";
}

const users = await jiraSoftwareCloudApiRequest.call(this, endpoint, 'GET', {}, query);

return users
.reduce((activeUsers: INodePropertyOptions[], user: IDataObject) => {
if (user.active) {
activeUsers.push({
name: user.displayName as string,
value: (user.accountId || user.name) as string,
});
}
return activeUsers;
}, [])
.sort((a: INodePropertyOptions, b: INodePropertyOptions) => {
return a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1;
});
return getUsers.call(this);
},

// Get all the groups to display them to user so that they can
Expand Down Expand Up @@ -985,7 +942,7 @@ export class Jira implements INodeType {
);

const executionData = this.helpers.constructExecutionMetaData(
this.helpers.returnJsonArray(responseData as IDataObject[]),
this.helpers.returnJsonArray({ success: true }), //endpoint returns no content
{ itemData: { item: i } },
);

Expand Down

0 comments on commit 7abf13b

Please sign in to comment.