feat: add update pull request

This commit is contained in:
Derrick Hammer 2024-02-12 16:54:59 -05:00
parent 946f57fe4e
commit 853de2cb64
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 69 additions and 0 deletions

View File

@ -363,6 +363,74 @@ func (r restApi) handlerCreatePullRequest(w http.ResponseWriter, request *http.R
r.respond(w, http.StatusCreated, newGhPullRequest)
}
func (r restApi) handlerUpdatePullRequest(w http.ResponseWriter, request *http.Request) {
vars := mux.Vars(request)
owner := vars["owner"]
repo := vars["repo"]
pullNumber := vars["pull_number"]
parsedPullNumber, err := strconv.ParseInt(pullNumber, 10, 64)
if err != nil {
http.Error(w, "Failed to parse pull number", http.StatusBadRequest)
r.logger.Error("Failed to parse pull number", zap.Error(err))
return
}
client := r.getClientOrError(w)
if client == nil {
return
}
type pullRequestUpdate struct {
Title *string `json:"title,omitempty"`
Body *string `json:"body,omitempty"`
State *string `json:"state,omitempty"`
Base *string `json:"base,omitempty"`
MaintainerCanModify *bool `json:"maintainer_can_modify,omitempty"`
}
ghPullRequest := pullRequestUpdate{}
pullRequest := gitea.EditPullRequestOption{}
if err := r.parseJsonBody(request, &ghPullRequest); err != nil {
http.Error(w, "Failed to parse request body", http.StatusBadRequest)
r.logger.Error("Failed to parse request body", zap.Error(err))
}
if ghPullRequest.State != nil {
if *ghPullRequest.State != "open" && *ghPullRequest.State != "closed" {
http.Error(w, "Invalid state", http.StatusBadRequest)
return
}
state := *ghPullRequest.State
stateType := gitea.StateType(state)
pullRequest.State = &stateType
}
if ghPullRequest.Title != nil {
pullRequest.Title = *ghPullRequest.Title
}
if ghPullRequest.Body != nil {
pullRequest.Body = *ghPullRequest.Body
}
pullRequestResponse, r2, err := client.EditPullRequest(owner, repo, parsedPullNumber, pullRequest)
if err != nil {
http.Error(w, "Failed to update pull request", http.StatusInternalServerError)
r.logger.Error("Failed to update pull request", zap.Error(err))
return
}
newGhPullRequest := convertPullRequest(pullRequestResponse)
r.sendPagingHeaders(w, r2)
r.respond(w, http.StatusOK, newGhPullRequest)
}
func (r restApi) parseJsonBody(request *http.Request, obj interface{}) error {
if obj == nil {
obj = make(map[string]interface{})
@ -393,6 +461,7 @@ func setupRestRoutes(params RouteParams) {
// Pull Request routes
r.HandleFunc("/repos/{owner}/{repo}/pulls", restApi.handlerCreatePullRequest).Methods("POST")
r.HandleFunc("/repos/{owner}/{repo}/pulls/{pull_number}", restApi.handlerUpdatePullRequest).Methods("PATCH")
}
restRouter := r.PathPrefix("/api").Subrouter()