mirror of https://github.com/velour/catbase.git
git: add git hook support for github & gitlab
This commit is contained in:
parent
134d5ea47b
commit
2694463964
1
go.mod
1
go.mod
|
@ -38,6 +38,7 @@ require (
|
|||
golang.org/x/exp v0.0.0-20191014171548-69215a2ee97e // indirect
|
||||
golang.org/x/net v0.0.0-20191014212845-da9a3fd4c582 // indirect
|
||||
gonum.org/v1/gonum v0.6.0 // indirect
|
||||
gopkg.in/go-playground/webhooks.v5 v5.13.0
|
||||
gopkg.in/sourcemap.v1 v1.0.5 // indirect
|
||||
gopkg.in/yaml.v2 v2.2.4 // indirect
|
||||
)
|
||||
|
|
2
go.sum
2
go.sum
|
@ -138,6 +138,8 @@ gonum.org/v1/gonum v0.6.0/go.mod h1:9mxDZsDKxgMAuccQkewq682L+0eCu4dCN2yonUJTCLU=
|
|||
gonum.org/v1/netlib v0.0.0-20190313105609-8cb42192e0e0/go.mod h1:wa6Ws7BG/ESfp6dHfk7C6KdzKA7wR7u/rKwOGE66zvw=
|
||||
gonum.org/v1/plot v0.0.0-20190515093506-e2840ee46a6b/go.mod h1:Wt8AAjI+ypCyYX3nZBvf6cAIx93T+c/OS2HFAYskSZc=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
|
||||
gopkg.in/go-playground/webhooks.v5 v5.13.0 h1:e9vtkQZK464+UdL3YjRox2yR8JSmh2094PUBMvdriFs=
|
||||
gopkg.in/go-playground/webhooks.v5 v5.13.0/go.mod h1:LZbya/qLVdbqDR1aKrGuWV6qbia2zCYSR5dpom2SInQ=
|
||||
gopkg.in/sourcemap.v1 v1.0.5 h1:inv58fC9f9J3TK2Y2R1NPntXEn3/wjWHkonhIUODNTI=
|
||||
gopkg.in/sourcemap.v1 v1.0.5/go.mod h1:2RlvNNSMglmRrcvhfuzp4hQHwOtjxlbjX7UPY/GXb78=
|
||||
gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw=
|
||||
|
|
2
main.go
2
main.go
|
@ -30,6 +30,7 @@ import (
|
|||
"github.com/velour/catbase/plugins/fact"
|
||||
"github.com/velour/catbase/plugins/first"
|
||||
"github.com/velour/catbase/plugins/fuck"
|
||||
"github.com/velour/catbase/plugins/git"
|
||||
"github.com/velour/catbase/plugins/inventory"
|
||||
"github.com/velour/catbase/plugins/leftpad"
|
||||
"github.com/velour/catbase/plugins/nerdepedia"
|
||||
|
@ -131,6 +132,7 @@ func main() {
|
|||
b.AddPlugin(stock.New(b))
|
||||
b.AddPlugin(newsbid.New(b))
|
||||
b.AddPlugin(twitter.New(b))
|
||||
b.AddPlugin(git.New(b))
|
||||
b.AddPlugin(cli.New(b))
|
||||
// catches anything left, will always return true
|
||||
b.AddPlugin(fact.New(b))
|
||||
|
|
|
@ -322,7 +322,7 @@ type Beers struct {
|
|||
|
||||
func (p *BeersPlugin) pullUntappd() ([]checkin, error) {
|
||||
token := p.Bot.Config().Get("Untappd.Token", "NONE")
|
||||
if token == "NONE" {
|
||||
if token == "NONE" || token == "" {
|
||||
return []checkin{}, fmt.Errorf("No untappd token")
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,143 @@
|
|||
package git
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"strings"
|
||||
|
||||
"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()
|
||||
return p
|
||||
}
|
||||
|
||||
func (p *GitPlugin) registerWeb() {
|
||||
http.HandleFunc("/git/github/event", p.githubEvent)
|
||||
http.HandleFunc("/git/github/gitlabEvent", p.gitlabEvent)
|
||||
p.b.RegisterWeb("/git", "Git")
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
msg, repo := "", ""
|
||||
switch payload.(type) {
|
||||
case gitlab.PushEventPayload:
|
||||
push := payload.(gitlab.PushEventPayload)
|
||||
repo = push.Repository.Name
|
||||
user := push.UserName
|
||||
commits := ""
|
||||
for _, c := range push.Commits {
|
||||
m := strings.Split(c.Message, "\n")[0]
|
||||
commits += fmt.Sprintf("%s (%s) %s", c.Author.Name, c.ID[:5], m)
|
||||
}
|
||||
msg = fmt.Sprintf("%s pushed to %s:\n%s", user, repo, commits)
|
||||
default:
|
||||
w.WriteHeader(500)
|
||||
fmt.Fprintf(w, "unknown payload: %+v", payload)
|
||||
return
|
||||
}
|
||||
chs := p.c.GetArray(fmt.Sprintf("gitlab.%s.channels", repo), []string{})
|
||||
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
|
||||
}
|
||||
msg := ""
|
||||
repo := ""
|
||||
switch payload.(type) {
|
||||
case github.PushPayload:
|
||||
push := payload.(github.PushPayload)
|
||||
repo = push.Repository.Name
|
||||
commits := ""
|
||||
for _, c := range push.Commits {
|
||||
m := strings.Split(c.Message, "\n")[0]
|
||||
commits += fmt.Sprintf("%s (%s) %s", c.Author.Name, c.Sha[:5], m)
|
||||
}
|
||||
msg = fmt.Sprintf("%s pushed to %s: \n%s", push.Pusher.Name, push.Repository.Name, commits)
|
||||
case github.PullRequestPayload:
|
||||
pr := payload.(github.PullRequestPayload)
|
||||
repo = pr.Repository.Name
|
||||
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
|
||||
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
|
||||
}
|
||||
|
||||
chs := p.c.GetArray(fmt.Sprintf("github.%s.channels", repo), []string{})
|
||||
for _, ch := range chs {
|
||||
p.b.Send(p.b.DefaultConnector(), bot.Message, ch, msg)
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue