diff --git a/packages/nodes-base/nodes/Jira/Jira.node.ts b/packages/nodes-base/nodes/Jira/Jira.node.ts index 26770e2eae..294c5d85ad 100644 --- a/packages/nodes-base/nodes/Jira/Jira.node.ts +++ b/packages/nodes-base/nodes/Jira/Jira.node.ts @@ -276,8 +276,12 @@ export class Jira implements INodeType { async getCustomFields(this: ILoadOptionsFunctions): Promise { const returnData: INodeListSearchItems[] = []; const operation = this.getCurrentNodeParameter('operation') as string; + const jiraVersion = this.getNodeParameter('jiraVersion', 0) as string; + let projectId: string; let issueTypeId: string; + let issueId: string = ''; // /editmeta endpoint requires issueId + if (operation === 'create') { projectId = this.getCurrentNodeParameter('project', { extractValue: true }) as string; issueTypeId = this.getCurrentNodeParameter('issueType', { extractValue: true }) as string; @@ -292,6 +296,26 @@ export class Jira implements INodeType { ); projectId = res.fields.project.id; issueTypeId = res.fields.issuetype.id; + issueId = res.id; + } + + if (jiraVersion === 'server' && operation === 'update' && issueId) { + // https://developer.atlassian.com/server/jira/platform/jira-rest-api-example-edit-issues-6291632/?utm_source=chatgpt.com + const { fields } = await jiraSoftwareCloudApiRequest.call( + this, + `/api/2/issue/${issueId}/editmeta`, + 'GET', + ); + + for (const field of Object.keys(fields || {})) { + if (field.startsWith('customfield_')) { + returnData.push({ + name: fields[field].name, + value: field, + }); + } + } + return { results: returnData }; } const res = await jiraSoftwareCloudApiRequest.call( diff --git a/packages/nodes-base/nodes/Jira/test/node.methods.test.ts b/packages/nodes-base/nodes/Jira/test/node.methods.test.ts new file mode 100644 index 0000000000..973ec9556d --- /dev/null +++ b/packages/nodes-base/nodes/Jira/test/node.methods.test.ts @@ -0,0 +1,126 @@ +import type { MockProxy } from 'jest-mock-extended'; +import { mock } from 'jest-mock-extended'; +import type { IHttpRequestMethods, ILoadOptionsFunctions } from 'n8n-workflow'; + +import { Jira } from '../Jira.node'; + +const ISSUE_KEY = 'KEY-1'; + +jest.mock('../GenericFunctions', () => { + const originalModule = jest.requireActual('../GenericFunctions'); + return { + ...originalModule, + jiraSoftwareCloudApiRequest: jest.fn(async function ( + endpoint: string, + method: IHttpRequestMethods, + ) { + if (method === 'GET' && endpoint === `/api/2/issue/${ISSUE_KEY}`) { + return { + id: 10000, + fields: { + project: { + id: 10001, + }, + issuetype: { + id: 10002, + }, + }, + }; + } else if (method === 'GET' && endpoint === '/api/2/issue/10000/editmeta') { + return { + fields: { + customfield_123: { + name: 'Field 123', + }, + customfield_456: { + name: 'Field 456', + }, + }, + }; + } else if ( + method === 'GET' && + endpoint === + '/api/2/issue/createmeta?projectIds=10001&issueTypeIds=10002&expand=projects.issuetypes.fields' + ) { + return { + projects: [ + { + id: 10001, + issuetypes: [ + { + id: 10002, + fields: { + customfield_abc: { + name: 'Field ABC', + schema: { customId: 'customfield_abc' }, + fieldId: 'customfield_abc', + }, + customfield_def: { + name: 'Field DEF', + schema: { customId: 'customfield_def' }, + fieldId: 'customfield_def', + }, + }, + }, + ], + }, + ], + }; + } + }), + }; +}); + +describe('Jira Node, methods', () => { + let jira: Jira; + let loadOptionsFunctions: MockProxy; + + beforeEach(() => { + jira = new Jira(); + loadOptionsFunctions = mock(); + }); + + describe('listSearch.getCustomFields', () => { + it('should call correct endpoint and return custom fields for server version', async () => { + loadOptionsFunctions.getCurrentNodeParameter.mockReturnValueOnce('update'); + loadOptionsFunctions.getNodeParameter.mockReturnValue('server'); + loadOptionsFunctions.getCurrentNodeParameter.mockReturnValueOnce(ISSUE_KEY); + + const { results } = await jira.methods.listSearch.getCustomFields.call( + loadOptionsFunctions as ILoadOptionsFunctions, + ); + + expect(results).toEqual([ + { + name: 'Field 123', + value: 'customfield_123', + }, + { + name: 'Field 456', + value: 'customfield_456', + }, + ]); + }); + + it('should call correct endpoint and return custom fields for cloud version', async () => { + loadOptionsFunctions.getCurrentNodeParameter.mockReturnValueOnce('update'); + loadOptionsFunctions.getNodeParameter.mockReturnValue('cloud'); + loadOptionsFunctions.getCurrentNodeParameter.mockReturnValueOnce(ISSUE_KEY); + + const { results } = await jira.methods.listSearch.getCustomFields.call( + loadOptionsFunctions as ILoadOptionsFunctions, + ); + + expect(results).toEqual([ + { + name: 'Field ABC', + value: 'customfield_abc', + }, + { + name: 'Field DEF', + value: 'customfield_def', + }, + ]); + }); + }); +});