diff --git a/bots-common/request_release.go b/bots-common/request_release.go index 60be4c0..0e7d55a 100644 --- a/bots-common/request_release.go +++ b/bots-common/request_release.go @@ -1,20 +1,19 @@ package common type ReleaseWebhookDetail struct { - Tag_name string - Name string - Body string - Draft bool - Author User - Created_at string + 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 + Release ReleaseWebhookDetail + Repository *Repository + Sender *User } - diff --git a/bots-common/request_repo.go b/bots-common/request_repo.go index 39c91c0..f388528 100644 --- a/bots-common/request_repo.go +++ b/bots-common/request_repo.go @@ -34,9 +34,9 @@ type User struct { type RepositoryAction struct { Action string - Sender User - Organization Organization - Repository Repository + Sender *User + Organization *Organization + Repository *Repository PrjGit string } diff --git a/gitea-events-rabbitmq-publisher/main.go b/gitea-events-rabbitmq-publisher/main.go index daec9ad..0214ce7 100644 --- a/gitea-events-rabbitmq-publisher/main.go +++ b/gitea-events-rabbitmq-publisher/main.go @@ -38,6 +38,69 @@ func dumpUnhandledData(reqType string, data []byte) { id++ os.WriteFile("/tmp/unhandled-json-"+reqType+"-"+fmt.Sprint(id)+".json", data, 0600) } + +func parseRequestJSON(reqType string, data []byte) (org *common.Organization, extraAction string, err error) { + extraAction = "" + + switch reqType { + case "create", "delete": + create := common.CreateWebhookEvent{} + if err = json.Unmarshal(data, &create); err != nil { + return + } + org = create.Repository.Owner + + case "fork": + fork := common.ForkWebhookEvent{} + if err = json.Unmarshal(data, &fork); err != nil { + return + } + org = fork.Forkee.Owner + + case "push": + push := common.PushRequest{} + if err = json.Unmarshal(data, &push); err != nil { + return + } + org = push.Repository.Owner + + case "repository": + repoAction := common.RepositoryAction{} + if err = json.Unmarshal(data, &repoAction); err != nil { + return + } + switch repoAction.Action { + case "created", "deleted": + break + default: + err = fmt.Errorf("Unknown repository webhook action type: %s", repoAction.Action) + return + } + org = repoAction.Organization + extraAction = repoAction.Action + + case "release": + release := common.ReleaseWebhookEvent{} + if err = json.Unmarshal(data, &release); err != nil { + return + } + switch release.Action { + case "published", "updated", "deleted": + break + default: + err = fmt.Errorf("Unknwon Release webhook action type: %s", release.Action) + return + } + + org = release.Repository.Owner + extraAction = release.Action + default: + err = fmt.Errorf("Unknown webhook request type: %s", reqType) + } + + return +} + func main() { var listenAddr string @@ -85,85 +148,10 @@ func main() { res.WriteHeader(http.StatusBadRequest) } - org := common.Organization{} - extraAction := "" - - switch reqType { - case "create", "delete": - create := common.CreateWebhookEvent{} - if err = json.Unmarshal(data, &create); err != nil { - res.WriteHeader(http.StatusBadRequest) - if DebugMode { - dumpUnhandledData(reqType, data) - } - return - } - org = *create.Repository.Owner - - case "fork": - fork := common.ForkWebhookEvent{} - if err = json.Unmarshal(data, &fork); err != nil { - res.WriteHeader(http.StatusBadRequest) - if DebugMode { - dumpUnhandledData(reqType, data) - } - return - } - org = *fork.Forkee.Owner - case "push": - push := common.PushRequest{} - if err = json.Unmarshal(data, &push); err != nil { - res.WriteHeader(http.StatusBadRequest) - if DebugMode { - dumpUnhandledData(reqType, data) - } - return - } - org = *push.Repository.Owner - case "repository": - repoAction := common.RepositoryAction{} - if err = json.Unmarshal(data, &repoAction); err != nil { - res.WriteHeader(http.StatusBadRequest) - if DebugMode { - dumpUnhandledData(reqType, data) - } - return - } - switch repoAction.Action { - case "created", "deleted": - break - default: - res.WriteHeader(http.StatusBadRequest) - if DebugMode { - dumpUnhandledData(reqType, data) - } - } - 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: + org, extraAction, err := parseRequestJSON(reqType, data) + if err != nil { res.WriteHeader(http.StatusBadRequest) + log.Printf("error parsing webhook %s JSON. err: %v", reqType, err) if DebugMode { dumpUnhandledData(reqType, data) } @@ -173,7 +161,7 @@ func main() { // write to file for review id++ os.WriteFile("test_data."+fmt.Sprint(id), data, 0644) - log.Println(id, org.Username, extraAction) + log.Println(id, org.Username, reqType, extraAction) /* err = PublishMessage(org.Name, reqType, extraAction, data)