made push messages clearer

This commit is contained in:
A_D 2018-11-17 21:42:42 +02:00
parent ec93fcaff9
commit e736688891
No known key found for this signature in database
GPG Key ID: C242F3DD220FA945

View File

@ -8,8 +8,6 @@ import (
"strings"
)
const INDENT = " "
func ListenForWebHook(config Config, msgChan chan string) {
http.HandleFunc("/", CreateHandler(msgChan))
@ -19,6 +17,11 @@ func ListenForWebHook(config Config, msgChan chan string) {
fmt.Println(err)
}
}
func shortHash(hash string) string {
return hash[0:7]
}
func CreateHandler(msgChan chan string) http.HandlerFunc {
return func(writer http.ResponseWriter, request *http.Request) {
defer request.Body.Close()
@ -67,13 +70,31 @@ func handlePush(data []byte, msgChan chan string) {
outIrcMessage := strings.Builder{}
outIrcMessage.WriteString(fmt.Sprintf(
"[%s] %s pushed %d commit(s)",
"[%s] %s pushed %d commit(s) to ref %s: %s..%s",
pushData.Repository.FullName,
pushData.Pusher.Login,
len(pushData.Commits),
pushData.Ref,
shortHash(pushData.Before),
shortHash(pushData.After),
))
msgChan <- outIrcMessage.String()
count := 0
for _, commit := range pushData.Commits {
if count > 4 {
break
}
commitDesc := strings.SplitN(commit.Message, "\n", 1)[0]
msgChan <- fmt.Sprintf(
"[%s] commit %s created by %s: %s",
pushData.Repository.FullName,
shortHash(commit.Hash),
commit.Author.Username,
commitDesc,
)
count++
}
}