Merge pull request #20 from appleboy/template

Add support handlebar template
This commit is contained in:
Bo-Yi Wu 2016-10-31 10:09:34 +08:00 committed by GitHub
commit bd3806e778
6 changed files with 58 additions and 24 deletions

View File

@ -7,7 +7,7 @@ services:
go: go:
- 1.5.4 - 1.5.4
- 1.6.3 - 1.6.3
- 1.7.1 - 1.7.3
- tip - tip
env: env:
@ -42,12 +42,12 @@ after_success:
- sed -i '/main.go/d' coverage.txt - sed -i '/main.go/d' coverage.txt
- bash <(curl -s https://codecov.io/bash) -f coverage.txt - bash <(curl -s https://codecov.io/bash) -f coverage.txt
# deploy from master # deploy from master
- if [ "$TRAVIS_BRANCH" == "master" ] && [ "$TRAVIS_GO_VERSION" == "1.7.1" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then - if [ "$TRAVIS_BRANCH" == "master" ] && [ "$TRAVIS_GO_VERSION" == "1.7.3" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then
docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD"; docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD";
make docker_deploy tag=latest; make docker_deploy tag=latest;
fi fi
# deploy from tag # deploy from tag
- if [ "$TRAVIS_GO_VERSION" == "1.7.1" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_TAG" != "" ]; then - if [ "$TRAVIS_GO_VERSION" == "1.7.3" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ] && [ "$TRAVIS_TAG" != "" ]; then
docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD"; docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD";
make docker_deploy tag=$TRAVIS_TAG; make docker_deploy tag=$TRAVIS_TAG;
fi fi

16
glide.lock generated
View File

@ -1,6 +1,16 @@
hash: b63ca4eb6e194481209bad2e58f3c4eb2c0ef2b4411ead5b3b7a77677ef815ab hash: 5bdcf53533d043ba1cb6ab55ae6a93aca40c812a20264406bd6e0fe342f51d61
updated: 2016-10-04T21:46:25.713693812+08:00 updated: 2016-10-31T09:50:34.654875319+08:00
imports: imports:
- name: github.com/appleboy/drone-facebook
version: abda8124c404d424b150cf2a9935e3e108519c6e
subpackages:
- template
- name: github.com/aymerick/raymond
version: a2232af10b53ef1ae5a767f5178db3a6c1dab655
subpackages:
- ast
- parser
- lexer
- name: github.com/joho/godotenv - name: github.com/joho/godotenv
version: 4ed13390c0acd2ff4e371e64d8b97c8954138243 version: 4ed13390c0acd2ff4e371e64d8b97c8954138243
subpackages: subpackages:
@ -8,7 +18,7 @@ imports:
- name: github.com/technoweenie/multipartstreamer - name: github.com/technoweenie/multipartstreamer
version: a90a01d73ae432e2611d178c18367fbaa13e0154 version: a90a01d73ae432e2611d178c18367fbaa13e0154
- name: github.com/urfave/cli - name: github.com/urfave/cli
version: d53eb991652b1d438abdd34ce4bfa3ef1539108e version: 3eb41f1bb122ac7535cc7b32db6b78b541879726
- name: gopkg.in/telegram-bot-api.v4 - name: gopkg.in/telegram-bot-api.v4
version: a7f48eb2dd301356942677e65bebe0c9aef07013 version: a7f48eb2dd301356942677e65bebe0c9aef07013
testImports: testImports:

View File

@ -5,6 +5,10 @@ import:
- autoload - autoload
- package: github.com/urfave/cli - package: github.com/urfave/cli
- package: gopkg.in/telegram-bot-api.v4 - package: gopkg.in/telegram-bot-api.v4
- package: github.com/appleboy/drone-facebook
version: ^1.3.0
subpackages:
- template
testImport: testImport:
- package: github.com/stretchr/testify - package: github.com/stretchr/testify
subpackages: subpackages:

28
main.go
View File

@ -136,6 +136,16 @@ func main() {
Usage: "build link", Usage: "build link",
EnvVar: "DRONE_BUILD_LINK", EnvVar: "DRONE_BUILD_LINK",
}, },
cli.Float64Flag{
Name: "job.started",
Usage: "job started",
EnvVar: "DRONE_JOB_STARTED",
},
cli.Float64Flag{
Name: "job.finished",
Usage: "job finished",
EnvVar: "DRONE_JOB_FINISHED",
},
} }
app.Run(os.Args) app.Run(os.Args)
} }
@ -147,14 +157,16 @@ func run(c *cli.Context) error {
Name: c.String("repo.name"), Name: c.String("repo.name"),
}, },
Build: Build{ Build: Build{
Number: c.Int("build.number"), Number: c.Int("build.number"),
Event: c.String("build.event"), Event: c.String("build.event"),
Status: c.String("build.status"), Status: c.String("build.status"),
Commit: c.String("commit.sha"), Commit: c.String("commit.sha"),
Branch: c.String("commit.branch"), Branch: c.String("commit.branch"),
Author: c.String("commit.author"), Author: c.String("commit.author"),
Message: c.String("commit.message"), Message: c.String("commit.message"),
Link: c.String("build.link"), Link: c.String("build.link"),
Started: c.Float64("job.started"),
Finished: c.Float64("job.finished"),
}, },
Config: Config{ Config: Config{
Token: c.String("token"), Token: c.String("token"),

View File

@ -8,6 +8,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/appleboy/drone-facebook/template"
"gopkg.in/telegram-bot-api.v4" "gopkg.in/telegram-bot-api.v4"
) )
@ -20,14 +21,16 @@ type (
// Build information. // Build information.
Build struct { Build struct {
Event string Event string
Number int Number int
Commit string Commit string
Message string Message string
Branch string Branch string
Author string Author string
Status string Status string
Link string Link string
Started float64
Finished float64
} }
// Config for the plugin. // Config for the plugin.
@ -187,7 +190,12 @@ func (p Plugin) Exec() error {
// send message. // send message.
for _, user := range ids { for _, user := range ids {
for _, value := range trimElement(message) { for _, value := range trimElement(message) {
msg := tgbotapi.NewMessage(user, value) txt, err := template.RenderTrim(value, p)
if err != nil {
return err
}
msg := tgbotapi.NewMessage(user, txt)
msg.ParseMode = p.Config.Format msg.ParseMode = p.Config.Format
p.Send(bot, msg) p.Send(bot, msg)
} }

View File

@ -68,7 +68,7 @@ func TestSendMessage(t *testing.T) {
Config: Config{ Config: Config{
Token: os.Getenv("TELEGRAM_TOKEN"), Token: os.Getenv("TELEGRAM_TOKEN"),
To: []string{os.Getenv("TELEGRAM_TO"), "中文ID", "1234567890"}, To: []string{os.Getenv("TELEGRAM_TO"), "中文ID", "1234567890"},
Message: []string{"Test Telegram Chat Bot From Travis or Local", " "}, Message: []string{"Test Telegram Chat Bot From Travis or Local", "commit message: 『{{ build.message }}』", " "},
Photo: []string{"tests/github.png", "1234", " "}, Photo: []string{"tests/github.png", "1234", " "},
Document: []string{"tests/gophercolor.png", "1234", " "}, Document: []string{"tests/gophercolor.png", "1234", " "},
Sticker: []string{"tests/github-logo.png", "tests/github.png", "1234", " "}, Sticker: []string{"tests/github-logo.png", "tests/github.png", "1234", " "},