2019-10-26 19:01:29 +00:00
|
|
|
package git
|
|
|
|
|
|
|
|
import (
|
2019-10-26 21:39:01 +00:00
|
|
|
"encoding/json"
|
2019-10-26 19:01:29 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
|
2019-10-26 21:39:01 +00:00
|
|
|
"github.com/velour/catbase/bot/msg"
|
|
|
|
|
2019-10-26 19:01:29 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"gopkg.in/go-playground/webhooks.v5/github"
|
|
|
|
"gopkg.in/go-playground/webhooks.v5/gitlab"
|
|
|
|
|
|
|
|
"github.com/velour/catbase/bot"
|
|
|
|
"github.com/velour/catbase/config"
|
|
|
|
)
|
|
|
|
|
|
|
|
type GitPlugin struct {
|
|
|
|
b bot.Bot
|
|
|
|
c *config.Config
|
|
|
|
glhook *gitlab.Webhook
|
|
|
|
ghhook *github.Webhook
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(b bot.Bot) *GitPlugin {
|
|
|
|
glhook, err := gitlab.New()
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("could not initialize hook")
|
|
|
|
glhook = nil
|
|
|
|
}
|
|
|
|
ghhook, err := github.New()
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("could not initialize hook")
|
|
|
|
ghhook = nil
|
|
|
|
}
|
|
|
|
p := &GitPlugin{
|
|
|
|
b: b,
|
|
|
|
c: b.Config(),
|
|
|
|
glhook: glhook,
|
|
|
|
ghhook: ghhook,
|
|
|
|
}
|
|
|
|
p.registerWeb()
|
2019-10-26 21:39:01 +00:00
|
|
|
b.Register(p, bot.Message, p.message)
|
2019-10-26 19:01:29 +00:00
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
2019-10-26 21:39:01 +00:00
|
|
|
func validService(service string) bool {
|
|
|
|
return service == "gitea" || service == "gitlab" || service == "github"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GitPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
|
|
|
body := message.Body
|
|
|
|
lower := strings.ToLower(body)
|
|
|
|
parts := strings.Split(lower, " ")
|
|
|
|
|
|
|
|
if !strings.HasPrefix(lower, "regrepo") || len(parts) != 2 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
u := parts[1]
|
|
|
|
u = strings.ReplaceAll(u, "/", ".")
|
|
|
|
uparts := strings.Split(u, ".")
|
|
|
|
if len(uparts) != 3 || !validService(uparts[0]) {
|
|
|
|
m := "Valid formats are: `service.owner.repo` and valid services are one of gitea, gitlab, or github."
|
|
|
|
p.b.Send(c, bot.Message, message.Channel, m)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
chs := p.c.GetArray(u+".channels", []string{})
|
|
|
|
for _, ch := range chs {
|
|
|
|
if ch == message.Channel {
|
|
|
|
p.b.Send(c, bot.Message, message.Channel, "That's already registered here.")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chs = append(chs, message.Channel)
|
|
|
|
p.c.SetArray(u+".channels", chs)
|
|
|
|
p.b.Send(c, bot.Message, message.Channel, "Registered new repository.")
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-10-26 19:01:29 +00:00
|
|
|
func (p *GitPlugin) registerWeb() {
|
2019-10-26 21:39:01 +00:00
|
|
|
http.HandleFunc("/git/gitea/event", p.giteaEvent)
|
2019-10-26 19:01:29 +00:00
|
|
|
http.HandleFunc("/git/github/event", p.githubEvent)
|
2019-10-26 19:37:26 +00:00
|
|
|
http.HandleFunc("/git/gitlab/event", p.gitlabEvent)
|
2019-10-26 19:01:29 +00:00
|
|
|
p.b.RegisterWeb("/git", "Git")
|
|
|
|
}
|
|
|
|
|
2019-10-26 21:39:01 +00:00
|
|
|
func (p *GitPlugin) giteaEvent(w http.ResponseWriter, r *http.Request) {
|
|
|
|
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 := ""
|
|
|
|
for _, c := range evt.Commits {
|
|
|
|
m := strings.Split(c.Message, "\n")[0]
|
2019-11-02 22:18:15 +00:00
|
|
|
msg += fmt.Sprintf("%s pushed to %s (<%s|%s>) %s\n",
|
2019-10-26 21:39:01 +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 19:01:29 +00:00
|
|
|
func (p *GitPlugin) gitlabEvent(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if p.glhook == nil {
|
|
|
|
log.Error().Msg("gitlab hook not initialized")
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprint(w, "not initialized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
payload, err := p.glhook.Parse(r,
|
|
|
|
gitlab.PushEvents,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("unknown event")
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprintf(w, "unknown event: %s", err)
|
|
|
|
return
|
|
|
|
}
|
2019-10-26 21:39:01 +00:00
|
|
|
msg, repo, owner := "", "", ""
|
2019-10-26 19:01:29 +00:00
|
|
|
switch payload.(type) {
|
|
|
|
case gitlab.PushEventPayload:
|
|
|
|
push := payload.(gitlab.PushEventPayload)
|
|
|
|
repo = push.Repository.Name
|
2019-10-26 21:39:01 +00:00
|
|
|
owner = strings.ReplaceAll(push.Project.PathWithNamespace, "/", ".")
|
2019-10-26 19:01:29 +00:00
|
|
|
commits := ""
|
|
|
|
for _, c := range push.Commits {
|
|
|
|
m := strings.Split(c.Message, "\n")[0]
|
2019-10-26 19:14:51 +00:00
|
|
|
commits += fmt.Sprintf("%s pushed to %s (<%s|%s>) %s\n",
|
|
|
|
c.Author.Name,
|
|
|
|
repo,
|
|
|
|
c.URL,
|
|
|
|
c.ID[:7],
|
|
|
|
m,
|
|
|
|
)
|
2019-10-26 19:01:29 +00:00
|
|
|
}
|
2019-10-26 19:14:51 +00:00
|
|
|
msg = commits
|
2019-10-26 19:01:29 +00:00
|
|
|
default:
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprintf(w, "unknown payload: %+v", payload)
|
|
|
|
return
|
|
|
|
}
|
2019-10-26 21:39:01 +00:00
|
|
|
chs := p.c.GetArray(fmt.Sprintf("gitlab.%s.channels", owner), []string{})
|
2019-10-26 19:01:29 +00:00
|
|
|
for _, ch := range chs {
|
|
|
|
p.b.Send(p.b.DefaultConnector(), bot.Message, ch, msg)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *GitPlugin) githubEvent(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if p.ghhook == nil {
|
|
|
|
log.Error().Msg("github hook not initialized")
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprintf(w, "not initialized")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
payload, err := p.ghhook.Parse(r,
|
|
|
|
github.PushEvent,
|
|
|
|
github.PullRequestEvent,
|
|
|
|
github.PingEvent,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("unknown event")
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprintf(w, "unknown event: %+v", err)
|
|
|
|
return
|
|
|
|
}
|
2019-10-26 21:39:01 +00:00
|
|
|
msg, repo, owner := "", "", ""
|
2019-10-26 19:01:29 +00:00
|
|
|
switch payload.(type) {
|
|
|
|
case github.PushPayload:
|
|
|
|
push := payload.(github.PushPayload)
|
|
|
|
repo = push.Repository.Name
|
2019-10-26 21:39:01 +00:00
|
|
|
owner = push.Repository.Owner.Login
|
2019-10-26 19:01:29 +00:00
|
|
|
commits := ""
|
|
|
|
for _, c := range push.Commits {
|
|
|
|
m := strings.Split(c.Message, "\n")[0]
|
2019-10-26 19:14:51 +00:00
|
|
|
commits += fmt.Sprintf("%s pushed to %s (<%s|%s>) %s\n",
|
|
|
|
c.Author.Name,
|
|
|
|
repo,
|
|
|
|
c.URL,
|
|
|
|
c.ID[:7],
|
|
|
|
m,
|
|
|
|
)
|
2019-10-26 19:01:29 +00:00
|
|
|
}
|
2019-10-26 19:14:51 +00:00
|
|
|
msg = commits
|
2019-10-26 19:01:29 +00:00
|
|
|
case github.PullRequestPayload:
|
|
|
|
pr := payload.(github.PullRequestPayload)
|
2019-10-26 19:07:37 +00:00
|
|
|
if pr.Action != "opened" {
|
|
|
|
w.WriteHeader(200)
|
|
|
|
fmt.Fprintf(w, "ignoring action %s", pr.Action)
|
|
|
|
return
|
|
|
|
}
|
2019-10-26 19:01:29 +00:00
|
|
|
repo = pr.Repository.Name
|
2019-10-26 21:39:01 +00:00
|
|
|
owner = pr.Repository.Owner.Login
|
2019-10-26 19:01:29 +00:00
|
|
|
msg = fmt.Sprintf("%s opened new pull request \"%s\" on %s: %s",
|
|
|
|
pr.PullRequest.User.Login,
|
|
|
|
pr.PullRequest.Title,
|
|
|
|
pr.Repository.Name,
|
|
|
|
pr.PullRequest.URL,
|
|
|
|
)
|
|
|
|
case github.PingPayload:
|
|
|
|
ping := payload.(github.PingPayload)
|
|
|
|
repo = ping.Repository.Name
|
2019-10-26 21:39:01 +00:00
|
|
|
owner = ping.Repository.Owner.Login
|
2019-10-26 19:01:29 +00:00
|
|
|
msg = fmt.Sprintf("Got a ping request on %s", repo)
|
|
|
|
default:
|
|
|
|
log.Error().Interface("payload", payload).Msg("unknown event payload")
|
|
|
|
w.WriteHeader(500)
|
|
|
|
fmt.Fprintf(w, "unknown event payload: %+v", payload)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-10-26 21:39:01 +00:00
|
|
|
chs := p.c.GetArray(fmt.Sprintf("github.%s.%s.channels", owner, repo), []string{})
|
2019-10-26 19:01:29 +00:00
|
|
|
for _, ch := range chs {
|
|
|
|
p.b.Send(p.b.DefaultConnector(), bot.Message, ch, msg)
|
|
|
|
}
|
|
|
|
}
|