mirror of
https://github.com/thegeeklab/drone-matrix.git
synced 2024-11-22 02:20:38 +00:00
refactor: switch to go mautrix library (#41)
This commit is contained in:
parent
bbb8dbb009
commit
d910fb4ca1
7
go.mod
7
go.mod
@ -7,9 +7,11 @@ require (
|
||||
github.com/matrix-org/gomatrix v0.0.0-20210324163249-be2af5ef2e16
|
||||
github.com/microcosm-cc/bluemonday v1.0.18
|
||||
github.com/russross/blackfriday/v2 v2.1.0
|
||||
github.com/sirupsen/logrus v1.8.1
|
||||
github.com/thegeeklab/drone-plugin-lib v1.0.0
|
||||
github.com/thegeeklab/drone-template-lib/v2 v2.1.1
|
||||
github.com/urfave/cli/v2 v2.8.0
|
||||
maunium.net/go/mautrix v0.11.0
|
||||
)
|
||||
|
||||
require (
|
||||
@ -27,9 +29,8 @@ require (
|
||||
github.com/mitchellh/copystructure v1.2.0 // indirect
|
||||
github.com/mitchellh/reflectwalk v1.0.2 // indirect
|
||||
github.com/shopspring/decimal v1.3.1 // indirect
|
||||
github.com/sirupsen/logrus v1.8.1 // indirect
|
||||
github.com/spf13/cast v1.4.1 // indirect
|
||||
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 // indirect
|
||||
golang.org/x/net v0.0.0-20220325170049-de3da57026de // indirect
|
||||
golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9 // indirect
|
||||
golang.org/x/net v0.0.0-20220513224357-95641704303c // indirect
|
||||
golang.org/x/sys v0.0.0-20220503163025-988cb79eb6c6 // indirect
|
||||
)
|
||||
|
13
go.sum
13
go.sum
@ -52,8 +52,8 @@ github.com/spf13/cast v1.4.1 h1:s0hze+J0196ZfEMTs80N7UlFt0BDuQ7Q+JDnHiMWKdA=
|
||||
github.com/spf13/cast v1.4.1/go.mod h1:Qx5cxh0v+4UWYiBimWS+eyWzqEqokIECu5etghLkUJE=
|
||||
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
|
||||
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
|
||||
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
|
||||
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
|
||||
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
|
||||
github.com/thegeeklab/drone-plugin-lib v1.0.0 h1:PQeLnJa0q988rODYod/XtDy/f0cHHUhOA2Y8m5XRW4g=
|
||||
github.com/thegeeklab/drone-plugin-lib v1.0.0/go.mod h1:XAlZDTSNNJogOH1+nS5u0notdj5dO/gtKnU7guzE95g=
|
||||
github.com/thegeeklab/drone-template-lib/v2 v2.1.1 h1:K/h7OAKycScck6jOfvN/HiiblNdd6vRpijY9B9SXVAo=
|
||||
@ -62,12 +62,12 @@ github.com/urfave/cli/v2 v2.8.0 h1:EZsAB20oRW4nHcB99TTL6PrXpBGIEujMEKdjwruY9KQ=
|
||||
github.com/urfave/cli/v2 v2.8.0/go.mod h1:TYFbtzt/azQoJOrGH5mDfZtS0jIkl/OeFwlRWPR9KRM=
|
||||
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
|
||||
golang.org/x/crypto v0.0.0-20200414173820-0848c9571904/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
|
||||
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064 h1:S25/rfnfsMVgORT4/J61MJ7rdyseOZOyvLIrZEZ7s6s=
|
||||
golang.org/x/crypto v0.0.0-20220321153916-2c7772ba3064/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9 h1:NUzdAbFtCJSXU20AOXgeqaUwg8Ypg4MPYmL+d+rsB5c=
|
||||
golang.org/x/crypto v0.0.0-20220513210258-46612604a0f9/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
|
||||
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
|
||||
golang.org/x/net v0.0.0-20210614182718-04defd469f4e/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
|
||||
golang.org/x/net v0.0.0-20220325170049-de3da57026de h1:pZB1TWnKi+o4bENlbzAgLrEbY4RMYmUIRobMcSmfeYc=
|
||||
golang.org/x/net v0.0.0-20220325170049-de3da57026de/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
|
||||
golang.org/x/net v0.0.0-20220513224357-95641704303c h1:nF9mHSvoKBLkQNQhJZNsc66z2UzAMUbLGjC95CF3pU0=
|
||||
golang.org/x/net v0.0.0-20220513224357-95641704303c/go.mod h1:CfG3xpIq0wQ8r1q4Su4UZFWDARRcnwPjda9FqA0JpMk=
|
||||
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
@ -84,3 +84,6 @@ gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
|
||||
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
|
||||
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo=
|
||||
maunium.net/go/mautrix v0.11.0 h1:B1FBHcvE4Mud+AC+zgNQQOw0JxSVrt40watCejhVA7w=
|
||||
maunium.net/go/mautrix v0.11.0/go.mod h1:K29EcHwsNg6r7fMfwvi0GHQ9o5wSjqB9+Q8RjCIQEjA=
|
||||
|
@ -13,7 +13,11 @@ import (
|
||||
"github.com/matrix-org/gomatrix"
|
||||
"github.com/microcosm-cc/bluemonday"
|
||||
"github.com/russross/blackfriday/v2"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/thegeeklab/drone-template-lib/v2/template"
|
||||
"maunium.net/go/mautrix"
|
||||
"maunium.net/go/mautrix/event"
|
||||
"maunium.net/go/mautrix/id"
|
||||
)
|
||||
|
||||
// Settings for the plugin.
|
||||
@ -29,32 +33,36 @@ type Settings struct {
|
||||
|
||||
// Validate handles the settings validation of the plugin.
|
||||
func (p *Plugin) Validate() error {
|
||||
// Currently there's no validation
|
||||
if (p.settings.Username == "" || p.settings.Password == "") && (p.settings.UserID == "" || p.settings.AccessToken == "") {
|
||||
return fmt.Errorf("either username and password or userid and accesstoken are required")
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// Execute provides the implementation of the plugin.
|
||||
func (p *Plugin) Execute() error {
|
||||
m, err := gomatrix.NewClient(p.settings.Homeserver, prepend("@", p.settings.UserID), p.settings.AccessToken)
|
||||
muid := id.NewUserID(prepend("@", p.settings.UserID), p.settings.Homeserver)
|
||||
client, err := mautrix.NewClient(p.settings.Homeserver, muid, p.settings.AccessToken)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to initialize client: %w", err)
|
||||
}
|
||||
|
||||
if p.settings.UserID == "" || p.settings.AccessToken == "" {
|
||||
r, err := m.Login(&gomatrix.ReqLogin{
|
||||
_, err := client.Login(&mautrix.ReqLogin{
|
||||
Type: "m.login.password",
|
||||
User: p.settings.Username,
|
||||
Identifier: mautrix.UserIdentifier{Type: mautrix.IdentifierTypeUser, User: p.settings.Username},
|
||||
Password: p.settings.Password,
|
||||
InitialDeviceDisplayName: "Drone",
|
||||
StoreCredentials: true,
|
||||
})
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to authenticate user: %w", err)
|
||||
}
|
||||
|
||||
m.SetCredentials(r.UserID, r.AccessToken)
|
||||
}
|
||||
logrus.Info("successfully logged in")
|
||||
|
||||
joined, err := m.JoinRoom(prepend("!", p.settings.RoomID), "", nil)
|
||||
joined, err := client.JoinRoom(prepend("!", p.settings.RoomID), "", nil)
|
||||
if err != nil {
|
||||
return fmt.Errorf("failed to join room: %w", err)
|
||||
}
|
||||
@ -75,9 +83,10 @@ func (p *Plugin) Execute() error {
|
||||
FormattedBody: string(formatted),
|
||||
}
|
||||
|
||||
if _, err := m.SendMessageEvent(joined.RoomID, "m.room.message", content); err != nil {
|
||||
if _, err := client.SendMessageEvent(joined.RoomID, event.EventMessage, content); err != nil {
|
||||
return fmt.Errorf("failed to submit message: %w", err)
|
||||
}
|
||||
logrus.Info("message sent successfully")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user