mirror of
https://github.com/zoriya/cish.git
synced 2025-12-06 07:16:16 +00:00
webhook base handling
This commit is contained in:
32
api/github/webhook.go
Normal file
32
api/github/webhook.go
Normal 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
|
||||||
|
}
|
||||||
@@ -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
|
||||||
|
|||||||
@@ -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=
|
||||||
|
|||||||
24
api/main.go
24
api/main.go
@@ -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())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user