fix(Jira Software Node): Get custom fields(RLC) in update operation for server deployment type (#12719)

This commit is contained in:
Michael Kret 2025-01-21 14:01:05 +02:00 committed by GitHub
parent d410b8f5a7
commit 353df79411
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 150 additions and 0 deletions

View file

@ -276,8 +276,12 @@ export class Jira implements INodeType {
async getCustomFields(this: ILoadOptionsFunctions): Promise<INodeListSearchResult> {
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(

View file

@ -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<ILoadOptionsFunctions>;
beforeEach(() => {
jira = new Jira();
loadOptionsFunctions = mock<ILoadOptionsFunctions>();
});
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',
},
]);
});
});
});