2020-12-02 02:24:25 -08:00
|
|
|
import {
|
|
|
|
OptionsWithUri,
|
|
|
|
} from 'request';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IAnalyzer,
|
|
|
|
IJob,
|
|
|
|
IResponder,
|
|
|
|
} from './AnalyzerInterface';
|
|
|
|
|
|
|
|
import {
|
|
|
|
IExecuteFunctions,
|
2020-12-02 02:54:10 -08:00
|
|
|
IExecuteSingleFunctions,
|
2020-12-02 02:24:25 -08:00
|
|
|
IHookFunctions,
|
|
|
|
ILoadOptionsFunctions,
|
|
|
|
} from 'n8n-core';
|
|
|
|
|
|
|
|
import {
|
2021-04-16 09:33:36 -07:00
|
|
|
IDataObject, NodeApiError, NodeOperationError,
|
2020-12-02 02:24:25 -08:00
|
|
|
} from 'n8n-workflow';
|
|
|
|
|
2022-04-08 14:32:08 -07:00
|
|
|
import moment from 'moment';
|
2020-12-02 02:24:25 -08:00
|
|
|
|
|
|
|
export async function cortexApiRequest(this: IHookFunctions | IExecuteFunctions | IExecuteSingleFunctions | ILoadOptionsFunctions, method: string, resource: string, body: any = {}, query: IDataObject = {}, uri?: string, option: IDataObject = {}): Promise<any> { // tslint:disable-line:no-any
|
|
|
|
|
2021-08-20 09:57:30 -07:00
|
|
|
const credentials = await this.getCredentials('cortexApi');
|
2020-12-02 02:24:25 -08:00
|
|
|
|
2020-12-02 02:54:10 -08:00
|
|
|
const headerWithAuthentication = Object.assign({}, { Authorization: ` Bearer ${credentials.cortexApiKey}` });
|
2020-12-02 02:24:25 -08:00
|
|
|
|
|
|
|
let options: OptionsWithUri = {
|
|
|
|
headers: headerWithAuthentication,
|
|
|
|
method,
|
|
|
|
qs: query,
|
|
|
|
uri: uri || `${credentials.host}/api${resource}`,
|
|
|
|
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
|
|
|
}
|
2020-12-02 02:54:10 -08:00
|
|
|
if (Object.keys(body).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;
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
|
|
|
return await this.helpers.request!(options);
|
|
|
|
} catch (error) {
|
2021-04-16 09:33:36 -07:00
|
|
|
throw new NodeApiError(this.getNode(), error);
|
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
|
2020-12-02 02:54:10 -08: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[] {
|
|
|
|
return tags.split(',').filter(tag => tag !== ' ' && tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|