Skip to content

Commit 81cfe24

Browse files
authored
Add top author stats to activity page (#9615)
1 parent 7d7ab1e commit 81cfe24

File tree

13 files changed

+578
-960
lines changed

13 files changed

+578
-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

+31-5
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"bufio"
99
"bytes"
1010
"fmt"
11+
"sort"
1112
"strconv"
1213
"strings"
1314
"time"
@@ -21,7 +22,14 @@ type CodeActivityStats struct {
2122
Additions int64
2223
Deletions int64
2324
CommitCountInAllBranches int64
24-
Authors map[string]int64
25+
Authors []*CodeActivityAuthor
26+
}
27+
28+
// CodeActivityAuthor represents git statistics data for commit authors
29+
type CodeActivityAuthor struct {
30+
Name string
31+
Email string
32+
Commits int64
2533
}
2634

2735
// GetCodeActivityStats returns code statistics for acitivity page
@@ -58,8 +66,9 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string)
5866
stats.CommitCount = 0
5967
stats.Additions = 0
6068
stats.Deletions = 0
61-
authors := make(map[string]int64)
69+
authors := make(map[string]*CodeActivityAuthor)
6270
files := make(map[string]bool)
71+
var author string
6372
p := 0
6473
for scanner.Scan() {
6574
l := strings.TrimSpace(scanner.Text())
@@ -78,10 +87,17 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string)
7887
case 2: // Commit sha-1
7988
stats.CommitCount++
8089
case 3: // Author
90+
author = l
8191
case 4: // E-mail
8292
email := strings.ToLower(l)
83-
i := authors[email]
84-
authors[email] = i + 1
93+
if _, ok := authors[email]; !ok {
94+
authors[email] = &CodeActivityAuthor{
95+
Name: author,
96+
Email: email,
97+
Commits: 0,
98+
}
99+
}
100+
authors[email].Commits++
85101
default: // Changed file
86102
if parts := strings.Fields(l); len(parts) >= 3 {
87103
if parts[0] != "-" {
@@ -100,9 +116,19 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string)
100116
}
101117
}
102118
}
119+
120+
a := make([]*CodeActivityAuthor, 0, len(authors))
121+
for _, v := range authors {
122+
a = append(a, v)
123+
}
124+
// Sort authors descending depending on commit count
125+
sort.Slice(a, func(i, j int) bool {
126+
return a[i].Commits > a[j].Commits
127+
})
128+
103129
stats.AuthorCount = int64(len(authors))
104130
stats.ChangedFiles = int64(len(files))
105-
stats.Authors = authors
131+
stats.Authors = a
106132

107133
return stats, nil
108134
}

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[0].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)