1
0
mirror of https://github.com/Unleash/unleash.git synced 2024-12-22 19:07:54 +01:00

feat: creates a PUT endpoint for updating tags (#3265)

<!-- Thanks for creating a PR! To make it easier for reviewers and
everyone else to understand what your changes relate to, please add some
relevant content to the headings below. Feel free to ignore or delete
sections that you don't think are relevant. Thank you! ❤️ -->
- Create UpdateTagsSchema
- Create PUT endpoint
## About the changes
<!-- Describe the changes introduced. What are they and why are they
being introduced? Feel free to also add screenshots or steps to view the
changes if they're visual. -->

<!-- Does it close an issue? Multiple? -->
Relates to#
https://linear.app/unleash/issue/1-767/refactor-existing-tag-component-to-also-allow-removing-tags

<!-- (For internal contributors): Does it relate to an issue on public
roadmap? -->
<!--
Relates to [roadmap](https://github.com/orgs/Unleash/projects/10) item:
#
-->

### Important files
<!-- PRs can contain a lot of changes, but not all changes are equally
important. Where should a reviewer start looking to get an overview of
the changes? Are any files particularly important? -->


## Discussion points
<!-- Anything about the PR you'd like to discuss before it gets merged?
Got any questions or doubts? -->

---------

Signed-off-by: andreas-unleash <andreas@getunleash.ai>
This commit is contained in:
andreas-unleash 2023-03-07 16:50:01 +02:00 committed by GitHub
parent 2e11c3be81
commit 5f41b51b10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 176 additions and 0 deletions

View File

@ -140,6 +140,7 @@ import { maintenanceSchema } from './spec/maintenance-schema';
import { bulkRegistrationSchema } from './spec/bulk-registration-schema';
import { bulkMetricsSchema } from './spec/bulk-metrics-schema';
import { clientMetricsEnvSchema } from './spec/client-metrics-env-schema';
import { updateTagsSchema } from './spec/update-tags-schema';
// All schemas in `openapi/spec` should be listed here.
export const schemas = {
@ -261,6 +262,7 @@ export const schemas = {
updateFeatureStrategySchema,
updateTagTypeSchema,
updateUserSchema,
updateTagsSchema,
upsertContextFieldSchema,
upsertStrategySchema,
userSchema,

View File

@ -0,0 +1,30 @@
import { FromSchema } from 'json-schema-to-ts';
import { tagSchema } from './tag-schema';
export const updateTagsSchema = {
$id: '#/components/schemas/updateTagsSchema',
type: 'object',
additionalProperties: false,
required: ['addedTags', 'removedTags'],
properties: {
addedTags: {
type: 'array',
items: {
$ref: '#/components/schemas/tagSchema',
},
},
removedTags: {
type: 'array',
items: {
$ref: '#/components/schemas/tagSchema',
},
},
},
components: {
schemas: {
tagSchema,
},
},
} as const;
export type UpdateTagsSchema = FromSchema<typeof updateTagsSchema>;

View File

@ -30,6 +30,7 @@ import {
resourceCreatedResponseSchema,
} from '../../openapi/util/create-response-schema';
import { emptyResponse } from '../../openapi/util/standard-responses';
import { UpdateTagsSchema } from '../../openapi/spec/update-tags-schema';
const version = 1;
@ -133,6 +134,23 @@ class FeatureController extends Controller {
],
});
this.route({
method: 'put',
path: '/:featureName/tags',
permission: UPDATE_FEATURE,
handler: this.updateTags,
middleware: [
openApiService.validPath({
tags: ['Features'],
operationId: 'updateTags',
requestBody: createRequestSchema('updateTagsSchema'),
responses: {
200: resourceCreatedResponseSchema('tagsSchema'),
},
}),
],
});
this.route({
method: 'delete',
path: '/:featureName/tags/:type/:value',
@ -230,6 +248,35 @@ class FeatureController extends Controller {
res.status(201).header('location', `${featureName}/tags`).json(tag);
}
async updateTags(
req: IAuthRequest<
{ featureName: string },
Response<TagsSchema>,
UpdateTagsSchema,
any
>,
res: Response<TagsSchema>,
): Promise<void> {
const { featureName } = req.params;
const { addedTags, removedTags } = req.body;
const userName = extractUsername(req);
await Promise.all(
addedTags.map((addedTag) =>
this.tagService.addTag(featureName, addedTag, userName),
),
);
await Promise.all(
removedTags.map((removedTag) =>
this.tagService.removeTag(featureName, removedTag, userName),
),
);
const tags = await this.tagService.listTags(featureName);
res.json({ version, tags });
}
// TODO
async removeTag(
req: IAuthRequest<{ featureName: string; type: string; value: string }>,

View File

@ -11,6 +11,7 @@ import {
IVariant,
} from '../../../../lib/types/model';
import { randomId } from '../../../../lib/util/random-id';
import { UpdateTagsSchema } from '../../../../lib/openapi/spec/update-tags-schema';
let app: IUnleashTest;
let db: ITestDb;
@ -842,3 +843,29 @@ test('should have access to the get all features endpoint even if api is disable
.get('/api/admin/features')
.expect(200);
});
test('Can add and remove tags at the same time', async () => {
const tag = { type: 'simple', value: 'addremove-first-tag' };
const secondTag = { type: 'simple', value: 'addremove-second-tag' };
await db.stores.tagStore.createTag(tag);
await db.stores.tagStore.createTag(secondTag);
const taggedWithFirst = await db.stores.featureToggleStore.create(
'default',
{
name: 'tagged-with-first-tag-1',
},
);
const data: UpdateTagsSchema = {
addedTags: [secondTag],
removedTags: [tag],
};
await db.stores.featureTagStore.tagFeature(taggedWithFirst.name, tag);
await app.request
.put(`/api/admin/features/${taggedWithFirst.name}/tags`)
.send(data)
.expect((res) => {
expect(res.body.tags).toHaveLength(1);
});
});

View File

@ -3853,6 +3853,28 @@ Stats are divided into current and previous **windows**.
},
"type": "object",
},
"updateTagsSchema": {
"additionalProperties": false,
"properties": {
"addedTags": {
"items": {
"$ref": "#/components/schemas/tagSchema",
},
"type": "array",
},
"removedTags": {
"items": {
"$ref": "#/components/schemas/tagSchema",
},
"type": "array",
},
},
"required": [
"addedTags",
"removedTags",
],
"type": "object",
},
"updateUserSchema": {
"additionalProperties": true,
"properties": {
@ -5250,6 +5272,54 @@ If the provided project does not exist, the list of events will be empty.",
"Features",
],
},
"put": {
"operationId": "updateTags",
"parameters": [
{
"in": "path",
"name": "featureName",
"required": true,
"schema": {
"type": "string",
},
},
],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/updateTagsSchema",
},
},
},
"description": "updateTagsSchema",
"required": true,
},
"responses": {
"200": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/tagsSchema",
},
},
},
"description": "The resource was successfully created.",
"headers": {
"location": {
"description": "The location of the newly created resource.",
"schema": {
"format": "uri",
"type": "string",
},
},
},
},
},
"tags": [
"Features",
],
},
},
"/api/admin/features/{featureName}/tags/{type}/{value}": {
"delete": {