catbase/plugins/git/gitea.go

50 lines
1.0 KiB
Go

package git
import (
"encoding/json"
"fmt"
"net/http"
"sort"
"strings"
"github.com/rs/zerolog/log"
"github.com/velour/catbase/bot"
)
func (p *GitPlugin) giteaEvent(w http.ResponseWriter, r *http.Request) {
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
msg := " "
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]
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 {
p.b.Send(p.b.DefaultConnector(), bot.Message, ch, msg)
}
}