mirror of https://github.com/velour/catbase.git
git: refactor
This commit is contained in:
parent
1f954f301c
commit
8f391ee7bc
|
@ -1,10 +1,9 @@
|
||||||
package git
|
package git
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"regexp"
|
||||||
|
|
||||||
"github.com/velour/catbase/bot/msg"
|
|
||||||
|
|
||||||
"github.com/rs/zerolog/log"
|
"github.com/rs/zerolog/log"
|
||||||
"gopkg.in/go-playground/webhooks.v5/github"
|
"gopkg.in/go-playground/webhooks.v5/github"
|
||||||
|
@ -15,10 +14,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type GitPlugin struct {
|
type GitPlugin struct {
|
||||||
b bot.Bot
|
b bot.Bot
|
||||||
c *config.Config
|
c *config.Config
|
||||||
glhook *gitlab.Webhook
|
glhook *gitlab.Webhook
|
||||||
ghhook *github.Webhook
|
ghhook *github.Webhook
|
||||||
|
handlers bot.HandlerTable
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(b bot.Bot) *GitPlugin {
|
func New(b bot.Bot) *GitPlugin {
|
||||||
|
@ -39,7 +39,7 @@ func New(b bot.Bot) *GitPlugin {
|
||||||
ghhook: ghhook,
|
ghhook: ghhook,
|
||||||
}
|
}
|
||||||
p.registerWeb()
|
p.registerWeb()
|
||||||
b.Register(p, bot.Message, p.message)
|
p.register()
|
||||||
return p
|
return p
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -47,33 +47,33 @@ func validService(service string) bool {
|
||||||
return service == "gitea" || service == "gitlab" || service == "github"
|
return service == "gitea" || service == "gitlab" || service == "github"
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *GitPlugin) message(c bot.Connector, kind bot.Kind, message msg.Message, args ...interface{}) bool {
|
func (p *GitPlugin) register() {
|
||||||
body := message.Body
|
p.handlers = bot.HandlerTable{
|
||||||
lower := strings.ToLower(body)
|
{Kind: bot.Message, IsCmd: true,
|
||||||
parts := strings.Split(lower, " ")
|
Regex: regexp.MustCompile(`(?i)^regrepo (?P<service>\S+)\.(?P<owner>\S+)\.(?P<repo>\S+)$`),
|
||||||
|
Handler: func(r bot.Request) bool {
|
||||||
if !strings.HasPrefix(lower, "regrepo") || len(parts) != 2 {
|
service := r.Values["service"]
|
||||||
return false
|
owner := r.Values["owner"]
|
||||||
|
repo := r.Values["repo"]
|
||||||
|
spec := fmt.Sprintf("%s.%s.%s", service, owner, repo)
|
||||||
|
if !validService(service) {
|
||||||
|
m := "Valid formats are: `service.owner.repo` and valid services are one of gitea, gitlab, or github."
|
||||||
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, m)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
chs := p.c.GetArray(spec, []string{})
|
||||||
|
for _, ch := range chs {
|
||||||
|
if ch == r.Msg.Channel {
|
||||||
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "That's already registered here.")
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
chs = append(chs, r.Msg.Channel)
|
||||||
|
p.c.SetArray(spec+".channels", chs)
|
||||||
|
p.b.Send(r.Conn, bot.Message, r.Msg.Channel, "Registered new repository.")
|
||||||
|
return true
|
||||||
|
}},
|
||||||
}
|
}
|
||||||
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
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *GitPlugin) registerWeb() {
|
func (p *GitPlugin) registerWeb() {
|
||||||
|
|
Loading…
Reference in New Issue