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