Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ Add option to use Jira field display names #2408

Merged
merged 7 commits into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
18 changes: 18 additions & 0 deletions packages/nodes-base/nodes/Jira/IssueDescription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,24 @@ export const issueFields: INodeProperties[] = [
default: '',
description: 'Issue Key',
},
{
displayName: 'Simplify Output',
name: 'simplifyOutput',
type: 'boolean',
required: false,
displayOptions: {
show: {
resource: [
'issue',
],
operation: [
'get',
],
},
},
default: false,
description: `Return the display value of fields instead of their ID`,
},
{
displayName: 'Additional Fields',
name: 'additionalFields',
Expand Down
84 changes: 52 additions & 32 deletions packages/nodes-base/nodes/Jira/Jira.node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,45 +303,29 @@ export class Jira implements INodeType {
// Get all the users to display them to user so that he can
// select them easily
async getUsers(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
const jiraVersion = this.getCurrentNodeParameter('jiraVersion') as string;
if (jiraVersion === 'server') {
// the interface call must bring username
const users = await jiraSoftwareCloudApiRequest.call(this, '/api/2/user/search', 'GET', {},
{
username: '\'',
},
);
for (const user of users) {
const userName = user.displayName;
const userId = user.name;
const query: IDataObject = {};
let endpoint = '/api/2/users/search';

returnData.push({
name: userName,
value: userId,
});
}
} else {
const users = await jiraSoftwareCloudApiRequest.call(this, '/api/2/users/search', 'GET');
if (jiraVersion === 'server') {
endpoint = '/api/2/user/search';
query.username = '\'';
}

for (const user of users) {
const userName = user.displayName;
const userId = user.accountId;
const users = await jiraSoftwareCloudApiRequest.call(this, endpoint, 'GET', {}, query);

returnData.push({
name: userName,
value: userId,
return users.reduce((activeUsers: INodePropertyOptions[], user: IDataObject) => {
if (user.active) {
activeUsers.push({
name: user.displayName as string,
value: (user.accountId || user.name) as string,
});
}
}

returnData.sort((a, b) => {
if (a.name < b.name) { return -1; }
if (a.name > b.name) { return 1; }
return 0;
return activeUsers;
}, []).sort((a: INodePropertyOptions, b: INodePropertyOptions) => {
return a.name.toLowerCase() > b.name.toLowerCase() ? 1 : -1;
});

return returnData;
},

// Get all the groups to display them to user so that he can
Expand Down Expand Up @@ -642,6 +626,7 @@ export class Jira implements INodeType {
if (operation === 'get') {
for (let i = 0; i < length; i++) {
const issueKey = this.getNodeParameter('issueKey', i) as string;
const simplifyOutput = this.getNodeParameter('simplifyOutput', i) as boolean;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
if (additionalFields.fields) {
qs.fields = additionalFields.fields as string;
Expand All @@ -652,14 +637,49 @@ export class Jira implements INodeType {
if (additionalFields.expand) {
qs.expand = additionalFields.expand as string;
}
if (simplifyOutput) {
qs.expand = `${qs.expand || ''},names`;
}
if (additionalFields.properties) {
qs.properties = additionalFields.properties as string;
}
if (additionalFields.updateHistory) {
qs.updateHistory = additionalFields.updateHistory as string;
}
responseData = await jiraSoftwareCloudApiRequest.call(this, `/api/2/issue/${issueKey}`, 'GET', {}, qs);
returnData.push(responseData);

if (simplifyOutput) {
const mappedFields: IDataObject = {};
// Sort custom fields last so we map them last
const customField = /^customfield_\d+$/;
const sortedFields: string[] = Object.keys(responseData.fields).sort((a, b) => {
if (customField.test(a) && customField.test(b)) {
return a > b ? 1 : -1;
}
if (customField.test(a)) {
return 1;
}
if (customField.test(b)) {
return -1;
}
return a > b ? 1 : -1;
});
for (const field of sortedFields) {
if (responseData.names[field] in mappedFields) {
let newField: string = responseData.names[field];
let counter = 0;
while (newField in mappedFields) {
counter++;
newField = `${responseData.names[field]}_${counter}`;
}
mappedFields[newField] = responseData.fields[field];
} else {
mappedFields[responseData.names[field] || field] = responseData.fields[field];
}
}
responseData.fields = mappedFields;
}
simplifyOutput ? returnData.push(responseData.fields) : returnData.push(responseData);
RicardoE105 marked this conversation as resolved.
Show resolved Hide resolved
}
}
//https://developer.atlassian.com/cloud/jira/platform/rest/v2/#api-rest-api-2-search-post
Expand Down