From 370476072471da6b3b849d8f5acc5cf1380a5ba8 Mon Sep 17 00:00:00 2001 From: Elias Meire Date: Tue, 3 Oct 2023 10:04:58 +0200 Subject: [PATCH] fix(Jira Software Node): Get all users in dropdown/RLC (#7322) Github issue / Community forum post (link here to close automatically): fixes #2670 --------- Co-authored-by: Michael Kret --- .../nodes-base/nodes/Jira/GenericFunctions.ts | 38 +++++++++++++ packages/nodes-base/nodes/Jira/Jira.node.ts | 53 ++----------------- 2 files changed, 43 insertions(+), 48 deletions(-) diff --git a/packages/nodes-base/nodes/Jira/GenericFunctions.ts b/packages/nodes-base/nodes/Jira/GenericFunctions.ts index 78c0f31039..7dbd16c86d 100644 --- a/packages/nodes-base/nodes/Jira/GenericFunctions.ts +++ b/packages/nodes-base/nodes/Jira/GenericFunctions.ts @@ -6,6 +6,7 @@ import type { IHookFunctions, ILoadOptionsFunctions, INodeListSearchItems, + INodePropertyOptions, JsonObject, } from 'n8n-workflow'; import { NodeApiError } from 'n8n-workflow'; @@ -228,3 +229,40 @@ export function filterSortSearchListItems(items: INodeListSearchItems[], filter? return 0; }); } + +export async function getUsers(this: ILoadOptionsFunctions): Promise { + 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; + }); +} diff --git a/packages/nodes-base/nodes/Jira/Jira.node.ts b/packages/nodes-base/nodes/Jira/Jira.node.ts index ba11ae9340..9d5826a6d0 100644 --- a/packages/nodes-base/nodes/Jira/Jira.node.ts +++ b/packages/nodes-base/nodes/Jira/Jira.node.ts @@ -17,6 +17,7 @@ import { BINARY_ENCODING, NodeOperationError } from 'n8n-workflow'; import { filterSortSearchListItems, + getUsers, jiraSoftwareCloudApiRequest, jiraSoftwareCloudApiRequestAllItems, simplifyIssueOutput, @@ -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 { - const jiraVersion = this.getCurrentNodeParameter('jiraVersion') as string; - const query: IDataObject = {}; - let endpoint = '/api/2/users/search'; + const users = await getUsers.call(this); - 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; - }, - [], - ); - - return { results: filterSortSearchListItems(returnData, filter) }; + return { results: filterSortSearchListItems(users, filter) }; }, // Get all the priorities to display them to user so that they can @@ -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 { - 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 @@ -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 } }, );