Add top author stats to activity page (#9615)
This commit is contained in:
parent
7d7ab1eeae
commit
81cfe243f9
13 changed files with 524 additions and 906 deletions
|
@ -8,6 +8,7 @@ import (
|
|||
"bufio"
|
||||
"bytes"
|
||||
"fmt"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
@ -21,7 +22,14 @@ type CodeActivityStats struct {
|
|||
Additions int64
|
||||
Deletions int64
|
||||
CommitCountInAllBranches int64
|
||||
Authors map[string]int64
|
||||
Authors []*CodeActivityAuthor
|
||||
}
|
||||
|
||||
// CodeActivityAuthor represents git statistics data for commit authors
|
||||
type CodeActivityAuthor struct {
|
||||
Name string
|
||||
Email string
|
||||
Commits int64
|
||||
}
|
||||
|
||||
// GetCodeActivityStats returns code statistics for acitivity page
|
||||
|
@ -58,8 +66,9 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string)
|
|||
stats.CommitCount = 0
|
||||
stats.Additions = 0
|
||||
stats.Deletions = 0
|
||||
authors := make(map[string]int64)
|
||||
authors := make(map[string]*CodeActivityAuthor)
|
||||
files := make(map[string]bool)
|
||||
var author string
|
||||
p := 0
|
||||
for scanner.Scan() {
|
||||
l := strings.TrimSpace(scanner.Text())
|
||||
|
@ -78,10 +87,17 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string)
|
|||
case 2: // Commit sha-1
|
||||
stats.CommitCount++
|
||||
case 3: // Author
|
||||
author = l
|
||||
case 4: // E-mail
|
||||
email := strings.ToLower(l)
|
||||
i := authors[email]
|
||||
authors[email] = i + 1
|
||||
if _, ok := authors[email]; !ok {
|
||||
authors[email] = &CodeActivityAuthor{
|
||||
Name: author,
|
||||
Email: email,
|
||||
Commits: 0,
|
||||
}
|
||||
}
|
||||
authors[email].Commits++
|
||||
default: // Changed file
|
||||
if parts := strings.Fields(l); len(parts) >= 3 {
|
||||
if parts[0] != "-" {
|
||||
|
@ -100,9 +116,19 @@ func (repo *Repository) GetCodeActivityStats(fromTime time.Time, branch string)
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
a := make([]*CodeActivityAuthor, 0, len(authors))
|
||||
for _, v := range authors {
|
||||
a = append(a, v)
|
||||
}
|
||||
// Sort authors descending depending on commit count
|
||||
sort.Slice(a, func(i, j int) bool {
|
||||
return a[i].Commits > a[j].Commits
|
||||
})
|
||||
|
||||
stats.AuthorCount = int64(len(authors))
|
||||
stats.ChangedFiles = int64(len(files))
|
||||
stats.Authors = authors
|
||||
stats.Authors = a
|
||||
|
||||
return stats, nil
|
||||
}
|
||||
|
|
|
@ -31,7 +31,7 @@ func TestRepository_GetCodeActivityStats(t *testing.T) {
|
|||
assert.EqualValues(t, 10, code.Additions)
|
||||
assert.EqualValues(t, 1, code.Deletions)
|
||||
assert.Len(t, code.Authors, 3)
|
||||
assert.Contains(t, code.Authors, "tris.git@shoddynet.org")
|
||||
assert.EqualValues(t, 3, code.Authors["tris.git@shoddynet.org"])
|
||||
assert.EqualValues(t, 5, code.Authors[""])
|
||||
assert.EqualValues(t, "tris.git@shoddynet.org", code.Authors[1].Email)
|
||||
assert.EqualValues(t, 3, code.Authors[1].Commits)
|
||||
assert.EqualValues(t, 5, code.Authors[0].Commits)
|
||||
}
|
||||
|
|
|
@ -182,6 +182,13 @@ func NewFuncMap() []template.FuncMap {
|
|||
}
|
||||
return path
|
||||
},
|
||||
"Json": func(in interface{}) string {
|
||||
out, err := json.Marshal(in)
|
||||
if err != nil {
|
||||
return ""
|
||||
}
|
||||
return string(out)
|
||||
},
|
||||
"JsonPrettyPrint": func(in string) string {
|
||||
var out bytes.Buffer
|
||||
err := json.Indent(&out, []byte(in), "", " ")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue