2019-10-26 21:39:01 +00:00
|
|
|
package git
|
|
|
|
|
2019-11-03 12:44:15 +00:00
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2019-11-29 16:07:52 +00:00
|
|
|
"sort"
|
2019-11-03 12:44:15 +00:00
|
|
|
"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:")
|
2019-11-03 12:44:15 +00:00
|
|
|
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 {
|
2019-11-03 12:44:15 +00:00
|
|
|
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,
|
2019-11-03 12:44:15 +00:00
|
|
|
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)
|
|
|
|
}
|
2019-10-26 21:39:01 +00:00
|
|
|
}
|