From 2dde7255746eaa99d06f54d2867caaf339d7bf36 Mon Sep 17 00:00:00 2001 From: Aaron Bieber Date: Thu, 28 Mar 2024 09:47:34 -0600 Subject: [PATCH] add v2 endpoint for upcoming got notifications --- got.go | 64 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 64 insertions(+) diff --git a/got.go b/got.go index 7d47b75..d3e5435 100644 --- a/got.go +++ b/got.go @@ -1,6 +1,7 @@ package main import ( + "encoding/json" "fmt" "log" "net/http" @@ -11,6 +12,34 @@ import ( "suah.dev/mcchunkie/plugins" ) +type Notification struct { + Short bool `json:"short"` + ID string `json:"id"` + Author string `json:"author"` + Date string `json:"date"` + Message string `json:"message"` + Diffstat struct { + } `json:"diffstat"` + Changes struct { + } `json:"changes"` +} + +func (n *Notification) String() string { + // op committed got.git f9e653700..f9e653700^1 (main): fix gotd_parse_url() (https://git.gameoftrees.org/gitweb/?p=got.git;a=commitdiff;h=f9e653700) + return fmt.Sprintf("%s committed %s: %s (%s)", + n.Author, + n.ID, + n.Message, + fmt.Sprintf("https://git.gameoftrees.org/gitweb/?p=%s;a=commitdiff;h=%s", + "repo", + n.ID), + ) +} + +type GotNotifications struct { + Notifications []Notification `json:"notifications"` +} + func gotListen(store *FStore, cli *gomatrix.Client) { var gotPort, _ = store.Get("got_listen") if gotPort != "" { @@ -19,6 +48,41 @@ func gotListen(store *FStore, cli *gomatrix.Client) { log.Printf("GOT: listening on %q and sending messages to %q\n", gotPort, gotRoom) + http.HandleFunc("/_got/v2", func(w http.ResponseWriter, r *http.Request) { + if r.Method != http.MethodPost { + http.Error(w, fmt.Sprintf("method %q not implemented", r.Method), http.StatusMethodNotAllowed) + return + } + user, pass, ok := r.BasicAuth() + err := bcrypt.CompareHashAndPassword([]byte(htpass), []byte(pass)) + if !(ok && err == nil && user == "got") { + log.Printf("GOT: failed auth '%s'\n", user) + w.Header().Set("WWW-Authenticate", `Basic realm="got notify"`) + http.Error(w, "Unauthorized", http.StatusUnauthorized) + return + } + gn := GotNotifications{} + + dec := json.NewDecoder(r.Body) + err = dec.Decode(&gn) + if err != nil { + http.Error(w, "invalid data sent to server", http.StatusBadRequest) + return + } + for _, line := range gn.Notifications { + log.Printf("GOT: sending '%s'\n", line.String()) + err = plugins.SendUnescNotice(cli, gotRoom, line.String()) + if err != nil { + http.Error( + w, + fmt.Sprintf("can not send commit info: %s", err), + http.StatusInternalServerError, + ) + return + } + } + }) + http.HandleFunc("/_got", func(w http.ResponseWriter, r *http.Request) { var msg string user, pass, ok := r.BasicAuth()