diff --git a/main.go b/main.go index f100169..976fbd8 100644 --- a/main.go +++ b/main.go @@ -40,6 +40,11 @@ func main() { Usage: "the message contents (up to 2000 characters)", EnvVar: "PLUGIN_MESSAGE,MESSAGE", }, + cli.StringFlag{ + Name: "color", + Usage: "color code of the embed", + EnvVar: "PLUGIN_COLOR,COLOR", + }, cli.BoolFlag{ Name: "wait", Usage: "waits for server confirmation of message send before response, and returns the created message body", @@ -176,6 +181,7 @@ func run(c *cli.Context) error { WebhookID: c.String("webhook-id"), WebhookToken: c.String("webhook-token"), Message: c.StringSlice("message"), + Color: c.String("color"), }, Payload: Payload{ Wait: c.Bool("wait"), diff --git a/plugin.go b/plugin.go index 505875a..c0c9dcf 100644 --- a/plugin.go +++ b/plugin.go @@ -7,6 +7,8 @@ import ( "fmt" "log" "net/http" + "strconv" + "strings" "github.com/appleboy/drone-facebook/template" ) @@ -39,6 +41,7 @@ type ( Config struct { WebhookID string WebhookToken string + Color string Message []string } @@ -145,7 +148,7 @@ func (p *Plugin) Message() { { Title: p.Build.Message, URL: p.Build.Link, - Color: color(p.Build), + Color: p.Color(), Author: EmbedAuthorObject{ Name: p.Build.Author, IconURL: p.Build.Avatar, @@ -157,16 +160,24 @@ func (p *Plugin) Message() { } } -func color(build Build) int { - switch build.Status { +// Color code of the embed +func (p *Plugin) Color() int { + if p.Config.Color != "" { + p.Config.Color = strings.Replace(p.Config.Color, "#", "", -1) + if s, err := strconv.ParseInt(p.Config.Color, 16, 32); err == nil { + return int(s) + } + } + + switch p.Build.Status { case "success": - // #1ac600 + // #1ac600 green return 1754624 case "failure", "error", "killed": - // #ff3232 + // #ff3232 red return 16724530 default: - // #ffd930 + // #ffd930 yellow return 16767280 } } diff --git a/plugin_test.go b/plugin_test.go index 64a8094..8398726 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -3,6 +3,7 @@ package main import ( "os" "testing" + "time" "github.com/stretchr/testify/assert" ) @@ -48,6 +49,12 @@ func TestSendMessage(t *testing.T) { err := plugin.Exec() assert.Nil(t, err) + plugin.Config.Message = []string{"I am appleboy"} + plugin.Payload.TTS = true + plugin.Payload.Wait = true + err = plugin.Exec() + assert.Nil(t, err) + // send success embed message plugin.Config.Message = []string{} err = plugin.Exec() @@ -55,17 +62,20 @@ func TestSendMessage(t *testing.T) { // send success embed message plugin.Build.Status = "failure" + plugin.Build.Message = "send failure embed message" err = plugin.Exec() assert.Nil(t, err) + time.Sleep(1 * time.Second) // send default embed message plugin.Build.Status = "test" + plugin.Build.Message = "send default embed message" err = plugin.Exec() assert.Nil(t, err) - plugin.Config.Message = []string{"I am appleboy"} - plugin.Payload.TTS = true - plugin.Payload.Wait = true + //change color for embed message + plugin.Config.Color = "#4842f4" + plugin.Build.Message = "Change embed color to #4842f4" err = plugin.Exec() assert.Nil(t, err) }