Skip to content

Commit

Permalink
feat: support sign (#13)
Browse files Browse the repository at this point in the history
  • Loading branch information
lddsb authored Dec 2, 2019
1 parent 1839998 commit 7c68447
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 21 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ dive.log
drone-dingtalk-message
.idea
vendor
coverage.txt
coverage.txt
coverage.out
env.list
5 changes: 5 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ steps:
settings:
token: your-groupbot-token
type: markdown
secret: your-secret-for-generate-sign
```

Expand All @@ -39,6 +40,10 @@ String. Access token for group bot. (you can get the access token when you add a

String. Message type, plan support text, markdown, link and action card, but due to time issue, it's only support `markdown` and `text` now, and you can get the best experience by use markdown.

`secret`

String. Secret for generate sign.

`tpl`

String. Your custom `tpl`, it can be a local path or a remote http link.
Expand Down
5 changes: 5 additions & 0 deletions README_ZH.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ steps:
settings:
token: your-groupbot-token
type: markdown
secret: your-secret-for-generate-sign
```

Expand All @@ -39,6 +40,10 @@ steps:

消息类型,因个人能力有限,目前仅支持`markdown`和`text`,其中,使用`markdown`可以获得最好的体验。

`secret`

如果你设置了`加签`,可以把你的`加签`密钥填入此项完成`加签`操作。

`tpl`

你可以通过该字段来自定义你的消息模版。该字段可以是一个本地路径也可以是一个远程的URL。
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ go 1.12

require (
github.com/joho/godotenv v1.3.0
github.com/lddsb/dingtalk-webhook v0.0.2
github.com/lddsb/dingtalk-webhook v0.0.4
github.com/urfave/cli v1.22.2
)
9 changes: 3 additions & 6 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,14 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSY
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU=
github.com/joho/godotenv v1.3.0 h1:Zjp+RcGpHhGlrMbJzXTrZZPrWj+1vfm90La1wgB6Bhc=
github.com/joho/godotenv v1.3.0/go.mod h1:7hK45KPybAkOC6peb+G5yklZfMxEjkZhHbwpqxOKXbg=
github.com/lddsb/dingtalk-webhook v0.0.1 h1:l4FdTMaRaHnrYfByALukFWK0ru9Rttl0dANg13/SnTI=
github.com/lddsb/dingtalk-webhook v0.0.1/go.mod h1:5E+/sOBb6m+3ztqnZl4danEY3I5FeIwb12v12s9osbw=
github.com/lddsb/dingtalk-webhook v0.0.2 h1:mNp6KNq/9r5tSohdS4jo7bEDNtM/Qdbp8nzuW/mvexg=
github.com/lddsb/dingtalk-webhook v0.0.2/go.mod h1:dwNU75Sog87wJXAFcY5mDFM7eW4hIdX7bNemrN92pH0=
github.com/lddsb/dingtalk-webhook v0.0.4 h1:gAJFy66L2gZ5j6kDrHRoz/zQFaNamcObtetNonq76RA=
github.com/lddsb/dingtalk-webhook v0.0.4/go.mod h1:dwNU75Sog87wJXAFcY5mDFM7eW4hIdX7bNemrN92pH0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0RK8m9o+Q=
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/urfave/cli v1.20.0 h1:fDqGv3UG/4jbVl/QkFwEdddtEDjh/5Ov6X+0B/3bPaw=
github.com/urfave/cli v1.20.0/go.mod h1:70zkFmudgCuE/ngEzBv17Jvp/497gISqfk5gWijbERA=
github.com/urfave/cli v1.22.2 h1:gsqYFH8bb9ekPA12kRo0hfjngWQjkJPlN9R0N78BoUo=
github.com/urfave/cli v1.22.2/go.mod h1:Gos4lmkARVdJ6EkW0WaNv/tZAAMe9V7XWyB60NtXRu0=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
Expand Down
18 changes: 12 additions & 6 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,12 @@ import (
)

// Version of cli
var Version = "0.1.1202"
var Version = "0.2.1130"

func main() {
app := cli.NewApp()
app.Name = "Drone Dingtalk Message Plugin"
app.Usage = "Sending message to Dingtalk group by robot using webhook"
app.Name = "Drone DingTalk Message Plugin"
app.Usage = "Sending message to DingTalk group by robot using WebHook"
app.Copyright = "© 2018 Dee Luo"
app.Authors = []cli.Author{
{
Expand All @@ -38,12 +38,17 @@ func main() {
},
cli.StringFlag{
Name: "config.token,access_token,token",
Usage: "dingtalk webhook access token",
Usage: "DingTalk webhook access token",
EnvVar: "PLUGIN_ACCESS_TOKEN,PLUGIN_TOKEN",
},
cli.StringFlag{
Name: "config.secret,secret",
Usage: "DingTalk WebHook secret for generate sign",
EnvVar: "PLUGIN_SECRET",
},
cli.StringFlag{
Name: "config.message.type,message_type",
Usage: "dingtalk message type, like text, markdown, action card, link and feed card...",
Usage: "DingTalk message type, like text, markdown, action card, link and feed card...",
EnvVar: "PLUGIN_MSG_TYPE,PLUGIN_TYPE,PLUGIN_MESSAGE_TYPE",
},
cli.StringFlag{
Expand All @@ -53,7 +58,7 @@ func main() {
},
cli.StringFlag{
Name: "config.message.at.mobiles",
Usage: "at someone in a dingtalk group need this guy bind's mobile",
Usage: "at someone in a DingTalk group need this guy bind's mobile",
EnvVar: "PLUGIN_MSG_AT_MOBILES",
},
cli.StringFlag{
Expand Down Expand Up @@ -246,6 +251,7 @@ func run(c *cli.Context) {
// custom config
Config: Config{
AccessToken: c.String("config.token"),
Secret: c.String("config.secret"),
IsAtALL: c.Bool("config.message.at.all"),
MsgType: c.String("config.message.type"),
Mobiles: c.String("config.message.at.mobiles"),
Expand Down
21 changes: 14 additions & 7 deletions plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ type (
Config struct {
Debug bool
AccessToken string
Secret string
IsAtALL bool
Mobiles string
Username string
Expand Down Expand Up @@ -134,11 +135,11 @@ type (
}
)

// Exec execute webhook
// Exec execute WebHook
func (p *Plugin) Exec() error {
var err error
if 0 == len(p.Config.AccessToken) {
msg := "missing dingtalk access token"
if "" == p.Config.AccessToken {
msg := "missing DingTalk access token"
return errors.New(msg)
}

Expand All @@ -151,15 +152,21 @@ func (p *Plugin) Exec() error {
p.Config.TipsTitle = "you have a new message"
}

newWebhook := webhook.NewWebHook(p.Config.AccessToken)
newWebHook := webhook.NewWebHook(p.Config.AccessToken)

// add sign
if "" != p.Config.Secret {
newWebHook.Secret = p.Config.Secret
}

mobiles := strings.Split(p.Config.Mobiles, ",")
switch strings.ToLower(p.Config.MsgType) {
case "markdown":
err = newWebhook.SendMarkdownMsg(p.Config.TipsTitle, tpl, p.Config.IsAtALL, mobiles...)
err = newWebHook.SendMarkdownMsg(p.Config.TipsTitle, tpl, p.Config.IsAtALL, mobiles...)
case "text":
err = newWebhook.SendTextMsg(tpl, p.Config.IsAtALL, mobiles...)
err = newWebHook.SendTextMsg(tpl, p.Config.IsAtALL, mobiles...)
case "link":
err = newWebhook.SendLinkMsg(p.Drone.Build.Status, tpl, p.Drone.Commit.Author.Avatar, p.Drone.Build.Link)
err = newWebHook.SendLinkMsg(p.Drone.Build.Status, tpl, p.Drone.Commit.Author.Avatar, p.Drone.Build.Link)
default:
msg := "not support message type"
err = errors.New(msg)
Expand Down

0 comments on commit 7c68447

Please sign in to comment.