From eee4278af40f7e6f70a54a3162876373d51d4df5 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Wed, 9 Nov 2016 10:35:55 +0800 Subject: [PATCH 1/3] add ONLY_MATCH_EMAIL boolean flag Signed-off-by: Bo-Yi Wu --- main.go | 32 +++++++++++++++++++------------- plugin.go | 27 ++++++++++++++------------- 2 files changed, 33 insertions(+), 26 deletions(-) diff --git a/main.go b/main.go index d406610..1148548 100644 --- a/main.go +++ b/main.go @@ -77,6 +77,11 @@ func main() { Usage: "enable debug message", EnvVar: "PLUGIN_DEBUG", }, + cli.BoolFlag{ + Name: "match.email", + Usage: "send message when only match email", + EnvVar: "PLUGIN_ONLY_MATCH_EMAIL", + }, cli.StringFlag{ Name: "format", Value: "markdown", @@ -175,19 +180,20 @@ func run(c *cli.Context) error { Finished: c.Float64("job.finished"), }, Config: Config{ - Token: c.String("token"), - Debug: c.Bool("debug"), - To: c.StringSlice("to"), - Message: c.StringSlice("message"), - Photo: c.StringSlice("photo"), - Document: c.StringSlice("document"), - Sticker: c.StringSlice("sticker"), - Audio: c.StringSlice("audio"), - Voice: c.StringSlice("voice"), - Location: c.StringSlice("location"), - Video: c.StringSlice("video"), - Venue: c.StringSlice("venue"), - Format: c.String("format"), + Token: c.String("token"), + Debug: c.Bool("debug"), + MatchEmail: c.Bool("match.email"), + To: c.StringSlice("to"), + Message: c.StringSlice("message"), + Photo: c.StringSlice("photo"), + Document: c.StringSlice("document"), + Sticker: c.StringSlice("sticker"), + Audio: c.StringSlice("audio"), + Voice: c.StringSlice("voice"), + Location: c.StringSlice("location"), + Video: c.StringSlice("video"), + Venue: c.StringSlice("venue"), + Format: c.String("format"), }, } diff --git a/plugin.go b/plugin.go index ecca361..e6648fd 100644 --- a/plugin.go +++ b/plugin.go @@ -36,19 +36,20 @@ type ( // Config for the plugin. Config struct { - Token string - Debug bool - To []string - Message []string - Photo []string - Document []string - Sticker []string - Audio []string - Voice []string - Location []string - Video []string - Venue []string - Format string + Token string + Debug bool + MatchEmail bool + To []string + Message []string + Photo []string + Document []string + Sticker []string + Audio []string + Voice []string + Location []string + Video []string + Venue []string + Format string } // Plugin values. From f3e25a0846d26e61b80fa6480a3ae93e23b00f97 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Wed, 9 Nov 2016 12:00:36 +0800 Subject: [PATCH 2/3] Support Match Email condition. Signed-off-by: Bo-Yi Wu --- plugin.go | 49 +++++++++++++++++++++++++++++++++---------------- plugin_test.go | 29 +++++++++-------------------- 2 files changed, 42 insertions(+), 36 deletions(-) diff --git a/plugin.go b/plugin.go index e6648fd..b3396d2 100644 --- a/plugin.go +++ b/plugin.go @@ -136,22 +136,43 @@ func convertLocation(value string) (Location, bool) { }, false } -func parseTo(value, authorEmail string) (int64, bool) { - ids := trimElement(strings.Split(value, ":")) +func parseTo(to []string, authorEmail string, matchEmail bool) []int64 { + var emails []int64 + var ids []int64 + attachChat := true - if len(ids) > 1 { - if email := ids[1]; email != authorEmail { - log.Println("email not match") - return int64(0), false + for _, value := range to { + idArray := trimElement(strings.Split(value, ":")) + + // check id + id, err := strconv.ParseInt(idArray[0], 10, 64) + if err != nil { + continue } + + // check match author email + if len(idArray) > 1 { + if email := idArray[1]; email != authorEmail { + continue + } + + emails = append(emails, id) + attachChat = false + continue + } + + ids = append(ids, id) } - id, err := strconv.ParseInt(ids[0], 10, 64) - if err != nil { - return int64(0), false + if matchEmail == true && attachChat == false { + return emails } - return id, true + for _, value := range emails { + ids = append(ids, value) + } + + return ids } // Exec executes the plugin. @@ -180,6 +201,7 @@ func (p Plugin) Exec() error { bot.Debug = p.Config.Debug + ids := parseTo(p.Config.To, p.Build.Email, p.Config.MatchEmail) photos := fileExist(trimElement(p.Config.Photo)) documents := fileExist(trimElement(p.Config.Document)) stickers := fileExist(trimElement(p.Config.Sticker)) @@ -190,12 +212,7 @@ func (p Plugin) Exec() error { venues := trimElement(p.Config.Venue) // send message. - for _, to := range p.Config.To { - user, enable := parseTo(to, p.Build.Email) - if !enable { - continue - } - + for _, user := range ids { for _, value := range trimElement(message) { txt, err := template.RenderTrim(value, p) if err != nil { diff --git a/plugin_test.go b/plugin_test.go index ab51b44..ba541d3 100644 --- a/plugin_test.go +++ b/plugin_test.go @@ -133,30 +133,19 @@ func TestTrimElement(t *testing.T) { } func TestParseTo(t *testing.T) { - id, enable := parseTo("1234567890", "test@gmail.com") + input := []string{"0", "1:1@gmail.com", "2:2@gmail.com", "3:3@gmail.com", "4", "5"} - assert.Equal(t, true, enable) - assert.Equal(t, int64(1234567890), id) + ids := parseTo(input, "1@gmail.com", false) + assert.Equal(t, []int64{0, 4, 5, 1}, ids) - id, enable = parseTo("1234567890:test2@gmail.com", "test@gmail.com") + ids = parseTo(input, "1@gmail.com", true) + assert.Equal(t, []int64{1}, ids) - assert.Equal(t, false, enable) - assert.Equal(t, int64(0), id) + ids = parseTo(input, "a@gmail.com", false) + assert.Equal(t, []int64{0, 4, 5}, ids) - id, enable = parseTo("1234567890:test@gmail.com", "test@gmail.com") - - assert.Equal(t, true, enable) - assert.Equal(t, int64(1234567890), id) - - id, enable = parseTo("測試:test@gmail.com", "test@gmail.com") - - assert.Equal(t, false, enable) - assert.Equal(t, int64(0), id) - - id, enable = parseTo("測試", "test@gmail.com") - - assert.Equal(t, false, enable) - assert.Equal(t, int64(0), id) + ids = parseTo(input, "a@gmail.com", true) + assert.Equal(t, []int64{0, 4, 5}, ids) } func TestCheckFileExist(t *testing.T) { From e6233bb494d9944ca4ba9b9c33a579b6f2070692 Mon Sep 17 00:00:00 2001 From: Bo-Yi Wu Date: Wed, 9 Nov 2016 12:23:19 +0800 Subject: [PATCH 3/3] update readme. Signed-off-by: Bo-Yi Wu --- README.md | 1 + plugin.go | 6 +++--- 2 files changed, 4 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6723663..3165426 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,7 @@ docker run --rm \ -e PLUGIN_VENUE=24.9163213,121.1424972,title,address \ -e PLUGIN_VIDEO=tests/video.mp4 \ -e PLUGIN_DEBUG=true \ + -e PLUGIN_ONLY_MATCH_EMAIL=false \ -e PLUGIN_FORMAT=markdown \ -e DRONE_REPO_OWNER=appleboy \ -e DRONE_REPO_NAME=go-hello \ diff --git a/plugin.go b/plugin.go index b3396d2..726292f 100644 --- a/plugin.go +++ b/plugin.go @@ -139,7 +139,7 @@ func convertLocation(value string) (Location, bool) { func parseTo(to []string, authorEmail string, matchEmail bool) []int64 { var emails []int64 var ids []int64 - attachChat := true + attachEmail := true for _, value := range to { idArray := trimElement(strings.Split(value, ":")) @@ -157,14 +157,14 @@ func parseTo(to []string, authorEmail string, matchEmail bool) []int64 { } emails = append(emails, id) - attachChat = false + attachEmail = false continue } ids = append(ids, id) } - if matchEmail == true && attachChat == false { + if matchEmail == true && attachEmail == false { return emails }