From 6cb286031996224c7ab33d91fbbe6184a25356393cce75fcff30e6874433bb29 Mon Sep 17 00:00:00 2001 From: Adam Majer Date: Sat, 24 Aug 2024 19:42:54 +0200 Subject: [PATCH] . --- bots-common/request_release.go | 20 +++++++++++++++++ gitea-events-rabbitmq-publisher/main.go | 29 ++++++++++++++++++++----- 2 files changed, 44 insertions(+), 5 deletions(-) create mode 100644 bots-common/request_release.go diff --git a/bots-common/request_release.go b/bots-common/request_release.go new file mode 100644 index 0000000..60be4c0 --- /dev/null +++ b/bots-common/request_release.go @@ -0,0 +1,20 @@ +package common + +type ReleaseWebhookDetail struct { + Tag_name string + Name string + Body string + Draft bool + Author User + Created_at string + Published_at string +} + +type ReleaseWebhookEvent struct { + Action string + + Release ReleaseWebhookDetail + Repository Repository + Sender User +} + diff --git a/gitea-events-rabbitmq-publisher/main.go b/gitea-events-rabbitmq-publisher/main.go index 4323668..daec9ad 100644 --- a/gitea-events-rabbitmq-publisher/main.go +++ b/gitea-events-rabbitmq-publisher/main.go @@ -38,7 +38,6 @@ func dumpUnhandledData(reqType string, data []byte) { id++ os.WriteFile("/tmp/unhandled-json-"+reqType+"-"+fmt.Sprint(id)+".json", data, 0600) } - func main() { var listenAddr string @@ -48,8 +47,6 @@ func main() { connectToRabbitMQ() - connectionId := 1 - http.HandleFunc("POST /test", func(res http.ResponseWriter, req *http.Request) { if len(req.Header.Get("Content-Type")) == 0 || req.Header["Content-Type"][0] != "application/json" || @@ -143,6 +140,28 @@ func main() { } org = repoAction.Organization extraAction = repoAction.Action + case "release": + release := common.ReleaseWebhookEvent{} + if err = json.Unmarshal(data, &release); err != nil { + res.WriteHeader(http.StatusBadRequest) + if DebugMode { + dumpUnhandledData(reqType, data) + } + return + } + switch release.Action { + case "published", "updated", "deleted": + break + default: + res.WriteHeader(http.StatusBadRequest) + if DebugMode { + dumpUnhandledData(reqType, data) + } + return + } + + org = *release.Repository.Owner + extraAction = release.Action default: res.WriteHeader(http.StatusBadRequest) if DebugMode { @@ -152,8 +171,8 @@ func main() { // err = PublishMessage(repo.Owner.Username, reqType, data) // write to file for review - os.WriteFile("test_data."+fmt.Sprint(connectionId), data, 0644) - connectionId++ + id++ + os.WriteFile("test_data."+fmt.Sprint(id), data, 0644) log.Println(id, org.Username, extraAction) /*