From b655153431c2ca63b9c5859c911c5837653d880d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Iv=C3=A1n=20Ovejero?= Date: Sun, 21 Feb 2021 07:00:27 -0300 Subject: [PATCH] :sparkles: Add user resource and operations to Jira (#1448) * Add user resource and operations * Update user resource description * :zap: Small improvements Co-authored-by: ricardo --- packages/nodes-base/nodes/Jira/Jira.node.ts | 55 ++++- .../nodes-base/nodes/Jira/UserDescription.ts | 207 ++++++++++++++++++ 2 files changed, 261 insertions(+), 1 deletion(-) create mode 100644 packages/nodes-base/nodes/Jira/UserDescription.ts diff --git a/packages/nodes-base/nodes/Jira/Jira.node.ts b/packages/nodes-base/nodes/Jira/Jira.node.ts index 6a89019268..868dcff9ba 100644 --- a/packages/nodes-base/nodes/Jira/Jira.node.ts +++ b/packages/nodes-base/nodes/Jira/Jira.node.ts @@ -43,6 +43,11 @@ import { NotificationRecipientsRestrictions, } from './IssueInterface'; +import { + userFields, + userOperations, +} from './UserDescription'; + export class Jira implements INodeType { description: INodeTypeDescription = { displayName: 'Jira Software', @@ -119,6 +124,11 @@ export class Jira implements INodeType { value: 'issueComment', description: 'Get, create, update, and delete a comment from an issue.', }, + { + name: 'User', + value: 'user', + description: 'Get, create and delete a user.', + }, ], default: 'issue', description: 'Resource to consume.', @@ -129,6 +139,8 @@ export class Jira implements INodeType { ...issueAttachmentFields, ...issueCommentOperations, ...issueCommentFields, + ...userOperations, + ...userFields, ], }; @@ -952,10 +964,51 @@ export class Jira implements INodeType { } } + if (resource === 'user') { + if (operation === 'create') { + // https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-users/#api-rest-api-3-user-post + for (let i = 0; i < length; i++) { + const body = { + name: this.getNodeParameter('username', i), + emailAddress: this.getNodeParameter('emailAddress', i), + displayName: this.getNodeParameter('displayName', i), + }; + + const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject; + + Object.assign(body, additionalFields); + + responseData = await jiraSoftwareCloudApiRequest.call(this, '/api/3/user', 'POST', body, {}); + returnData.push(responseData); + } + } else if (operation === 'delete') { + // https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-users/#api-rest-api-3-user-delete + for (let i = 0; i < length; i++) { + qs.accountId = this.getNodeParameter('accountId', i); + responseData = await jiraSoftwareCloudApiRequest.call(this, '/api/3/user', 'DELETE', {}, qs); + returnData.push({ success: true }); + } + } else if (operation === 'get') { + // https://developer.atlassian.com/cloud/jira/platform/rest/v3/api-group-users/#api-rest-api-3-user-get + for (let i = 0; i < length; i++) { + qs.accountId = this.getNodeParameter('accountId', i); + + const { expand } = this.getNodeParameter('additionalFields', i) as { expand: string[] }; + + if (expand) { + qs.expand = expand.join(','); + } + + responseData = await jiraSoftwareCloudApiRequest.call(this, '/api/3/user', 'GET', {}, qs); + returnData.push(responseData); + } + } + } + if (resource === 'issueAttachment' && (operation === 'getAll' || operation === 'get')) { return this.prepareOutputData(returnData as unknown as INodeExecutionData[]); } else { return [this.helpers.returnJsonArray(returnData)]; } } -} \ No newline at end of file +} diff --git a/packages/nodes-base/nodes/Jira/UserDescription.ts b/packages/nodes-base/nodes/Jira/UserDescription.ts new file mode 100644 index 0000000000..11cb886535 --- /dev/null +++ b/packages/nodes-base/nodes/Jira/UserDescription.ts @@ -0,0 +1,207 @@ +import { + INodeProperties, +} from 'n8n-workflow'; + +export const userOperations = [ + { + displayName: 'Operation', + name: 'operation', + type: 'options', + displayOptions: { + show: { + resource: [ + 'user', + ], + }, + }, + options: [ + { + name: 'Create', + value: 'create', + description: 'Create a new user.', + }, + { + name: 'Delete', + value: 'delete', + description: 'Delete a user.', + }, + { + name: 'Get', + value: 'get', + description: 'Retrieve a user.', + }, + ], + default: 'create', + description: 'The operation to perform.', + }, +] as INodeProperties[]; + +export const userFields = [ + + /* -------------------------------------------------------------------------- */ + /* user:create */ + /* -------------------------------------------------------------------------- */ + { + displayName: 'Username', + name: 'username', + type: 'string', + required: true, + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'create', + ], + }, + }, + }, + { + displayName: 'Email Address', + name: 'emailAddress', + type: 'string', + required: true, + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'create', + ], + }, + }, + }, + { + displayName: 'Display Name', + name: 'displayName', + type: 'string', + required: true, + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'create', + ], + }, + }, + default: '', + }, + { + displayName: 'Additional Fields', + name: 'additionalFields', + type: 'collection', + placeholder: 'Add Field', + default: {}, + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'create', + ], + }, + }, + options: [ + { + displayName: 'Password', + name: 'password', + type: 'string', + default: '', + description: 'Password for the user. If a password is not set, a random password is generated.', + typeOptions: { + password: true, + }, + }, + { + displayName: 'Notification', + name: 'notification', + type: 'boolean', + default: false, + description: 'Send the user an email confirmation that they have been added to Jira.', + }, + ], + }, + /* -------------------------------------------------------------------------- */ + /* user:delete */ + /* -------------------------------------------------------------------------- */ + { + displayName: 'Account ID', + name: 'accountId', + type: 'string', + default: '', + description: 'Account ID of the user to delete.', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'delete', + ], + }, + }, + }, + /* -------------------------------------------------------------------------- */ + /* user:get */ + /* -------------------------------------------------------------------------- */ + { + displayName: 'Account ID', + name: 'accountId', + type: 'string', + default: '', + description: 'Account ID of the user to retrieve.', + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'get', + ], + }, + }, + }, + { + displayName: 'Additional Fields', + name: 'additionalFields', + type: 'collection', + placeholder: 'Add Field', + default: {}, + displayOptions: { + show: { + resource: [ + 'user', + ], + operation: [ + 'get', + ], + }, + }, + options: [ + { + displayName: 'Expand', + name: 'expand', + type: 'multiOptions', + default: [], + description: 'Include more information about the user.', + options: [ + { + name: 'Groups', + value: 'groups', + description: 'Include all groups to which the user belongs.', + }, + { + name: 'Application Roles', + value: 'applicationRoles', + description: 'Include details of all the applications the user can access.', + }, + ], + }, + ], + }, +] as INodeProperties[];