From 538c0d79b54d73231a8610512d9256f7fb4c56c3 Mon Sep 17 00:00:00 2001 From: A_D Date: Sat, 17 Nov 2018 17:13:47 +0200 Subject: [PATCH] added handling for create hooks --- webhooks-structs.go | 15 ++++++++++++--- webhooks.go | 27 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 3 deletions(-) diff --git a/webhooks-structs.go b/webhooks-structs.go index 9f8bc44..1476ea8 100644 --- a/webhooks-structs.go +++ b/webhooks-structs.go @@ -46,13 +46,13 @@ type RepoPermissions struct { type Commit struct { Hash string `json:"id"` Message string `json:"message"` - Author Person `json:"author"` - Committer Person `json:"committer"` + Author GitUser `json:"author"` + Committer GitUser `json:"committer"` Verification interface{} `json:"verification"` Timestamp string `json:"timestamp"` } -type Person struct { +type GitUser struct { Name string `json:"name"` Email string `json:"email"` Username string `json:"username"` @@ -114,3 +114,12 @@ type IssueAction struct { Sender RepoUser `json:"sender"` Comment IssueComment `json:"comment"` } + +type CreateAction struct { + BaseWebHook + Sha string `json:"sha"` + Ref string `json:"ref"` + RefType string `json:"ref_type"` + Repository Repository `json:"repository"` + Sender RepoUser `json:"sender"` +} diff --git a/webhooks.go b/webhooks.go index fe3a86f..f6d4af9 100644 --- a/webhooks.go +++ b/webhooks.go @@ -45,6 +45,9 @@ func handleWebHook(writer http.ResponseWriter, request *http.Request) { case "issue_comment": handleIssueComment(data) + case "create": + handleCreate(data) + } } @@ -118,3 +121,27 @@ func handleIssueComment(data []byte) { msg := strings.Replace(stripped, "\n", strings.Repeat(INDENT, 2) + "\n", -1) fmt.Printf(strings.Repeat(INDENT, 2) + msg) } + +func handleCreate(data []byte) { + created := CreateAction{} + json.Unmarshal(data, &created) + switch created.RefType { + case "branch": + handleBranchCreate(created) + default: + fmt.Printf("Unknown create reftype %q:\n", created.RefType) + fmt.Println(string(data)) + } +} + +func handleBranchCreate(branchAction CreateAction) { + printSep() + fmt.Printf( + "%q (%q) created branch %q on repo %q (owned by %q)\n", + branchAction.Sender.Login, + branchAction.Sender.Email, + branchAction.Ref, + branchAction.Repository.FullName, + branchAction.Repository.Owner.Login, + ) +}