Merge pull request #16833 from Roohn/master

Entity feedback plugin: Add aggregate results by entityRef
This commit is contained in:
Fredrik Adelöw
2023-03-16 14:09:38 +01:00
committed by GitHub
9 changed files with 100 additions and 7 deletions
+4
View File
@@ -13,6 +13,7 @@ import { EntityRatingsData } from '@backstage/plugin-entity-feedback-common';
import { FeedbackResponse } from '@backstage/plugin-entity-feedback-common';
import { FetchApi } from '@backstage/core-plugin-api';
import { Rating } from '@backstage/plugin-entity-feedback-common';
import { Ratings } from '@backstage/plugin-entity-feedback-common';
import { ReactNode } from 'react';
import { RouteRef } from '@backstage/core-plugin-api';
@@ -22,6 +23,7 @@ export interface EntityFeedbackApi {
getAllRatings(): Promise<EntityRatingsData[]>;
// (undocumented)
getOwnedRatings(ownerRef: string): Promise<EntityRatingsData[]>;
getRatingAggregates(entityRef: string): Promise<Ratings>;
// (undocumented)
getRatings(entityRef: string): Promise<Omit<Rating, 'entityRef'>[]>;
// (undocumented)
@@ -48,6 +50,8 @@ export class EntityFeedbackClient implements EntityFeedbackApi {
// (undocumented)
getOwnedRatings(ownerRef: string): Promise<EntityRatingsData[]>;
// (undocumented)
getRatingAggregates(entityRef: string): Promise<Ratings>;
// (undocumented)
getRatings(entityRef: string): Promise<Omit<Rating, 'entityRef'>[]>;
// (undocumented)
getResponses(
@@ -19,6 +19,7 @@ import {
EntityRatingsData,
FeedbackResponse,
Rating,
Ratings,
} from '@backstage/plugin-entity-feedback-common';
/**
@@ -40,6 +41,11 @@ export interface EntityFeedbackApi {
getRatings(entityRef: string): Promise<Omit<Rating, 'entityRef'>[]>;
/**
* Returns anonymized aggregated results for one entityRef
*/
getRatingAggregates(entityRef: string): Promise<Ratings>;
recordResponse(
entityRef: string,
response: Omit<FeedbackResponse, 'entityRef' | 'userRef'>,
@@ -121,6 +121,24 @@ describe('EntityFeedbackClient', () => {
expect(response).toEqual(ratings);
});
it('getRatingAggregates', async () => {
const ratings = { LIKE: 3, DISLIKE: 5 };
server.use(
rest.get(
`${mockBaseUrl}/ratings/${encodeURIComponent(
'component:default/service',
)}/aggregate`,
(_, res, ctx) => res(ctx.json(ratings)),
),
);
const response = await client.getRatingAggregates(
'component:default/service',
);
expect(response).toEqual(ratings);
});
it('recordResponse', async () => {
expect.assertions(1);
const response = {
@@ -20,6 +20,7 @@ import {
EntityRatingsData,
FeedbackResponse,
Rating,
Ratings,
} from '@backstage/plugin-entity-feedback-common';
import { EntityFeedbackApi } from './EntityFeedbackApi';
@@ -97,6 +98,22 @@ export class EntityFeedbackClient implements EntityFeedbackApi {
return resp.json();
}
async getRatingAggregates(entityRef: string): Promise<Ratings> {
const baseUrl = await this.discoveryApi.getBaseUrl('entity-feedback');
const resp = await this.fetchApi.fetch(
`${baseUrl}/ratings/${encodeURIComponent(entityRef)}/aggregate`,
{
method: 'GET',
},
);
if (!resp.ok) {
throw await ResponseError.fromResponse(resp);
}
return resp.json();
}
async recordResponse(
entityRef: string,
response: Omit<FeedbackResponse, 'entityRef' | 'userRef'>,