drone-gotify/drone-gotify.go
2020-04-19 11:45:47 +02:00

179 lines
3.7 KiB
Go

package main
import (
"fmt"
"os"
"github.com/sirupsen/logrus"
"github.com/joho/godotenv"
"github.com/urfave/cli"
)
var (
version = "0.0.1"
build = "0"
)
func main() {
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 := 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()
}