diff --git a/.gitignore b/.gitignore index beb9ec9..1f3feb7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ dive.log drone-dingtalk-message -.idea \ No newline at end of file +.idea +vendor \ No newline at end of file diff --git a/README.md b/README.md index 34548c6..42fa91d 100644 --- a/README.md +++ b/README.md @@ -7,7 +7,7 @@ go get github.com/lddsb/drone-dingtalk-message ``` > get dependent lib ```shell -go get github.com/urfave/cli +dep ensure ``` > build ```shell @@ -17,13 +17,47 @@ cd $GOPATH/src/github.com/lddsb/drone-dingtalk-message && go build . ```shell ./drone-dingtalk-message -h ``` + ### Drone CI Plugin Config +`0.8.x` ```yaml pipeline: - # other step here - message: - image: lddsb/drone-dingtalk-message - environment: - - PLUGIN_ACCESS_TOKEN=xxx - - PLUGIN_MSG_TYPE=markdown -``` \ No newline at end of file + ... + 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 + +``` + +### Screen Shot +- Drone Build Step +![build-step](https://ws1.sinaimg.cn/large/006tNbRwgy1fym86hefglj30eo04i749.jpg) + + +- Markdown DingTalk Message +![markdown](https://ws3.sinaimg.cn/large/006tNbRwgy1fym82mg57fj30bo04pjrd.jpg) + + +- Markdown DingTalk Message(beta tag) +![markdown-massage-beta-tag](https://ws3.sinaimg.cn/large/006tNc79gy1fzgcennwy3j30a00abwf3.jpg) + +### Todo + +- Multi-Type +- Multi-Lang +- More User Customization \ No newline at end of file diff --git a/main.go b/main.go index f7c3bb4..d38656a 100644 --- a/main.go +++ b/main.go @@ -26,9 +26,9 @@ func main() { app.Version = Version app.Flags = []cli.Flag{ cli.StringFlag{ - Name: "config.token", + Name: "config.token,access_token,token", Usage: "dingtalk webhook access token", - EnvVar: "PLUGIN_ACCESS_TOKEN", + EnvVar: "PLUGIN_ACCESS_TOKEN,PLUGIN_TOKEN", }, cli.StringFlag{ Name: "config.lang", @@ -37,9 +37,9 @@ func main() { EnvVar: "PLUGIN_LANG", }, cli.StringFlag{ - Name: "config.message.type", + Name: "config.message.type,msg_type,message_type,type", Usage: "dingtalk message type, like text, markdown, action card, link and feed card...", - EnvVar: "PLUGIN_MSG_TYPE", + EnvVar: "PLUGIN_MSG_TYPE,PLUGIN_TYPE,PLUGIN_MESSAGE_TYPE", }, cli.StringFlag{ Name: "config.message.at.all", @@ -290,6 +290,31 @@ func main() { Usage: "job finished", EnvVar: "DRONE_JOB_FINISHED", }, + cli.StringFlag{ + Name: "ci.repo.link", + Usage: "ci repo link", + EnvVar: "CI_REPO_LINK", + }, + cli.StringFlag{ + Name: "config.success.pic.url", + Usage: "config success picture url", + EnvVar: "SUCCESS_PICTURE_URL", + }, + cli.StringFlag{ + Name: "config.failure.pic.url", + Usage: "config failure picture url", + EnvVar: "FAILURE_PICTURE_URL", + }, + cli.StringFlag{ + Name: "config.success.color", + Usage: "config success color for title in markdown", + EnvVar: "SUCCESS_COLOR", + }, + cli.StringFlag{ + Name: "config.failure.color", + Usage: "config failure color for title in markdown", + EnvVar: "FAILURE_COLOR", + }, } if err := app.Run(os.Args); nil != err { @@ -304,8 +329,8 @@ func run(c *cli.Context) { // repo info Repo: Repo{ FullName: c.String("repo.fullname"), - Owner: c.String("repo.owner"), - Name: c.String("repo.name"), + Owner: c.String("repo.owner"), + Name: c.String("repo.name"), }, // build info Build: Build{ @@ -334,11 +359,18 @@ func run(c *cli.Context) { }, // custom config Config: Config{ - AccessToken: c.String("config.token"), - Lang: c.String("config.lang"), - IsAtALL: c.Bool("config.message.at.all"), - MsgType: c.String("config.message.type"), - Mobiles: c.String("config.message.at.mobiles"), + AccessToken: c.String("config.token"), + Lang: c.String("config.lang"), + IsAtALL: c.Bool("config.message.at.all"), + MsgType: c.String("config.message.type"), + Mobiles: c.String("config.message.at.mobiles"), + SuccessPicUrl: c.String("config.success.pic.url"), + FailurePicUrl: c.String("config.failure.pic.url"), + SuccessColor: c.String("config.success.color"), + FailureColor: c.String("config.failure.color"), + }, + CI: CI{ + RepoLink: c.String("ci.repo.link"), }, } diff --git a/plugin.go b/plugin.go index 2d92785..84b733e 100644 --- a/plugin.go +++ b/plugin.go @@ -65,6 +65,10 @@ type ( Hostname string // providers the Drone server hostname Version string // providers the Drone server version } + // Drone CI Info + CI struct { + RepoLink string + } // plugin private config Config struct { AccessToken string @@ -81,9 +85,14 @@ type ( BtnOrientation bool PicURL string MsgURL string + SuccessPicUrl string + FailurePicUrl string + SuccessColor string + FailureColor string } // plugin all config Plugin struct { + CI CI Git Git Runner Runner System System @@ -158,20 +167,25 @@ func (p *Plugin) baseTpl() string { tpl := "" switch strings.ToLower(p.Config.MsgType) { case "markdown": - tpl = fmt.Sprintf(`# **%s** -### [%s](%s) -##### %s (%s) -##### @%s -##### %s(%s) -`, + tpl = fmt.Sprintf("# %s *Branch Build %s*\n"+ + "![%s](%s)\n\n"+ + "%s\n\n"+ + "[%s](%s)\n\n"+ + "`%s(%s)`\n\n"+ + "[Build's Detail Click Me %s](%s)", + p.getColor(), + strings.Title(p.Commit.Branch), strings.Title(p.Build.Status), - strings.TrimSpace(p.Commit.Message), - p.Build.Link, - p.Repo.FullName, - p.Commit.Branch, + p.Build.Status, + p.getPicUrl(), + p.getColor(), + p.Commit.Message, p.Commit.Sha, + p.Commit.Link, p.Commit.Authors.Name, - p.Commit.Authors.Email) + p.Commit.Authors.Email, + p.getEmoticon(), + p.Build.Link) case "text": tpl = fmt.Sprintf(`[%s] %s %s (%s) @@ -192,8 +206,74 @@ func (p *Plugin) baseTpl() string { p.Commit.Sha[:6], p.Commit.Authors.Name, p.Commit.Authors.Email) + case "actionCard": + // coming soon } return tpl } + +/** +get emoticon + */ +func (p *Plugin) getEmoticon() string { + emoticons := make(map[string]string) + emoticons["success"] = ":)" + emoticons["failure"] = ":(" + + emoticon, ok := emoticons[p.Build.Status] + if ok { + return emoticon + } + + return ":(" +} + +/** +get picture url + */ +func (p *Plugin) getPicUrl() string { + pics := make(map[string]string) + // success picture url + pics["success"] = "https://ws4.sinaimg.cn/large/006tNc79gy1fz05g5a7utj30he0bfjry.jpg" + if p.Config.SuccessPicUrl != "" { + pics["success"] = p.Config.SuccessPicUrl + } + // failure picture url + pics["failure"] = "https://ws1.sinaimg.cn/large/006tNc79gy1fz0b4fghpnj30hd0bdmxn.jpg" + if p.Config.FailurePicUrl != "" { + pics["failure"] = p.Config.FailurePicUrl + } + + url, ok := pics[p.Build.Status] + if ok { + return url + } + + return "" +} + +/** +get color for message title + */ +func (p *Plugin) getColor() string { + colors := make(map[string]string) + // success color + colors["success"] = "#008000" + if p.Config.SuccessColor != "" { + colors["success"] = "#" + p.Config.SuccessColor + } + // failure color + colors["failure"] = "#FF0000" + if p.Config.FailureColor != "" { + colors["failure"] = "#" + p.Config.FailureColor + } + + color, ok := colors[p.Build.Status] + if ok { + return color + } + + return "" +}