Skip to content

Commit

Permalink
Merge branch 'main' into #3361
Browse files Browse the repository at this point in the history
  • Loading branch information
alexwizp committed May 15, 2024
2 parents 68a9ecf + 47cdb1a commit 89f7c67
Show file tree
Hide file tree
Showing 49 changed files with 1,044 additions and 338 deletions.
2 changes: 1 addition & 1 deletion .buildkite/pipelines/artifacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ steps:
imageProject: elastic-images-qa
provider: gcp
machineType: c2-standard-16
timeout_in_minutes: 60
timeout_in_minutes: 75
retry:
automatic:
- exit_status: '*'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,73 @@

import { ElasticsearchClient } from '@kbn/core/server';

import { CONNECTORS_INDEX } from '..';
import { fetchConnectorById } from './fetch_connectors';
import { ConnectorStatus } from '../types/connectors';
import { errors } from '@elastic/elasticsearch';

import { updateConnectorConfiguration } from './update_connector_configuration';
import { fetchConnectorById } from './fetch_connectors';

jest.mock('./fetch_connectors', () => ({ fetchConnectorById: jest.fn() }));

describe('updateConnectorConfiguration lib function', () => {
const mockClient = {
update: jest.fn(),
transport: {
request: jest.fn(),
},
};

beforeEach(() => {
jest.clearAllMocks();
(fetchConnectorById as jest.Mock).mockResolvedValue({
configuration: { test: { label: 'haha', value: 'this' } },
id: 'connectorId',
status: ConnectorStatus.NEEDS_CONFIGURATION,
});
});

it('should update configuration', async () => {
(fetchConnectorById as jest.Mock).mockResolvedValue({
configuration: { test: { value: 'haha' } },
});

mockClient.transport.request.mockResolvedValueOnce({ result: 'updated' });

await expect(
updateConnectorConfiguration(mockClient as unknown as ElasticsearchClient, 'connectorId', {
test: 'newValue',
test: 'haha',
})
).resolves.toEqual({ test: { label: 'haha', value: 'newValue' } });
expect(mockClient.update).toHaveBeenCalledWith({
doc: {
configuration: { test: { label: 'haha', value: 'newValue' } },
status: ConnectorStatus.CONFIGURED,
).resolves.toEqual({ test: { value: 'haha' } });
expect(mockClient.transport.request).toHaveBeenCalledWith({
body: {
values: {
test: 'haha',
},
},
id: 'connectorId',
index: CONNECTORS_INDEX,
method: 'PUT',
path: '/_connector/connectorId/_configuration',
});
});

it('should reject if connector does not exist', async () => {
(fetchConnectorById as jest.Mock).mockImplementation(() => undefined);

mockClient.transport.request.mockImplementationOnce(() => {
return Promise.reject(
new errors.ResponseError({
statusCode: 404,
body: {
error: {
type: `document_missing_exception`,
},
},
} as any)
);
});
await expect(
updateConnectorConfiguration(mockClient as unknown as ElasticsearchClient, 'connectorId', {
test: 'newValue',
test: 'haha',
})
).rejects.toEqual(new Error('Could not find connector'));
expect(mockClient.update).not.toHaveBeenCalled();
).rejects.toEqual(
new errors.ResponseError({
statusCode: 404,
body: {
error: {
type: `document_missing_exception`,
},
},
} as any)
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,21 @@

import { ElasticsearchClient } from '@kbn/core/server';

import { i18n } from '@kbn/i18n';

import { CONNECTORS_INDEX } from '..';

import { Result } from '@elastic/elasticsearch/lib/api/types';
import { fetchConnectorById } from './fetch_connectors';
import { ConnectorConfiguration, ConnectorDocument, ConnectorStatus } from '../types/connectors';
import { isConfigEntry } from '../utils/is_category_entry';
import { isNotNullish } from '../utils/is_not_nullish';

export const updateConnectorConfiguration = async (
client: ElasticsearchClient,
connectorId: string,
configuration: Record<string, string | number | boolean>
) => {
await client.transport.request<Result>({
method: 'PUT',
path: `/_connector/${connectorId}/_configuration`,
body: {
values: configuration,
},
});
const connector = await fetchConnectorById(client, connectorId);
if (connector) {
const status =
connector.status === ConnectorStatus.NEEDS_CONFIGURATION ||
connector.status === ConnectorStatus.CREATED
? ConnectorStatus.CONFIGURED
: connector.status;
const updatedConfig: ConnectorConfiguration = Object.keys(connector.configuration)
.map((key) => {
const configEntry = connector.configuration[key];
return isConfigEntry(configEntry)
? {
...configEntry, // ugly but needed because typescript refuses to believe this is defined
key,
value: configuration[key] ?? configEntry.value,
}
: undefined;
})
.filter(isNotNullish)
.reduce((prev: ConnectorConfiguration, curr) => {
const { key, ...config } = curr;
return { ...prev, [curr.key]: config };
}, {});
await client.update<ConnectorDocument>({
doc: { configuration: updatedConfig, status },
id: connectorId,
index: CONNECTORS_INDEX,
});
return updatedConfig;
} else {
throw new Error(
i18n.translate('searchConnectors.server.connectors.configuration.error', {
defaultMessage: 'Could not find connector',
})
);
}
return connector?.configuration;
};
32 changes: 26 additions & 6 deletions packages/kbn-search-connectors/types/native_connectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -631,13 +631,33 @@ export const NATIVE_CONNECTOR_DEFINITIONS: Record<string, NativeConnector | unde
validations: [],
value: '*',
},
index_labels: {
default_value: null,
depends_on: [],
display: DisplayType.TOGGLE,
label: i18n.translate('searchConnectors.nativeConnectors.confluence.indexLabelsLabel', {
defaultMessage: 'Enable indexing labels',
}),
options: [],
order: 10,
required: true,
sensitive: false,
tooltip: i18n.translate('searchConnectors.nativeConnectors.confluence.indexLabelsTooltip', {
defaultMessage:
'Enabling this will increase the amount of network calls to the source, and may decrease performance',
}),
type: FieldType.BOOLEAN,
ui_restrictions: [],
validations: [],
value: false,
},
ssl_enabled: {
default_value: null,
depends_on: [],
display: DisplayType.TOGGLE,
label: ENABLE_SSL_LABEL,
options: [],
order: 10,
order: 11,
required: true,
sensitive: false,
tooltip: null,
Expand All @@ -657,7 +677,7 @@ export const NATIVE_CONNECTOR_DEFINITIONS: Record<string, NativeConnector | unde
display: DisplayType.TEXTBOX,
label: SSL_CERTIFICATE_LABEL,
options: [],
order: 11,
order: 12,
required: true,
sensitive: false,
tooltip: null,
Expand All @@ -672,7 +692,7 @@ export const NATIVE_CONNECTOR_DEFINITIONS: Record<string, NativeConnector | unde
display: DisplayType.NUMERIC,
label: RETRIES_PER_REQUEST_LABEL,
options: [],
order: 12,
order: 13,
required: false,
sensitive: false,
tooltip: null,
Expand All @@ -687,7 +707,7 @@ export const NATIVE_CONNECTOR_DEFINITIONS: Record<string, NativeConnector | unde
display: DisplayType.NUMERIC,
label: MAX_CONCURRENT_DOWNLOADS_LABEL,
options: [],
order: 13,
order: 14,
required: false,
sensitive: false,
tooltip: null,
Expand All @@ -712,7 +732,7 @@ export const NATIVE_CONNECTOR_DEFINITIONS: Record<string, NativeConnector | unde
display: DisplayType.TOGGLE,
label: ENABLE_DOCUMENT_LEVEL_SECURITY_LABEL,
options: [],
order: 14,
order: 15,
required: true,
sensitive: false,
tooltip: getEnableDocumentLevelSecurityTooltip(
Expand All @@ -731,7 +751,7 @@ export const NATIVE_CONNECTOR_DEFINITIONS: Record<string, NativeConnector | unde
display: DisplayType.TOGGLE,
label: USE_TEXT_EXTRACTION_SERVICE_LABEL,
options: [],
order: 15,
order: 16,
required: true,
sensitive: false,
tooltip: USE_TEXT_EXTRACTION_SERVICE_TOOLTIP,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ export const MonacoEditor = ({ initialTextValue }: EditorProps) => {
return actionsProvider.current!.getDocumentationLink(docLinkVersion);
}, [docLinkVersion]);

const autoIndentCallback = useCallback(async () => {
return actionsProvider.current!.autoIndent();
}, []);

const sendRequestsCallback = useCallback(async () => {
await actionsProvider.current?.sendRequests(toasts, dispatch, trackUiMetric, http);
}, [dispatch, http, toasts, trackUiMetric]);
Expand Down Expand Up @@ -125,7 +129,7 @@ export const MonacoEditor = ({ initialTextValue }: EditorProps) => {
<ConsoleMenu
getCurl={getCurlCallback}
getDocumentation={getDocumenationLink}
autoIndent={() => {}}
autoIndent={autoIndentCallback}
notifications={notifications}
/>
</EuiFlexItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,13 @@ import {
SELECTED_REQUESTS_CLASSNAME,
stringifyRequest,
trackSentRequests,
getAutoIndentedRequests,
} from './utils';

import type { AdjustedParsedRequest } from './types';

const AUTO_INDENTATION_ACTION_LABEL = 'Apply indentations';

export class MonacoEditorActionsProvider {
private parsedRequestsProvider: ConsoleParsedRequestsProvider;
private highlightedLines: monaco.editor.IEditorDecorationsCollection;
Expand Down Expand Up @@ -343,4 +346,57 @@ export class MonacoEditorActionsProvider {
): monaco.languages.ProviderResult<monaco.languages.CompletionList> {
return this.getSuggestions(model, position, context);
}

/*
This function returns the text in the provided range.
If no range is provided, it returns all text in the editor.
*/
private getTextInRange(selectionRange?: monaco.IRange): string {
const model = this.editor.getModel();
if (!model) {
return '';
}
if (selectionRange) {
const { startLineNumber, startColumn, endLineNumber, endColumn } = selectionRange;
return model.getValueInRange({
startLineNumber,
startColumn,
endLineNumber,
endColumn,
});
}
// If no range is provided, return all text in the editor
return model.getValue();
}

/**
* This function applies indentations to the request in the selected text.
*/
public async autoIndent() {
const parsedRequests = await this.getSelectedParsedRequests();
const selectionStartLineNumber = parsedRequests[0].startLineNumber;
const selectionEndLineNumber = parsedRequests[parsedRequests.length - 1].endLineNumber;
const selectedRange = new monaco.Range(
selectionStartLineNumber,
1,
selectionEndLineNumber,
this.editor.getModel()?.getLineMaxColumn(selectionEndLineNumber) ?? 1
);

if (parsedRequests.length < 1) {
return;
}

const selectedText = this.getTextInRange(selectedRange);
const allText = this.getTextInRange();

const autoIndentedText = getAutoIndentedRequests(parsedRequests, selectedText, allText);

this.editor.executeEdits(AUTO_INDENTATION_ACTION_LABEL, [
{
range: selectedRange,
text: autoIndentedText,
},
]);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ export {
replaceRequestVariables,
getCurlRequest,
trackSentRequests,
getAutoIndentedRequests,
} from './requests_utils';
export {
getDocumentationLinkFromAutocomplete,
Expand Down
Loading

0 comments on commit 89f7c67

Please sign in to comment.