Skip to content

Commit

Permalink
fix(Jira Software Node): Get custom fields(RLC) in update operation f…
Browse files Browse the repository at this point in the history
…or server deployment type (#12719)
michael-radency authored Jan 21, 2025
1 parent d410b8f commit 353df79
Showing 2 changed files with 150 additions and 0 deletions.
24 changes: 24 additions & 0 deletions packages/nodes-base/nodes/Jira/Jira.node.ts
Original file line number Diff line number Diff line change
@@ -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(
126 changes: 126 additions & 0 deletions packages/nodes-base/nodes/Jira/test/node.methods.test.ts
Original file line number Diff line number Diff line change
@@ -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',
},
]);
});
});
});

0 comments on commit 353df79

Please sign in to comment.