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

84 lines
2.6 KiB
TypeScript
Raw Normal View History

2019-11-26 12:38:38 -08:00
import { OptionsWithUri } from 'request';
import {
IExecuteFunctions,
2020-02-06 19:03:29 -08:00
IExecuteSingleFunctions,
2019-11-26 12:38:38 -08:00
IHookFunctions,
ILoadOptionsFunctions,
} from 'n8n-core';
import {
IDataObject,
} 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;
const jiraCloudCredentials = this.getCredentials('jiraSoftwareCloudApi');
const jiraServerCredentials = this.getCredentials('jiraSoftwareServerApi');
2020-02-06 19:03:29 -08:00
if (jiraCloudCredentials === undefined && jiraServerCredentials === undefined) {
2019-11-26 12:38:38 -08:00
throw new Error('No credentials got returned!');
}
2020-02-02 07:01:56 -08:00
if (jiraCloudCredentials !== undefined) {
domain = jiraCloudCredentials!.domain;
2020-02-06 19:03:29 -08:00
data = Buffer.from(`${jiraCloudCredentials!.email}:${jiraCloudCredentials!.apiToken}`).toString('base64');
2020-02-02 07:01:56 -08:00
} else {
domain = jiraServerCredentials!.domain;
2020-02-06 19:03:29 -08:00
data = Buffer.from(`${jiraServerCredentials!.email}:${jiraServerCredentials!.password}`).toString('base64');
2020-02-02 07:01:56 -08: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,
2020-02-02 07:01:56 -08:00
uri: uri || `${domain}/rest/api/2${endpoint}`,
2020-01-31 07:21:14 -08:00
body,
json: true
};
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-02-02 07:01:56 -08:00
let errorMessage = error;
if (error.error && error.error.errorMessages) {
errorMessage = error.error.errorMessages;
2020-01-31 07:21:14 -08:00
}
2020-02-02 07:01:56 -08:00
throw new Error(errorMessage);
2020-01-31 07:21:14 -08:00
}
}
2019-11-26 12:38:38 -08:00
export async function jiraSoftwareCloudApiRequestAllItems(this: IHookFunctions | IExecuteFunctions, 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;
}