[maint] api for PR metadata added (#319)

This commit is contained in:
Enver Bisevac 2023-02-13 06:39:56 +01:00 committed by GitHub
parent 821610a375
commit fd08fa9775
3 changed files with 43 additions and 1 deletions

View File

@ -0,0 +1,41 @@
// Copyright 2022 Harness Inc. All rights reserved.
// Use of this source code is governed by the Polyform Free Trial License
// that can be found in the LICENSE.md file for this repository.
package pullreq
import (
"net/http"
"github.com/harness/gitness/internal/api/controller/pullreq"
"github.com/harness/gitness/internal/api/render"
"github.com/harness/gitness/internal/api/request"
)
// HandleMetadata returns a http.HandlerFunc that returns PR metadata.
func HandleMetadata(pullreqCtrl *pullreq.Controller) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) {
ctx := r.Context()
session, _ := request.AuthSessionFrom(ctx)
repoRef, err := request.GetRepoRefFromPath(r)
if err != nil {
render.TranslatedUserError(w, err)
return
}
pullreqNumber, err := request.GetPullReqNumberFromPath(r)
if err != nil {
render.TranslatedUserError(w, err)
return
}
pr, err := pullreqCtrl.Find(ctx, session, repoRef, pullreqNumber)
if err != nil {
render.TranslatedUserError(w, err)
return
}
render.JSON(w, http.StatusOK, pr.Stats)
}
}

View File

@ -441,7 +441,7 @@ func pullReqOperations(reflector *openapi3.Reflector) {
opMetaData.WithTags("pullreq")
opMetaData.WithMapOfAnything(map[string]interface{}{"operationId": "pullReqMetaData"})
_ = reflector.SetRequest(&opMetaData, new(pullReqRequest), http.MethodGet)
_ = reflector.SetStringResponse(&opMetaData, http.StatusOK, "text/plain")
_ = reflector.SetJSONResponse(&opMetaData, new(types.PullReqStats), http.StatusOK)
_ = reflector.SetJSONResponse(&opMetaData, new(usererror.Error), http.StatusInternalServerError)
_ = reflector.SetJSONResponse(&opMetaData, new(usererror.Error), http.StatusUnauthorized)
_ = reflector.SetJSONResponse(&opMetaData, new(usererror.Error), http.StatusForbidden)

View File

@ -270,6 +270,7 @@ func SetupPullReq(r chi.Router, pullreqCtrl *pullreq.Controller) {
r.Post("/merge", handlerpullreq.HandleMerge(pullreqCtrl))
r.Get("/diff", handlerpullreq.HandleRawDiff(pullreqCtrl))
r.Get("/commits", handlerpullreq.HandleCommits(pullreqCtrl))
r.Get("/metadata", handlerpullreq.HandleMetadata(pullreqCtrl))
})
})
}