Skip to content

Commit

Permalink
fix duplicate leaks in same file
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexAkulov committed Jun 15, 2018
1 parent 8d506be commit 430e102
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 28 deletions.
2 changes: 1 addition & 1 deletion cmd/hungryfox/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func main() {
logger.Error().Str("service", "leaks searcher").Str("error", err.Error()).Msg("fail")
os.Exit(1)
}
logger.Debug().Str("service", "leaks searcher").Msg("started")
logger.Debug().Str("service", "leaks searcher").Int("workers", numCPUs).Msg("started")

logger.Debug().Str("service", "state manager").Msg("start")
stateManager := &filestate.StateManager{
Expand Down
4 changes: 2 additions & 2 deletions hungryfox.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,14 @@ type Repo struct {

type IMessageSender interface {
Start() error
Send(*Leak) error
Send(Leak) error
Stop() error
}

type ILeakSearcher interface {
Start() error
SetConfig() error
Search(*Diff)
Search(Diff)
Stop() error
}

Expand Down
29 changes: 15 additions & 14 deletions router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,20 @@ func (r *LeaksRouter) Start() error {
return fmt.Errorf("can't parse delay with: %v", err)
}
r.senders = map[string]hungryfox.IMessageSender{}

r.senders["email"] = &email.Sender{
AuditorEmail: r.Config.SMTP.Recipient,
Config: &email.Config{
From: r.Config.SMTP.From,
SMTPHost: r.Config.SMTP.Host,
SMTPPort: r.Config.SMTP.Port,
InsecureTLS: !r.Config.SMTP.TLS,
Username: r.Config.SMTP.Username,
Password: r.Config.SMTP.Password,
Delay: delay,
},
Log: r.Log,
if r.Config.SMTP.Enable {
r.senders["email"] = &email.Sender{
AuditorEmail: r.Config.SMTP.Recipient,
Config: &email.Config{
From: r.Config.SMTP.From,
SMTPHost: r.Config.SMTP.Host,
SMTPPort: r.Config.SMTP.Port,
InsecureTLS: !r.Config.SMTP.TLS,
Username: r.Config.SMTP.Username,
Password: r.Config.SMTP.Password,
Delay: delay,
},
Log: r.Log,
}
}
r.senders["file"] = &file.File{
LeaksFile: r.Config.Common.LeaksFile,
Expand All @@ -60,7 +61,7 @@ func (r *LeaksRouter) Start() error {
return nil
case leak := <-r.LeakChannel:
for _, sender := range r.senders {
sender.Send(leak)
sender.Send(*leak)
}
}
}
Expand Down
11 changes: 5 additions & 6 deletions searcher/searcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,12 @@ func (s *Searcher) worker() error {
case diff := <-s.DiffChannel:
leaks := s.GetLeaks(*diff)
filtredLeaks := 0
for _, leak := range leaks {
if s.filterLeak(leak) {
for i := range leaks {
if s.filterLeak(leaks[i]) {
filtredLeaks++
continue
}
s.LeakChannel <- &leak
s.LeakChannel <- &leaks[i]
}
leaksCount := len(leaks) - filtredLeaks
if leaksCount > 0 || filtredLeaks > 0 {
Expand Down Expand Up @@ -214,7 +214,7 @@ func (s *Searcher) GetLeaks(diff hungryfox.Diff) []hungryfox.Leak {
if len(line) > 1024 {
line = line[:1024]
}
leak := hungryfox.Leak{
leaks = append(leaks, hungryfox.Leak{
RepoPath: diff.RepoPath,
FilePath: diff.FilePath,
PatternName: pattern.Name,
Expand All @@ -225,8 +225,7 @@ func (s *Searcher) GetLeaks(diff hungryfox.Diff) []hungryfox.Leak {
CommitAuthor: diff.Author,
CommitEmail: diff.AuthorEmail,
RepoURL: diff.RepoURL,
}
leaks = append(leaks, leak)
})
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions senders/email/sender.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ type mailTemplateStruct struct {

type mailTemplateRepoStruct struct {
RepoURL string
Items []*hungryfox.Leak
Items []hungryfox.Leak
}

func (s *Sender) batchMaker() muster.Batch {
Expand Down Expand Up @@ -58,15 +58,15 @@ func (b *batch) Fire(notifier muster.Notifier) {
}

func (b *batch) Add(item interface{}) {
leak := item.(*hungryfox.Leak)
leak := item.(hungryfox.Leak)
leak.LeakString = strings.TrimSpace(leak.LeakString)
if len(leak.LeakString) > 512 {
leak.LeakString = "too long"
}
if b.Repos[leak.RepoURL] == nil {
b.Repos[leak.RepoURL] = &mailTemplateRepoStruct{
RepoURL: leak.RepoURL,
Items: []*hungryfox.Leak{},
Items: []hungryfox.Leak{},
}
}
b.Repos[leak.RepoURL].Items = append(b.Repos[leak.RepoURL].Items, leak)
Expand Down
2 changes: 1 addition & 1 deletion senders/email/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (s *Sender) Stop() error {
}

// Send - send leaks
func (s *Sender) Send(leak *hungryfox.Leak) error {
func (s *Sender) Send(leak hungryfox.Leak) error {
s.muster.Work <- leak
return nil
}
2 changes: 1 addition & 1 deletion senders/file/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func (self *File) Stop() error {
return nil
}

func (self *File) Send(leak *hungryfox.Leak) error {
func (self *File) Send(leak hungryfox.Leak) error {
f, err := os.OpenFile(self.LeaksFile, os.O_APPEND|os.O_WRONLY|os.O_CREATE, 0644)
if err != nil {
return err
Expand Down

0 comments on commit 430e102

Please sign in to comment.