Files
gitlab.nvim/cmd/app/create_mr.go
Harrison (Harry) Cramer 22bfd0c83e 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.
2024-09-14 16:53:00 -04:00

68 lines
1.9 KiB
Go

package app
import (
"encoding/json"
"fmt"
"net/http"
"github.com/xanzy/go-gitlab"
)
type CreateMrRequest struct {
Title string `json:"title" validate:"required"`
TargetBranch string `json:"target_branch" validate:"required"`
Description string `json:"description"`
TargetProjectID int `json:"forked_project_id,omitempty"`
DeleteBranch bool `json:"delete_branch"`
Squash bool `json:"squash"`
}
type MergeRequestCreator interface {
CreateMergeRequest(pid interface{}, opt *gitlab.CreateMergeRequestOptions, options ...gitlab.RequestOptionFunc) (*gitlab.MergeRequest, *gitlab.Response, error)
}
type mergeRequestCreatorService struct {
data
client MergeRequestCreator
}
/* createMr creates a merge request */
func (a mergeRequestCreatorService) ServeHTTP(w http.ResponseWriter, r *http.Request) {
createMrRequest := r.Context().Value(payload("payload")).(*CreateMrRequest)
opts := gitlab.CreateMergeRequestOptions{
Title: &createMrRequest.Title,
Description: &createMrRequest.Description,
TargetBranch: &createMrRequest.TargetBranch,
SourceBranch: &a.gitInfo.BranchName,
RemoveSourceBranch: &createMrRequest.DeleteBranch,
Squash: &createMrRequest.Squash,
}
if createMrRequest.TargetProjectID != 0 {
opts.TargetProjectID = gitlab.Ptr(createMrRequest.TargetProjectID)
}
_, res, err := a.client.CreateMergeRequest(a.projectInfo.ProjectId, &opts)
if err != nil {
handleError(w, err, "Could not create MR", http.StatusInternalServerError)
return
}
if res.StatusCode >= 300 {
handleError(w, GenericError{r.URL.Path}, "Could not create MR", res.StatusCode)
return
}
response := SuccessResponse{Message: fmt.Sprintf("MR '%s' created", createMrRequest.Title)}
w.WriteHeader(http.StatusOK)
err = json.NewEncoder(w).Encode(response)
if err != nil {
handleError(w, err, "Could not encode response", http.StatusInternalServerError)
}
}