Compare commits

..

No commits in common. "7f5f62d04a86cdd925e5762dc020ba79142e13e0" and "6e62ccf00da304a9d51e02b169b16cbb34752746" have entirely different histories.

5 changed files with 212 additions and 250 deletions

View File

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

View File

@ -1,68 +0,0 @@
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"`
}

View File

@ -1,85 +0,0 @@
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)
}
}

View File

@ -1,57 +0,0 @@
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)
}
}