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

79 lines
1.5 KiB
Go

package common
import (
"encoding/json"
"fmt"
"io"
"strings"
)
type Repository struct {
Id uint
Name string
Full_Name string
Fork bool
Parent *Repository
Owner *Organization
Clone_Url string
Ssh_Url string
Default_Branch string
Object_Format_Name string
}
type Organization struct {
Id uint
Username string
}
type User struct {
Id uint
Username string
}
type RepositoryWebhookEvent struct {
Action string
Sender *User
Organization *Organization
Repository *Repository
PrjGit string
}
func (r *RepositoryWebhookEvent) GetAction() string {
return r.Action
}
// TODO: sanity check values!!!!
func (h *RequestHandler) parseRepositoryRequest(dataReader io.Reader) *RepositoryWebhookEvent {
if h.HasError() {
return nil
}
var data RepositoryWebhookEvent
h.Error = json.NewDecoder(dataReader).Decode(&data)
if h.HasError() {
return nil
}
repoIdx := strings.LastIndex(data.Repository.Ssh_Url, "/")
if repoIdx == -1 || data.Repository.Ssh_Url[repoIdx+1:] != data.Repository.Name+".git" {
h.Error = fmt.Errorf("No data, skipping")
h.ErrLogger.Printf("Unexpected URL for SSH repository: %s\n", data.Repository.Name)
return nil
}
data.PrjGit = data.Repository.Ssh_Url[:repoIdx+1] + DefaultGitPrj + ".git"
h.StdLogger.Printf("Request '%s' for repo: %s\n", data.Action, data.Repository.Full_Name)
if len(data.Action) < 1 {
h.Error = fmt.Errorf("No data, skipping")
h.ErrLogger.Println("Request has no data.... skipping")
}
h.Request.Data = &data
return &data
}