Merge pull request #24 from appleboy/match

Support MATCH_EMAIL flag
This commit is contained in:
Bo-Yi Wu 2016-11-09 14:36:56 +08:00 committed by GitHub
commit 9f288d99d9
4 changed files with 76 additions and 62 deletions

View File

@ -66,6 +66,7 @@ docker run --rm \
-e PLUGIN_VENUE=24.9163213,121.1424972,title,address \ -e PLUGIN_VENUE=24.9163213,121.1424972,title,address \
-e PLUGIN_VIDEO=tests/video.mp4 \ -e PLUGIN_VIDEO=tests/video.mp4 \
-e PLUGIN_DEBUG=true \ -e PLUGIN_DEBUG=true \
-e PLUGIN_ONLY_MATCH_EMAIL=false \
-e PLUGIN_FORMAT=markdown \ -e PLUGIN_FORMAT=markdown \
-e DRONE_REPO_OWNER=appleboy \ -e DRONE_REPO_OWNER=appleboy \
-e DRONE_REPO_NAME=go-hello \ -e DRONE_REPO_NAME=go-hello \

32
main.go
View File

@ -77,6 +77,11 @@ func main() {
Usage: "enable debug message", Usage: "enable debug message",
EnvVar: "PLUGIN_DEBUG", EnvVar: "PLUGIN_DEBUG",
}, },
cli.BoolFlag{
Name: "match.email",
Usage: "send message when only match email",
EnvVar: "PLUGIN_ONLY_MATCH_EMAIL",
},
cli.StringFlag{ cli.StringFlag{
Name: "format", Name: "format",
Value: "markdown", Value: "markdown",
@ -175,19 +180,20 @@ func run(c *cli.Context) error {
Finished: c.Float64("job.finished"), Finished: c.Float64("job.finished"),
}, },
Config: Config{ Config: Config{
Token: c.String("token"), Token: c.String("token"),
Debug: c.Bool("debug"), Debug: c.Bool("debug"),
To: c.StringSlice("to"), MatchEmail: c.Bool("match.email"),
Message: c.StringSlice("message"), To: c.StringSlice("to"),
Photo: c.StringSlice("photo"), Message: c.StringSlice("message"),
Document: c.StringSlice("document"), Photo: c.StringSlice("photo"),
Sticker: c.StringSlice("sticker"), Document: c.StringSlice("document"),
Audio: c.StringSlice("audio"), Sticker: c.StringSlice("sticker"),
Voice: c.StringSlice("voice"), Audio: c.StringSlice("audio"),
Location: c.StringSlice("location"), Voice: c.StringSlice("voice"),
Video: c.StringSlice("video"), Location: c.StringSlice("location"),
Venue: c.StringSlice("venue"), Video: c.StringSlice("video"),
Format: c.String("format"), Venue: c.StringSlice("venue"),
Format: c.String("format"),
}, },
} }

View File

@ -36,19 +36,20 @@ type (
// Config for the plugin. // Config for the plugin.
Config struct { Config struct {
Token string Token string
Debug bool Debug bool
To []string MatchEmail bool
Message []string To []string
Photo []string Message []string
Document []string Photo []string
Sticker []string Document []string
Audio []string Sticker []string
Voice []string Audio []string
Location []string Voice []string
Video []string Location []string
Venue []string Video []string
Format string Venue []string
Format string
} }
// Plugin values. // Plugin values.
@ -135,22 +136,43 @@ func convertLocation(value string) (Location, bool) {
}, false }, false
} }
func parseTo(value, authorEmail string) (int64, bool) { func parseTo(to []string, authorEmail string, matchEmail bool) []int64 {
ids := trimElement(strings.Split(value, ":")) var emails []int64
var ids []int64
attachEmail := true
if len(ids) > 1 { for _, value := range to {
if email := ids[1]; email != authorEmail { idArray := trimElement(strings.Split(value, ":"))
log.Println("email not match")
return int64(0), false // 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)
attachEmail = false
continue
}
ids = append(ids, id)
} }
id, err := strconv.ParseInt(ids[0], 10, 64) if matchEmail == true && attachEmail == false {
if err != nil { return emails
return int64(0), false
} }
return id, true for _, value := range emails {
ids = append(ids, value)
}
return ids
} }
// Exec executes the plugin. // Exec executes the plugin.
@ -179,6 +201,7 @@ func (p Plugin) Exec() error {
bot.Debug = p.Config.Debug bot.Debug = p.Config.Debug
ids := parseTo(p.Config.To, p.Build.Email, p.Config.MatchEmail)
photos := fileExist(trimElement(p.Config.Photo)) photos := fileExist(trimElement(p.Config.Photo))
documents := fileExist(trimElement(p.Config.Document)) documents := fileExist(trimElement(p.Config.Document))
stickers := fileExist(trimElement(p.Config.Sticker)) stickers := fileExist(trimElement(p.Config.Sticker))
@ -189,12 +212,7 @@ func (p Plugin) Exec() error {
venues := trimElement(p.Config.Venue) venues := trimElement(p.Config.Venue)
// send message. // send message.
for _, to := range p.Config.To { for _, user := range ids {
user, enable := parseTo(to, p.Build.Email)
if !enable {
continue
}
for _, value := range trimElement(message) { for _, value := range trimElement(message) {
txt, err := template.RenderTrim(value, p) txt, err := template.RenderTrim(value, p)
if err != nil { if err != nil {

View File

@ -133,30 +133,19 @@ func TestTrimElement(t *testing.T) {
} }
func TestParseTo(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) ids := parseTo(input, "1@gmail.com", false)
assert.Equal(t, int64(1234567890), id) 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) ids = parseTo(input, "a@gmail.com", false)
assert.Equal(t, int64(0), id) assert.Equal(t, []int64{0, 4, 5}, ids)
id, enable = parseTo("1234567890:test@gmail.com", "test@gmail.com") ids = parseTo(input, "a@gmail.com", true)
assert.Equal(t, []int64{0, 4, 5}, ids)
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)
} }
func TestCheckFileExist(t *testing.T) { func TestCheckFileExist(t *testing.T) {