autogits/pr-review/main.go
Adam Majer 598ecbbd5a .
2024-07-07 21:12:40 +02:00

96 lines
1.9 KiB
Go

package main
import (
"errors"
"fmt"
"log"
"net/http"
"os"
"path/filepath"
"src.opensuse.org/autogits/common"
)
const (
ListenPort = 8001
ListenAddr = "[::1]"
GitAuthor = "GiteaBot - AutoStaging"
)
func createListenServer() {
http.HandleFunc("/pr-review", func(res http.ResponseWriter, req *http.Request) {
h := createRequestHandler("pr-review")
// defer h.Close()
if len(req.Header.Get("Content-Type")) == 0 ||
req.Header["Content-Type"][0] != "application/json" ||
req.Method != "POST" {
h.writeError()
res.WriteHeader(http.StatusInternalServerError)
return
}
hdr := req.Header[GiteaRequestHeader]
if len(hdr) != 1 {
h.LogError("Unsupported number of %s headers: %d: %#v\n", GiteaRequestHeader, len(hdr), hdr)
h.writeError()
res.WriteHeader(http.StatusInternalServerError)
return
}
reqType := hdr[0]
switch reqType {
case "pull_request":
h.parsePullRequest(req.Body)
h.processPullRequestAction()
default:
h.LogError("Unsupported request type: %s", reqType)
res.WriteHeader(http.StatusInternalServerError)
}
if h.HasError() {
h.writeError()
res.WriteHeader(http.StatusInternalServerError)
return
}
res.Header().Add("Content-Type", "application/json")
res.WriteHeader(http.StatusOK)
})
log.Fatal(http.ListenAndServe(ListenAddr+":"+fmt.Sprint(ListenPort), nil))
}
func parseObsSecretToken() error {
return nil
}
func parseGiteaSecretToken() error {
GiteaToken = os.Getenv(GiteaTokenEnv)
if len(GiteaToken) < 10 {
return errors.New(GiteaTokenEnv + " not provided")
}
err := os.Setenv(GiteaTokenEnv, "")
if err != nil {
return fmt.Errorf("%s: %v", "Cannot reset "+GiteaTokenEnv, err)
}
return nil
}
func main() {
if err := parseGiteaSecretToken(); err != nil {
fmt.Print(err)
return
}
if err := parseObsSecretToken(); err != nil {
fmt.Print(err)
return
}
createListenServer()
}