2022-08-17 08:50:24 -07:00
|
|
|
import { OptionsWithUri } from 'request';
|
2020-09-07 08:56:14 -07:00
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
|
|
|
IExecuteSingleFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
IWebhookFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
import { IDataObject, NodeApiError } from 'n8n-workflow';
|
|
|
|
|
|
|
|
export async function sentryIoApiRequest(
|
|
|
|
this:
|
|
|
|
| IHookFunctions
|
|
|
|
| IExecuteFunctions
|
|
|
|
| IExecuteSingleFunctions
|
|
|
|
| ILoadOptionsFunctions
|
|
|
|
| IWebhookFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
body: any = {},
|
|
|
|
qs: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
option: IDataObject = {},
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
): Promise<any> {
|
2020-09-07 08:56:14 -07:00
|
|
|
const authentication = this.getNodeParameter('authentication', 0);
|
|
|
|
|
2020-09-17 14:06:58 -07:00
|
|
|
const version = this.getNodeParameter('sentryVersion', 0);
|
|
|
|
|
2020-09-07 08:56:14 -07:00
|
|
|
const options: OptionsWithUri = {
|
|
|
|
headers: {},
|
|
|
|
method,
|
|
|
|
qs,
|
|
|
|
body,
|
2021-03-21 04:19:15 -07:00
|
|
|
uri: uri || `https://sentry.io${resource}`,
|
2020-10-22 06:46:03 -07:00
|
|
|
json: true,
|
2020-09-07 08:56:14 -07:00
|
|
|
};
|
|
|
|
if (!Object.keys(body).length) {
|
|
|
|
delete options.body;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Object.keys(option).length !== 0) {
|
|
|
|
Object.assign(options, option);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (options.qs.limit) {
|
|
|
|
delete options.qs.limit;
|
|
|
|
}
|
|
|
|
|
2020-09-17 14:06:58 -07:00
|
|
|
let credentialName;
|
|
|
|
|
2020-09-07 08:56:14 -07:00
|
|
|
try {
|
|
|
|
if (authentication === 'accessToken') {
|
2020-09-17 14:06:58 -07:00
|
|
|
if (version === 'cloud') {
|
|
|
|
credentialName = 'sentryIoApi';
|
|
|
|
} else {
|
|
|
|
credentialName = 'sentryIoServerApi';
|
|
|
|
}
|
|
|
|
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials(credentialName);
|
2020-09-17 14:06:58 -07:00
|
|
|
|
2022-04-14 23:00:47 -07:00
|
|
|
if (credentials.url) {
|
2020-09-17 14:06:58 -07:00
|
|
|
options.uri = `${credentials?.url}${resource}`;
|
|
|
|
}
|
2020-09-07 08:56:14 -07:00
|
|
|
|
|
|
|
options.headers = {
|
|
|
|
Authorization: `Bearer ${credentials?.token}`,
|
|
|
|
};
|
|
|
|
|
|
|
|
//@ts-ignore
|
|
|
|
return this.helpers.request(options);
|
|
|
|
} else {
|
|
|
|
return await this.helpers.requestOAuth2!.call(this, 'sentryIoOAuth2Api', options);
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
2020-09-07 08:56:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-17 08:50:24 -07:00
|
|
|
export async function sentryApiRequestAllItems(
|
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
|
|
|
method: string,
|
|
|
|
resource: string,
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
// tslint:disable-next-line:no-any
|
|
|
|
): Promise<any> {
|
2020-09-07 08:56:14 -07:00
|
|
|
const returnData: IDataObject[] = [];
|
|
|
|
|
|
|
|
let responseData;
|
|
|
|
|
|
|
|
let link;
|
|
|
|
|
|
|
|
let uri: string | undefined;
|
|
|
|
|
|
|
|
do {
|
2022-08-17 08:50:24 -07:00
|
|
|
responseData = await sentryIoApiRequest.call(this, method, resource, body, query, uri, {
|
|
|
|
resolveWithFullResponse: true,
|
|
|
|
});
|
2020-09-07 08:56:14 -07:00
|
|
|
link = responseData.headers.link;
|
|
|
|
uri = getNext(link);
|
|
|
|
returnData.push.apply(returnData, responseData.body);
|
2022-08-17 08:50:24 -07:00
|
|
|
if (query.limit && query.limit >= returnData.length) {
|
2020-09-07 08:56:14 -07:00
|
|
|
return;
|
|
|
|
}
|
2022-08-17 08:50:24 -07:00
|
|
|
} while (hasMore(link));
|
2020-09-07 08:56:14 -07:00
|
|
|
|
|
|
|
return returnData;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getNext(link: string) {
|
2021-03-21 04:19:15 -07:00
|
|
|
if (link === undefined) {
|
2020-09-07 08:56:14 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const next = link.split(',')[1];
|
|
|
|
if (next.includes('rel="next"')) {
|
2021-03-21 04:19:15 -07:00
|
|
|
return next.split(';')[0].replace('<', '').replace('>', '').trim();
|
2020-09-07 08:56:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function hasMore(link: string) {
|
2021-03-21 04:19:15 -07:00
|
|
|
if (link === undefined) {
|
2020-09-07 08:56:14 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
const next = link.split(',')[1];
|
|
|
|
if (next.includes('rel="next"')) {
|
|
|
|
return next.includes('results="true"');
|
|
|
|
}
|
|
|
|
}
|