2018-11-16 16:43:18 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2018-11-16 20:34:13 +00:00
|
|
|
"strings"
|
2018-11-16 16:43:18 +00:00
|
|
|
)
|
|
|
|
|
2018-11-16 20:34:13 +00:00
|
|
|
const INDENT = " "
|
|
|
|
|
2018-11-16 19:31:45 +00:00
|
|
|
func ListenForWebHook(config Config) {
|
2018-11-16 20:34:13 +00:00
|
|
|
http.HandleFunc("/", handleWebHook)
|
2018-11-16 19:31:45 +00:00
|
|
|
|
2018-11-16 20:34:13 +00:00
|
|
|
err := http.ListenAndServe(config.BindHost+":"+config.BindPort, nil)
|
2018-11-16 19:31:45 +00:00
|
|
|
|
2018-11-16 16:43:18 +00:00
|
|
|
if err != nil {
|
|
|
|
fmt.Println(err)
|
|
|
|
}
|
|
|
|
}
|
2018-11-16 20:34:13 +00:00
|
|
|
|
|
|
|
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 {
|
2018-11-17 14:10:01 +00:00
|
|
|
default:
|
|
|
|
fmt.Printf("Unknown event type: %q\n", eventType)
|
|
|
|
handleUnknown(data)
|
2018-11-16 20:34:13 +00:00
|
|
|
|
|
|
|
case "":
|
|
|
|
// Not a gitea event.
|
|
|
|
fmt.Printf("Got a non-event HTTP request %+v\n", request)
|
|
|
|
|
2018-11-17 14:10:01 +00:00
|
|
|
case "push":
|
|
|
|
handlePush(data)
|
|
|
|
|
|
|
|
case "issues":
|
|
|
|
handleIssueAction(data)
|
|
|
|
|
2018-11-17 14:32:22 +00:00
|
|
|
case "issue_comment":
|
|
|
|
handleIssueComment(data)
|
|
|
|
|
2018-11-17 15:13:47 +00:00
|
|
|
case "create":
|
|
|
|
handleCreate(data)
|
|
|
|
|
2018-11-16 20:34:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-17 13:07:33 +00:00
|
|
|
func printSep() {
|
|
|
|
fmt.Println("---")
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleUnknown(data []byte) {
|
|
|
|
printSep()
|
|
|
|
fmt.Println(string(data))
|
|
|
|
}
|
|
|
|
|
2018-11-16 20:34:13 +00:00
|
|
|
func handlePush(data []byte) {
|
2018-11-17 13:07:33 +00:00
|
|
|
printSep()
|
2018-11-16 20:34:13 +00:00
|
|
|
pushData := Push{}
|
|
|
|
json.Unmarshal(data, &pushData)
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-11-17 14:10:01 +00:00
|
|
|
|
|
|
|
func handleIssueAction(data []byte) {
|
|
|
|
action := IssueAction{}
|
|
|
|
json.Unmarshal(data, &action)
|
|
|
|
printSep()
|
|
|
|
fmt.Printf(
|
|
|
|
"%q (%q) performed the action %q on repo %q and issue %q\n",
|
|
|
|
action.Sender.Login,
|
|
|
|
action.Sender.Email,
|
|
|
|
action.Action,
|
|
|
|
action.Repository.FullName,
|
|
|
|
action.Issue.Title,
|
|
|
|
)
|
|
|
|
}
|
2018-11-17 14:32:22 +00:00
|
|
|
|
|
|
|
func handleIssueComment(data []byte) {
|
|
|
|
action := IssueAction{}
|
|
|
|
json.Unmarshal(data, &action)
|
|
|
|
printSep()
|
|
|
|
fmt.Printf(
|
|
|
|
"%q (%q) performed the comment action %q on repo %q and issue %q\n",
|
|
|
|
action.Sender.Login,
|
|
|
|
action.Sender.Email,
|
|
|
|
action.Action,
|
|
|
|
action.Repository.FullName,
|
|
|
|
action.Issue.Title,
|
|
|
|
)
|
|
|
|
|
2018-11-17 14:33:41 +00:00
|
|
|
fmt.Printf(INDENT + "---\n")
|
2018-11-17 14:55:50 +00:00
|
|
|
stripped := strings.Replace(action.Comment.Body, "\r", "", -1)
|
|
|
|
msg := strings.Replace(stripped, "\n", strings.Repeat(INDENT, 2) + "\n", -1)
|
2018-11-17 14:32:22 +00:00
|
|
|
fmt.Printf(strings.Repeat(INDENT, 2) + msg)
|
|
|
|
}
|
2018-11-17 15:13:47 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
}
|