Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions packages/nodes-base/nodes/Jenkins/Jenkins.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -452,15 +452,25 @@ export class Jenkins implements INodeType {
async getJobParameters(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const job = this.getCurrentNodeParameter('job') as string;
const returnData: INodePropertyOptions[] = [];
const endpoint = `/job/${job}/api/json?tree=actions[parameterDefinitions[*]]`;
const { actions } = await jenkinsApiRequest.call(this, 'GET', endpoint);
for (const { _class, parameterDefinitions } of actions) {
if (_class?.includes('ParametersDefinitionProperty')) {
for (const { name, type } of parameterDefinitions) {
const endpoint = `/job/${job}/api/json?tree=actions[parameterDefinitions[*]],property[parameterDefinitions[*]]`;
const result = await jenkinsApiRequest.call(this, 'GET', endpoint);
const allParameters = [...(result.actions ?? []), ...(result.property ?? [])];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can Jenkins return both result.actions and result.property leading to duplicate parameters?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, here's one of the API responses from actual Jenkins service, as you can see both property and action have a parameter str_freestyle
image

const seenParameterNames = new Set<string>();
for (const { _class, parameterDefinitions } of allParameters) {
if (
!_class?.includes('ParametersDefinitionProperty') ||
!Array.isArray(parameterDefinitions)
) {
continue;
}

for (const { name, type } of parameterDefinitions) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure if Jenkins API may allow having null here, but maybe worth adding guard against null/undefined here

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added Array.isArray check for parameterDefinitions which covers nullish values as well

if (!seenParameterNames.has(name)) {
returnData.push({
name: `${name} - (${type})`,
value: name,
});
seenParameterNames.add(name);
}
}
}
Expand Down
121 changes: 121 additions & 0 deletions packages/nodes-base/nodes/Jenkins/test/Jenkins.node.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
import { mockDeep } from 'jest-mock-extended';
import type { ILoadOptionsFunctions, INodePropertyOptions } from 'n8n-workflow';

import * as GenericFunctions from '../GenericFunctions';
import { Jenkins } from '../Jenkins.node';

describe('Jenkins node', () => {
let node: Jenkins;
let loadOptionsFunctions: jest.Mocked<ILoadOptionsFunctions>;
const jenkinsApiRequestSpy = jest.spyOn(GenericFunctions, 'jenkinsApiRequest');

beforeEach(() => {
node = new Jenkins();
loadOptionsFunctions = mockDeep<ILoadOptionsFunctions>();
loadOptionsFunctions.getCurrentNodeParameter.mockReturnValue('demo-job');
jest.clearAllMocks();
});

afterEach(() => {
jest.resetAllMocks();
});

describe('loadOptions.getJobParameters', () => {
it('loads parameters from actions', async () => {
jenkinsApiRequestSpy.mockResolvedValue({
actions: [
{
_class: 'hudson.model.ParametersDefinitionProperty',
parameterDefinitions: [
{ name: 'BRANCH', type: 'StringParameterDefinition' },
{ name: 'DRY_RUN', type: 'BooleanParameterDefinition' },
],
},
],
});

const result = await node.methods.loadOptions.getJobParameters.call(loadOptionsFunctions);

expect(result).toEqual<INodePropertyOptions[]>([
{ name: 'BRANCH - (StringParameterDefinition)', value: 'BRANCH' },
{ name: 'DRY_RUN - (BooleanParameterDefinition)', value: 'DRY_RUN' },
]);
expect(jenkinsApiRequestSpy).toHaveBeenCalledWith(
'GET',
'/job/demo-job/api/json?tree=actions[parameterDefinitions[*]],property[parameterDefinitions[*]]',
);
});

it('loads parameters from property', async () => {
jenkinsApiRequestSpy.mockResolvedValue({
property: [
{
_class:
'org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty ParametersDefinitionProperty',
parameterDefinitions: [{ name: 'VERSION', type: 'StringParameterDefinition' }],
},
],
});

const result = await node.methods.loadOptions.getJobParameters.call(loadOptionsFunctions);

expect(result).toEqual<INodePropertyOptions[]>([
{ name: 'VERSION - (StringParameterDefinition)', value: 'VERSION' },
]);
});

it('merges actions and property results and deduplicates parameter names', async () => {
jenkinsApiRequestSpy.mockResolvedValue({
actions: [
{
_class: 'hudson.model.ParametersDefinitionProperty',
parameterDefinitions: [{ name: 'ENV', type: 'StringParameterDefinition' }],
},
],
property: [
{
_class: 'hudson.model.ParametersDefinitionProperty',
parameterDefinitions: [
{ name: 'ENV', type: 'StringParameterDefinition' },
{ name: 'REGION', type: 'ChoiceParameterDefinition' },
],
},
],
});

const result = await node.methods.loadOptions.getJobParameters.call(loadOptionsFunctions);

expect(result).toEqual<INodePropertyOptions[]>([
{ name: 'ENV - (StringParameterDefinition)', value: 'ENV' },
{ name: 'REGION - (ChoiceParameterDefinition)', value: 'REGION' },
]);
});

it('filters non parameter classes and sorts by display name', async () => {
jenkinsApiRequestSpy.mockResolvedValue({
actions: [
{
_class: 'hudson.model.ScmProperty',
parameterDefinitions: [
{ name: 'SHOULD_NOT_APPEAR', type: 'StringParameterDefinition' },
],
},
{
_class: 'hudson.model.ParametersDefinitionProperty',
parameterDefinitions: [
{ name: 'ZZZ', type: 'StringParameterDefinition' },
{ name: 'AAA', type: 'StringParameterDefinition' },
],
},
],
});

const result = await node.methods.loadOptions.getJobParameters.call(loadOptionsFunctions);

expect(result).toEqual<INodePropertyOptions[]>([
{ name: 'AAA - (StringParameterDefinition)', value: 'AAA' },
{ name: 'ZZZ - (StringParameterDefinition)', value: 'ZZZ' },
]);
});
});
});
Loading