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

@ -5,7 +5,7 @@ type ReleaseWebhookDetail struct {
Name string Name string
Body string Body string
Draft bool Draft bool
Author User Author *User
Created_at string Created_at string
Published_at string Published_at string
} }
@ -14,7 +14,6 @@ type ReleaseWebhookEvent struct {
Action string Action string
Release ReleaseWebhookDetail Release ReleaseWebhookDetail
Repository Repository Repository *Repository
Sender User Sender *User
} }

View File

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

View File

@ -38,6 +38,69 @@ func dumpUnhandledData(reqType string, data []byte) {
id++ id++
os.WriteFile("/tmp/unhandled-json-"+reqType+"-"+fmt.Sprint(id)+".json", data, 0600) 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() { func main() {
var listenAddr string var listenAddr string
@ -85,85 +148,10 @@ func main() {
res.WriteHeader(http.StatusBadRequest) res.WriteHeader(http.StatusBadRequest)
} }
org := common.Organization{} org, extraAction, err := parseRequestJSON(reqType, data)
extraAction := "" if err != nil {
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:
res.WriteHeader(http.StatusBadRequest) res.WriteHeader(http.StatusBadRequest)
log.Printf("error parsing webhook %s JSON. err: %v", reqType, err)
if DebugMode { if DebugMode {
dumpUnhandledData(reqType, data) dumpUnhandledData(reqType, data)
} }
@ -173,7 +161,7 @@ func main() {
// write to file for review // write to file for review
id++ id++
os.WriteFile("test_data."+fmt.Sprint(id), data, 0644) 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) err = PublishMessage(org.Name, reqType, extraAction, data)