mirror of
https://github.com/thegeeklab/wp-matrix.git
synced 2024-11-21 14:20:41 +00:00
Use new template lib
This commit is contained in:
parent
d6bf193ae0
commit
22b1a98e17
25
Gopkg.lock
generated
25
Gopkg.lock
generated
@ -3,9 +3,20 @@
|
|||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
name = "github.com/aymerick/raymond"
|
name = "github.com/aymerick/raymond"
|
||||||
packages = [".","ast","lexer","parser"]
|
packages = [
|
||||||
revision = "a2232af10b53ef1ae5a767f5178db3a6c1dab655"
|
".",
|
||||||
version = "v2.0.1"
|
"ast",
|
||||||
|
"lexer",
|
||||||
|
"parser"
|
||||||
|
]
|
||||||
|
revision = "2eebd0f5dd9564c0c6e439df11d8a3c7b9b9ab11"
|
||||||
|
version = "v2.0.2"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
branch = "master"
|
||||||
|
name = "github.com/drone/drone-template-lib"
|
||||||
|
packages = ["template"]
|
||||||
|
revision = "5748d3149f4859c6d6cf43edb4fa35bba379c918"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
branch = "master"
|
branch = "master"
|
||||||
@ -13,6 +24,12 @@
|
|||||||
packages = ["."]
|
packages = ["."]
|
||||||
revision = "a7fc80c8060c2544fe5d4dae465b584f8e9b4e27"
|
revision = "a7fc80c8060c2544fe5d4dae465b584f8e9b4e27"
|
||||||
|
|
||||||
|
[[projects]]
|
||||||
|
name = "github.com/pkg/errors"
|
||||||
|
packages = ["."]
|
||||||
|
revision = "645ef00459ed84a119197bfb8d8205042c6df63d"
|
||||||
|
version = "v0.8.0"
|
||||||
|
|
||||||
[[projects]]
|
[[projects]]
|
||||||
name = "github.com/urfave/cli"
|
name = "github.com/urfave/cli"
|
||||||
packages = ["."]
|
packages = ["."]
|
||||||
@ -22,6 +39,6 @@
|
|||||||
[solve-meta]
|
[solve-meta]
|
||||||
analyzer-name = "dep"
|
analyzer-name = "dep"
|
||||||
analyzer-version = 1
|
analyzer-version = 1
|
||||||
inputs-digest = "02adaa1a61f60449825943c7e92d6d66d4b88b225834f75dc6c181a96836f25b"
|
inputs-digest = "e4283d82ea22e941f9ab4d2020b12f3721cd0235cb78cca670d2ae1105e148ef"
|
||||||
solver-name = "gps-cdcl"
|
solver-name = "gps-cdcl"
|
||||||
solver-version = 1
|
solver-version = 1
|
||||||
|
12
Gopkg.toml
12
Gopkg.toml
@ -1,11 +1,19 @@
|
|||||||
[[constraint]]
|
[[constraint]]
|
||||||
name = "github.com/aymerick/raymond"
|
branch = "master"
|
||||||
version = "2.0.1"
|
name = "github.com/drone/drone-template-lib"
|
||||||
|
|
||||||
[[constraint]]
|
[[constraint]]
|
||||||
branch = "master"
|
branch = "master"
|
||||||
name = "github.com/matrix-org/gomatrix"
|
name = "github.com/matrix-org/gomatrix"
|
||||||
|
|
||||||
|
[[constraint]]
|
||||||
|
name = "github.com/pkg/errors"
|
||||||
|
version = "0.8.0"
|
||||||
|
|
||||||
[[constraint]]
|
[[constraint]]
|
||||||
name = "github.com/urfave/cli"
|
name = "github.com/urfave/cli"
|
||||||
version = "1.20.0"
|
version = "1.20.0"
|
||||||
|
|
||||||
|
[prune]
|
||||||
|
go-tests = true
|
||||||
|
unused-packages = true
|
||||||
|
7
main.go
7
main.go
@ -15,8 +15,8 @@ var (
|
|||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
app := cli.NewApp()
|
app := cli.NewApp()
|
||||||
app.Name = "codecov plugin"
|
app.Name = "matrix plugin"
|
||||||
app.Usage = "codecov plugin"
|
app.Usage = "matrix plugin"
|
||||||
app.Version = fmt.Sprintf("%s+%s", version, build)
|
app.Version = fmt.Sprintf("%s+%s", version, build)
|
||||||
app.Action = run
|
app.Action = run
|
||||||
app.Flags = []cli.Flag{
|
app.Flags = []cli.Flag{
|
||||||
@ -42,9 +42,9 @@ func main() {
|
|||||||
},
|
},
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "homeserver",
|
Name: "homeserver",
|
||||||
Value: "https://matrix.org",
|
|
||||||
Usage: "matrix home server",
|
Usage: "matrix home server",
|
||||||
EnvVar: "PLUGIN_HOMESERVER,MATRIX_HOMESERVER",
|
EnvVar: "PLUGIN_HOMESERVER,MATRIX_HOMESERVER",
|
||||||
|
Value: "https://matrix.org",
|
||||||
},
|
},
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "roomid",
|
Name: "roomid",
|
||||||
@ -55,6 +55,7 @@ func main() {
|
|||||||
Name: "template",
|
Name: "template",
|
||||||
Usage: "template for the message",
|
Usage: "template for the message",
|
||||||
EnvVar: "PLUGIN_TEMPLATE,MATRIX_TEMPLATE",
|
EnvVar: "PLUGIN_TEMPLATE,MATRIX_TEMPLATE",
|
||||||
|
Value: "Build {{ build.status }} <{{ build.link }}|{{ repo.Owner }}/{{ repo.Name }}#{{ truncate build.commit 8 }}> ({{ build.branch }}) by {{ build.author }}",
|
||||||
},
|
},
|
||||||
cli.StringFlag{
|
cli.StringFlag{
|
||||||
Name: "repo.owner",
|
Name: "repo.owner",
|
||||||
|
36
plugin.go
36
plugin.go
@ -1,10 +1,11 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/drone/drone-template-lib/template"
|
||||||
"github.com/matrix-org/gomatrix"
|
"github.com/matrix-org/gomatrix"
|
||||||
|
"github.com/pkg/errors"
|
||||||
)
|
)
|
||||||
|
|
||||||
type (
|
type (
|
||||||
@ -55,7 +56,7 @@ func (p Plugin) Exec() error {
|
|||||||
m, err := gomatrix.NewClient(p.Config.Homeserver, prepend("@", p.Config.UserID), p.Config.AccessToken)
|
m, err := gomatrix.NewClient(p.Config.Homeserver, prepend("@", p.Config.UserID), p.Config.AccessToken)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.Wrap(err, "failed to initialize client")
|
||||||
}
|
}
|
||||||
|
|
||||||
if p.Config.UserID == "" || p.Config.AccessToken == "" {
|
if p.Config.UserID == "" || p.Config.AccessToken == "" {
|
||||||
@ -67,7 +68,7 @@ func (p Plugin) Exec() error {
|
|||||||
})
|
})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.Wrap(err, "failed to authenticate user")
|
||||||
}
|
}
|
||||||
|
|
||||||
m.SetCredentials(r.UserID, r.AccessToken)
|
m.SetCredentials(r.UserID, r.AccessToken)
|
||||||
@ -76,41 +77,22 @@ func (p Plugin) Exec() error {
|
|||||||
joined, err := m.JoinRoom(p.Config.RoomID, "", nil)
|
joined, err := m.JoinRoom(p.Config.RoomID, "", nil)
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return errors.Wrap(err, "failed to join room")
|
||||||
}
|
}
|
||||||
|
|
||||||
message := message(p.Repo, p.Build)
|
message, err := template.RenderTrim(p.Config.Template, p)
|
||||||
|
|
||||||
if p.Config.Template != "" {
|
if err != nil {
|
||||||
if message, err = RenderTrim(p.Config.Template, p); err != nil {
|
return errors.Wrap(err, "failed to render template")
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if _, err := m.SendNotice(joined.RoomID, message); err != nil {
|
if _, err := m.SendNotice(joined.RoomID, message); err != nil {
|
||||||
return err
|
return errors.Wrap(err, "failed to submit message")
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func message(repo Repo, build Build) string {
|
|
||||||
return fmt.Sprintf(
|
|
||||||
"Build %s <%s|%s/%s#%s> (%s) by %s",
|
|
||||||
build.Status,
|
|
||||||
build.Link,
|
|
||||||
repo.Owner,
|
|
||||||
repo.Name,
|
|
||||||
build.Commit[:8],
|
|
||||||
build.Branch,
|
|
||||||
build.Author,
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
func prepend(prefix, s string) string {
|
func prepend(prefix, s string) string {
|
||||||
if s == "" {
|
if s == "" {
|
||||||
return s
|
return s
|
||||||
|
137
template.go
137
template.go
@ -1,137 +0,0 @@
|
|||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"fmt"
|
|
||||||
"io/ioutil"
|
|
||||||
"net/http"
|
|
||||||
"net/url"
|
|
||||||
"strings"
|
|
||||||
"time"
|
|
||||||
"unicode"
|
|
||||||
"unicode/utf8"
|
|
||||||
|
|
||||||
"github.com/aymerick/raymond"
|
|
||||||
)
|
|
||||||
|
|
||||||
func init() {
|
|
||||||
raymond.RegisterHelpers(funcs)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Render parses and executes a template, returning the results in string format.
|
|
||||||
func Render(template string, payload interface{}) (s string, err error) {
|
|
||||||
u, err := url.Parse(template)
|
|
||||||
if err == nil {
|
|
||||||
switch u.Scheme {
|
|
||||||
case "http", "https":
|
|
||||||
res, err := http.Get(template)
|
|
||||||
if err != nil {
|
|
||||||
return s, err
|
|
||||||
}
|
|
||||||
defer res.Body.Close()
|
|
||||||
out, err := ioutil.ReadAll(res.Body)
|
|
||||||
if err != nil {
|
|
||||||
return s, err
|
|
||||||
}
|
|
||||||
template = string(out)
|
|
||||||
|
|
||||||
case "file":
|
|
||||||
out, err := ioutil.ReadFile(u.Path)
|
|
||||||
if err != nil {
|
|
||||||
return s, err
|
|
||||||
}
|
|
||||||
template = string(out)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return raymond.Render(template, payload)
|
|
||||||
}
|
|
||||||
|
|
||||||
// RenderTrim parses and executes a template, returning the results in string
|
|
||||||
// format. The result is trimmed to remove left and right padding and newlines
|
|
||||||
// that may be added unintentially in the template markup.
|
|
||||||
func RenderTrim(template string, playload interface{}) (string, error) {
|
|
||||||
out, err := Render(template, playload)
|
|
||||||
return strings.Trim(out, " \n"), err
|
|
||||||
}
|
|
||||||
|
|
||||||
var funcs = map[string]interface{}{
|
|
||||||
"uppercasefirst": uppercaseFirst,
|
|
||||||
"uppercase": strings.ToUpper,
|
|
||||||
"lowercase": strings.ToLower,
|
|
||||||
"duration": toDuration,
|
|
||||||
"datetime": toDatetime,
|
|
||||||
"success": isSuccess,
|
|
||||||
"failure": isFailure,
|
|
||||||
"truncate": truncate,
|
|
||||||
"urlencode": urlencode,
|
|
||||||
"since": since,
|
|
||||||
}
|
|
||||||
|
|
||||||
func truncate(s string, len int) string {
|
|
||||||
if utf8.RuneCountInString(s) <= len {
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
runes := []rune(s)
|
|
||||||
return string(runes[:len])
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
func uppercaseFirst(s string) string {
|
|
||||||
a := []rune(s)
|
|
||||||
a[0] = unicode.ToUpper(a[0])
|
|
||||||
s = string(a)
|
|
||||||
return s
|
|
||||||
}
|
|
||||||
|
|
||||||
func toDuration(started, finished float64) string {
|
|
||||||
return fmt.Sprintln(time.Duration(finished-started) * time.Second)
|
|
||||||
}
|
|
||||||
|
|
||||||
func toDatetime(timestamp float64, layout, zone string) string {
|
|
||||||
if len(zone) == 0 {
|
|
||||||
return time.Unix(int64(timestamp), 0).Format(layout)
|
|
||||||
}
|
|
||||||
loc, err := time.LoadLocation(zone)
|
|
||||||
if err != nil {
|
|
||||||
return time.Unix(int64(timestamp), 0).Local().Format(layout)
|
|
||||||
}
|
|
||||||
return time.Unix(int64(timestamp), 0).In(loc).Format(layout)
|
|
||||||
}
|
|
||||||
|
|
||||||
func isSuccess(conditional bool, options *raymond.Options) string {
|
|
||||||
if !conditional {
|
|
||||||
return options.Inverse()
|
|
||||||
}
|
|
||||||
|
|
||||||
switch options.ParamStr(0) {
|
|
||||||
case "success":
|
|
||||||
return options.Fn()
|
|
||||||
default:
|
|
||||||
return options.Inverse()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func isFailure(conditional bool, options *raymond.Options) string {
|
|
||||||
if !conditional {
|
|
||||||
return options.Inverse()
|
|
||||||
}
|
|
||||||
|
|
||||||
switch options.ParamStr(0) {
|
|
||||||
case "failure", "error", "killed":
|
|
||||||
return options.Fn()
|
|
||||||
default:
|
|
||||||
return options.Inverse()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func urlencode(options *raymond.Options) string {
|
|
||||||
return url.QueryEscape(options.Fn())
|
|
||||||
}
|
|
||||||
|
|
||||||
func since(start int64) string {
|
|
||||||
// NOTE: not using `time.Since()` because the fractional second component
|
|
||||||
// will give us something like "40m12.917523438s" vs "40m12s". We lose
|
|
||||||
// some precision, but the format is much more readable.
|
|
||||||
now := time.Unix(time.Now().Unix(), 0)
|
|
||||||
return fmt.Sprintln(now.Sub(time.Unix(start, 0)))
|
|
||||||
}
|
|
Loading…
Reference in New Issue
Block a user