webhook base handling

This commit is contained in:
GitBluub
2025-08-23 15:42:34 +02:00
parent 8a17dc6b5e
commit bcc4c817d5
4 changed files with 58 additions and 1 deletions

32
api/github/webhook.go Normal file
View File

@@ -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
}

View File

@@ -7,6 +7,7 @@ require (
github.com/labstack/gommon v0.4.2 // indirect github.com/labstack/gommon v0.4.2 // indirect
github.com/mattn/go-colorable v0.1.14 // indirect github.com/mattn/go-colorable v0.1.14 // indirect
github.com/mattn/go-isatty v0.0.20 // 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/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect github.com/valyala/fasttemplate v1.2.2 // indirect
golang.org/x/crypto v0.38.0 // indirect golang.org/x/crypto v0.38.0 // indirect

View File

@@ -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-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 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y= 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 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc= github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo= github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=

View File

@@ -1,10 +1,32 @@
package api 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() { func main() {
e := echo.New() 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()) e.Logger.Fatal(e.Server.ListenAndServe())
} }