added specific webhook handling

This commit is contained in:
A_D 2018-11-16 22:34:13 +02:00
parent 99afed6a1c
commit 1a6323d68b
No known key found for this signature in database
GPG Key ID: C242F3DD220FA945
1 changed files with 57 additions and 17 deletions

View File

@ -5,8 +5,11 @@ import (
"fmt"
"io/ioutil"
"net/http"
"strings"
)
const INDENT = " "
type BaseWebHook struct {
Secret string `json:"secret"`
}
@ -70,7 +73,7 @@ type Commit struct {
Hash string `json:"id"`
Message string `json:"message"`
Author Person `json:"author"`
Commiter Person `json:"commiter"`
Committer Person `json:"committer"`
Verification interface{} `json:"verification"`
Timestamp string `json:"timestamp"`
}
@ -82,25 +85,62 @@ type Person struct {
}
func ListenForWebHook(config Config) {
http.HandleFunc(
"/", func(writer http.ResponseWriter, request *http.Request) {
data, err := ioutil.ReadAll(request.Body)
if err != nil {
panic(err)
}
push := Push{}
json.Unmarshal(data, &push)
fmt.Println(data)
fmt.Println(string(data))
fmt.Println(request)
fmt.Printf("%+v\n", push)
request.Body.Close()
},
)
http.HandleFunc("/", handleWebHook)
err := http.ListenAndServe(config.BindHost + ":" + config.BindPort, nil)
err := http.ListenAndServe(config.BindHost+":"+config.BindPort, nil)
if err != nil {
fmt.Println(err)
}
}
func handleWebHook(writer http.ResponseWriter, request *http.Request) {
defer request.Body.Close()
data, err := ioutil.ReadAll(request.Body)
if err != nil {
panic(err)
}
switch eventType := request.Header.Get("X-Gitea-Event"); eventType {
case "push":
handlePush(data)
case "":
// Not a gitea event.
fmt.Printf("Got a non-event HTTP request %+v\n", request)
return
default:
fmt.Printf("Unknown event type: %q\n", eventType)
return
}
}
func handlePush(data []byte) {
pushData := Push{}
json.Unmarshal(data, &pushData)
fmt.Println("---")
fmt.Printf(
"Got a push for repo %q owned by %q. Pushed by %q (%q)\n",
pushData.Repository.FullName,
pushData.Repository.Owner.Login,
pushData.Pusher.Login,
pushData.Pusher.Email,
)
for _, commit := range pushData.Commits {
fmt.Println(INDENT + "--")
fmt.Printf(
INDENT+"Commit added by %q (%q), made by %q (%q), with hash %q and message:\n",
commit.Committer.Name,
commit.Committer.Email,
commit.Author.Name,
commit.Author.Email,
commit.Hash,
)
msg := strings.Replace(commit.Message, "\n", strings.Repeat(INDENT, 2)+"\n", -1)
fmt.Println(strings.Repeat(INDENT, 2) + msg)
}
}