Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/main' into tests-redesign
Browse files Browse the repository at this point in the history
  • Loading branch information
rshen91 committed May 15, 2024
2 parents aaf103d + 5e95a76 commit b07e2d1
Show file tree
Hide file tree
Showing 121 changed files with 3,171 additions and 571 deletions.
5 changes: 4 additions & 1 deletion .buildkite/pipelines/artifacts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,10 @@ steps:
- command: KIBANA_DOCKER_CONTEXT=chainguard .buildkite/scripts/steps/artifacts/docker_context.sh
label: 'Docker Context Verification'
agents:
queue: n2-2
image: family/kibana-ubuntu-2004
imageProject: elastic-images-qa
provider: gcp
machineType: n2-standard-2
timeout_in_minutes: 30
retry:
automatic:
Expand Down
8 changes: 4 additions & 4 deletions .buildkite/scripts/steps/artifacts/docker_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,7 @@ node scripts/build \
--skip-docker-chainguard \
--skip-docker-ubi \
--skip-docker-fips \
--skip-docker-cloud \
--skip-docker-contexts
--skip-docker-cloud

echo "--- Tag images"
docker rmi "$KIBANA_IMAGE"
Expand Down Expand Up @@ -102,8 +101,9 @@ ts-node "$(git rev-parse --show-toplevel)/.buildkite/scripts/steps/artifacts/val
echo "--- Upload archives"
buildkite-agent artifact upload "kibana-$BASE_VERSION-linux-x86_64.tar.gz"
buildkite-agent artifact upload "kibana-$BASE_VERSION-linux-aarch64.tar.gz"
buildkite-agent artifact upload "kibana-$BASE_VERSION-docker-image.tar.gz"
buildkite-agent artifact upload "kibana-$BASE_VERSION-docker-image-aarch64.tar.gz"
buildkite-agent artifact upload "kibana-serverless-$BASE_VERSION-docker-image.tar.gz"
buildkite-agent artifact upload "kibana-serverless-$BASE_VERSION-docker-image-aarch64.tar.gz"
buildkite-agent artifact upload "kibana-serverless-$BASE_VERSION-docker-build-context.tar.gz"
buildkite-agent artifact upload "kibana-$BASE_VERSION-cdn-assets.tar.gz"
buildkite-agent artifact upload "dependencies-$GIT_ABBREV_COMMIT.csv"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import React, { useMemo } from 'react';
import { css, CSSObject } from '@emotion/react';
import { EuiIcon, EuiToolTip } from '@elastic/eui';
import { EuiIconTip } from '@elastic/eui';
import type { DataView, DataViewField } from '@kbn/data-views-plugin/common';
import { FieldIcon, getFieldIconProps, getTextBasedColumnIconType } from '@kbn/field-utils';
import { isNestedFieldParent } from '@kbn/discover-utils';
Expand Down Expand Up @@ -129,11 +129,9 @@ export const DataTableTimeColumnHeader = ({
text-align: left;
`}
>
<EuiToolTip content={primaryTimeTooltip}>
<ColumnHeaderTruncateContainer headerRowHeight={headerRowHeight}>
{timeFieldName} <EuiIcon type="clock" />
</ColumnHeaderTruncateContainer>
</EuiToolTip>
<ColumnHeaderTruncateContainer headerRowHeight={headerRowHeight}>
{timeFieldName} <EuiIconTip type="clock" content={primaryTimeTooltip} />
</ColumnHeaderTruncateContainer>
</div>
);
};
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
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

import {
getAutoIndentedRequests,
getCurlRequest,
replaceRequestVariables,
stringifyRequest,
Expand Down Expand Up @@ -160,4 +161,195 @@ describe('requests_utils', () => {
expect(mockMetricsTracker.count).toHaveBeenNthCalledWith(2, 'POST__test');
});
});

describe('getAutoIndentedRequests', () => {
const sampleEditorTextLines = [
' ', // line 1
'GET _search ', // line 2
'{ ', // line 3
' "query": { ', // line 4
' "match_all": { } ', // line 5
' } ', // line 6
' } ', // line 7
' ', // line 8
'// single comment before Request 2 ', // line 9
' GET _all ', // line 10
' ', // line 11
'/* ', // line 12
' multi-line comment before Request 3', // line 13
'*/ ', // line 14
'POST /_bulk ', // line 15
'{ ', // line 16
' "index":{ ', // line 17
' "_index":"books" ', // line 18
' } ', // line 19
' } ', // line 20
'{ ', // line 21
'"name":"1984" ', // line 22
'}{"name":"Atomic habits"} ', // line 23
' ', // line 24
'GET _search // test comment ', // line 25
'{ ', // line 26
' "query": { ', // line 27
' "match_all": { } // comment', // line 28
' } ', // line 29
'} ', // line 30
' // some comment ', // line 31
' ', // line 32
];

const TEST_REQUEST_1 = {
method: 'GET',
url: '_search',
data: [{ query: { match_all: {} } }],
// Offsets are with respect to the sample editor text
startLineNumber: 2,
endLineNumber: 7,
startOffset: 1,
endOffset: 36,
};

const TEST_REQUEST_2 = {
method: 'GET',
url: '_all',
data: [],
// Offsets are with respect to the sample editor text
startLineNumber: 10,
endLineNumber: 10,
startOffset: 1,
endOffset: 36,
};

const TEST_REQUEST_3 = {
method: 'POST',
url: '/_bulk',
// Multi-data
data: [{ index: { _index: 'books' } }, { name: '1984' }, { name: 'Atomic habits' }],
// Offsets are with respect to the sample editor text
startLineNumber: 15,
endLineNumber: 23,
startOffset: 1,
endOffset: 36,
};

const TEST_REQUEST_4 = {
method: 'GET',
url: '_search',
data: [{ query: { match_all: {} } }],
// Offsets are with respect to the sample editor text
startLineNumber: 24,
endLineNumber: 30,
startOffset: 1,
endOffset: 36,
};

it('correctly auto-indents a single request with data', () => {
const formattedData = getAutoIndentedRequests(
[TEST_REQUEST_1],
sampleEditorTextLines
.slice(TEST_REQUEST_1.startLineNumber - 1, TEST_REQUEST_1.endLineNumber)
.join('\n'),
sampleEditorTextLines.join('\n')
);
const expectedResultLines = [
'GET _search',
'{',
' "query": {',
' "match_all": {}',
' }',
'}',
];

expect(formattedData).toBe(expectedResultLines.join('\n'));
});

it('correctly auto-indents a single request with no data', () => {
const formattedData = getAutoIndentedRequests(
[TEST_REQUEST_2],
sampleEditorTextLines
.slice(TEST_REQUEST_2.startLineNumber - 1, TEST_REQUEST_2.endLineNumber)
.join('\n'),
sampleEditorTextLines.join('\n')
);
const expectedResult = 'GET _all';

expect(formattedData).toBe(expectedResult);
});

it('correctly auto-indents a single request with multiple data', () => {
const formattedData = getAutoIndentedRequests(
[TEST_REQUEST_3],
sampleEditorTextLines
.slice(TEST_REQUEST_3.startLineNumber - 1, TEST_REQUEST_3.endLineNumber)
.join('\n'),
sampleEditorTextLines.join('\n')
);
const expectedResultLines = [
'POST /_bulk',
'{',
' "index": {',
' "_index": "books"',
' }',
'}',
'{',
' "name": "1984"',
'}',
'{',
' "name": "Atomic habits"',
'}',
];

expect(formattedData).toBe(expectedResultLines.join('\n'));
});

it('auto-indents multiple request with comments in between', () => {
const formattedData = getAutoIndentedRequests(
[TEST_REQUEST_1, TEST_REQUEST_2, TEST_REQUEST_3],
sampleEditorTextLines.slice(1, 23).join('\n'),
sampleEditorTextLines.join('\n')
);
const expectedResultLines = [
'GET _search',
'{',
' "query": {',
' "match_all": {}',
' }',
'}',
'',
'// single comment before Request 2',
'GET _all',
'',
'/*',
'multi-line comment before Request 3',
'*/',
'POST /_bulk',
'{',
' "index": {',
' "_index": "books"',
' }',
'}',
'{',
' "name": "1984"',
'}',
'{',
' "name": "Atomic habits"',
'}',
];

expect(formattedData).toBe(expectedResultLines.join('\n'));
});

it('does not auto-indent a request with comments', () => {
const requestText = sampleEditorTextLines
.slice(TEST_REQUEST_4.startLineNumber - 1, TEST_REQUEST_4.endLineNumber)
.join('\n');
const formattedData = getAutoIndentedRequests(
[TEST_REQUEST_4],
requestText,
sampleEditorTextLines.join('\n')
);

expect(formattedData).toBe(requestText);
});
});
});
Loading

0 comments on commit b07e2d1

Please sign in to comment.