2023-01-27 03:22:44 -08:00
|
|
|
import type { OptionsWithUri } from 'request';
|
2020-12-02 02:24:25 -08:00
|
|
|
|
2023-01-27 03:22:44 -08:00
|
|
|
import type {
|
2023-03-09 09:13:15 -08:00
|
|
|
IDataObject,
|
2020-12-02 02:24:25 -08:00
|
|
|
IExecuteFunctions,
|
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
2023-03-09 09:13:15 -08:00
|
|
|
} from 'n8n-workflow';
|
2020-12-02 02:24:25 -08:00
|
|
|
|
2024-01-15 06:45:33 -08:00
|
|
|
import moment from 'moment-timezone';
|
2020-12-02 02:24:25 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
export async function cortexApiRequest(
|
2023-08-16 06:52:41 -07:00
|
|
|
this: IHookFunctions | IExecuteFunctions | ILoadOptionsFunctions,
|
2022-08-01 13:47:55 -07:00
|
|
|
method: string,
|
|
|
|
resource: string,
|
2022-12-02 06:25:21 -08:00
|
|
|
|
2022-08-01 13:47:55 -07:00
|
|
|
body: any = {},
|
|
|
|
query: IDataObject = {},
|
|
|
|
uri?: string,
|
|
|
|
option: IDataObject = {},
|
|
|
|
): Promise<any> {
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('cortexApi');
|
2020-12-02 02:24:25 -08:00
|
|
|
|
|
|
|
let options: OptionsWithUri = {
|
2022-06-28 23:42:35 -07:00
|
|
|
headers: {},
|
2020-12-02 02:24:25 -08:00
|
|
|
method,
|
|
|
|
qs: query,
|
2023-01-19 04:37:19 -08:00
|
|
|
uri: uri || `${credentials.host}/api${resource}`,
|
2020-12-02 02:24:25 -08:00
|
|
|
body,
|
|
|
|
json: true,
|
|
|
|
};
|
|
|
|
if (Object.keys(option).length !== 0) {
|
2020-12-02 02:54:10 -08:00
|
|
|
options = Object.assign({}, options, option);
|
2020-12-02 02:24:25 -08:00
|
|
|
}
|
2023-02-27 19:39:43 -08:00
|
|
|
if (Object.keys(body as IDataObject).length === 0) {
|
2020-12-02 02:24:25 -08:00
|
|
|
delete options.body;
|
|
|
|
}
|
2020-12-02 02:54:10 -08:00
|
|
|
if (Object.keys(query).length === 0) {
|
2020-12-02 02:24:25 -08:00
|
|
|
delete options.qs;
|
|
|
|
}
|
|
|
|
|
2024-01-17 07:08:50 -08:00
|
|
|
return await this.helpers.requestWithAuthentication.call(this, 'cortexApi', options);
|
2020-12-02 02:24:25 -08:00
|
|
|
}
|
|
|
|
|
2020-12-02 02:54:10 -08:00
|
|
|
export function getEntityLabel(entity: IDataObject): string {
|
2020-12-02 02:24:25 -08:00
|
|
|
let label = '';
|
|
|
|
switch (entity._type) {
|
2020-12-02 02:54:10 -08:00
|
|
|
case 'case':
|
2020-12-02 02:24:25 -08:00
|
|
|
label = `#${entity.caseId} ${entity.title}`;
|
|
|
|
break;
|
2020-12-02 02:54:10 -08:00
|
|
|
case 'case_artifact':
|
2020-12-02 02:24:25 -08:00
|
|
|
//@ts-ignore
|
2022-08-01 13:47:55 -07:00
|
|
|
label = `[${entity.dataType}] ${entity.data ? entity.data : entity.attachment.name}`;
|
2020-12-02 02:24:25 -08:00
|
|
|
break;
|
2020-12-02 02:54:10 -08:00
|
|
|
case 'alert':
|
2020-12-02 02:24:25 -08:00
|
|
|
label = `[${entity.source}:${entity.sourceRef}] ${entity.title}`;
|
|
|
|
break;
|
2020-12-02 02:54:10 -08:00
|
|
|
case 'case_task_log':
|
2020-12-02 02:24:25 -08:00
|
|
|
label = `${entity.message} from ${entity.createdBy}`;
|
|
|
|
break;
|
2020-12-02 02:54:10 -08:00
|
|
|
case 'case_task':
|
2020-12-02 02:24:25 -08:00
|
|
|
label = `${entity.title} (${entity.status})`;
|
|
|
|
break;
|
2020-12-02 02:54:10 -08:00
|
|
|
case 'job':
|
2020-12-02 02:24:25 -08:00
|
|
|
label = `${entity.analyzerName} (${entity.status})`;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return label;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function splitTags(tags: string): string[] {
|
2022-08-01 13:47:55 -07:00
|
|
|
return tags.split(',').filter((tag) => tag !== ' ' && tag);
|
2020-12-02 02:24:25 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
export function prepareParameters(values: IDataObject): IDataObject {
|
|
|
|
const response: IDataObject = {};
|
|
|
|
for (const key in values) {
|
2020-12-02 02:54:10 -08:00
|
|
|
if (values[key] !== undefined && values[key] !== null && values[key] !== '') {
|
2020-12-02 02:24:25 -08:00
|
|
|
if (moment(values[key] as string, moment.ISO_8601).isValid()) {
|
|
|
|
response[key] = Date.parse(values[key] as string);
|
|
|
|
} else if (key === 'tags') {
|
|
|
|
response[key] = splitTags(values[key] as string);
|
|
|
|
} else {
|
|
|
|
response[key] = values[key];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return response;
|
|
|
|
}
|