From 3b65f97d8f3f7dd784ac0795cce5b15898edef6e Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Sat, 10 Jun 2017 17:28:05 +0800 Subject: [PATCH] rename `content` to `message` Signed-off-by: Bo-Yi Wu --- DOCS.md | 12 ++++++------ README.md | 6 +++--- doc.go | 2 +- main.go | 6 +++--- plugin.go | 26 +++++++++++--------------- plugin_test.go | 12 +++++++++--- 6 files changed, 33 insertions(+), 31 deletions(-) diff --git a/DOCS.md b/DOCS.md index a209dd8..6ac5bb8 100644 --- a/DOCS.md +++ b/DOCS.md @@ -18,7 +18,7 @@ pipeline: image: appleboy/drone-discord webhook_id: xxxxxxxxxx webhook_token: xxxxxxxxxx - content: "Testing from drone image" + message: "Testing from drone image" ``` Example configuration with TTS message: @@ -30,7 +30,7 @@ pipeline: webhook_id: xxxxxxxxxx webhook_token: xxxxxxxxxx + tts: true - content: "Testing from drone image" + message: "Testing from drone image" ``` Example configuration with override the default username of the webhook: @@ -42,7 +42,7 @@ pipeline: webhook_id: xxxxxxxxxx webhook_token: xxxxxxxxxx + username: appleboy - content: "Testing from drone image" + message: "Testing from drone image" ``` Example configuration with override the default avatar of the webhook: @@ -54,7 +54,7 @@ pipeline: webhook_id: xxxxxxxxxx webhook_token: xxxxxxxxxx + avatar_url: http://exampple.com/appleboy.png - content: "Testing from drone image" + message: "Testing from drone image" ``` Example configuration with a custom message template: @@ -65,7 +65,7 @@ pipeline: image: appleboy/drone-discord webhook_id: xxxxxxxxxx webhook_token: xxxxxxxxxx -+ content: | ++ messageq: | + {{ #success build.status }} + build {{ build.number }} succeeded. Good job. + {{ else }} @@ -90,7 +90,7 @@ username tts : true if this is a TTS message -content +message : the message contents (up to 2000 characters) # Template Reference diff --git a/README.md b/README.md index 0e9b5df..70d7c54 100644 --- a/README.md +++ b/README.md @@ -69,7 +69,7 @@ There are three ways to send notification. drone-discord \ --webhook-id xxxx \ --webhook-token xxxx \ - --content "Test Message" + --message "Test Message" ``` @@ -85,7 +85,7 @@ docker run --rm \ -e TTS=false \ -e USERNAME=test \ -e AVATAR_URL=http://example.com/xxxx.png \ - -e CONTENT=test \ + -e MESSAGE=test \ appleboy/drone-discord ``` @@ -104,7 +104,7 @@ docker run --rm \ -e TTS=false \ -e USERNAME=test \ -e AVATAR_URL=http://example.com/xxxx.png \ - -e CONTENT=test \ + -e MESSAGE=test \ -e DRONE_REPO_OWNER=appleboy \ -e DRONE_REPO_NAME=go-hello \ -e DRONE_COMMIT_SHA=e5e82b5eb3737205c25955dcc3dcacc839b7be52 \ diff --git a/doc.go b/doc.go index 0b47904..33a20fd 100644 --- a/doc.go +++ b/doc.go @@ -13,7 +13,7 @@ // --webhook-token xxxx \ // --username value \ // --avatar-url value \ -// --content "Test Message" +// --message "Test Message" // // For more details, see the documentation and example. // diff --git a/main.go b/main.go index 6605ff9..8052c6f 100644 --- a/main.go +++ b/main.go @@ -36,9 +36,9 @@ func main() { EnvVar: "PLUGIN_WEBHOOK_TOKEN,WEBHOOK_TOKEN", }, cli.StringSliceFlag{ - Name: "content", + Name: "message", Usage: "the message contents (up to 2000 characters)", - EnvVar: "PLUGIN_CONTENT,CONTENT", + EnvVar: "PLUGIN_MESSAGE,MESSAGE", }, cli.BoolFlag{ Name: "wait", @@ -170,7 +170,7 @@ func run(c *cli.Context) error { WebhookID: c.String("webhook-id"), WebhookToken: c.String("webhook-token"), Wait: c.Bool("wait"), - Content: c.StringSlice("content"), + Message: c.StringSlice("message"), Username: c.String("username"), AvatarURL: c.String("avatar-url"), TTS: c.Bool("tts"), diff --git a/plugin.go b/plugin.go index 595f520..46978ae 100644 --- a/plugin.go +++ b/plugin.go @@ -38,11 +38,12 @@ type ( Config struct { WebhookID string WebhookToken string - Wait bool - Content []string - Username string - AvatarURL string - TTS bool + Message []string + Wait bool `json:"wait"` + Content string `json:"content"` + Username string `json:"username"` + AvatarURL string `json:"avatar_url"` + TTS bool `json:"tts"` } // Plugin values. @@ -64,8 +65,8 @@ func (p Plugin) Exec() error { webhookURL := fmt.Sprintf("https://discordapp.com/api/webhooks/%s/%s", p.Config.WebhookID, p.Config.WebhookToken) var messages []string - if len(p.Config.Content) > 0 { - messages = p.Config.Content + if len(p.Config.Message) > 0 { + messages = p.Config.Message } else { messages = p.Message(p.Repo, p.Build) } @@ -76,15 +77,10 @@ func (p Plugin) Exec() error { return err } - content := map[string]interface{}{ - "wait": p.Config.Wait, - "content": txt, - "username": p.Config.Username, - "avatar_url": p.Config.AvatarURL, - "tts": p.Config.TTS, - } + // + p.Config.Content = txt b := new(bytes.Buffer) - json.NewEncoder(b).Encode(content) + json.NewEncoder(b).Encode(p.Config) _, err = http.Post(webhookURL, "application/json; charset=utf-8", b) if err != nil { diff --git a/plugin_test.go b/plugin_test.go index 1aafa29..c67f06e 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -37,7 +37,7 @@ func TestDefaultMessageFormat(t *testing.T) { assert.Equal(t, []string{"[success] (master)『update by drone line plugin.』by Bo-Yi Wu"}, message) } -func TestErrorSendMessage(t *testing.T) { +func TestSendMessage(t *testing.T) { plugin := Plugin{ Repo: Repo{ Name: "go-hello", @@ -57,7 +57,7 @@ func TestErrorSendMessage(t *testing.T) { WebhookID: os.Getenv("WEBHOOK_ID"), WebhookToken: os.Getenv("WEBHOOK_TOKEN"), Wait: false, - Content: []string{"test one message from drone testing", "test two message from drone testing"}, + Message: []string{"test one message from drone testing", "test two message from drone testing"}, Username: "drone-ci", TTS: false, }, @@ -66,7 +66,13 @@ func TestErrorSendMessage(t *testing.T) { err := plugin.Exec() assert.Nil(t, err) - plugin.Config.Content = []string{} + plugin.Config.Message = []string{} + err = plugin.Exec() + assert.Nil(t, err) + + plugin.Config.Message = []string{"I am appleboy"} + plugin.Config.TTS = true + plugin.Config.Wait = true err = plugin.Exec() assert.Nil(t, err) }