117 lines
3.1 KiB
Go
117 lines
3.1 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"io/ioutil"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
const INDENT = " "
|
|
|
|
func ListenForWebHook(config Config, msgChan chan string) {
|
|
http.HandleFunc("/", CreateHandler(msgChan))
|
|
|
|
err := http.ListenAndServe(config.BindHost+":"+config.BindPort, nil)
|
|
|
|
if err != nil {
|
|
fmt.Println(err)
|
|
}
|
|
}
|
|
func CreateHandler(msgChan chan string) http.HandlerFunc {
|
|
return func (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 {
|
|
default:
|
|
fmt.Printf("Unknown event type: %q\n", eventType)
|
|
handleUnknown(data, msgChan)
|
|
|
|
case "":
|
|
// Not a gitea event.
|
|
fmt.Printf("Got a non-event HTTP request %+v\n", request)
|
|
|
|
case "push":
|
|
handlePush(data, msgChan)
|
|
|
|
case "issues":
|
|
handleIssueAction(data, msgChan)
|
|
|
|
case "issue_comment":
|
|
handleIssueComment(data, msgChan)
|
|
|
|
case "create":
|
|
handleCreate(data, msgChan)
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
func printSep() {
|
|
fmt.Println("---")
|
|
}
|
|
|
|
func handleUnknown(data []byte, msgChan chan string) {
|
|
printSep()
|
|
fmt.Println(string(data))
|
|
}
|
|
|
|
func handlePush(data []byte, msgChan chan string) {
|
|
printSep()
|
|
pushData := Push{}
|
|
json.Unmarshal(data, &pushData)
|
|
outIrcMessage := strings.Builder{}
|
|
|
|
outIrcMessage.WriteString(fmt.Sprintf(
|
|
"[%s] %s pushed %d commits",
|
|
pushData.Repository.FullName,
|
|
pushData.Pusher.Login,
|
|
len(pushData.Commits),
|
|
))
|
|
|
|
msgChan <- outIrcMessage.String()
|
|
|
|
}
|
|
|
|
func handleIssueAction(data []byte, msgChan chan string) {
|
|
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,
|
|
)
|
|
msgChan <- fmt.Sprintf("[%s] %s performed the action %s on issue %f (%s)", action.Repository.FullName, action.Sender.Login, action.Action, action.Issue.Id, action.Issue.Title)
|
|
}
|
|
|
|
func handleIssueComment(data []byte, msgChan chan string) {
|
|
action := IssueAction{}
|
|
json.Unmarshal(data, &action)
|
|
msgChan <- fmt.Sprintf("[%s] %s commented on issue %f (%s)", action.Repository.FullName, action.Sender.Login, action.Issue.Id, action.Issue.Title)
|
|
}
|
|
|
|
func handleCreate(data []byte, msgChan chan string) {
|
|
created := CreateAction{}
|
|
json.Unmarshal(data, &created)
|
|
switch created.RefType {
|
|
case "branch":
|
|
handleBranchCreate(created, msgChan)
|
|
default:
|
|
fmt.Printf("Unknown create reftype %q:\n", created.RefType)
|
|
fmt.Println(string(data))
|
|
}
|
|
}
|
|
|
|
func handleBranchCreate(branchAction CreateAction, msgChan chan string) {
|
|
msgChan <- fmt.Sprintf("[%s] %s created branch %s", branchAction.Repository.FullName, branchAction.Sender.Login, branchAction.Ref)
|
|
}
|