Renamed and added interfaces

This commit is contained in:
Ricardo Espinoza
2019-11-28 01:10:41 -05:00
parent bb2ba588f5
commit 8e5ad3f4b1
6 changed files with 86 additions and 72 deletions

View File

@@ -3,9 +3,9 @@ import {
NodePropertyTypes, NodePropertyTypes,
} from 'n8n-workflow'; } from 'n8n-workflow';
export class JiraApi implements ICredentialType { export class JiraSoftwareCloudApi implements ICredentialType {
name = 'jiraApi'; name = 'jiraSoftwareCloudApi';
displayName = 'Jira API'; displayName = 'Jira Software Cloud API';
properties = [ properties = [
{ {
displayName: 'Email', displayName: 'Email',

View File

@@ -12,13 +12,12 @@ import {
IDataObject, IDataObject,
} from 'n8n-workflow'; } from 'n8n-workflow';
export async function jiraApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, endpoint: string, method: string, body: any = {}, query?: IDataObject, uri?: string): Promise<any> { // tslint:disable-line:no-any export async function jiraSoftwareCloudApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, endpoint: string, method: string, body: any = {}, query?: IDataObject, uri?: string): Promise<any> { // tslint:disable-line:no-any
const credentials = this.getCredentials('jiraApi'); const credentials = this.getCredentials('jiraSoftwareCloudApi');
if (credentials === undefined) { if (credentials === undefined) {
throw new Error('No credentials got returned!'); throw new Error('No credentials got returned!');
} }
const data = Buffer.from(`${credentials!.email}:${credentials!.apiToken}`).toString(BINARY_ENCODING); const data = Buffer.from(`${credentials!.email}:${credentials!.apiToken}`).toString(BINARY_ENCODING);
console.log(data)
const headerWithAuthentication = Object.assign({}, const headerWithAuthentication = Object.assign({},
{ Authorization: `Basic ${data}`, Accept: 'application/json', 'Content-Type': 'application/json' }); { Authorization: `Basic ${data}`, Accept: 'application/json', 'Content-Type': 'application/json' });
@@ -49,7 +48,7 @@ export async function jiraApiRequest(this: IHookFunctions | IExecuteFunctions |
* Make an API request to paginated intercom endpoint * Make an API request to paginated intercom endpoint
* and return all results * and return all results
*/ */
export async function jiraApiRequestAllItems(this: IHookFunctions | IExecuteFunctions, propertyName: string, endpoint: string, method: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any export async function jiraSoftwareCloudApiRequestAllItems(this: IHookFunctions | IExecuteFunctions, propertyName: string, endpoint: string, method: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
const returnData: IDataObject[] = []; const returnData: IDataObject[] = [];
@@ -60,7 +59,7 @@ export async function jiraApiRequestAllItems(this: IHookFunctions | IExecuteFunc
let uri: string | undefined; let uri: string | undefined;
do { do {
responseData = await jiraApiRequest.call(this, endpoint, method, body, query, uri); responseData = await jiraSoftwareCloudApiRequest.call(this, endpoint, method, body, query, uri);
uri = responseData.pages.next; uri = responseData.pages.next;
returnData.push.apply(returnData, responseData[propertyName]); returnData.push.apply(returnData, responseData[propertyName]);
} while ( } while (

View File

@@ -88,10 +88,9 @@ export const issueFields = [
description: 'Summary', description: 'Summary',
}, },
{ {
displayName: 'Parent Issue Identifier', displayName: 'Has Parent Issue?',
name: 'parentIssueId', name: 'hasParentIssue',
type: 'options', type: 'boolean',
required: false,
displayOptions: { displayOptions: {
show: { show: {
resource: [ resource: [
@@ -102,25 +101,12 @@ export const issueFields = [
], ],
}, },
}, },
default: 'id', default: false,
options: [ description: 'Weather The Issue Has A Parent Issue ID/Key or Not',
{
name: 'ID',
value: 'id',
description: 'Issue ID',
},
{
name: 'Key',
value: 'key',
description: 'Issue Key',
}
],
description: 'Parent Issue Identifier',
}, },
{ {
displayName: 'Parent Issue Identifier Value', displayName: 'Parent Issue Key',
name: 'parentIssueIdValue', name: 'parentIssueKey',
type: 'string', type: 'string',
required: false, required: false,
displayOptions: { displayOptions: {
@@ -131,10 +117,13 @@ export const issueFields = [
operation: [ operation: [
'create', 'create',
], ],
hasParentIssue: [
true,
],
}, },
}, },
default: '', default: '',
description: 'Parent Issue ID/Key valie', description: 'Parent Issue Key',
}, },
{ {
displayName: 'Additional Fields', displayName: 'Additional Fields',
@@ -186,6 +175,14 @@ export const issueFields = [
required : false, required : false,
description: 'Assignee', description: 'Assignee',
}, },
{
displayName: 'Description',
name: 'description',
type: 'string',
default: '',
required : false,
description: 'Description',
},
], ],
}, },
] as INodeProperties[]; ] as INodeProperties[];

View File

@@ -0,0 +1,16 @@
import { IDataObject } from "n8n-workflow";
export interface IFields {
summary: string;
project?: IDataObject;
issuetype?: IDataObject;
labels?: string[];
priority?: IDataObject;
assignee?: IDataObject;
description?: string;
parent?: IDataObject;
}
export interface IIssue {
fields?: IFields;
}

View File

@@ -10,33 +10,37 @@ import {
INodePropertyOptions, INodePropertyOptions,
} from 'n8n-workflow'; } from 'n8n-workflow';
import { import {
jiraApiRequest, jiraSoftwareCloudApiRequest,
jiraApiRequestAllItems, jiraSoftwareCloudApiRequestAllItems,
validateJSON, validateJSON,
} from './GenericFunctions'; } from './GenericFunctions';
import { import {
issueOpeations, issueOpeations,
issueFields, issueFields,
} from './IssueDescription'; } from './IssueDescription';
import {
IIssue,
IFields,
} from './IssueInterface';
export class Jira implements INodeType { export class JiraSoftwareCloud implements INodeType {
description: INodeTypeDescription = { description: INodeTypeDescription = {
displayName: 'Jira', displayName: 'Jira Software Cloud',
name: 'Jira', name: 'Jira Software Cloud',
icon: 'file:jira.png', icon: 'file:jira.png',
group: ['output'], group: ['output'],
version: 1, version: 1,
subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}', subtitle: '={{$parameter["operation"] + ": " + $parameter["resource"]}}',
description: 'Consume Jira API', description: 'Consume Jira Software Cloud API',
defaults: { defaults: {
name: 'Jira', name: 'Jira Software Cloud',
color: '#c02428', color: '#c02428',
}, },
inputs: ['main'], inputs: ['main'],
outputs: ['main'], outputs: ['main'],
credentials: [ credentials: [
{ {
name: 'jiraApi', name: 'jiraSoftwareCloudApi',
required: true, required: true,
} }
], ],
@@ -68,7 +72,7 @@ export class Jira implements INodeType {
const returnData: INodePropertyOptions[] = []; const returnData: INodePropertyOptions[] = [];
let projects; let projects;
try { try {
projects = await jiraApiRequest.call(this, '/project/search', 'GET'); projects = await jiraSoftwareCloudApiRequest.call(this, '/project/search', 'GET');
} catch (err) { } catch (err) {
throw new Error(`Jira Error: ${err}`); throw new Error(`Jira Error: ${err}`);
} }
@@ -90,7 +94,7 @@ export class Jira implements INodeType {
const returnData: INodePropertyOptions[] = []; const returnData: INodePropertyOptions[] = [];
let issueTypes; let issueTypes;
try { try {
issueTypes = await jiraApiRequest.call(this, '/issuetype', 'GET'); issueTypes = await jiraSoftwareCloudApiRequest.call(this, '/issuetype', 'GET');
} catch (err) { } catch (err) {
throw new Error(`Jira Error: ${err}`); throw new Error(`Jira Error: ${err}`);
} }
@@ -112,7 +116,7 @@ export class Jira implements INodeType {
const returnData: INodePropertyOptions[] = []; const returnData: INodePropertyOptions[] = [];
let labels; let labels;
try { try {
labels = await jiraApiRequest.call(this, '/label', 'GET'); labels = await jiraSoftwareCloudApiRequest.call(this, '/label', 'GET');
} catch (err) { } catch (err) {
throw new Error(`Jira Error: ${err}`); throw new Error(`Jira Error: ${err}`);
} }
@@ -134,7 +138,7 @@ export class Jira implements INodeType {
const returnData: INodePropertyOptions[] = []; const returnData: INodePropertyOptions[] = [];
let priorities; let priorities;
try { try {
priorities = await jiraApiRequest.call(this, '/priority', 'GET'); priorities = await jiraSoftwareCloudApiRequest.call(this, '/priority', 'GET');
} catch (err) { } catch (err) {
throw new Error(`Jira Error: ${err}`); throw new Error(`Jira Error: ${err}`);
} }
@@ -156,7 +160,7 @@ export class Jira implements INodeType {
const returnData: INodePropertyOptions[] = []; const returnData: INodePropertyOptions[] = [];
let users; let users;
try { try {
users = await jiraApiRequest.call(this, '/users/search', 'GET'); users = await jiraSoftwareCloudApiRequest.call(this, '/users/search', 'GET');
} catch (err) { } catch (err) {
throw new Error(`Jira Error: ${err}`); throw new Error(`Jira Error: ${err}`);
} }
@@ -188,50 +192,48 @@ export class Jira implements INodeType {
const summary = this.getNodeParameter('summary', i) as string; const summary = this.getNodeParameter('summary', i) as string;
const projectId = this.getNodeParameter('project', i) as string; const projectId = this.getNodeParameter('project', i) as string;
const issueTypeId = this.getNodeParameter('issueType', i) as string; const issueTypeId = this.getNodeParameter('issueType', i) as string;
const parentIssueId = this.getNodeParameter('parentIssueId', i) as string; const hasParentIssue = this.getNodeParameter('hasParentIssue', i) as boolean;
const parentIssueIdValue = this.getNodeParameter('parentIssueIdValue', i) as string;
const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject; const additionalFields = this.getNodeParameter('additionalFields', i) as IDataObject;
const body = { const body: IIssue = {};
fields: { const fields: IFields = {
summary, summary,
project: { project: {
id: projectId, id: projectId,
}, },
issuetype: { issuetype: {
id: issueTypeId id: issueTypeId,
}, },
}
}; };
if (additionalFields.labels) { if (additionalFields.labels) {
body.fields.labels = additionalFields.labels as string[]; fields.labels = additionalFields.labels as string[];
} }
if (additionalFields.priority) { if (additionalFields.priority) {
body.fields.priority = { fields.priority = {
id: additionalFields.priority as string, id: additionalFields.priority as string,
}; };
} }
if (additionalFields.assignee) { if (additionalFields.assignee) {
body.fields.assignee = { fields.assignee = {
id: additionalFields.assignee as string, id: additionalFields.assignee as string,
}; };
} }
if (!parentIssueIdValue && issueTypeId === 'sub-task') { if (additionalFields.description) {
throw new Error('You must define a Parent ID/Key when Issue type is sub-task'); fields.description = additionalFields.description as string;
}
if (hasParentIssue) {
const parentIssueKey = this.getNodeParameter('parentIssueKey', i) as string;
if (!parentIssueKey && issueTypeId === 'sub-task') {
throw new Error('You must define a Parent Issue Key when Issue type is sub-task');
} else if (parentIssueIdValue && issueTypeId === 'sub-task') { } else if (parentIssueKey && issueTypeId === 'sub-task') {
if (parentIssueId === 'id') { fields.parent = {
body.fields.parent = { key: parentIssueKey,
id: parentIssueIdValue,
};
}
if (parentIssueId === 'key') {
body.fields.parent = {
key: parentIssueIdValue,
}; };
} }
} }
body.fields = fields;
try { try {
responseData = await jiraApiRequest.call(this, '/issue', 'POST', body); responseData = await jiraSoftwareCloudApiRequest.call(this, '/issue', 'POST', body);
} catch (err) { } catch (err) {
throw new Error(`Jira Error: ${JSON.stringify(err)}`); throw new Error(`Jira Error: ${JSON.stringify(err)}`);
} }

View File

@@ -43,7 +43,7 @@
"dist/credentials/HttpHeaderAuth.credentials.js", "dist/credentials/HttpHeaderAuth.credentials.js",
"dist/credentials/IntercomApi.credentials.js", "dist/credentials/IntercomApi.credentials.js",
"dist/credentials/Imap.credentials.js", "dist/credentials/Imap.credentials.js",
"dist/credentials/JiraApi.credentials.js", "dist/credentials/JiraSoftwareCloudApi.credentials.js",
"dist/credentials/LinkFishApi.credentials.js", "dist/credentials/LinkFishApi.credentials.js",
"dist/credentials/MailchimpApi.credentials.js", "dist/credentials/MailchimpApi.credentials.js",
"dist/credentials/MailgunApi.credentials.js", "dist/credentials/MailgunApi.credentials.js",
@@ -102,7 +102,7 @@
"dist/nodes/If.node.js", "dist/nodes/If.node.js",
"dist/nodes/Interval.node.js", "dist/nodes/Interval.node.js",
"dist/nodes/Intercom/Intercom.node.js", "dist/nodes/Intercom/Intercom.node.js",
"dist/nodes/Jira/Jira.node.js", "dist/nodes/Jira/JiraSoftwareCloud.node.js",
"dist/nodes/LinkFish/LinkFish.node.js", "dist/nodes/LinkFish/LinkFish.node.js",
"dist/nodes/Mailchimp/Mailchimp.node.js", "dist/nodes/Mailchimp/Mailchimp.node.js",
"dist/nodes/Mailgun/Mailgun.node.js", "dist/nodes/Mailgun/Mailgun.node.js",