Skip to content

Commit 2c5d728

Browse files
committed
Add top author stats to acitivity page
1 parent 9343d2f commit 2c5d728

File tree

13 files changed

+573
-960
lines changed

13 files changed

+573
-960
lines changed

models/repo_activity.go

+16-6
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ type ActivityAuthorData struct {
1919
Name string `json:"name"`
2020
Login string `json:"login"`
2121
AvatarLink string `json:"avatar_link"`
22+
HomeLink string `json:"home_link"`
2223
Commits int64 `json:"commits"`
2324
}
2425

@@ -91,12 +92,20 @@ func GetActivityStatsTopAuthors(repo *Repository, timeFrom time.Time, count int)
9192
return nil, nil
9293
}
9394
users := make(map[int64]*ActivityAuthorData)
94-
for k, v := range code.Authors {
95-
if len(k) == 0 {
95+
var unknownUserID int64
96+
unknownUserAvatarLink := NewGhostUser().AvatarLink()
97+
for _, v := range code.Authors {
98+
if len(v.Email) == 0 {
9699
continue
97100
}
98-
u, err := GetUserByEmail(k)
101+
u, err := GetUserByEmail(v.Email)
99102
if u == nil || IsErrUserNotExist(err) {
103+
unknownUserID--
104+
users[unknownUserID] = &ActivityAuthorData{
105+
Name: v.Name,
106+
AvatarLink: unknownUserAvatarLink,
107+
Commits: v.Commits,
108+
}
100109
continue
101110
}
102111
if err != nil {
@@ -107,10 +116,11 @@ func GetActivityStatsTopAuthors(repo *Repository, timeFrom time.Time, count int)
107116
Name: u.DisplayName(),
108117
Login: u.LowerName,
109118
AvatarLink: u.AvatarLink(),
110-
Commits: v,
119+
HomeLink: u.HomeLink(),
120+
Commits: v.Commits,
111121
}
112122
} else {
113-
user.Commits += v
123+
user.Commits += v.Commits
114124
}
115125
}
116126
v := make([]*ActivityAuthorData, 0)
@@ -119,7 +129,7 @@ func GetActivityStatsTopAuthors(repo *Repository, timeFrom time.Time, count int)
119129
}
120130

121131
sort.Slice(v, func(i, j int) bool {
122-
return v[i].Commits < v[j].Commits
132+
return v[i].Commits > v[j].Commits
123133
})
124134

125135
cnt := count

modules/git/repo_stats.go

+26-5
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,14 @@ type CodeActivityStats struct {
2121
Additions int64
2222
Deletions int64
2323
CommitCountInAllBranches int64
24-
Authors map[string]int64
24+
Authors []*CodeActivityAuthor
25+
}
26+
27+
// CodeActivityAuthor represents git statistics data for commit authors
28+
type CodeActivityAuthor struct {
29+
Name string
30+
Email string
31+
Commits int64
2532
}
2633

2734
// GetCodeActivityStats returns code statistics for acitivity page
@@ -58,8 +65,9 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string)
5865
stats.CommitCount = 0
5966
stats.Additions = 0
6067
stats.Deletions = 0
61-
authors := make(map[string]int64)
68+
authors := make(map[string]*CodeActivityAuthor)
6269
files := make(map[string]bool)
70+
var author string
6371
p := 0
6472
for scanner.Scan() {
6573
l := strings.TrimSpace(scanner.Text())
@@ -78,10 +86,17 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string)
7886
case 2: // Commit sha-1
7987
stats.CommitCount++
8088
case 3: // Author
89+
author = l
8190
case 4: // E-mail
8291
email := strings.ToLower(l)
83-
i := authors[email]
84-
authors[email] = i + 1
92+
if _, ok := authors[email]; !ok {
93+
authors[email] = &CodeActivityAuthor{
94+
Name: author,
95+
Email: email,
96+
Commits: 0,
97+
}
98+
}
99+
authors[email].Commits++
85100
default: // Changed file
86101
if parts := strings.Fields(l); len(parts) >= 3 {
87102
if parts[0] != "-" {
@@ -100,9 +115,15 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string)
100115
}
101116
}
102117
}
118+
119+
a := make([]*CodeActivityAuthor, 0, len(authors))
120+
for _, v := range authors {
121+
a = append(a, v)
122+
}
123+
103124
stats.AuthorCount = int64(len(authors))
104125
stats.ChangedFiles = int64(len(files))
105-
stats.Authors = authors
126+
stats.Authors = a
106127

107128
return stats, nil
108129
}

modules/git/repo_stats_test.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestRepository_GetCodeActivityStats(t *testing.T) {
3131
assert.EqualValues(t, 10, code.Additions)
3232
assert.EqualValues(t, 1, code.Deletions)
3333
assert.Len(t, code.Authors, 3)
34-
assert.Contains(t, code.Authors, "[email protected]")
35-
assert.EqualValues(t, 3, code.Authors["[email protected]"])
36-
assert.EqualValues(t, 5, code.Authors[""])
34+
assert.EqualValues(t, "[email protected]", code.Authors[1].Email)
35+
assert.EqualValues(t, 3, code.Authors[1].Commits)
36+
assert.EqualValues(t, 5, code.Authors[2].Commits)
3737
}

modules/templates/helper.go

+7
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,13 @@ func NewFuncMap() []template.FuncMap {
182182
}
183183
return path
184184
},
185+
"Json": func(in interface{}) string {
186+
out, err := json.Marshal(in)
187+
if err != nil {
188+
return ""
189+
}
190+
return string(out)
191+
},
185192
"JsonPrettyPrint": func(in string) string {
186193
var out bytes.Buffer
187194
err := json.Indent(&out, []byte(in), "", " ")

0 commit comments

Comments
 (0)