irc-webhooks/src/webhooks.go

140 lines
3.9 KiB
Go
Raw Permalink Normal View History

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-17 19:03:51 +00:00
func ListenForWebHook(config Config, msgChan chan string) {
http.HandleFunc("/", CreateHandler(msgChan))
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-17 19:42:42 +00:00
func shortHash(hash string) string {
return hash[0:7]
}
2018-11-17 19:03:51 +00:00
func CreateHandler(msgChan chan string) http.HandlerFunc {
2018-11-17 19:42:42 +00:00
return func(writer http.ResponseWriter, request *http.Request) {
2018-11-17 19:03:51 +00:00
defer request.Body.Close()
data, err := ioutil.ReadAll(request.Body)
if err != nil {
panic(err)
}
2018-11-16 20:34:13 +00:00
2018-11-17 19:03:51 +00:00
switch eventType := request.Header.Get("X-Gitea-Event"); eventType {
default:
fmt.Printf("Unknown event type: %q\n", eventType)
handleUnknown(data, msgChan)
2018-11-16 20:34:13 +00:00
2018-11-17 19:03:51 +00:00
case "":
// Not a gitea event.
fmt.Printf("Got a non-event HTTP request %+v\n", request)
2018-11-16 20:34:13 +00:00
2018-11-17 19:03:51 +00:00
case "push":
handlePush(data, msgChan)
2018-11-17 14:10:01 +00:00
2018-11-17 19:03:51 +00:00
case "issues":
handleIssueAction(data, msgChan)
2018-11-17 14:10:01 +00:00
2018-11-17 19:03:51 +00:00
case "issue_comment":
handleIssueComment(data, msgChan)
2018-11-17 14:32:22 +00:00
2018-11-17 19:03:51 +00:00
case "create":
handleCreate(data, msgChan)
2018-11-17 15:13:47 +00:00
2018-11-17 19:03:51 +00:00
}
2018-11-16 20:34:13 +00:00
}
}
func printSep() {
fmt.Println("---")
}
2018-11-17 19:03:51 +00:00
func handleUnknown(data []byte, msgChan chan string) {
printSep()
fmt.Println(string(data))
}
2018-11-17 19:03:51 +00:00
func handlePush(data []byte, msgChan chan string) {
2018-11-16 20:34:13 +00:00
pushData := Push{}
json.Unmarshal(data, &pushData)
2018-11-17 19:03:51 +00:00
outIrcMessage := strings.Builder{}
2018-11-17 19:13:56 +00:00
2018-11-17 19:03:51 +00:00
outIrcMessage.WriteString(fmt.Sprintf(
2018-11-17 19:42:42 +00:00
"[%s] %s pushed %d commit(s) to ref %s: %s..%s",
2018-11-17 19:03:51 +00:00
pushData.Repository.FullName,
pushData.Pusher.Login,
len(pushData.Commits),
2018-11-17 19:42:42 +00:00
pushData.Ref,
shortHash(pushData.Before),
shortHash(pushData.After),
))
2018-11-16 20:34:13 +00:00
2018-11-17 19:03:51 +00:00
msgChan <- outIrcMessage.String()
2018-11-17 19:42:42 +00:00
count := 0
for _, commit := range pushData.Commits {
if count > 4 {
break
}
commitDesc := strings.SplitN(commit.Message, "\n", 1)[0]
msgChan <- fmt.Sprintf(
2018-11-17 19:45:40 +00:00
"[%s] commit %s created by %s (%s): %s",
2018-11-17 19:42:42 +00:00
pushData.Repository.FullName,
shortHash(commit.Hash),
2018-11-17 19:45:40 +00:00
commit.Author.Name,
commit.Author.Email,
2018-11-17 19:42:42 +00:00
commitDesc,
)
count++
}
2018-11-16 20:34:13 +00:00
}
2018-11-17 14:10:01 +00:00
2018-11-17 19:03:51 +00:00
func handleIssueAction(data []byte, msgChan chan string) {
2018-11-17 14:10:01 +00:00
action := IssueAction{}
json.Unmarshal(data, &action)
2018-11-17 19:22:39 +00:00
outMsg := strings.Builder{}
outMsg.WriteString(fmt.Sprintf("[%s] ", action.Repository.FullName))
switch action.Action {
case "opened":
outMsg.WriteString(fmt.Sprintf("%s opened an issue: %q", action.Sender.Login, action.Issue.Title))
case "closed":
outMsg.WriteString(fmt.Sprintf("%s closed issue %q", action.Sender.Login, action.Issue.Title))
default:
2018-11-17 19:49:50 +00:00
outMsg.WriteString(fmt.Sprintf("%s performed an unknown action (%s) on issue %d", action.Sender.Login, action.Action, int(action.Issue.Id)))
fmt.Println(string(data))
2018-11-17 19:22:39 +00:00
}
msgChan <- outMsg.String()
2018-11-17 14:10:01 +00:00
}
2018-11-17 14:32:22 +00:00
2018-11-17 19:03:51 +00:00
func handleIssueComment(data []byte, msgChan chan string) {
2018-11-17 14:32:22 +00:00
action := IssueAction{}
json.Unmarshal(data, &action)
2018-11-17 19:49:50 +00:00
msgChan <- fmt.Sprintf("[%s] %s commented on issue %d (%s)", action.Repository.FullName, action.Sender.Login, int(action.Issue.Id), action.Issue.Title)
2018-11-17 14:32:22 +00:00
}
2018-11-17 15:13:47 +00:00
2018-11-17 19:03:51 +00:00
func handleCreate(data []byte, msgChan chan string) {
2018-11-17 15:13:47 +00:00
created := CreateAction{}
json.Unmarshal(data, &created)
switch created.RefType {
case "branch":
2018-11-17 19:03:51 +00:00
handleBranchCreate(created, msgChan)
2018-11-17 15:13:47 +00:00
default:
fmt.Printf("Unknown create reftype %q:\n", created.RefType)
fmt.Println(string(data))
}
}
2018-11-17 19:03:51 +00:00
func handleBranchCreate(branchAction CreateAction, msgChan chan string) {
2018-11-17 19:13:56 +00:00
msgChan <- fmt.Sprintf("[%s] %s created branch %s", branchAction.Repository.FullName, branchAction.Sender.Login, branchAction.Ref)
2018-11-17 15:13:47 +00:00
}