autogits/bots-common/request_push.go
2024-08-28 00:45:47 +02:00

56 lines
1.1 KiB
Go

package common
import (
"encoding/json"
"io"
"strings"
)
type Commit struct {
Id string
Message string
}
type PushWebhookEvent struct {
Total_Commits int
Head_Commit Commit
Commits []Commit
Pusher User
Sender User
Repository *Repository
}
func (*PushWebhookEvent) GetAction() string {
return "push"
}
func (h *RequestHandler) parsePushRequest(data io.Reader) *PushWebhookEvent {
if h.HasError() {
return nil
}
var action PushWebhookEvent
h.Error = json.NewDecoder(data).Decode(&action)
if h.HasError() {
h.ErrLogger.Printf("Got error while parsing: %v\n", h.Error)
return nil
}
repoIdx := strings.LastIndex(action.Repository.Ssh_Url, "/")
if repoIdx == -1 || action.Repository.Ssh_Url[repoIdx+1:] != action.Repository.Name+".git" {
h.ErrLogger.Printf("Unexpected URL for SSH repository: '%s'\n", action.Repository.Name)
return nil
}
h.StdLogger.Printf("Request push for repo: %s\n", action.Repository.Full_Name)
h.Request.Data = &action
if len(action.Commits) < 1 || len(action.Head_Commit.Id) != 64 {
h.ErrLogger.Println("Request has no action .... skipping")
return nil
}
return &action
}