drone-gotify/plugin.go
2019-06-09 19:41:37 +02:00

111 lines
1.7 KiB
Go

package main
import (
"fmt"
"net/http"
"net/url"
"strings"
"github.com/drone/drone-template-lib/template"
)
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
Status string
Link string
Started int64
Created int64
}
Config struct {
Endpoint string
Template string
Token string
Title string
Priority string
}
Job struct {
Started int64
}
Plugin struct {
Repo Repo
Build Build
Config Config
Job Job
}
)
func (p Plugin) Exec() error {
text := message(p.Repo, p.Build)
if p.Config.Template != "" {
txt, err := template.RenderTrim(p.Config.Template, p)
if err != nil {
return err
}
text = txt
}
resp, err := http.PostForm(p.Config.Endpoint+"/message?token="+p.Config.Token, url.Values{"message": {text}, "title": {p.Config.Title}, "priority": {p.Config.Priority}})
if resp != nil {
resp.Body.Close()
}
return err
}
func message(repo Repo, build Build) string {
return fmt.Sprintf("*%s* <%s|%s/%s#%s> (%s) by %s",
build.Status,
build.Link,
repo.Owner,
repo.Name,
build.Commit[:8],
build.Branch,
build.Author,
)
}
func fallback(repo Repo, build Build) string {
return fmt.Sprintf("%s %s/%s#%s (%s) by %s",
build.Status,
repo.Owner,
repo.Name,
build.Commit[:8],
build.Branch,
build.Author,
)
}
func color(build Build) string {
switch build.Status {
case "success":
return "good"
case "failure", "error", "killed":
return "danger"
default:
return "warning"
}
}
func prepend(prefix, s string) string {
if !strings.HasPrefix(s, prefix) {
return prefix + s
}
return s
}