mirror of
https://github.com/lddsb/drone-dingtalk-message.git
synced 2026-06-13 18:51:40 +08:00
Compare commits
12 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c8bc5d889d | |||
| a651a73cbd | |||
| 7c68447d1d | |||
| 183999881b | |||
| 4c52115e2a | |||
| fb601a0d6c | |||
| 8bd745becf | |||
| e9aea90aac | |||
| c77b865a43 | |||
| fbfbec3bde | |||
| 30c2324f4e | |||
| 11d34af11a |
+3
-1
@@ -2,4 +2,6 @@ dive.log
|
||||
drone-dingtalk-message
|
||||
.idea
|
||||
vendor
|
||||
coverage.txt
|
||||
coverage.txt
|
||||
coverage.out
|
||||
env.list
|
||||
@@ -1,5 +1,7 @@
|
||||
# Drone CI DingTalk Message Plugin
|
||||
[](https://hub.docker.com/r/lddsb/drone-dingtalk-message) [](https://goreportcard.com/report/github.com/lddsb/drone-dingtalk-message) [](https://codecov.io/gh/lddsb/drone-dingtalk-message) [](LICENSE)
|
||||
[](https://hub.docker.com/r/lddsb/drone-dingtalk-message) [](https://goreportcard.com/report/github.com/lddsb/drone-dingtalk-message) [](https://codecov.io/gh/lddsb/drone-dingtalk-message) [](https://app.dependabot.com/accounts/lddsb/repos/159822771) [](LICENSE)
|
||||
|
||||
[中文说明](README_ZH.md)
|
||||
|
||||
just support `text`, `markdown` and `link` type now
|
||||
### Drone CI Plugin Config
|
||||
@@ -25,6 +27,7 @@ steps:
|
||||
settings:
|
||||
token: your-groupbot-token
|
||||
type: markdown
|
||||
secret: your-secret-for-generate-sign
|
||||
|
||||
```
|
||||
|
||||
@@ -37,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.
|
||||
@@ -73,7 +80,7 @@ String. You can customize the [TPL_REPO_SHORT_NAME] by this configuration item.
|
||||
|
||||
String. You can customize the [TPL_REPO_FULL_NAME] by this configuration item.
|
||||
|
||||
`tpl_build_status_sucdess`
|
||||
`tpl_build_status_success`
|
||||
|
||||
String. You can customize the [TPL_BUILD_STATUS] (when status=`success`) by this configuration item.
|
||||
|
||||
|
||||
+176
@@ -0,0 +1,176 @@
|
||||
# Drone CI的钉钉群组机器人通知插件
|
||||
[](https://hub.docker.com/r/lddsb/drone-dingtalk-message) [](https://goreportcard.com/report/github.com/lddsb/drone-dingtalk-message) [](https://codecov.io/gh/lddsb/drone-dingtalk-message) [](https://app.dependabot.com/accounts/lddsb/repos/159822771) [](LICENSE)
|
||||
|
||||
目前仅支持 `text`, `markdown` 以及 `link` 类型的消息,建议使用`markdown`类型。
|
||||
### 怎么使用本插件
|
||||
添加一个`step`到你的`.drone.yml`中,下面是简单的例子:
|
||||
|
||||
`0.8.x`
|
||||
```yaml
|
||||
pipeline:
|
||||
...
|
||||
notification:
|
||||
image: lddsb/drone-dingtalk-message
|
||||
token: your-group-bot-token
|
||||
type: markdown
|
||||
```
|
||||
|
||||
`1.0.x`
|
||||
```yaml
|
||||
kind: pipeline
|
||||
name: default
|
||||
|
||||
steps:
|
||||
...
|
||||
- name: notification
|
||||
image: lddsb/drone-dingtalk-message
|
||||
settings:
|
||||
token: your-groupbot-token
|
||||
type: markdown
|
||||
secret: your-secret-for-generate-sign
|
||||
|
||||
```
|
||||
|
||||
### 插件参数
|
||||
`token`(必须)
|
||||
|
||||
你可以通过加入和创建一个群组来添加钉钉自定义机器人,添加自定义机器人完成后即可获得所需要的`access token`。
|
||||
|
||||
`type`(必须)
|
||||
|
||||
消息类型,因个人能力有限,目前仅支持`markdown`和`text`,其中,使用`markdown`可以获得最好的体验。
|
||||
|
||||
`secret`
|
||||
|
||||
如果你设置了`加签`,可以把你的`加签`密钥填入此项完成`加签`操作。
|
||||
|
||||
`tpl`
|
||||
|
||||
你可以通过该字段来自定义你的消息模版。该字段可以是一个本地路径也可以是一个远程的URL。
|
||||
|
||||
`tips_title`
|
||||
|
||||
你可以通过该字段自定义钉钉机器人的消息通知提醒标题。(注意,不是消息内容的标题,是收到钉钉机器人发的消息后,会有一个外显的标题)
|
||||
|
||||
`success_color`
|
||||
|
||||
你可以通过该字段自定义打包成功的颜色。比如:`008000`。
|
||||
|
||||
`failure_color`
|
||||
|
||||
你可以通过该字段自定义打包失败的颜色。比如:`FF0000`。
|
||||
|
||||
`success_pic`
|
||||
|
||||
你可以通过该字段自定义打包成功的图片。
|
||||
|
||||
`failure_pic`
|
||||
|
||||
字符串,你可以通过该字段自定义打包失败的图片。
|
||||
|
||||
`tpl_commit_branch_name`
|
||||
|
||||
你可以通过该字段自定义分支的名称,可以在模版中通过[TPL_COMMIT_BRANCH]来使用该值。
|
||||
|
||||
`tpl_repo_short_name`
|
||||
|
||||
你可以通过该字段自定义仓库的名字,可以在模版中通过[TPL_REPO_SHORT_NAME]来使用该值。
|
||||
|
||||
`tpl_repo_full_name`
|
||||
|
||||
你可以通过该字段自定义仓库的全名(包含组织名称),可以在模版中通过[TPL_REPO_FULL_NAME]来使用该值。
|
||||
|
||||
`tpl_build_status_success`
|
||||
|
||||
你可以通过该字段自定义运行成功状态的值,可以在模版中通过[TPL_BUILD_STATUS]来使用该值。(仅当前方`step`运行结果为成功时该值会生效)
|
||||
|
||||
`tpl_build_status_failure`
|
||||
|
||||
你可以通过该字段自定义运行失败状态的值,可以在模版中通过[TPL_BUILD_STATUS]来使用该值。(仅当前方`step`运行结果为失败时该值会生效)
|
||||
|
||||
### 模版
|
||||
> `tpl` 对 `link` 类型的消息并不支持 !!!
|
||||
|
||||
感天动地,我们终于支持自定义模版了!下面是一个`markdown`的自定义模版例子:
|
||||
|
||||
# [TPL_REPO_FULL_NAME] build [TPL_BUILD_STATUS], takes [TPL_BUILD_CONSUMING]s
|
||||
[TPL_COMMIT_MSG]
|
||||
|
||||
[TPL_COMMIT_SHA]([TPL_COMMIT_LINK])
|
||||
|
||||
[[TPL_AUTHOR_NAME]([TPL_AUTHOR_EMAIL])](mailto:[TPL_AUTHOR_EMAIL])
|
||||
|
||||
[Click To The Build Detail Page [TPL_STATUS_EMOTICON)]]([TPL_BUILD_LINK])
|
||||
|
||||
你可以写自己喜欢的模版,终于不用再对着默认模版发愁啦!并且模版的语法非常简单!比较可惜的是目前支持的变量还比较少,下面是当前支持的变量的列表:
|
||||
|
||||
| Variable | Value |
|
||||
| :-------------------: | :-------------------------------------------------: |
|
||||
| [TPL_REPO_SHORT_NAME] | current repo name(bare name) |
|
||||
| [TPL_REPO_FULL_NAME] | the full name(with group name) of current repo |
|
||||
| [TPL_REPO_GROUP_NAME] | the group name of current repo |
|
||||
| [TPL_REPO_OWNER_NAME] | the owner name of current repo |
|
||||
| [TPL_REPO_REMOTE_URL] | the remote url of current repo |
|
||||
| [TPL_BUILD_STATUS] | current build status(e.g., success, failure) |
|
||||
| [TPL_BUILD_LINK] | current build link |
|
||||
| [TPL_BUILD_EVENT] | current build event(e.g., push, pull request, etc.) |
|
||||
| [TPL_BUILD_CONSUMING] | current build consuming, second |
|
||||
| [TPL_COMMIT_SHA] | current commit sha |
|
||||
| [TPL_COMMIT_REF] | current commit ref(e.g., refs/heads/master, etc.) |
|
||||
| [TPL_COMMIT_LINK] | current commit remote url link |
|
||||
| [TPL_COMMIT_BRANCH] | current branch name(e.g., dev, etc) |
|
||||
| [TPL_COMMIT_MSG] | current commit message |
|
||||
| [TPL_AUTHOR_NAME] | current commit author name |
|
||||
| [TPL_AUTHOR_EMAIL] | current commit author email |
|
||||
| [TPL_AUTHOR_USERNAME] | current commit author username |
|
||||
| [TPL_AUTHOR_AVATAR] | current commit author avatar |
|
||||
| [TPL_STATUS_PIC] | custom pic for build status |
|
||||
| [TPL_STATUS_COLOR] | custom color for build status |
|
||||
| [TPL_STATUS_EMOTICON] | custom emoticon for build status |
|
||||
|
||||
|
||||
|
||||
### 截图展示
|
||||
- 发送成功(Drone Web)
|
||||
|
||||

|
||||
|
||||
- 忘记填写Access Token(Drone Web)
|
||||
|
||||

|
||||
|
||||
- 忘记填写消息类型或者不支持的消息类型
|
||||
|
||||

|
||||
|
||||
- 默认的`markdown`消息
|
||||
|
||||

|
||||
|
||||
- 带颜色和链接的`markdown`消息
|
||||
|
||||

|
||||
|
||||
- 带颜色、链接和图片的`markdown`消息
|
||||
|
||||

|
||||
|
||||
|
||||
### 贡献代码
|
||||
本项目使用了`go mod`来管理依赖,因此要编译本项目相当简单。
|
||||
|
||||
- 先把项目代码拷贝到本地
|
||||
```shell
|
||||
$ git clone https://github.com/lddsb/drone-dingtalk-message.git /path/to/you/want
|
||||
```
|
||||
- 然后直接执行编译即可
|
||||
```shell
|
||||
$ cd /path/to/you/want && GO111MODULE=on go build .
|
||||
```
|
||||
- 跑个`help`
|
||||
```shell
|
||||
$ ./drone-dingtalk-message -h
|
||||
```
|
||||
|
||||
### 待办
|
||||
- 实现更多的消息类型
|
||||
@@ -4,6 +4,6 @@ go 1.12
|
||||
|
||||
require (
|
||||
github.com/joho/godotenv v1.3.0
|
||||
github.com/lddsb/dingtalk-webhook v0.0.1
|
||||
github.com/urfave/cli v1.20.0
|
||||
github.com/lddsb/dingtalk-webhook v0.0.4
|
||||
github.com/urfave/cli v1.22.2
|
||||
)
|
||||
|
||||
@@ -1,6 +1,17 @@
|
||||
github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU=
|
||||
github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d h1:U+s90UTSYgptZMwQh2aRr3LuazLJIa+Pg3Kc1ylSYVY=
|
||||
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/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/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.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=
|
||||
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
|
||||
|
||||
@@ -5,17 +5,17 @@ import (
|
||||
"log"
|
||||
"os"
|
||||
|
||||
_ "github.com/joho/godotenv/autoload"
|
||||
"github.com/joho/godotenv"
|
||||
"github.com/urfave/cli"
|
||||
)
|
||||
|
||||
// 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{
|
||||
{
|
||||
@@ -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{
|
||||
@@ -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{
|
||||
@@ -205,6 +210,11 @@ func main() {
|
||||
},
|
||||
}
|
||||
|
||||
// kubernetes runner patch
|
||||
if _, err := os.Stat("/run/drone/env"); err == nil {
|
||||
godotenv.Overload("/run/drone/env")
|
||||
}
|
||||
|
||||
if err := app.Run(os.Args); nil != err {
|
||||
log.Println(err)
|
||||
}
|
||||
@@ -246,6 +256,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"),
|
||||
|
||||
@@ -62,6 +62,7 @@ type (
|
||||
Config struct {
|
||||
Debug bool
|
||||
AccessToken string
|
||||
Secret string
|
||||
IsAtALL bool
|
||||
Mobiles string
|
||||
Username string
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
@@ -338,12 +345,12 @@ func (p *Plugin) getEmoticon() string {
|
||||
func (p *Plugin) getPicURL() string {
|
||||
pics := make(map[string]string)
|
||||
// success picture url
|
||||
pics["success"] = "https://ws4.sinaimg.cn/large/006tNc79gy1fz05g5a7utj30he0bfjry.jpg"
|
||||
pics["success"] = "https://wx1.sinaimg.cn/large/006tNc79gy1fz05g5a7utj30he0bfjry.jpg"
|
||||
if p.Custom.Pic.SuccessPicURL != "" {
|
||||
pics["success"] = p.Custom.Pic.SuccessPicURL
|
||||
}
|
||||
// failure picture url
|
||||
pics["failure"] = "https://ws1.sinaimg.cn/large/006tNc79gy1fz0b4fghpnj30hd0bdmxn.jpg"
|
||||
pics["failure"] = "https://wx1.sinaimg.cn/large/006tNc79gy1fz0b4fghpnj30hd0bdmxn.jpg"
|
||||
if p.Custom.Pic.FailurePicURL != "" {
|
||||
pics["failure"] = p.Custom.Pic.FailurePicURL
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user