Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[8.x] [Tags] Expose TagsClient and utils on SOTagging server (#208109) #208237

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/platform/plugins/shared/dashboard/kibana.jsonc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
"home",
"spaces",
"savedObjectsTaggingOss",
"savedObjectsTagging",
"screenshotMode",
"usageCollection",
"taskManager",
Expand All @@ -51,4 +52,4 @@
"savedObjects"
]
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ describe('getSpaceAwareSaveobjectsClients', () => {
const mockedSavedObjectTagging = {
createInternalAssignmentService: jest.fn(),
createTagClient: jest.fn(),
getTagsFromReferences: jest.fn(),
convertTagNameToId: jest.fn(),
replaceTagReferences: jest.fn(),
};

const scoppedSoClient = savedObjectsClientMock.create();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,12 @@ export {
tagNameMaxLength,
tagDescriptionMaxLength,
} from './validation';
export {
convertTagNameToId,
getObjectTags,
getTag,
getTagIdsFromReferences,
getTagsFromReferences,
replaceTagReferences,
tagIdToReference,
} from './references';
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,16 @@
* 2.0.
*/

import { SavedObjectReference } from '@kbn/core/types';
import { tagIdToReference, replaceTagReferences, updateTagReferences } from './references';
import type { SavedObject, SavedObjectReference } from '@kbn/core/server';
import {
convertTagNameToId,
getObjectTags,
getTag,
getTagIdsFromReferences,
replaceTagReferences,
tagIdToReference,
updateTagReferences,
} from './references';

const ref = (type: string, id: string): SavedObjectReference => ({
id,
Expand All @@ -16,6 +24,80 @@ const ref = (type: string, id: string): SavedObjectReference => ({

const tagRef = (id: string) => ref('tag', id);

const createObject = (refs: SavedObjectReference[]): SavedObject => {
return {
type: 'unkown',
id: 'irrelevant',
references: refs,
} as SavedObject;
};

const createTag = (id: string, name: string = id) => ({
id,
name,
description: `desc ${id}`,
color: '#FFCC00',
managed: false,
});

const tag1 = createTag('id-1', 'name-1');
const tag2 = createTag('id-2', 'name-2');
const tag3 = createTag('id-3', 'name-3');

const allTags = [tag1, tag2, tag3];

describe('convertTagNameToId', () => {
it('returns the id for the given tag name', () => {
expect(convertTagNameToId('name-2', allTags)).toBe('id-2');
});

it('returns undefined if no tag was found', () => {
expect(convertTagNameToId('name-4', allTags)).toBeUndefined();
});
});

describe('getObjectTags', () => {
it('returns the tags for the tag references of the object', () => {
const { tags } = getObjectTags(
createObject([tagRef('id-1'), ref('dashboard', 'dash-1'), tagRef('id-3')]),
allTags
);

expect(tags).toEqual([tag1, tag3]);
});

it('returns the missing references for tags that were not found', () => {
const missingRef = tagRef('missing-tag');
const refs = [tagRef('id-1'), ref('dashboard', 'dash-1'), missingRef];
const { tags, missingRefs } = getObjectTags(createObject(refs), allTags);

expect(tags).toEqual([tag1]);
expect(missingRefs).toEqual([missingRef]);
});
});

describe('getTag', () => {
it('returns the tag for the given id', () => {
expect(getTag('id-2', allTags)).toEqual(tag2);
});
it('returns undefined if no tag was found', () => {
expect(getTag('id-4', allTags)).toBeUndefined();
});
});

describe('getTagIdsFromReferences', () => {
it('returns the tag ids from the given references', () => {
expect(
getTagIdsFromReferences([
tagRef('tag-1'),
ref('dashboard', 'dash-1'),
tagRef('tag-2'),
ref('lens', 'lens-1'),
])
).toEqual(['tag-1', 'tag-2']);
});
});

describe('tagIdToReference', () => {
it('returns a reference for given tag id', () => {
expect(tagIdToReference('some-tag-id')).toEqual({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,15 @@
*/

import { uniq, intersection } from 'lodash';
import { SavedObjectReference } from '@kbn/core/types';
import type {
SavedObject,
SavedObjectReference,
SavedObjectsFindOptionsReference,
} from '@kbn/core/server';
import { tagSavedObjectTypeName } from './constants';
import { Tag } from './types';

type SavedObjectReferenceLike = SavedObjectReference | SavedObjectsFindOptionsReference;

/**
* Create a {@link SavedObjectReference | reference} for given tag id.
Expand Down Expand Up @@ -64,3 +71,43 @@ export const updateTagReferences = ({

return [...nonTagReferences, ...newTagIds.map(tagIdToReference)];
};

export const getTagsFromReferences = (references: SavedObjectReference[], allTags: Tag[]) => {
const tagReferences = references.filter((ref) => ref.type === tagSavedObjectTypeName);

const foundTags: Tag[] = [];
const missingRefs: SavedObjectReference[] = [];

tagReferences.forEach((ref) => {
const found = allTags.find((tag) => tag.id === ref.id);
if (found) {
foundTags.push(found);
} else {
missingRefs.push(ref);
}
});

return {
tags: foundTags,
missingRefs,
};
};

export const convertTagNameToId = (tagName: string, allTags: Tag[]): string | undefined => {
const found = allTags.find((tag) => tag.name.toLowerCase() === tagName.toLowerCase());
return found?.id;
};

export const getObjectTags = (
object: { references: SavedObject['references'] },
allTags: Tag[]
) => {
return getTagsFromReferences(object.references, allTags);
};

export const getTag = (tagId: string, allTags: Tag[]): Tag | undefined => {
return allTags.find(({ id }) => id === tagId);
};
export const getTagIdsFromReferences = (references: SavedObjectReferenceLike[]): string[] => {
return references.filter((ref) => ref.type === tagSavedObjectTypeName).map(({ id }) => id);
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import useObservable from 'react-use/lib/useObservable';
import type { SavedObjectReference } from '@kbn/core/types';
import type { TagListComponentProps } from '@kbn/saved-objects-tagging-oss-plugin/public';
import type { Tag, TagWithOptionalId } from '../../../common/types';
import { getObjectTags } from '../../utils';
import { TagList } from '../base';
import type { ITagsCache } from '../../services';
import { byNameTagSorter } from '../../utils';
import { getObjectTags } from '../../../common';

interface SavedObjectTagListProps {
object: { references: SavedObjectReference[] };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
*/

import { SavedObjectsTaggingApiUi } from '@kbn/saved-objects-tagging-oss-plugin/public';
import { convertTagNameToId } from '../../common';
import { ITagsCache } from '../services';
import { convertTagNameToId } from '../utils';

export interface BuildConvertNameToReferenceOptions {
cache: ITagsCache;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ import {
GetTableColumnDefinitionOptions,
} from '@kbn/saved-objects-tagging-oss-plugin/public';
import { ITagsCache } from '../services';
import { getTagsFromReferences, byNameTagSorter } from '../utils';
import { byNameTagSorter } from '../utils';
import { getTagsFromReferences } from '../../common';

export interface BuildGetTableColumnDefinitionOptions {
components: SavedObjectsTaggingApiUiComponent;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import { ITagsCache, ITagInternalClient } from '../services';
import { StartServices } from '../types';
import {
getTagIdsFromReferences,
updateTagsReferences,
replaceTagReferences,
convertTagNameToId,
getTag,
} from '../utils';
} from '../../common';
import { getComponents } from './components';
import { buildGetTableColumnDefinition } from './get_table_column_definition';
import { buildGetSearchBarFilter } from './get_search_bar_filter';
Expand Down Expand Up @@ -51,7 +51,7 @@ export const getUiApi = ({
convertNameToReference: buildConvertNameToReference({ cache }),
getTagIdsFromReferences,
getTagIdFromName: (tagName: string) => convertTagNameToId(tagName, cache.getState()),
updateTagsReferences,
updateTagsReferences: replaceTagReferences,
getTag: (tagId: string) => getTag(tagId, cache.getState()),
getTagList,
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,7 @@
* 2.0.
*/

import { SavedObject, SavedObjectReference } from '@kbn/core/types';
import {
getObjectTags,
convertTagNameToId,
byNameTagSorter,
getTagIdsFromReferences,
getTag,
} from './utils';
import { byNameTagSorter } from './utils';

const createTag = (id: string, name: string = id) => ({
id,
Expand All @@ -22,67 +15,6 @@ const createTag = (id: string, name: string = id) => ({
managed: false,
});

const ref = (type: string, id: string): SavedObjectReference => ({
id,
type,
name: `${type}-ref-${id}`,
});

const tagRef = (id: string) => ref('tag', id);

const createObject = (refs: SavedObjectReference[]): SavedObject => {
return {
type: 'unkown',
id: 'irrelevant',
references: refs,
} as SavedObject;
};

const tag1 = createTag('id-1', 'name-1');
const tag2 = createTag('id-2', 'name-2');
const tag3 = createTag('id-3', 'name-3');

const allTags = [tag1, tag2, tag3];

describe('getObjectTags', () => {
it('returns the tags for the tag references of the object', () => {
const { tags } = getObjectTags(
createObject([tagRef('id-1'), ref('dashboard', 'dash-1'), tagRef('id-3')]),
allTags
);

expect(tags).toEqual([tag1, tag3]);
});

it('returns the missing references for tags that were not found', () => {
const missingRef = tagRef('missing-tag');
const refs = [tagRef('id-1'), ref('dashboard', 'dash-1'), missingRef];
const { tags, missingRefs } = getObjectTags(createObject(refs), allTags);

expect(tags).toEqual([tag1]);
expect(missingRefs).toEqual([missingRef]);
});
});

describe('convertTagNameToId', () => {
it('returns the id for the given tag name', () => {
expect(convertTagNameToId('name-2', allTags)).toBe('id-2');
});

it('returns undefined if no tag was found', () => {
expect(convertTagNameToId('name-4', allTags)).toBeUndefined();
});
});

describe('getTag', () => {
it('returns the tag for the given id', () => {
expect(getTag('id-2', allTags)).toEqual(tag2);
});
it('returns undefined if no tag was found', () => {
expect(getTag('id-4', allTags)).toBeUndefined();
});
});

describe('byNameTagSorter', () => {
it('sorts tags by name', () => {
const tags = [
Expand All @@ -97,16 +29,3 @@ describe('byNameTagSorter', () => {
expect(tags.map(({ id }) => id)).toEqual(['id-2', 'id-1', 'id-4', 'id-3']);
});
});

describe('getTagIdsFromReferences', () => {
it('returns the tag ids from the given references', () => {
expect(
getTagIdsFromReferences([
tagRef('tag-1'),
ref('dashboard', 'dash-1'),
tagRef('tag-2'),
ref('lens', 'lens-1'),
])
).toEqual(['tag-1', 'tag-2']);
});
});
Loading
Loading