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

102 lines
3.1 KiB
TypeScript
Raw Normal View History

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';
import {
2019-11-26 12:38:38 -08:00
IDataObject,
ICredentialDataDecryptedObject,
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;
let jiraCloudCredentials: ICredentialDataDecryptedObject | undefined;
try {
jiraCloudCredentials = this.getCredentials('jiraSoftwareCloudApi');
} catch (error) {
}
2020-02-02 07:01:56 -08:00
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-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, 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;
}