From 1c4dd9fbbb7a7555120042d95409d1fa8ca96982 Mon Sep 17 00:00:00 2001 From: fredix Date: Sun, 9 Jun 2019 18:53:08 +0200 Subject: [PATCH] added drone template --- drone-gotify.go | 200 ++++++++++++++++++++++++++++++++++++++++++++---- plugin.go | 119 ++++++++++++++++++++++++++++ 2 files changed, 306 insertions(+), 13 deletions(-) create mode 100644 plugin.go diff --git a/drone-gotify.go b/drone-gotify.go index 3990d59..2a70478 100644 --- a/drone-gotify.go +++ b/drone-gotify.go @@ -1,24 +1,198 @@ package main import ( - "net/http" - "net/url" + //"log" + //"net/http" + //"net/url" + "fmt" "os" - "log" + + "github.com/Sirupsen/logrus" + "github.com/joho/godotenv" + "github.com/urfave/cli" +) + +var ( + version = "0.0.1" + build = "0" ) func main() { - token := os.Getenv("PLUGIN_GOTIFYTOKEN") - endpoint := os.Getenv("PLUGIN_GOTIFYENDPOINT") - title := os.Getenv("PLUGIN_GOTIFYTITLE") - message := os.Getenv("PLUGIN_MESSAGE") - priority := os.Getenv("PLUGIN_GOTIFYPRIORITY") - resp, err := http.PostForm(endpoint + "/message?token=" + token, url.Values{"message": {message}, "title": {title}, "priority": {priority}}) - if resp != nil { - resp.Body.Close() + /* + token := os.Getenv("PLUGIN_GOTIFYTOKEN") + endpoint := os.Getenv("PLUGIN_GOTIFYENDPOINT") + title := os.Getenv("PLUGIN_GOTIFYTITLE") + message := os.Getenv("PLUGIN_MESSAGE") + priority := os.Getenv("PLUGIN_GOTIFYPRIORITY") + + resp, err := http.PostForm(endpoint+"/message?token="+token, url.Values{"message": {message}, "title": {title}, "priority": {priority}}) + if resp != nil { + resp.Body.Close() + } + if err != nil { + log.Fatalln(err) + } + */ + + app := cli.NewApp() + app.Name = "gotify plugin" + app.Usage = "gotify plugin" + app.Action = run + app.Version = fmt.Sprintf("%s+%s", version, build) + app.Flags = []cli.Flag{ + cli.StringFlag{ + Name: "token", + Usage: "gotify token", + EnvVar: "PLUGIN_GOTIFY_TOKEN, PLUGIN_GOTIFYTOKEN", + }, + cli.StringFlag{ + Name: "endpoint", + Usage: "gotify endpoint", + EnvVar: "PLUGIN_GOTIFY_ENDPOINT, PLUGIN_GOTIFYENDPOINT", + }, + cli.StringFlag{ + Name: "title", + Usage: "gotify title", + EnvVar: "PLUGIN_GOTIFY_TITLE, PLUGIN_GOTIFYTITLE", + }, + cli.StringFlag{ + Name: "priority", + Usage: "gotify priority", + EnvVar: "PLUGIN_GOTIFY_PRIORITY, PLUGIN_GOTIFYPRIORITY", + }, + + cli.StringFlag{ + Name: "template", + Usage: "message template", + EnvVar: "PLUGIN_MESSAGE, PLUGIN_TEMPLATE", + }, + cli.StringFlag{ + Name: "repo.owner", + Usage: "repository owner", + EnvVar: "DRONE_REPO_OWNER", + }, + cli.StringFlag{ + Name: "repo.name", + Usage: "repository name", + EnvVar: "DRONE_REPO_NAME", + }, + cli.StringFlag{ + Name: "commit.sha", + Usage: "git commit sha", + EnvVar: "DRONE_COMMIT_SHA", + }, + cli.StringFlag{ + Name: "commit.ref", + Value: "refs/heads/master", + Usage: "git commit ref", + EnvVar: "DRONE_COMMIT_REF", + }, + cli.StringFlag{ + Name: "commit.branch", + Value: "master", + Usage: "git commit branch", + EnvVar: "DRONE_COMMIT_BRANCH", + }, + cli.StringFlag{ + Name: "commit.author", + Usage: "git author name", + EnvVar: "DRONE_COMMIT_AUTHOR", + }, + cli.StringFlag{ + Name: "commit.message", + Usage: "commit message", + EnvVar: "DRONE_COMMIT_MESSAGE", + }, + cli.StringFlag{ + Name: "build.event", + Value: "push", + Usage: "build event", + EnvVar: "DRONE_BUILD_EVENT", + }, + cli.IntFlag{ + Name: "build.number", + Usage: "build number", + EnvVar: "DRONE_BUILD_NUMBER", + }, + cli.StringFlag{ + Name: "build.status", + Usage: "build status", + Value: "success", + EnvVar: "DRONE_BUILD_STATUS", + }, + cli.StringFlag{ + Name: "build.link", + Usage: "build link", + EnvVar: "DRONE_BUILD_LINK", + }, + cli.Int64Flag{ + Name: "build.started", + Usage: "build started", + EnvVar: "DRONE_BUILD_STARTED", + }, + cli.Int64Flag{ + Name: "build.created", + Usage: "build created", + EnvVar: "DRONE_BUILD_CREATED", + }, + cli.StringFlag{ + Name: "build.tag", + Usage: "build tag", + EnvVar: "DRONE_TAG", + }, + cli.Int64Flag{ + Name: "job.started", + Usage: "job started", + EnvVar: "DRONE_JOB_STARTED", + }, + cli.StringFlag{ + Name: "env-file", + Usage: "source env file", + }, } - if err != nil { - log.Fatalln(err) + + if err := app.Run(os.Args); err != nil { + logrus.Fatal(err) } + +} + +func run(c *cli.Context) error { + if c.String("env-file") != "" { + _ = godotenv.Load(c.String("env-file")) + } + + plugin := Plugin{ + Repo: Repo{ + Owner: c.String("repo.owner"), + Name: c.String("repo.name"), + }, + Build: Build{ + Tag: c.String("build.tag"), + Number: c.Int("build.number"), + Event: c.String("build.event"), + Status: c.String("build.status"), + Commit: c.String("commit.sha"), + Ref: c.String("commit.ref"), + Branch: c.String("commit.branch"), + Author: c.String("commit.author"), + Message: c.String("commit.message"), + Link: c.String("build.link"), + Started: c.Int64("build.started"), + Created: c.Int64("build.created"), + }, + Job: Job{ + Started: c.Int64("job.started"), + }, + Config: Config{ + Endpoint: c.String("endpoint"), + Template: c.String("template"), + Token: c.String("token"), + Title: c.String("title"), + Priority: c.String("priority"), + }, + } + + return plugin.Exec() } diff --git a/plugin.go b/plugin.go new file mode 100644 index 0000000..9eb5db7 --- /dev/null +++ b/plugin.go @@ -0,0 +1,119 @@ +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 + } + + //client := google_chat.NewClient(p.Config.Webhook, p.Config.Key, p.Config.Token, p.Config.ConversationKey) + //return client.SendMessage(&google_chat.Message{text}) + + 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() + } + /* + if err != nil { + log.Fatalln(err) + } + */ + + 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 +}