-
Notifications
You must be signed in to change notification settings - Fork 3
/
related.go
129 lines (104 loc) · 2.69 KB
/
related.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package git
import (
"fmt"
"regexp"
"sort"
"time"
"github.com/HeySquirrel/tribe/config"
)
type Contributor struct {
Name string
Count int
LastCommit *Commit
}
type Contributors []*Contributor
func (c *Contributor) String() string {
return c.Name
}
func NewContributor(name string, lastCommit *Commit) *Contributor {
contributor := new(Contributor)
contributor.Name = name
contributor.Count = 1
contributor.LastCommit = lastCommit
return contributor
}
func (commits *Commits) RelatedItems() []string {
workItems := make([]string, 0)
seen := make(map[string]string)
for _, commit := range *commits {
for _, re := range config.Matchers() {
search := fmt.Sprintf("%s %s", commit.Subject, commit.Body)
found := re.FindAllString(search, -1)
if found != nil {
for _, item := range found {
_, ok := seen[item]
if !ok {
workItems = append(workItems, item)
seen[item] = item
}
}
}
}
}
return workItems
}
func (commits *Commits) RelatedContributors() Contributors {
contributors := make(Contributors, 0)
namedContributors := make(map[string]*Contributor)
remove := regexp.MustCompile(" ?<[^>]+>")
re := regexp.MustCompile(", | ab?nd |,")
for _, commit := range *commits {
authors := remove.ReplaceAllString(commit.Author, "")
names := re.Split(authors, -1)
for _, name := range names {
contributor, ok := namedContributors[name]
if ok {
contributor.Count += 1
} else {
contributor := NewContributor(name, commit)
namedContributors[name] = contributor
contributors = append(contributors, contributor)
}
}
}
return contributors
}
type RelatedFile struct {
Name string
Count int
LastCommit time.Time
}
type byRelevance []*RelatedFile
func (a byRelevance) Len() int { return len(a) }
func (a byRelevance) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byRelevance) Less(i, j int) bool {
return a[i].Count < a[j].Count
}
func NewRelatedFile(name string, lastCommitTime time.Time) *RelatedFile {
relatedFile := new(RelatedFile)
relatedFile.Name = name
relatedFile.Count = 1
relatedFile.LastCommit = lastCommitTime
return relatedFile
}
func (commits *Commits) relatedFiles(filename string) []*RelatedFile {
files := make([]*RelatedFile, 0)
namedFiles := make(map[string]*RelatedFile)
for _, commit := range *commits {
for _, file := range commit.Files {
if file == filename {
continue
}
relatedFile, ok := namedFiles[file]
if ok {
relatedFile.Count += 1
} else {
relatedFile := NewRelatedFile(file, commit.Date)
namedFiles[file] = relatedFile
files = append(files, relatedFile)
}
}
}
sort.Sort(sort.Reverse(byRelevance(files)))
return files
}