2018-01-08 08:54:30 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
|
|
|
|
2018-04-14 22:41:05 +00:00
|
|
|
"github.com/drone/drone-template-lib/template"
|
2018-01-08 08:54:30 +00:00
|
|
|
"github.com/matrix-org/gomatrix"
|
2018-04-14 22:41:05 +00:00
|
|
|
"github.com/pkg/errors"
|
2018-01-08 08:54:30 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type (
|
|
|
|
Repo struct {
|
|
|
|
Owner string
|
|
|
|
Name string
|
|
|
|
}
|
|
|
|
|
|
|
|
Build struct {
|
|
|
|
Tag string
|
|
|
|
Event string
|
|
|
|
Number int
|
|
|
|
Commit string
|
|
|
|
Ref string
|
|
|
|
Branch string
|
|
|
|
Author string
|
|
|
|
Message string
|
|
|
|
DeployTo string
|
|
|
|
Status string
|
|
|
|
Link string
|
|
|
|
Started int64
|
|
|
|
Created int64
|
|
|
|
}
|
|
|
|
|
|
|
|
Job struct {
|
|
|
|
Started int64
|
|
|
|
}
|
|
|
|
|
|
|
|
Config struct {
|
|
|
|
Username string
|
|
|
|
Password string
|
|
|
|
UserID string
|
|
|
|
AccessToken string
|
|
|
|
Homeserver string
|
|
|
|
RoomID string
|
|
|
|
Template string
|
|
|
|
}
|
|
|
|
|
|
|
|
Plugin struct {
|
|
|
|
Repo Repo
|
|
|
|
Build Build
|
|
|
|
Job Job
|
|
|
|
Config Config
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (p Plugin) Exec() error {
|
|
|
|
m, err := gomatrix.NewClient(p.Config.Homeserver, prepend("@", p.Config.UserID), p.Config.AccessToken)
|
|
|
|
|
|
|
|
if err != nil {
|
2018-04-14 22:41:05 +00:00
|
|
|
return errors.Wrap(err, "failed to initialize client")
|
2018-01-08 08:54:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if p.Config.UserID == "" || p.Config.AccessToken == "" {
|
|
|
|
r, err := m.Login(&gomatrix.ReqLogin{
|
|
|
|
Type: "m.login.password",
|
|
|
|
User: p.Config.Username,
|
|
|
|
Password: p.Config.Password,
|
|
|
|
InitialDeviceDisplayName: "Drone",
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
2018-04-14 22:41:05 +00:00
|
|
|
return errors.Wrap(err, "failed to authenticate user")
|
2018-01-08 08:54:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m.SetCredentials(r.UserID, r.AccessToken)
|
|
|
|
}
|
|
|
|
|
|
|
|
joined, err := m.JoinRoom(p.Config.RoomID, "", nil)
|
|
|
|
|
|
|
|
if err != nil {
|
2018-04-14 22:41:05 +00:00
|
|
|
return errors.Wrap(err, "failed to join room")
|
2018-01-08 08:54:30 +00:00
|
|
|
}
|
|
|
|
|
2018-04-14 22:41:05 +00:00
|
|
|
message, err := template.RenderTrim(p.Config.Template, p)
|
2018-01-08 08:54:30 +00:00
|
|
|
|
2018-04-14 22:41:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to render template")
|
2018-01-08 08:54:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := m.SendNotice(joined.RoomID, message); err != nil {
|
2018-04-14 22:41:05 +00:00
|
|
|
return errors.Wrap(err, "failed to submit message")
|
2018-01-08 08:54:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func prepend(prefix, s string) string {
|
|
|
|
if s == "" {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
if strings.HasPrefix(s, prefix) {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
|
|
|
|
return prefix + s
|
|
|
|
}
|