diff --git a/plugins/git/git.go b/plugins/git/git.go index 82a6e2e..d5c0231 100644 --- a/plugins/git/git.go +++ b/plugins/git/git.go @@ -1,8 +1,6 @@ package git import ( - "encoding/json" - "fmt" "net/http" "strings" @@ -84,148 +82,3 @@ func (p *GitPlugin) registerWeb() { http.HandleFunc("/git/gitlab/event", p.gitlabEvent) 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) - } -} diff --git a/plugins/git/gitea.go b/plugins/git/gitea.go index a102478..d13003e 100644 --- a/plugins/git/gitea.go +++ b/plugins/git/gitea.go @@ -1,68 +1,43 @@ 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"` +import ( + "encoding/json" + "fmt" + "net/http" + "strings" + + "github.com/rs/zerolog/log" + "github.com/velour/catbase/bot" +) + +func (p *GitPlugin) giteaEvent(w http.ResponseWriter, r *http.Request) { + icon := p.c.Get("gitlab.icon", ":tea:") + 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 := icon + " " + 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) + } } diff --git a/plugins/git/gitea_push.go b/plugins/git/gitea_push.go new file mode 100644 index 0000000..a102478 --- /dev/null +++ b/plugins/git/gitea_push.go @@ -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"` +} diff --git a/plugins/git/github.go b/plugins/git/github.go new file mode 100644 index 0000000..c211a26 --- /dev/null +++ b/plugins/git/github.go @@ -0,0 +1,81 @@ +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 := "" + 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) + } +} diff --git a/plugins/git/gitlab.go b/plugins/git/gitlab.go new file mode 100644 index 0000000..9855796 --- /dev/null +++ b/plugins/git/gitlab.go @@ -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) + } +}