mirror of
https://github.com/n8n-io/n8n.git
synced 2024-11-10 14:44:05 -08:00
84 lines
2.7 KiB
TypeScript
84 lines
2.7 KiB
TypeScript
import { OptionsWithUri } from 'request';
|
|
|
|
import {
|
|
IExecuteFunctions,
|
|
IHookFunctions,
|
|
ILoadOptionsFunctions,
|
|
IExecuteSingleFunctions,
|
|
BINARY_ENCODING
|
|
} 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
|
|
let data; let domain;
|
|
const jiraCloudCredentials = this.getCredentials('jiraSoftwareCloudApi');
|
|
const jiraServerCredentials = this.getCredentials('jiraSoftwareServerApi');
|
|
if (jiraCloudCredentials === undefined
|
|
&& jiraServerCredentials === undefined) {
|
|
throw new Error('No credentials got returned!');
|
|
}
|
|
if (jiraCloudCredentials !== undefined) {
|
|
domain = jiraCloudCredentials!.domain;
|
|
data = Buffer.from(`${jiraCloudCredentials!.email}:${jiraCloudCredentials!.apiToken}`).toString(BINARY_ENCODING);
|
|
} else {
|
|
domain = jiraServerCredentials!.domain;
|
|
data = Buffer.from(`${jiraServerCredentials!.email}:${jiraServerCredentials!.password}`).toString(BINARY_ENCODING);
|
|
}
|
|
const headerWithAuthentication = Object.assign({},
|
|
{ Authorization: `Basic ${data}`, Accept: 'application/json', 'Content-Type': 'application/json' });
|
|
const options: OptionsWithUri = {
|
|
headers: headerWithAuthentication,
|
|
method,
|
|
qs: query,
|
|
uri: uri || `${domain}/rest/api/2${endpoint}`,
|
|
body,
|
|
json: true
|
|
};
|
|
|
|
try {
|
|
return await this.helpers.request!(options);
|
|
} catch (error) {
|
|
let errorMessage = error;
|
|
if (error.error && error.error.errorMessages) {
|
|
errorMessage = error.error.errorMessages;
|
|
}
|
|
throw new Error(errorMessage);
|
|
}
|
|
}
|
|
|
|
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[] = [];
|
|
|
|
let responseData;
|
|
|
|
query.startAt = 0;
|
|
body.startAt = 0;
|
|
query.maxResults = 100;
|
|
body.maxResults = 100;
|
|
|
|
do {
|
|
responseData = await jiraSoftwareCloudApiRequest.call(this, endpoint, method, body, query);
|
|
returnData.push.apply(returnData, responseData[propertyName]);
|
|
query.startAt = responseData.startAt + responseData.maxResults;
|
|
body.startAt = responseData.startAt + responseData.maxResults;
|
|
} while (
|
|
(responseData.startAt + responseData.maxResults < responseData.total)
|
|
);
|
|
|
|
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;
|
|
}
|