n8n/packages/nodes-base/nodes/Jira/GenericFunctions.ts

163 lines
4.3 KiB
TypeScript
Raw Normal View History

2020-05-05 13:56:24 -07:00
import {
2020-04-23 22:59:19 -07:00
OptionsWithUri,
} from 'request';
2019-11-26 12:38:38 -08:00
import {
IExecuteFunctions,
2020-02-06 19:03:29 -08:00
IExecuteSingleFunctions,
2019-11-26 12:38:38 -08:00
IHookFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
2020-05-05 13:56:24 -07:00
import {
ICredentialDataDecryptedObject,
IDataObject,
2019-11-26 12:38:38 -08:00
} from 'n8n-workflow';
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
2020-02-02 07:01:56 -08:00
let data; let domain;
2020-05-05 13:56:24 -07:00
const jiraVersion = this.getNodeParameter('jiraVersion', 0) as string;
let jiraCredentials: ICredentialDataDecryptedObject | undefined;
if (jiraVersion === 'server') {
jiraCredentials = this.getCredentials('jiraSoftwareServerApi');
} else {
jiraCredentials = this.getCredentials('jiraSoftwareCloudApi');
}
2020-05-05 13:56:24 -07:00
if (jiraCredentials === undefined) {
2019-11-26 12:38:38 -08:00
throw new Error('No credentials got returned!');
}
2020-05-05 13:56:24 -07:00
if (jiraVersion === 'server') {
domain = jiraCredentials!.domain;
data = Buffer.from(`${jiraCredentials!.email}:${jiraCredentials!.password}`).toString('base64');
2020-02-02 07:01:56 -08:00
} else {
2020-05-05 13:56:24 -07:00
domain = jiraCredentials!.domain;
data = Buffer.from(`${jiraCredentials!.email}:${jiraCredentials!.apiToken}`).toString('base64');
2020-02-02 07:01:56 -08:00
}
2020-05-05 13:56:24 -07:00
2020-01-31 07:21:14 -08:00
const options: OptionsWithUri = {
2020-02-06 19:03:29 -08:00
headers: {
Authorization: `Basic ${data}`,
Accept: 'application/json',
'Content-Type': 'application/json',
},
2020-01-31 07:21:14 -08:00
method,
qs: query,
uri: uri || `${domain}/rest${endpoint}`,
2020-01-31 07:21:14 -08:00
body,
2020-10-22 06:46:03 -07:00
json: true,
2020-01-31 07:21:14 -08:00
};
2019-11-26 12:38:38 -08:00
2020-01-31 07:21:14 -08:00
try {
return await this.helpers.request!(options);
} catch (error) {
2020-04-24 00:50:56 -07:00
let errorMessage = error.message;
if (error.response.body) {
if (error.response.body.errorMessages && error.response.body.errorMessages.length) {
errorMessage = JSON.stringify(error.response.body.errorMessages);
} else {
errorMessage = error.response.body.message || error.response.body.error || error.response.body.errors || error.message;
}
}
if (typeof errorMessage !== 'string') {
errorMessage = JSON.stringify(errorMessage);
2020-01-31 07:21:14 -08:00
}
2020-04-24 00:50:56 -07:00
throw new Error(`Jira error response [${error.statusCode}]: ${errorMessage}`);
2020-01-31 07:21:14 -08:00
}
}
2019-11-26 12:38:38 -08:00
export async function jiraSoftwareCloudApiRequestAllItems(this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions, propertyName: string, endpoint: string, method: string, body: any = {}, query: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
2019-11-26 12:38:38 -08:00
const returnData: IDataObject[] = [];
let responseData;
2020-02-02 07:01:56 -08:00
query.startAt = 0;
body.startAt = 0;
2019-11-29 14:30:00 -08:00
query.maxResults = 100;
2020-02-02 07:01:56 -08:00
body.maxResults = 100;
2019-11-26 12:38:38 -08:00
do {
2020-02-02 07:01:56 -08:00
responseData = await jiraSoftwareCloudApiRequest.call(this, endpoint, method, body, query);
2019-11-26 12:38:38 -08:00
returnData.push.apply(returnData, responseData[propertyName]);
2020-02-02 07:01:56 -08:00
query.startAt = responseData.startAt + responseData.maxResults;
body.startAt = responseData.startAt + responseData.maxResults;
2019-11-26 12:38:38 -08:00
} while (
2020-02-02 07:01:56 -08:00
(responseData.startAt + responseData.maxResults < responseData.total)
2019-11-26 12:38:38 -08:00
);
return returnData;
}
export function validateJSON(json: string | undefined): any { // tslint:disable-line:no-any
let result;
try {
result = JSON.parse(json!);
} catch (exception) {
result = '';
}
return result;
}
export function eventExists (currentEvents : string[], webhookEvents: string[]) {
for (const currentEvent of currentEvents) {
if (!webhookEvents.includes(currentEvent)) {
return false;
}
}
return true;
}
export function getId (url: string) {
return url.split('/').pop();
}
export const allEvents = [
'board_created',
'board_updated',
'board_deleted',
'board_configuration_changed',
'comment_created',
'comment_updated',
'comment_deleted',
'jira:issue_created',
'jira:issue_updated',
'jira:issue_deleted',
'option_voting_changed',
'option_watching_changed',
'option_unassigned_issues_changed',
'option_subtasks_changed',
'option_attachments_changed',
'option_issuelinks_changed',
'option_timetracking_changed',
'project_created',
'project_updated',
'project_deleted',
'sprint_created',
'sprint_deleted',
'sprint_updated',
'sprint_started',
'sprint_closed',
'user_created',
'user_updated',
'user_deleted',
'jira:version_released',
'jira:version_unreleased',
'jira:version_created',
'jira:version_moved',
'jira:version_updated',
'jira:version_deleted',
'issuelink_created',
'issuelink_deleted',
'worklog_created',
'worklog_updated',
'worklog_deleted',
];