Compare commits

...

4 Commits

Author SHA1 Message Date
Chris Sexton 7f5f62d04a git: fix icon 2019-11-03 08:27:50 -05:00
Chris Sexton 97ca444df9 git: don't show empty changeset 2019-11-03 08:10:01 -05:00
Chris Sexton d1149e7022 git: actually do the thing 2019-11-03 07:59:53 -05:00
Chris Sexton 6ada638968 git: add icons
Also break services out into individual files
2019-11-03 07:47:45 -05:00
5 changed files with 250 additions and 212 deletions

View File

@ -1,8 +1,6 @@
package git package git
import ( import (
"encoding/json"
"fmt"
"net/http" "net/http"
"strings" "strings"
@ -84,148 +82,3 @@ func (p *GitPlugin) registerWeb() {
http.HandleFunc("/git/gitlab/event", p.gitlabEvent) http.HandleFunc("/git/gitlab/event", p.gitlabEvent)
p.b.RegisterWeb("/git", "Git") p.b.RegisterWeb("/git", "Git")
} }
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]
msg += fmt.Sprintf("%s pushed to %s (<%s|%s>) %s\n",
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)
}
}
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, owner := "", "", ""
switch payload.(type) {
case gitlab.PushEventPayload:
push := payload.(gitlab.PushEventPayload)
repo = push.Repository.Name
owner = strings.ReplaceAll(push.Project.PathWithNamespace, "/", ".")
commits := ""
for _, c := range push.Commits {
m := strings.Split(c.Message, "\n")[0]
commits += fmt.Sprintf("%s pushed to %s (<%s|%s>) %s\n",
c.Author.Name,
repo,
c.URL,
c.ID[:7],
m,
)
}
msg = commits
default:
w.WriteHeader(500)
fmt.Fprintf(w, "unknown payload: %+v", payload)
return
}
chs := p.c.GetArray(fmt.Sprintf("gitlab.%s.channels", owner), []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, owner := "", "", ""
switch payload.(type) {
case github.PushPayload:
push := payload.(github.PushPayload)
repo = push.Repository.Name
owner = push.Repository.Owner.Login
commits := ""
for _, c := range push.Commits {
m := strings.Split(c.Message, "\n")[0]
commits += fmt.Sprintf("%s pushed to %s (<%s|%s>) %s\n",
c.Author.Name,
repo,
c.URL,
c.ID[:7],
m,
)
}
msg = commits
case github.PullRequestPayload:
pr := payload.(github.PullRequestPayload)
if pr.Action != "opened" {
w.WriteHeader(200)
fmt.Fprintf(w, "ignoring action %s", pr.Action)
return
}
repo = pr.Repository.Name
owner = pr.Repository.Owner.Login
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
owner = ping.Repository.Owner.Login
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.%s.channels", owner, repo), []string{})
for _, ch := range chs {
p.b.Send(p.b.DefaultConnector(), bot.Message, ch, msg)
}
}

View File

@ -1,68 +1,43 @@
package git package git
type GiteaPush struct { import (
Secret string `json:"secret"` "encoding/json"
Ref string `json:"ref"` "fmt"
Before string `json:"before"` "net/http"
After string `json:"after"` "strings"
CompareURL string `json:"compare_url"`
Commits []struct { "github.com/rs/zerolog/log"
ID string `json:"id"` "github.com/velour/catbase/bot"
Message string `json:"message"` )
URL string `json:"url"`
Author struct { func (p *GitPlugin) giteaEvent(w http.ResponseWriter, r *http.Request) {
Name string `json:"name"` icon := p.c.Get("gitea.icon", ":tea:")
Email string `json:"email"` evt := GiteaPush{}
Username string `json:"username"` dec := json.NewDecoder(r.Body)
} `json:"author"` err := dec.Decode(&evt)
Committer struct { if err != nil {
Name string `json:"name"` log.Error().Err(err).Msg("could not decode gitea push")
Email string `json:"email"` w.WriteHeader(500)
Username string `json:"username"` fmt.Fprintf(w, " Error parsing event: %s", err)
} `json:"committer"` return
Timestamp string `json:"timestamp"` }
} `json:"commits"` org := evt.Repository.Owner.Username
Repository struct { repo := evt.Repository.Name
ID int `json:"id"`
Owner struct { msg := icon + " "
ID int `json:"id"` for _, c := range evt.Commits {
Login string `json:"login"` m := strings.Split(c.Message, "\n")[0]
FullName string `json:"full_name"` msg += fmt.Sprintf("%s pushed to %s (<%s|%s>) %s\n",
Email string `json:"email"` c.Author.Name,
AvatarURL string `json:"avatar_url"` repo,
Username string `json:"username"` c.URL,
} `json:"owner"` c.ID[:7],
Name string `json:"name"` m,
FullName string `json:"full_name"` )
Description string `json:"description"` }
Private bool `json:"private"`
Fork bool `json:"fork"` chs := p.c.GetArray(fmt.Sprintf("gitea.%s.%s.channels", org, repo), []string{})
HTMLURL string `json:"html_url"` for _, ch := range chs {
SSHURL string `json:"ssh_url"` p.b.Send(p.b.DefaultConnector(), bot.Message, ch, msg)
CloneURL string `json:"clone_url"` }
Website string `json:"website"`
StarsCount int `json:"stars_count"`
ForksCount int `json:"forks_count"`
WatchersCount int `json:"watchers_count"`
OpenIssuesCount int `json:"open_issues_count"`
DefaultBranch string `json:"default_branch"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
} `json:"repository"`
Pusher struct {
ID int `json:"id"`
Login string `json:"login"`
FullName string `json:"full_name"`
Email string `json:"email"`
AvatarURL string `json:"avatar_url"`
Username string `json:"username"`
} `json:"pusher"`
Sender struct {
ID int `json:"id"`
Login string `json:"login"`
FullName string `json:"full_name"`
Email string `json:"email"`
AvatarURL string `json:"avatar_url"`
Username string `json:"username"`
} `json:"sender"`
} }

68
plugins/git/gitea_push.go Normal file
View File

@ -0,0 +1,68 @@
package git
type GiteaPush struct {
Secret string `json:"secret"`
Ref string `json:"ref"`
Before string `json:"before"`
After string `json:"after"`
CompareURL string `json:"compare_url"`
Commits []struct {
ID string `json:"id"`
Message string `json:"message"`
URL string `json:"url"`
Author struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
} `json:"author"`
Committer struct {
Name string `json:"name"`
Email string `json:"email"`
Username string `json:"username"`
} `json:"committer"`
Timestamp string `json:"timestamp"`
} `json:"commits"`
Repository struct {
ID int `json:"id"`
Owner struct {
ID int `json:"id"`
Login string `json:"login"`
FullName string `json:"full_name"`
Email string `json:"email"`
AvatarURL string `json:"avatar_url"`
Username string `json:"username"`
} `json:"owner"`
Name string `json:"name"`
FullName string `json:"full_name"`
Description string `json:"description"`
Private bool `json:"private"`
Fork bool `json:"fork"`
HTMLURL string `json:"html_url"`
SSHURL string `json:"ssh_url"`
CloneURL string `json:"clone_url"`
Website string `json:"website"`
StarsCount int `json:"stars_count"`
ForksCount int `json:"forks_count"`
WatchersCount int `json:"watchers_count"`
OpenIssuesCount int `json:"open_issues_count"`
DefaultBranch string `json:"default_branch"`
CreatedAt string `json:"created_at"`
UpdatedAt string `json:"updated_at"`
} `json:"repository"`
Pusher struct {
ID int `json:"id"`
Login string `json:"login"`
FullName string `json:"full_name"`
Email string `json:"email"`
AvatarURL string `json:"avatar_url"`
Username string `json:"username"`
} `json:"pusher"`
Sender struct {
ID int `json:"id"`
Login string `json:"login"`
FullName string `json:"full_name"`
Email string `json:"email"`
AvatarURL string `json:"avatar_url"`
Username string `json:"username"`
} `json:"sender"`
}

85
plugins/git/github.go Normal file
View File

@ -0,0 +1,85 @@
package git
import (
"fmt"
"net/http"
"strings"
"github.com/rs/zerolog/log"
"github.com/velour/catbase/bot"
"gopkg.in/go-playground/webhooks.v5/github"
)
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
}
icon := p.c.Get("github.icon", ":octocat:")
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, owner := icon+" ", "", ""
switch payload.(type) {
case github.PushPayload:
push := payload.(github.PushPayload)
repo = push.Repository.Name
owner = push.Repository.Owner.Login
commits := ""
if len(push.Commits) == 0 {
log.Debug().Msg("GitHub sent an empty changeset")
return
}
for _, c := range push.Commits {
m := strings.Split(c.Message, "\n")[0]
commits += fmt.Sprintf("%s pushed to %s (<%s|%s>) %s\n",
c.Author.Name,
repo,
c.URL,
c.ID[:7],
m,
)
}
msg += commits
case github.PullRequestPayload:
pr := payload.(github.PullRequestPayload)
if pr.Action != "opened" {
w.WriteHeader(200)
fmt.Fprintf(w, "ignoring action %s", pr.Action)
return
}
repo = pr.Repository.Name
owner = pr.Repository.Owner.Login
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
owner = ping.Repository.Owner.Login
msg += fmt.Sprintf(icon+"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.%s.channels", owner, repo), []string{})
for _, ch := range chs {
p.b.Send(p.b.DefaultConnector(), bot.Message, ch, msg)
}
}

57
plugins/git/gitlab.go Normal file
View File

@ -0,0 +1,57 @@
package git
import (
"fmt"
"net/http"
"strings"
"github.com/rs/zerolog/log"
"github.com/velour/catbase/bot"
"gopkg.in/go-playground/webhooks.v5/gitlab"
)
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
}
icon := p.c.Get("gitlab.icon", ":gitlab:")
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, owner := icon+" ", "", ""
switch payload.(type) {
case gitlab.PushEventPayload:
push := payload.(gitlab.PushEventPayload)
repo = push.Repository.Name
owner = strings.ReplaceAll(push.Project.PathWithNamespace, "/", ".")
commits := ""
for _, c := range push.Commits {
m := strings.Split(c.Message, "\n")[0]
commits += fmt.Sprintf("%s pushed to %s (<%s|%s>) %s\n",
c.Author.Name,
repo,
c.URL,
c.ID[:7],
m,
)
}
msg += commits
default:
w.WriteHeader(500)
fmt.Fprintf(w, "unknown payload: %+v", payload)
return
}
chs := p.c.GetArray(fmt.Sprintf("gitlab.%s.channels", owner), []string{})
for _, ch := range chs {
p.b.Send(p.b.DefaultConnector(), bot.Message, ch, msg)
}
}