mirror of
https://git.ferricyanide.solutions/A_D/irc-webhooks.git
synced 2024-12-22 14:27:04 +00:00
added specific webhook handling
This commit is contained in:
parent
99afed6a1c
commit
1a6323d68b
72
webhooks.go
72
webhooks.go
@ -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,21 +85,7 @@ 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)
|
||||
|
||||
@ -104,3 +93,54 @@ func ListenForWebHook(config Config) {
|
||||
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)
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user