n8n/packages/nodes-base/nodes/Jira/JiraSoftwareCloud.node.ts

506 lines
17 KiB
TypeScript
Raw Normal View History

2019-11-26 12:38:38 -08:00
import {
IExecuteFunctions,
} from 'n8n-core';
import {
IDataObject,
INodeTypeDescription,
INodeExecutionData,
INodeType,
ILoadOptionsFunctions,
INodePropertyOptions,
} from 'n8n-workflow';
import {
jiraSoftwareCloudApiRequest,
jiraSoftwareCloudApiRequestAllItems,
2019-11-26 12:38:38 -08:00
validateJSON,
} from './GenericFunctions';
2019-11-27 14:42:28 -08:00
import {
2020-02-01 15:15:56 -08:00
issueOperations,
2019-11-27 14:42:28 -08:00
issueFields,
} from './IssueDescription';
import {
IIssue,
IFields,
2019-11-29 14:30:00 -08:00
INotify,
INotificationRecipients,
NotificationRecipientsRestrictions,
} from './IssueInterface';
2019-11-26 12:38:38 -08:00
export class JiraSoftwareCloud implements INodeType {
2019-11-26 12:38:38 -08:00
description: INodeTypeDescription = {
2020-02-01 15:15:56 -08:00
displayName: 'Jira Software',
name: 'Jira Software Cloud',
2019-11-26 12:38:38 -08:00
icon: 'file:jira.png',
group: ['output'],
version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
2020-02-01 15:15:56 -08:00
description: 'Consume Jira Software API',
2019-11-26 12:38:38 -08:00
defaults: {
2020-02-01 15:15:56 -08:00
name: 'Jira Software',
2019-11-26 12:38:38 -08:00
color: '#c02428',
},
inputs: ['main'],
outputs: ['main'],
credentials: [
{
name: 'jiraSoftwareCloudApi',
2019-11-26 12:38:38 -08:00
required: true,
2020-02-01 15:15:56 -08:00
},
2019-11-26 12:38:38 -08:00
],
properties: [
{
displayName: 'Resource',
name: 'resource',
type: 'options',
options: [
{
name: 'Issue',
value: 'issue',
description: 'Creates an issue or, where the option to create subtasks is enabled in Jira, a subtask',
},
],
default: 'issue',
description: 'Resource to consume.',
},
2020-02-01 15:15:56 -08:00
...issueOperations,
2019-11-27 14:42:28 -08:00
...issueFields,
2019-11-26 12:38:38 -08:00
],
};
2019-11-27 14:42:28 -08:00
methods = {
loadOptions: {
// Get all the projects to display them to user so that he can
// select them easily
async getProjects(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
2020-02-01 15:15:56 -08:00
const credentials = this.getCredentials('jiraSoftwareCloudApi');
2019-11-27 14:42:28 -08:00
let projects;
2020-02-01 15:15:56 -08:00
let endpoint = '/project/search';
if (credentials!.jiraVersion === 'server') {
endpoint = '/project';
}
2019-11-27 14:42:28 -08:00
try {
2020-02-01 15:15:56 -08:00
projects = await jiraSoftwareCloudApiRequest.call(this, endpoint, 'GET');
2019-11-27 14:42:28 -08:00
} catch (err) {
throw new Error(`Jira Error: ${err}`);
}
2020-02-01 15:15:56 -08:00
if (projects.values && Array.isArray(projects.values)) {
projects = projects.values;
}
for (const project of projects) {
2019-11-27 14:42:28 -08:00
const projectName = project.name;
const projectId = project.id;
returnData.push({
name: projectName,
value: projectId,
});
}
return returnData;
},
// Get all the issue types to display them to user so that he can
// select them easily
async getIssueTypes(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
let issueTypes;
try {
issueTypes = await jiraSoftwareCloudApiRequest.call(this, '/issuetype', 'GET');
2019-11-27 14:42:28 -08:00
} catch (err) {
throw new Error(`Jira Error: ${err}`);
}
for (const issueType of issueTypes) {
const issueTypeName = issueType.name;
const issueTypeId = issueType.id;
returnData.push({
name: issueTypeName,
value: issueTypeId,
});
}
return returnData;
},
// Get all the labels to display them to user so that he can
// select them easily
async getLabels(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
let labels;
try {
labels = await jiraSoftwareCloudApiRequest.call(this, '/label', 'GET');
2019-11-27 14:42:28 -08:00
} catch (err) {
throw new Error(`Jira Error: ${err}`);
}
for (const label of labels.values) {
const labelName = label;
const labelId = label;
returnData.push({
name: labelName,
value: labelId,
});
}
return returnData;
},
// Get all the priorities to display them to user so that he can
// select them easily
async getPriorities(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
let priorities;
try {
priorities = await jiraSoftwareCloudApiRequest.call(this, '/priority', 'GET');
2019-11-27 14:42:28 -08:00
} catch (err) {
throw new Error(`Jira Error: ${err}`);
}
for (const priority of priorities) {
const priorityName = priority.name;
const priorityId = priority.id;
returnData.push({
name: priorityName,
value: priorityId,
});
}
return returnData;
},
// 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[] = [];
let users;
try {
users = await jiraSoftwareCloudApiRequest.call(this, '/users/search', 'GET');
2019-11-27 14:42:28 -08:00
} catch (err) {
throw new Error(`Jira Error: ${err}`);
}
for (const user of users) {
const userName = user.displayName;
const userId = user.accountId;
returnData.push({
name: userName,
value: userId,
});
}
return returnData;
},
2019-11-29 14:30:00 -08:00
// Get all the groups to display them to user so that he can
// select them easily
async getGroups(this: ILoadOptionsFunctions): Promise<INodePropertyOptions[]> {
const returnData: INodePropertyOptions[] = [];
let groups;
try {
groups = await jiraSoftwareCloudApiRequest.call(this, '/groups/picker', 'GET');
} catch (err) {
throw new Error(`Jira Error: ${err}`);
}
for (const group of groups.groups) {
const groupName = group.name;
const groupId = group.name;
returnData.push({
name: groupName,
value: groupId,
});
}
return returnData;
}
2019-11-27 14:42:28 -08:00
}
};
2019-11-26 12:38:38 -08:00
async execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]> {
2019-11-27 14:42:28 -08:00
const items = this.getInputData();
const returnData: IDataObject[] = [];
const length = items.length as unknown as number;
let responseData;
2019-11-29 14:30:00 -08:00
const qs: IDataObject = {};
2019-12-02 13:40:24 -08:00
const resource = this.getNodeParameter('resource', 0) as string;
const operation = this.getNodeParameter('operation', 0) as string;
2019-11-27 14:42:28 -08:00
for (let i = 0; i < length; i++) {
if (resource === 'issue') {
2019-11-29 14:36:07 -08:00
//https://developer.atlassian.com/cloud/jira/platform/rest/v2/#api-rest-api-2-issue-post
2019-11-27 14:42:28 -08:00
if (operation === 'create') {
const summary = this.getNodeParameter('summary', i) as string;
const projectId = this.getNodeParameter('project', i) as string;
const issueTypeId = this.getNodeParameter('issueType', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const body: IIssue = {};
const fields: IFields = {
summary,
project: {
id: projectId,
},
issuetype: {
id: issueTypeId,
},
2019-11-27 14:42:28 -08:00
};
if (additionalFields.labels) {
fields.labels = additionalFields.labels as string[];
2019-11-27 14:42:28 -08:00
}
if (additionalFields.priority) {
fields.priority = {
2019-11-27 14:42:28 -08:00
id: additionalFields.priority as string,
};
}
if (additionalFields.assignee) {
fields.assignee = {
2019-11-27 14:42:28 -08:00
id: additionalFields.assignee as string,
};
}
if (additionalFields.description) {
fields.description = additionalFields.description as string;
}
2019-11-29 14:30:00 -08:00
if (additionalFields.updateHistory) {
qs.updateHistory = additionalFields.updateHistory as boolean;
}
const issueTypes = await jiraSoftwareCloudApiRequest.call(this, '/issuetype', 'GET', body, qs);
const subtaskIssues = [];
for (const issueType of issueTypes) {
if (issueType.subtask) {
subtaskIssues.push(issueType.id);
2019-11-27 14:42:28 -08:00
}
}
2019-12-01 13:47:53 -08:00
if (!additionalFields.parentIssueKey
&& subtaskIssues.includes(issueTypeId)) {
2019-11-29 14:30:00 -08:00
throw new Error('You must define a Parent Issue Key when Issue type is sub-task');
2019-12-01 13:47:53 -08:00
} else if (additionalFields.parentIssueKey
&& subtaskIssues.includes(issueTypeId)) {
2019-11-29 14:30:00 -08:00
fields.parent = {
2019-12-01 13:47:53 -08:00
key: (additionalFields.parentIssueKey as string).toUpperCase(),
2019-11-29 14:30:00 -08:00
};
}
body.fields = fields;
2019-11-27 14:42:28 -08:00
try {
responseData = await jiraSoftwareCloudApiRequest.call(this, '/issue', 'POST', body);
2019-11-27 14:42:28 -08:00
} catch (err) {
throw new Error(`Jira Error: ${JSON.stringify(err)}`);
}
}
2019-11-29 14:36:07 -08:00
//https://developer.atlassian.com/cloud/jira/platform/rest/v2/#api-rest-api-2-issue-issueIdOrKey-put
2019-11-29 14:30:00 -08:00
if (operation === 'update') {
const issueKey = this.getNodeParameter('issueKey', i) as string;
2019-12-01 13:47:53 -08:00
const updateFields = this.getNodeParameter('updateFields', i) as IDataObject;
2019-11-29 14:30:00 -08:00
const body: IIssue = {};
2019-12-01 13:47:53 -08:00
const fields: IFields = {};
if (updateFields.summary) {
fields.summary = updateFields.summary as string;
2019-11-29 14:30:00 -08:00
}
2019-12-01 13:47:53 -08:00
if (updateFields.issueType) {
fields.issuetype = {
id: updateFields.issueType as string,
};
}
if (updateFields.labels) {
fields.labels = updateFields.labels as string[];
}
if (updateFields.priority) {
2019-11-29 14:30:00 -08:00
fields.priority = {
2019-12-01 13:47:53 -08:00
id: updateFields.priority as string,
2019-11-29 14:30:00 -08:00
};
}
2019-12-01 13:47:53 -08:00
if (updateFields.assignee) {
2019-11-29 14:30:00 -08:00
fields.assignee = {
2019-12-01 13:47:53 -08:00
id: updateFields.assignee as string,
2019-11-29 14:30:00 -08:00
};
}
2019-12-01 13:47:53 -08:00
if (updateFields.description) {
fields.description = updateFields.description as string;
2019-11-29 14:30:00 -08:00
}
const issueTypes = await jiraSoftwareCloudApiRequest.call(this, '/issuetype', 'GET', body);
const subtaskIssues = [];
for (const issueType of issueTypes) {
if (issueType.subtask) {
subtaskIssues.push(issueType.id);
}
}
2019-12-01 13:47:53 -08:00
if (!updateFields.parentIssueKey
&& subtaskIssues.includes(updateFields.issueType)) {
2019-11-29 14:30:00 -08:00
throw new Error('You must define a Parent Issue Key when Issue type is sub-task');
2019-12-01 13:47:53 -08:00
} else if (updateFields.parentIssueKey
&& subtaskIssues.includes(updateFields.issueType)) {
2019-11-29 14:30:00 -08:00
fields.parent = {
2019-12-01 13:47:53 -08:00
key: (updateFields.parentIssueKey as string).toUpperCase(),
2019-11-29 14:30:00 -08:00
};
}
body.fields = fields;
try {
responseData = await jiraSoftwareCloudApiRequest.call(this, `/issue/${issueKey}`, 'PUT', body);
} catch (err) {
throw new Error(`Jira Error: ${JSON.stringify(err)}`);
}
}
2019-11-29 14:36:07 -08:00
//https://developer.atlassian.com/cloud/jira/platform/rest/v2/#api-rest-api-2-issue-issueIdOrKey-get
2019-11-29 14:30:00 -08:00
if (operation === 'get') {
const issueKey = this.getNodeParameter('issueKey', i) as string;
2019-12-01 13:47:53 -08:00
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
if (additionalFields.fields) {
qs.fields = additionalFields.fields as string;
}
if (additionalFields.fieldsByKey) {
qs.fieldsByKey = additionalFields.fieldsByKey as boolean;
}
if (additionalFields.expand) {
qs.expand = additionalFields.expand as string;
}
if (additionalFields.properties) {
qs.properties = additionalFields.properties as string;
}
if (additionalFields.updateHistory) {
qs.updateHistory = additionalFields.updateHistory as string;
}
2019-11-29 14:30:00 -08:00
try {
responseData = await jiraSoftwareCloudApiRequest.call(this, `/issue/${issueKey}`, 'GET', {}, qs);
} catch (err) {
throw new Error(`Jira Error: ${JSON.stringify(err)}`);
}
}
2019-11-29 14:36:07 -08:00
//https://developer.atlassian.com/cloud/jira/platform/rest/v2/#api-rest-api-2-issue-issueIdOrKey-changelog-get
2019-11-29 14:30:00 -08:00
if (operation === 'changelog') {
const issueKey = this.getNodeParameter('issueKey', i) as string;
const returnAll = this.getNodeParameter('returnAll', i) as boolean;
try {
if (returnAll) {
responseData = await jiraSoftwareCloudApiRequestAllItems.call(this, 'values',`/issue/${issueKey}/changelog`, 'GET');
} else {
qs.maxResults = this.getNodeParameter('limit', i) as number;
responseData = await jiraSoftwareCloudApiRequest.call(this, `/issue/${issueKey}/changelog`, 'GET', {}, qs);
responseData = responseData.values;
}
} catch (err) {
throw new Error(`Jira Error: ${JSON.stringify(err)}`);
}
}
2019-11-29 14:36:07 -08:00
//https://developer.atlassian.com/cloud/jira/platform/rest/v2/#api-rest-api-2-issue-issueIdOrKey-notify-post
2019-11-29 14:30:00 -08:00
if (operation === 'notify') {
const issueKey = this.getNodeParameter('issueKey', i) as string;
2019-12-01 13:47:53 -08:00
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
2019-11-29 14:30:00 -08:00
const jsonActive = this.getNodeParameter('jsonParameters', 0) as boolean;
const body: INotify = {};
2019-12-01 13:47:53 -08:00
if (additionalFields.textBody) {
body.textBody = additionalFields.textBody as string;
}
if (additionalFields.htmlBody) {
body.htmlBody = additionalFields.htmlBody as string;
}
2019-11-29 14:30:00 -08:00
if (!jsonActive) {
const notificationRecipientsValues = (this.getNodeParameter('notificationRecipientsUi', i) as IDataObject).notificationRecipientsValues as IDataObject[];
const notificationRecipients: INotificationRecipients = {};
if (notificationRecipientsValues) {
// @ts-ignore
if (notificationRecipientsValues.reporter) {
// @ts-ignore
notificationRecipients.reporter = notificationRecipientsValues.reporter as boolean;
}
// @ts-ignore
if (notificationRecipientsValues.assignee) {
// @ts-ignore
notificationRecipients.assignee = notificationRecipientsValues.assignee as boolean;
}
// @ts-ignore
if (notificationRecipientsValues.assignee) {
// @ts-ignore
notificationRecipients.watchers = notificationRecipientsValues.watchers as boolean;
}
// @ts-ignore
if (notificationRecipientsValues.voters) {
// @ts-ignore
notificationRecipients.watchers = notificationRecipientsValues.voters as boolean;
}
// @ts-ignore
if (notificationRecipientsValues.users.length > 0) {
// @ts-ignore
notificationRecipients.users = notificationRecipientsValues.users.map(user => {
return {
accountId: user
};
});
}
// @ts-ignore
if (notificationRecipientsValues.groups.length > 0) {
// @ts-ignore
notificationRecipients.groups = notificationRecipientsValues.groups.map(group => {
return {
name: group
};
});
}
}
body.to = notificationRecipients;
const notificationRecipientsRestrictionsValues = (this.getNodeParameter('notificationRecipientsRestrictionsUi', i) as IDataObject).notificationRecipientsRestrictionsValues as IDataObject[];
const notificationRecipientsRestrictions: NotificationRecipientsRestrictions = {};
if (notificationRecipientsRestrictionsValues) {
// @ts-ignore
if (notificationRecipientsRestrictionsValues.groups. length > 0) {
// @ts-ignore
notificationRecipientsRestrictions.groups = notificationRecipientsRestrictionsValues.groups.map(group => {
return {
name: group
};
});
}
}
body.restrict = notificationRecipientsRestrictions;
} else {
const notificationRecipientsJson = validateJSON(this.getNodeParameter('notificationRecipientsJson', i) as string);
if (notificationRecipientsJson) {
body.to = notificationRecipientsJson;
}
const notificationRecipientsRestrictionsJson = validateJSON(this.getNodeParameter('notificationRecipientsRestrictionsJson', i) as string);
if (notificationRecipientsRestrictionsJson) {
body.restrict = notificationRecipientsRestrictionsJson;
}
}
try {
responseData = await jiraSoftwareCloudApiRequest.call(this, `/issue/${issueKey}/notify`, 'POST', body, qs);
} catch (err) {
throw new Error(`Jira Error: ${JSON.stringify(err)}`);
}
}
2019-12-01 13:47:53 -08:00
//https://developer.atlassian.com/cloud/jira/platform/rest/v2/#api-rest-api-2-issue-issueIdOrKey-transitions-get
2019-11-29 14:30:00 -08:00
if (operation === 'transitions') {
const issueKey = this.getNodeParameter('issueKey', i) as string;
2019-12-01 13:47:53 -08:00
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
if (additionalFields.transitionId) {
qs.transitionId = additionalFields.transitionId as string;
2019-11-29 14:30:00 -08:00
}
2019-12-01 13:47:53 -08:00
if (additionalFields.expand) {
qs.expand = additionalFields.expand as string;
}
if (additionalFields.skipRemoteOnlyCondition) {
qs.skipRemoteOnlyCondition = additionalFields.skipRemoteOnlyCondition as boolean;
2019-11-29 14:30:00 -08:00
}
try {
responseData = await jiraSoftwareCloudApiRequest.call(this, `/issue/${issueKey}/transitions`, 'GET', {}, qs);
responseData = responseData.transitions;
} catch (err) {
throw new Error(`Jira Error: ${JSON.stringify(err)}`);
}
}
2019-11-29 14:36:07 -08:00
//https://developer.atlassian.com/cloud/jira/platform/rest/v2/#api-rest-api-2-issue-issueIdOrKey-delete
2019-11-29 14:30:00 -08:00
if (operation === 'delete') {
const issueKey = this.getNodeParameter('issueKey', i) as string;
const deleteSubtasks = this.getNodeParameter('deleteSubtasks', i) as boolean;
qs.deleteSubtasks = deleteSubtasks;
try {
responseData = await jiraSoftwareCloudApiRequest.call(this, `/issue/${issueKey}`, 'DELETE', {}, qs);
} catch (err) {
throw new Error(`Jira Error: ${JSON.stringify(err)}`);
}
}
2019-11-27 14:42:28 -08:00
}
if (Array.isArray(responseData)) {
returnData.push.apply(returnData, responseData as IDataObject[]);
} else {
returnData.push(responseData as IDataObject);
}
}
return [this.helpers.returnJsonArray(returnData)];
2019-11-26 12:38:38 -08:00
}
}