From bcc4c817d57c119a2957d833d341b12e3c2b48ea Mon Sep 17 00:00:00 2001 From: GitBluub Date: Sat, 23 Aug 2025 15:42:34 +0200 Subject: [PATCH] webhook base handling --- api/github/webhook.go | 32 ++++++++++++++++++++++++++++++++ api/go.mod | 1 + api/go.sum | 2 ++ api/main.go | 24 +++++++++++++++++++++++- 4 files changed, 58 insertions(+), 1 deletion(-) create mode 100644 api/github/webhook.go diff --git a/api/github/webhook.go b/api/github/webhook.go new file mode 100644 index 0000000..587249b --- /dev/null +++ b/api/github/webhook.go @@ -0,0 +1,32 @@ +package github + +import ( + "log" + + "github.com/ren3gadem4rm0t/github-hook-types-go" +) + +// handleGitHubEvent processes GitHub webhook events +func HandleEvent(event *github.WebhookEvent) error { + log.Printf("Received %s event (Delivery ID: %s)\n", event.Type, event.DeliveryID) + + // Type switch to handle different event types + switch event.Type { + case github.PushEvent: + payload := event.Payload.(*github.PushPayload) + log.Printf("Push to %s by %s", + payload.Repository.FullName, + payload.PusherPerson.Name) + + case github.PullRequestEvent: + payload := event.Payload.(*github.PullRequestPayload) + log.Printf("Pull request in repo %s by %s", + payload.Repository.FullName, + payload.Sender.Login) + + default: + log.Printf("Received unhandled event type: %s", event.Type) + } + + return nil +} diff --git a/api/go.mod b/api/go.mod index d4ec5d8..6dc0741 100644 --- a/api/go.mod +++ b/api/go.mod @@ -7,6 +7,7 @@ require ( github.com/labstack/gommon v0.4.2 // indirect github.com/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-isatty v0.0.20 // indirect + github.com/ren3gadem4rm0t/github-hook-types-go v0.1.1 // indirect github.com/valyala/bytebufferpool v1.0.0 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect golang.org/x/crypto v0.38.0 // indirect diff --git a/api/go.sum b/api/go.sum index de42c50..359fd5b 100644 --- a/api/go.sum +++ b/api/go.sum @@ -6,6 +6,8 @@ github.com/mattn/go-colorable v0.1.14 h1:9A9LHSqF/7dyVVX6g0U9cwm9pG3kP9gSzcuIPHP github.com/mattn/go-colorable v0.1.14/go.mod h1:6LmQG8QLFO4G5z1gPvYEzlUgJ2wF+stgPZH1UqBm1s8= github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY= github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= +github.com/ren3gadem4rm0t/github-hook-types-go v0.1.1 h1:SozFGAr0+2DcEBxaVxP8ky+y8F30QSpHHJNZbeNna3k= +github.com/ren3gadem4rm0t/github-hook-types-go v0.1.1/go.mod h1:5EhdzGV5wRZK5YFd3wgZmB+i+TD+XiPB5nYDcZcmnlM= github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= diff --git a/api/main.go b/api/main.go index 85a0ad5..f976ae4 100644 --- a/api/main.go +++ b/api/main.go @@ -1,10 +1,32 @@ package api -import "github.com/labstack/echo/v4" +import ( + "net/http" + "os" + "github.com/labstack/echo/v4" + "github.com/ren3gadem4rm0t/github-hook-types-go/webhook" + "github.com/zoriya/cish/api/github" +) func main() { e := echo.New() + secret := os.Getenv("GITHUB_WEBHOOK_SECRET") + + // Create a new webhook handler + handler := webhook.NewHandler(secret) + + e.POST("/hooks/github", func(c echo.Context) error { + event, err := handler.ProcessWebhook(c.Request()) + if err != nil { + return c.String(http.StatusBadRequest, "Webhook payload parsing failed") + } + if err := github.HandleEvent(event); err != nil { + return c.String(http.StatusInternalServerError, "Webhook event handler failed") + } + return c.String(http.StatusOK, "Webhook handled succesfully") + }) + e.Logger.Fatal(e.Server.ListenAndServe()) }