This commit is contained in:
Adam Majer 2024-08-24 21:39:50 +02:00
parent 6cb2860319
commit 6106b96a49
3 changed files with 79 additions and 92 deletions

View File

@ -1,20 +1,19 @@
package common
type ReleaseWebhookDetail struct {
Tag_name string
Name string
Body string
Draft bool
Author User
Created_at string
Tag_name string
Name string
Body string
Draft bool
Author *User
Created_at string
Published_at string
}
type ReleaseWebhookEvent struct {
Action string
Release ReleaseWebhookDetail
Repository Repository
Sender User
Release ReleaseWebhookDetail
Repository *Repository
Sender *User
}

View File

@ -34,9 +34,9 @@ type User struct {
type RepositoryAction struct {
Action string
Sender User
Organization Organization
Repository Repository
Sender *User
Organization *Organization
Repository *Repository
PrjGit string
}

View File

@ -38,6 +38,69 @@ func dumpUnhandledData(reqType string, data []byte) {
id++
os.WriteFile("/tmp/unhandled-json-"+reqType+"-"+fmt.Sprint(id)+".json", data, 0600)
}
func parseRequestJSON(reqType string, data []byte) (org *common.Organization, extraAction string, err error) {
extraAction = ""
switch reqType {
case "create", "delete":
create := common.CreateWebhookEvent{}
if err = json.Unmarshal(data, &create); err != nil {
return
}
org = create.Repository.Owner
case "fork":
fork := common.ForkWebhookEvent{}
if err = json.Unmarshal(data, &fork); err != nil {
return
}
org = fork.Forkee.Owner
case "push":
push := common.PushRequest{}
if err = json.Unmarshal(data, &push); err != nil {
return
}
org = push.Repository.Owner
case "repository":
repoAction := common.RepositoryAction{}
if err = json.Unmarshal(data, &repoAction); err != nil {
return
}
switch repoAction.Action {
case "created", "deleted":
break
default:
err = fmt.Errorf("Unknown repository webhook action type: %s", repoAction.Action)
return
}
org = repoAction.Organization
extraAction = repoAction.Action
case "release":
release := common.ReleaseWebhookEvent{}
if err = json.Unmarshal(data, &release); err != nil {
return
}
switch release.Action {
case "published", "updated", "deleted":
break
default:
err = fmt.Errorf("Unknwon Release webhook action type: %s", release.Action)
return
}
org = release.Repository.Owner
extraAction = release.Action
default:
err = fmt.Errorf("Unknown webhook request type: %s", reqType)
}
return
}
func main() {
var listenAddr string
@ -85,85 +148,10 @@ func main() {
res.WriteHeader(http.StatusBadRequest)
}
org := common.Organization{}
extraAction := ""
switch reqType {
case "create", "delete":
create := common.CreateWebhookEvent{}
if err = json.Unmarshal(data, &create); err != nil {
res.WriteHeader(http.StatusBadRequest)
if DebugMode {
dumpUnhandledData(reqType, data)
}
return
}
org = *create.Repository.Owner
case "fork":
fork := common.ForkWebhookEvent{}
if err = json.Unmarshal(data, &fork); err != nil {
res.WriteHeader(http.StatusBadRequest)
if DebugMode {
dumpUnhandledData(reqType, data)
}
return
}
org = *fork.Forkee.Owner
case "push":
push := common.PushRequest{}
if err = json.Unmarshal(data, &push); err != nil {
res.WriteHeader(http.StatusBadRequest)
if DebugMode {
dumpUnhandledData(reqType, data)
}
return
}
org = *push.Repository.Owner
case "repository":
repoAction := common.RepositoryAction{}
if err = json.Unmarshal(data, &repoAction); err != nil {
res.WriteHeader(http.StatusBadRequest)
if DebugMode {
dumpUnhandledData(reqType, data)
}
return
}
switch repoAction.Action {
case "created", "deleted":
break
default:
res.WriteHeader(http.StatusBadRequest)
if DebugMode {
dumpUnhandledData(reqType, data)
}
}
org = repoAction.Organization
extraAction = repoAction.Action
case "release":
release := common.ReleaseWebhookEvent{}
if err = json.Unmarshal(data, &release); err != nil {
res.WriteHeader(http.StatusBadRequest)
if DebugMode {
dumpUnhandledData(reqType, data)
}
return
}
switch release.Action {
case "published", "updated", "deleted":
break
default:
res.WriteHeader(http.StatusBadRequest)
if DebugMode {
dumpUnhandledData(reqType, data)
}
return
}
org = *release.Repository.Owner
extraAction = release.Action
default:
org, extraAction, err := parseRequestJSON(reqType, data)
if err != nil {
res.WriteHeader(http.StatusBadRequest)
log.Printf("error parsing webhook %s JSON. err: %v", reqType, err)
if DebugMode {
dumpUnhandledData(reqType, data)
}
@ -173,7 +161,7 @@ func main() {
// write to file for review
id++
os.WriteFile("test_data."+fmt.Sprint(id), data, 0644)
log.Println(id, org.Username, extraAction)
log.Println(id, org.Username, reqType, extraAction)
/*
err = PublishMessage(org.Name, reqType, extraAction, data)