0
0
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:
Thomas Boerger 2018-04-15 00:41:05 +02:00 committed by Don
parent d6bf193ae0
commit 22b1a98e17
5 changed files with 44 additions and 173 deletions

25
Gopkg.lock generated
View File

@ -3,9 +3,20 @@
[[projects]]
name = "github.com/aymerick/raymond"
packages = [".","ast","lexer","parser"]
revision = "a2232af10b53ef1ae5a767f5178db3a6c1dab655"
version = "v2.0.1"
packages = [
".",
"ast",
"lexer",
"parser"
]
revision = "2eebd0f5dd9564c0c6e439df11d8a3c7b9b9ab11"
version = "v2.0.2"
[[projects]]
branch = "master"
name = "github.com/drone/drone-template-lib"
packages = ["template"]
revision = "5748d3149f4859c6d6cf43edb4fa35bba379c918"
[[projects]]
branch = "master"
@ -13,6 +24,12 @@
packages = ["."]
revision = "a7fc80c8060c2544fe5d4dae465b584f8e9b4e27"
[[projects]]
name = "github.com/pkg/errors"
packages = ["."]
revision = "645ef00459ed84a119197bfb8d8205042c6df63d"
version = "v0.8.0"
[[projects]]
name = "github.com/urfave/cli"
packages = ["."]
@ -22,6 +39,6 @@
[solve-meta]
analyzer-name = "dep"
analyzer-version = 1
inputs-digest = "02adaa1a61f60449825943c7e92d6d66d4b88b225834f75dc6c181a96836f25b"
inputs-digest = "e4283d82ea22e941f9ab4d2020b12f3721cd0235cb78cca670d2ae1105e148ef"
solver-name = "gps-cdcl"
solver-version = 1

View File

@ -1,11 +1,19 @@
[[constraint]]
name = "github.com/aymerick/raymond"
version = "2.0.1"
branch = "master"
name = "github.com/drone/drone-template-lib"
[[constraint]]
branch = "master"
name = "github.com/matrix-org/gomatrix"
[[constraint]]
name = "github.com/pkg/errors"
version = "0.8.0"
[[constraint]]
name = "github.com/urfave/cli"
version = "1.20.0"
[prune]
go-tests = true
unused-packages = true

View File

@ -15,8 +15,8 @@ var (
func main() {
app := cli.NewApp()
app.Name = "codecov plugin"
app.Usage = "codecov plugin"
app.Name = "matrix plugin"
app.Usage = "matrix plugin"
app.Version = fmt.Sprintf("%s+%s", version, build)
app.Action = run
app.Flags = []cli.Flag{
@ -42,9 +42,9 @@ func main() {
},
cli.StringFlag{
Name: "homeserver",
Value: "https://matrix.org",
Usage: "matrix home server",
EnvVar: "PLUGIN_HOMESERVER,MATRIX_HOMESERVER",
Value: "https://matrix.org",
},
cli.StringFlag{
Name: "roomid",
@ -55,6 +55,7 @@ func main() {
Name: "template",
Usage: "template for the message",
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{
Name: "repo.owner",

View File

@ -1,10 +1,11 @@
package main
import (
"fmt"
"strings"
"github.com/drone/drone-template-lib/template"
"github.com/matrix-org/gomatrix"
"github.com/pkg/errors"
)
type (
@ -55,7 +56,7 @@ func (p Plugin) Exec() error {
m, err := gomatrix.NewClient(p.Config.Homeserver, prepend("@", p.Config.UserID), p.Config.AccessToken)
if err != nil {
return err
return errors.Wrap(err, "failed to initialize client")
}
if p.Config.UserID == "" || p.Config.AccessToken == "" {
@ -67,7 +68,7 @@ func (p Plugin) Exec() error {
})
if err != nil {
return err
return errors.Wrap(err, "failed to authenticate user")
}
m.SetCredentials(r.UserID, r.AccessToken)
@ -76,41 +77,22 @@ func (p Plugin) Exec() error {
joined, err := m.JoinRoom(p.Config.RoomID, "", 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 message, err = RenderTrim(p.Config.Template, p); err != nil {
return err
}
if err != nil {
return err
}
if err != nil {
return errors.Wrap(err, "failed to render template")
}
if _, err := m.SendNotice(joined.RoomID, message); err != nil {
return err
return errors.Wrap(err, "failed to submit message")
}
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 {
if s == "" {
return s

View File

@ -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)))
}