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

feat: filter by tags #5163

Merged
merged 2 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 6 additions & 1 deletion src/lib/features/feature-search/feature-search-controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import Controller from '../../routes/controller';
import { FeatureSearchService, OpenApiService } from '../../services';
import {
IFlagResolver,
ITag,
IUnleashConfig,
IUnleashServices,
NONE,
Expand Down Expand Up @@ -71,13 +72,17 @@ export default class FeatureSearchController extends Controller {
res: Response,
): Promise<void> {
if (this.config.flagResolver.isEnabled('featureSearchAPI')) {
const { query, projectId, type } = req.query;
const { query, projectId, type, tag } = req.query;
const userId = req.user.id;
const normalizedTag = tag
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this actually tags since it's an array?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have to decide whether our query params use singular or plural form. Currently I stick to singular because in the url it looks like this: tag[]=simple:tag1&tag[]=simple:tag2

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we decide to make plural then I'll adjust all query params in one go. For now I prefer consistency

?.map((t) => t.split(':'))
.filter((t) => t.length === 2);
const features = await this.featureSearchService.search({
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
?.map((t) => t.split(':'))
.filter((t) => t.length === 2);
const features = await this.featureSearchService.search({
?.map((tag) => tag.split(':'))
.filter((tag) => tag.length === 2);
const features = await this.featureSearchService.search({

query,
projectId,
type,
userId,
tag: normalizedTag,
});
res.json({ features });
} else {
Expand Down
1 change: 1 addition & 0 deletions src/lib/features/feature-search/feature-search-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ export class FeatureSearchService {
query: params.query,
userId: params.userId,
type: params.type,
tag: params.tag,
});

return features;
Expand Down
31 changes: 31 additions & 0 deletions src/lib/features/feature-search/feature.search.e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ const filterFeaturesByType = async (types: string[], expectedCode = 200) => {
.expect(expectedCode);
};

const filterFeaturesByTag = async (tags: string[], expectedCode = 200) => {
const tagParams = tags.map((tag) => `tag[]=${tag}`).join('&');
return app.request
.get(`/api/admin/search/features?${tagParams}`)
.expect(expectedCode);
};

const searchFeaturesWithoutQueryParams = async (expectedCode = 200) => {
return app.request.get(`/api/admin/search/features`).expect(expectedCode);
};
Expand Down Expand Up @@ -80,6 +87,30 @@ test('should filter features by type', async () => {
});
});

test('should filter features by tag', async () => {
await app.createFeature('my_feature_a');
await app.createFeature('my_feature_b');
await app.addTag('my_feature_a', { type: 'simple', value: 'my_tag' });

const { body } = await filterFeaturesByTag(['simple:my_tag']);

expect(body).toMatchObject({
features: [{ name: 'my_feature_a' }],
});
});

test('filter with invalid tag should ignore filter', async () => {
await app.createFeature('my_feature_a');
await app.createFeature('my_feature_b');
await app.addTag('my_feature_a', { type: 'simple', value: 'my_tag' });

const { body } = await filterFeaturesByTag(['simple']);

expect(body).toMatchObject({
features: [{ name: 'my_feature_a' }, { name: 'my_feature_b' }],
});
});

test('should search matching features by tag', async () => {
await app.createFeature('my_feature_a');
await app.createFeature('my_feature_b');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,7 @@ class FeatureStrategiesStore implements IFeatureStrategiesStore {
userId,
query: queryString,
type,
tag,
}: IFeatureSearchParams): Promise<IFeatureOverview[]> {
let query = this.db('features');
if (projectId) {
Expand All @@ -542,6 +543,13 @@ class FeatureStrategiesStore implements IFeatureStrategiesStore {
.whereILike('features.name', `%${queryString}%`)
.orWhereIn('features.name', tagQuery);
}
if (tag && tag.length > 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again, plural here?

const tagQuery = this.db
.from('feature_tag')
.select('feature_name')
.whereIn(['tag_type', 'tag_value'], tag);
query = query.whereIn('features.name', tagQuery);
}
if (type) {
query = query.whereIn('features.type', type);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
IFeatureOverview,
IFeatureStrategy,
IStrategyConfig,
ITag,
IVariant,
} from '../../../types/model';
import { Store } from '../../../types/stores/store';
Expand All @@ -25,6 +26,7 @@ export interface IFeatureSearchParams {
query?: string;
projectId?: string;
type?: string[];
tag?: string[][];
}

export interface IFeatureStrategiesStore
Expand Down
13 changes: 13 additions & 0 deletions src/lib/openapi/spec/feature-search-query-parameters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,19 @@ export const featureSearchQueryParameters = [
description: 'The list of feature types to filter by',
in: 'query',
},
{
name: 'tag',
schema: {
type: 'array',
items: {
type: 'string',
example: 'simple:my_tag',
},
},
description:
'The list of feature tags to filter by. Feature tag has to specify type and value joined with a colon.',
in: 'query',
},
] as const;

export type FeatureSearchQueryParameters = Partial<
Expand Down
Loading