From 4b0d3b56cb301837535fc1dba4835363ae31de50 Mon Sep 17 00:00:00 2001 From: Ricardo Espinoza Date: Sun, 13 Jun 2021 11:30:05 -0400 Subject: [PATCH] :zap: Improve jira issue field lookup (#1877) * Improved/fixed collecting of custom fields based on project and issue type. * :zap: Improvements to #1610 * :shirt: Fix linter issue Co-authored-by: jemos --- packages/nodes-base/nodes/Jira/Jira.node.ts | 32 +++++++++++++-------- 1 file changed, 20 insertions(+), 12 deletions(-) diff --git a/packages/nodes-base/nodes/Jira/Jira.node.ts b/packages/nodes-base/nodes/Jira/Jira.node.ts index cb2a9399b5..1f26a3e5ff 100644 --- a/packages/nodes-base/nodes/Jira/Jira.node.ts +++ b/packages/nodes-base/nodes/Jira/Jira.node.ts @@ -358,21 +358,29 @@ export class Jira implements INodeType { async getCustomFields(this: ILoadOptionsFunctions): Promise { const returnData: INodePropertyOptions[] = []; const operation = this.getCurrentNodeParameter('operation') as string; - let projectId; + let projectId: string; + let issueTypeId: string; if (operation === 'create') { - projectId = this.getCurrentNodeParameter('project'); + projectId = this.getCurrentNodeParameter('project') as string; + issueTypeId = this.getCurrentNodeParameter('issueType') as string; } else { - const issueKey = this.getCurrentNodeParameter('issueKey'); - const { fields: { project: { id } } } = await jiraSoftwareCloudApiRequest.call(this, `/api/2/issue/${issueKey}`, 'GET', {}, {}); - projectId = id; + const issueKey = this.getCurrentNodeParameter('issueKey') as string; + const res = await jiraSoftwareCloudApiRequest.call(this, `/api/2/issue/${issueKey}`, 'GET', {}, {}); + projectId = res.fields.project.id; + issueTypeId = res.fields.issuetype.id; } - const fields = await jiraSoftwareCloudApiRequest.call(this, `/api/2/field`, 'GET'); - for (const field of fields) { - if (field.custom === true && field.scope && field.scope.project && field.scope.project.id === projectId) { - returnData.push({ - name: field.name, - value: field.id, - }); + + const res = await jiraSoftwareCloudApiRequest.call(this, `/api/2/issue/createmeta?projectIds=${projectId}&issueTypeIds=${issueTypeId}&expand=projects.issuetypes.fields`, 'GET'); + + // tslint:disable-next-line: no-any + const fields = res.projects.find((o: any) => o.id === projectId).issuetypes.find((o: any) => o.id === issueTypeId).fields; + for (const key of Object.keys(fields)) { + const field = fields[key]; + if (field.schema && Object.keys(field.schema).includes('customId')) { + returnData.push({ + name: field.name, + value: field.key, + }); } } return returnData;