feat: add pull request create

This commit is contained in:
Derrick Hammer 2024-02-12 16:25:43 -05:00
parent 63e9d93f9b
commit bf9e062644
Signed by: pcfreak30
GPG Key ID: C997C339BE476FF2
1 changed files with 39 additions and 0 deletions

View File

@ -319,6 +319,42 @@ func (r restApi) handlerCreateRelease(w http.ResponseWriter, request *http.Reque
r.respond(w, http.StatusCreated, releaseResponse) 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 { func (r restApi) parseJsonBody(request *http.Request, obj interface{}) error {
if obj == nil { if obj == nil {
obj = make(map[string]interface{}) obj = make(map[string]interface{})
@ -346,6 +382,9 @@ func setupRestRoutes(params RouteParams) {
// Repo Release routes // Repo Release routes
r.HandleFunc("/repos/{owner}/{repo}/releases", restApi.handlerCreateRelease).Methods("POST") 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() restRouter := r.PathPrefix("/api").Subrouter()