AnnotationsApi: GET /api/annotations/:annotationId (#47739)

This commit is contained in:
Scott Bock 2022-05-16 10:16:36 -05:00 committed by GitHub
parent 2d4065600c
commit 3d922a4e67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 55 additions and 0 deletions

View File

@ -351,6 +351,26 @@ func (hs *HTTPServer) MassDeleteAnnotations(c *models.ReqContext) response.Respo
return response.Success("Annotations deleted")
}
func (hs *HTTPServer) GetAnnotationByID(c *models.ReqContext) response.Response {
annotationID, err := strconv.ParseInt(web.Params(c.Req)[":annotationId"], 10, 64)
if err != nil {
return response.Error(http.StatusBadRequest, "annotationId is invalid", err)
}
repo := annotations.GetRepository()
annotation, resp := findAnnotationByID(c.Req.Context(), repo, annotationID, c.SignedInUser)
if resp != nil {
return resp
}
if annotation.Email != "" {
annotation.AvatarUrl = dtos.GetGravatarUrl(annotation.Email)
}
return response.JSON(200, annotation)
}
func (hs *HTTPServer) DeleteAnnotationByID(c *models.ReqContext) response.Response {
annotationID, err := strconv.ParseInt(web.Params(c.Req)[":annotationId"], 10, 64)
if err != nil {

View File

@ -512,6 +512,24 @@ func TestAPI_Annotations_AccessControl(t *testing.T) {
},
want: http.StatusForbidden,
},
{
name: "AccessControl getting annotation by ID with correct permissions is allowed",
args: args{
permissions: []*accesscontrol.Permission{{Action: accesscontrol.ActionAnnotationsRead, Scope: accesscontrol.ScopeAnnotationsAll}},
url: "/api/annotations/1",
method: http.MethodGet,
},
want: http.StatusOK,
},
{
name: "AccessControl getting annotation by ID without permissions is forbidden",
args: args{
permissions: []*accesscontrol.Permission{},
url: "/api/annotations",
method: http.MethodGet,
},
want: http.StatusForbidden,
},
{
name: "AccessControl getting tags for annotations with correct permissions is allowed",
args: args{

View File

@ -475,6 +475,7 @@ func (hs *HTTPServer) registerRoutes() {
apiRoute.Group("/annotations", func(annotationsRoute routing.RouteRegister) {
annotationsRoute.Post("/", authorize(reqSignedIn, ac.EvalPermission(ac.ActionAnnotationsCreate)), routing.Wrap(hs.PostAnnotation))
annotationsRoute.Get("/:annotationId", authorize(reqSignedIn, ac.EvalPermission(ac.ActionAnnotationsRead, ac.ScopeAnnotationsID)), routing.Wrap(hs.GetAnnotationByID))
annotationsRoute.Delete("/:annotationId", authorize(reqSignedIn, ac.EvalPermission(ac.ActionAnnotationsDelete, ac.ScopeAnnotationsID)), routing.Wrap(hs.DeleteAnnotationByID))
annotationsRoute.Put("/:annotationId", authorize(reqSignedIn, ac.EvalPermission(ac.ActionAnnotationsWrite, ac.ScopeAnnotationsID)), routing.Wrap(hs.UpdateAnnotation))
annotationsRoute.Patch("/:annotationId", authorize(reqSignedIn, ac.EvalPermission(ac.ActionAnnotationsWrite, ac.ScopeAnnotationsID)), routing.Wrap(hs.PatchAnnotation))

View File

@ -16,6 +16,15 @@ import (
// 401: unauthorisedError
// 500: internalServerError
// swagger:route GET /annotations/{annotation_id} annotations getAnnotation
//
// Get Annotation by Id.
//
// Responses:
// 200: getAnnotationResponse
// 401: unauthorisedError
// 500: internalServerError
// swagger:route POST /annotations/mass-delete annotations massDeleteAnnotations
//
// Delete multiple annotations.
@ -216,6 +225,13 @@ type GetAnnotationsResponse struct {
Body []*annotations.ItemDTO `json:"body"`
}
// swagger:response getAnnotationResponse
type GetAnnotationResponse struct {
// The response message
// in: body
Body *annotations.ItemDTO `json:"body"`
}
// swagger:response createAnnotationResponse
type CreateAnnotationResponse struct {
// The response message