diff --git a/api/routes_rest_api.go b/api/routes_rest_api.go index f588b42..f48996f 100644 --- a/api/routes_rest_api.go +++ b/api/routes_rest_api.go @@ -319,6 +319,42 @@ func (r restApi) handlerCreateRelease(w http.ResponseWriter, request *http.Reque r.respond(w, http.StatusCreated, releaseResponse) } +func (r restApi) handlerCreatePullRequest(w http.ResponseWriter, request *http.Request) { + vars := mux.Vars(request) + owner := vars["owner"] + repo := vars["repo"] + + client := r.getClientOrError(w) + + if client == nil { + return + } + + ghPullRequest := github.NewPullRequest{} + + 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)) + } + + pullRequest := gitea.CreatePullRequestOption{ + Title: ghPullRequest.GetTitle(), + Head: ghPullRequest.GetHead(), + Base: ghPullRequest.GetBase(), + Body: ghPullRequest.GetBody(), + } + + pullRequestResponse, r2, err := client.CreatePullRequest(owner, repo, pullRequest) + if err != nil { + http.Error(w, "Failed to create pull request", http.StatusInternalServerError) + r.logger.Error("Failed to create pull request", zap.Error(err)) + return + } + + r.sendPagingHeaders(w, r2) + r.respond(w, http.StatusCreated, pullRequestResponse) +} + func (r restApi) parseJsonBody(request *http.Request, obj interface{}) error { if obj == nil { obj = make(map[string]interface{}) @@ -346,6 +382,9 @@ func setupRestRoutes(params RouteParams) { // Repo Release routes r.HandleFunc("/repos/{owner}/{repo}/releases", restApi.handlerCreateRelease).Methods("POST") + + // Pull Request routes + r.HandleFunc("/repos/{owner}/{repo}/pulls", restApi.handlerCreatePullRequest).Methods("POST") } restRouter := r.PathPrefix("/api").Subrouter()