fix: date fixes; go middleware refactors; regex fixes; etc (#368)
fix: format of date when MR was closed or merged (#367) refactor: Add Payload Validators + Middleware In Go Code (#366) fix: Add better checks for leaving comments (#369) fix: regex support for http credentials embedded in remote url (#372) fix: Comment on single line selects two lines (#371) This is a #PATCH release.
This commit is contained in:
committed by
GitHub
parent
f1faf603b0
commit
22bfd0c83e
@@ -3,17 +3,11 @@ package app
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
|
||||
"github.com/xanzy/go-gitlab"
|
||||
)
|
||||
|
||||
type DiscussionResolveRequest struct {
|
||||
DiscussionID string `json:"discussion_id"`
|
||||
Resolved bool `json:"resolved"`
|
||||
}
|
||||
|
||||
type DiscussionResolver interface {
|
||||
ResolveMergeRequestDiscussion(pid interface{}, mergeRequest int, discussion string, opt *gitlab.ResolveMergeRequestDiscussionOptions, options ...gitlab.RequestOptionFunc) (*gitlab.Discussion, *gitlab.Response, error)
|
||||
}
|
||||
@@ -23,40 +17,24 @@ type discussionsResolutionService struct {
|
||||
client DiscussionResolver
|
||||
}
|
||||
|
||||
type DiscussionResolveRequest struct {
|
||||
DiscussionID string `json:"discussion_id" validate:"required"`
|
||||
Resolved bool `json:"resolved"`
|
||||
}
|
||||
|
||||
/* discussionsResolveHandler sets a discussion to be "resolved" or not resolved, depending on the payload */
|
||||
func (a discussionsResolutionService) handler(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
if r.Method != http.MethodPut {
|
||||
w.Header().Set("Access-Control-Allow-Methods", http.MethodPut)
|
||||
handleError(w, InvalidRequestError{}, "Expected PUT", http.StatusMethodNotAllowed)
|
||||
return
|
||||
}
|
||||
|
||||
body, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not read request body", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
defer r.Body.Close()
|
||||
|
||||
var resolveDiscussionRequest DiscussionResolveRequest
|
||||
err = json.Unmarshal(body, &resolveDiscussionRequest)
|
||||
|
||||
if err != nil {
|
||||
handleError(w, err, "Could not read JSON from request", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
func (a discussionsResolutionService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
payload := r.Context().Value(payload("payload")).(*DiscussionResolveRequest)
|
||||
|
||||
_, res, err := a.client.ResolveMergeRequestDiscussion(
|
||||
a.projectInfo.ProjectId,
|
||||
a.projectInfo.MergeId,
|
||||
resolveDiscussionRequest.DiscussionID,
|
||||
&gitlab.ResolveMergeRequestDiscussionOptions{Resolved: &resolveDiscussionRequest.Resolved},
|
||||
payload.DiscussionID,
|
||||
&gitlab.ResolveMergeRequestDiscussionOptions{Resolved: &payload.Resolved},
|
||||
)
|
||||
|
||||
friendlyName := "unresolve"
|
||||
if resolveDiscussionRequest.Resolved {
|
||||
if payload.Resolved {
|
||||
friendlyName = "resolve"
|
||||
}
|
||||
|
||||
@@ -66,15 +44,12 @@ func (a discussionsResolutionService) handler(w http.ResponseWriter, r *http.Req
|
||||
}
|
||||
|
||||
if res.StatusCode >= 300 {
|
||||
handleError(w, GenericError{endpoint: "/mr/discussions/resolve"}, fmt.Sprintf("Could not %s discussion", friendlyName), res.StatusCode)
|
||||
handleError(w, GenericError{r.URL.Path}, fmt.Sprintf("Could not %s discussion", friendlyName), res.StatusCode)
|
||||
return
|
||||
}
|
||||
|
||||
w.WriteHeader(http.StatusOK)
|
||||
response := SuccessResponse{
|
||||
Message: fmt.Sprintf("Discussion %sd", friendlyName),
|
||||
Status: http.StatusOK,
|
||||
}
|
||||
response := SuccessResponse{Message: fmt.Sprintf("Discussion %sd", friendlyName)}
|
||||
|
||||
err = json.NewEncoder(w).Encode(response)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user