diff --git a/internal/api/handler/pullreq/pr_metadata.go b/internal/api/handler/pullreq/pr_metadata.go new file mode 100644 index 000000000..5ccc379e1 --- /dev/null +++ b/internal/api/handler/pullreq/pr_metadata.go @@ -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) + } +} diff --git a/internal/api/openapi/pullreq.go b/internal/api/openapi/pullreq.go index c39bb0d84..b90b2b2b2 100644 --- a/internal/api/openapi/pullreq.go +++ b/internal/api/openapi/pullreq.go @@ -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) diff --git a/internal/router/api.go b/internal/router/api.go index 009053ca4..7cdaade11 100644 --- a/internal/router/api.go +++ b/internal/router/api.go @@ -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)) }) }) }