catbase/plugins/git/gitea.go

50 lines
1.1 KiB
Go
Raw Permalink Normal View History

package git
import (
"encoding/json"
"fmt"
"net/http"
2019-11-29 16:07:52 +00:00
"sort"
"strings"
"github.com/rs/zerolog/log"
"github.com/velour/catbase/bot"
)
func (p *GitPlugin) giteaEvent(w http.ResponseWriter, r *http.Request) {
2019-11-03 13:27:50 +00:00
icon := p.c.Get("gitea.icon", ":tea:")
evt := GiteaPush{}
dec := json.NewDecoder(r.Body)
err := dec.Decode(&evt)
if err != nil {
log.Error().Err(err).Msg("could not decode gitea push")
w.WriteHeader(500)
fmt.Fprintf(w, " Error parsing event: %s", err)
return
}
org := evt.Repository.Owner.Username
repo := evt.Repository.Name
2019-11-04 13:15:54 +00:00
msg := " "
2019-11-29 16:07:52 +00:00
commits := evt.Commits
sort.Slice(commits, func(i, j int) bool {
return commits[i].Timestamp.Before(commits[j].Timestamp)
})
for _, c := range commits {
m := strings.Split(c.Message, "\n")[0]
2019-11-04 13:15:54 +00:00
msg += fmt.Sprintf("%s %s pushed to %s (<%s|%s>) %s\n",
icon,
c.Author.Name,
repo,
c.URL,
c.ID[:7],
m,
)
}
chs := p.c.GetArray(fmt.Sprintf("gitea.%s.%s.channels", org, repo), []string{})
for _, ch := range chs {
2021-01-09 21:55:55 +00:00
p.b.Send(p.b.DefaultConnector(), bot.Message, ch, msg, bot.UnfurlLinks(false))
}
}