Merge pull request #22 from appleboy/parseID
Support send notification to commit author by check email.
This commit is contained in:
		@@ -40,7 +40,7 @@ script:
 | 
			
		||||
after_success:
 | 
			
		||||
  # ignore main.go coverage
 | 
			
		||||
  - sed -i '/main.go/d' coverage.txt
 | 
			
		||||
  - bash <(curl -s https://codecov.io/bash) -f coverage.txt
 | 
			
		||||
  - bash <(curl -s https://codecov.io/bash)
 | 
			
		||||
  # deploy from master
 | 
			
		||||
  - if [ "$TRAVIS_BRANCH" == "master" ] && [ "$TRAVIS_GO_VERSION" == "1.7.3" ] && [ "$TRAVIS_PULL_REQUEST" == "false" ]; then
 | 
			
		||||
    docker login -u="$DOCKER_USERNAME" -p="$DOCKER_PASSWORD";
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										6
									
								
								main.go
									
									
									
									
									
								
							
							
						
						
									
										6
									
								
								main.go
									
									
									
									
									
								
							@@ -109,6 +109,11 @@ func main() {
 | 
			
		||||
			Usage:  "git author name",
 | 
			
		||||
			EnvVar: "DRONE_COMMIT_AUTHOR",
 | 
			
		||||
		},
 | 
			
		||||
		cli.StringFlag{
 | 
			
		||||
			Name:   "commit.author.email",
 | 
			
		||||
			Usage:  "git author email",
 | 
			
		||||
			EnvVar: "DRONE_COMMIT_AUTHOR_EMAIL",
 | 
			
		||||
		},
 | 
			
		||||
		cli.StringFlag{
 | 
			
		||||
			Name:   "commit.message",
 | 
			
		||||
			Usage:  "commit message",
 | 
			
		||||
@@ -163,6 +168,7 @@ func run(c *cli.Context) error {
 | 
			
		||||
			Commit:   c.String("commit.sha"),
 | 
			
		||||
			Branch:   c.String("commit.branch"),
 | 
			
		||||
			Author:   c.String("commit.author"),
 | 
			
		||||
			Email:    c.String("commit.author.email"),
 | 
			
		||||
			Message:  c.String("commit.message"),
 | 
			
		||||
			Link:     c.String("build.link"),
 | 
			
		||||
			Started:  c.Float64("job.started"),
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										31
									
								
								plugin.go
									
									
									
									
									
								
							
							
						
						
									
										31
									
								
								plugin.go
									
									
									
									
									
								
							@@ -27,6 +27,7 @@ type (
 | 
			
		||||
		Message  string
 | 
			
		||||
		Branch   string
 | 
			
		||||
		Author   string
 | 
			
		||||
		Email    string
 | 
			
		||||
		Status   string
 | 
			
		||||
		Link     string
 | 
			
		||||
		Started  float64
 | 
			
		||||
@@ -134,20 +135,21 @@ func convertLocation(value string) (Location, bool) {
 | 
			
		||||
	}, false
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func parseID(keys []string) []int64 {
 | 
			
		||||
	var newKeys []int64
 | 
			
		||||
func parseTo(value, authorEmail string) (int64, bool) {
 | 
			
		||||
	ids := trimElement(strings.Split(value, ","))
 | 
			
		||||
 | 
			
		||||
	for _, value := range keys {
 | 
			
		||||
		id, err := strconv.ParseInt(value, 10, 64)
 | 
			
		||||
		if err != nil {
 | 
			
		||||
			log.Println(err.Error())
 | 
			
		||||
 | 
			
		||||
			continue
 | 
			
		||||
	if len(ids) > 1 {
 | 
			
		||||
		if email := ids[1]; email != authorEmail {
 | 
			
		||||
			return int64(0), false
 | 
			
		||||
		}
 | 
			
		||||
		newKeys = append(newKeys, id)
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return newKeys
 | 
			
		||||
	id, err := strconv.ParseInt(ids[0], 10, 64)
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return int64(0), false
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return id, true
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// Exec executes the plugin.
 | 
			
		||||
@@ -176,8 +178,6 @@ func (p Plugin) Exec() error {
 | 
			
		||||
 | 
			
		||||
	bot.Debug = p.Config.Debug
 | 
			
		||||
 | 
			
		||||
	// parse ids
 | 
			
		||||
	ids := parseID(p.Config.To)
 | 
			
		||||
	photos := fileExist(trimElement(p.Config.Photo))
 | 
			
		||||
	documents := fileExist(trimElement(p.Config.Document))
 | 
			
		||||
	stickers := fileExist(trimElement(p.Config.Sticker))
 | 
			
		||||
@@ -188,7 +188,12 @@ func (p Plugin) Exec() error {
 | 
			
		||||
	venues := trimElement(p.Config.Venue)
 | 
			
		||||
 | 
			
		||||
	// send message.
 | 
			
		||||
	for _, user := range ids {
 | 
			
		||||
	for _, to := range p.Config.To {
 | 
			
		||||
		user, enable := parseTo(to, p.Build.Email)
 | 
			
		||||
		if !enable {
 | 
			
		||||
			continue
 | 
			
		||||
		}
 | 
			
		||||
 | 
			
		||||
		for _, value := range trimElement(message) {
 | 
			
		||||
			txt, err := template.RenderTrim(value, p)
 | 
			
		||||
			if err != nil {
 | 
			
		||||
 
 | 
			
		||||
@@ -63,11 +63,12 @@ func TestSendMessage(t *testing.T) {
 | 
			
		||||
			Branch:  "master",
 | 
			
		||||
			Message: "update travis by drone plugin",
 | 
			
		||||
			Commit:  "e7c4f0a63ceeb42a39ac7806f7b51f3f0d204fd2",
 | 
			
		||||
			Email:   "test@gmail.com",
 | 
			
		||||
		},
 | 
			
		||||
 | 
			
		||||
		Config: Config{
 | 
			
		||||
			Token:    os.Getenv("TELEGRAM_TOKEN"),
 | 
			
		||||
			To:       []string{os.Getenv("TELEGRAM_TO"), "中文ID", "1234567890"},
 | 
			
		||||
			To:       []string{os.Getenv("TELEGRAM_TO"), os.Getenv("TELEGRAM_TO") + ",appleboy@gmail.com", "中文ID", "1234567890"},
 | 
			
		||||
			Message:  []string{"Test Telegram Chat Bot From Travis or Local", "commit message: 『{{ build.message }}』", " "},
 | 
			
		||||
			Photo:    []string{"tests/github.png", "1234", " "},
 | 
			
		||||
			Document: []string{"tests/gophercolor.png", "1234", " "},
 | 
			
		||||
@@ -131,19 +132,31 @@ func TestTrimElement(t *testing.T) {
 | 
			
		||||
	assert.Equal(t, result, trimElement(input))
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestParseID(t *testing.T) {
 | 
			
		||||
	var input []string
 | 
			
		||||
	var result []int64
 | 
			
		||||
func TestParseTo(t *testing.T) {
 | 
			
		||||
	id, enable := parseTo("1234567890", "test@gmail.com")
 | 
			
		||||
 | 
			
		||||
	input = []string{"1", "測試", "3"}
 | 
			
		||||
	result = []int64{int64(1), int64(3)}
 | 
			
		||||
	assert.Equal(t, true, enable)
 | 
			
		||||
	assert.Equal(t, int64(1234567890), id)
 | 
			
		||||
 | 
			
		||||
	assert.Equal(t, result, parseID(input))
 | 
			
		||||
	id, enable = parseTo("1234567890,test2@gmail.com", "test@gmail.com")
 | 
			
		||||
 | 
			
		||||
	input = []string{"1", "2"}
 | 
			
		||||
	result = []int64{int64(1), int64(2)}
 | 
			
		||||
	assert.Equal(t, false, enable)
 | 
			
		||||
	assert.Equal(t, int64(0), id)
 | 
			
		||||
 | 
			
		||||
	assert.Equal(t, result, parseID(input))
 | 
			
		||||
	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)
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
func TestCheckFileExist(t *testing.T) {
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user