aboutsummaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorLauris BH <lauris@nix.lv>2020-01-20 12:07:30 +0200
committerGitHub <noreply@github.com>2020-01-20 12:07:30 +0200
commit81cfe243f9cb90b0a75de7a03bb2d264c97f0036 (patch)
tree0b98db10d626eff18384ad460ec04e2a6da61468 /modules
parent7d7ab1eeae43d99fe329878ac9c8db5e45e2dee5 (diff)
downloadgitea-81cfe243f9cb90b0a75de7a03bb2d264c97f0036.tar.gz
gitea-81cfe243f9cb90b0a75de7a03bb2d264c97f0036.zip
Add top author stats to activity page (#9615)
Diffstat (limited to 'modules')
-rw-r--r--modules/git/repo_stats.go36
-rw-r--r--modules/git/repo_stats_test.go6
-rw-r--r--modules/templates/helper.go7
3 files changed, 41 insertions, 8 deletions
diff --git a/modules/git/repo_stats.go b/modules/git/repo_stats.go
index aa62e74203..bfa368b6df 100644
--- a/modules/git/repo_stats.go
+++ b/modules/git/repo_stats.go
@@ -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
}
diff --git a/modules/git/repo_stats_test.go b/modules/git/repo_stats_test.go
index bc1f6a5662..c5dd66182b 100644
--- a/modules/git/repo_stats_test.go
+++ b/modules/git/repo_stats_test.go
@@ -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)
}
diff --git a/modules/templates/helper.go b/modules/templates/helper.go
index a9fc652ca9..1e5fa01089 100644
--- a/modules/templates/helper.go
+++ b/modules/templates/helper.go
@@ -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), "", " ")