Skip to content

Commit 9af88ec

Browse files
committed
Refactor
1 parent f210a51 commit 9af88ec

File tree

5 files changed

+119
-160
lines changed

5 files changed

+119
-160
lines changed

models/contributors/contributors_graph.go

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
package contributors
22

33
import (
4+
"context"
5+
"fmt"
6+
"time"
7+
48
repo_model "code.gitea.io/gitea/models/repo"
59
user_model "code.gitea.io/gitea/models/user"
610
"code.gitea.io/gitea/modules/git"
7-
"code.gitea.io/gitea/modules/json"
811
util "code.gitea.io/gitea/modules/util"
9-
"context"
10-
"fmt"
11-
"time"
1212
)
1313

1414
type WeekData struct {
@@ -20,12 +20,12 @@ type WeekData struct {
2020

2121
// ContributorData represents statistical git commit count data
2222
type ContributorData struct {
23-
Name string `json:"name"`
24-
Login string `json:"login"`
25-
AvatarLink string `json:"avatar_link"`
26-
HomeLink string `json:"home_link"`
27-
Total int64 `json:"total"`
28-
Weeks []*WeekData `json:"weeks"`
23+
Name string `json:"name"`
24+
Login string `json:"login"`
25+
AvatarLink string `json:"avatar_link"`
26+
HomeLink string `json:"home_link"`
27+
TotalCommits int64 `json:"total_commits"`
28+
Weeks []*WeekData `json:"weeks"`
2929
}
3030

3131
func CreateWeeks(sundays []int64) []*WeekData {
@@ -51,7 +51,7 @@ func GetContributorStats(ctx context.Context, repo *repo_model.Repository) (map[
5151
defer closer.Close()
5252

5353
default_branch, _ := gitRepo.GetDefaultBranch()
54-
extended_commit_stats, err := gitRepo.ExtendedCommitStats(default_branch, 6000)
54+
extended_commit_stats, err := gitRepo.ExtendedCommitStats(default_branch)
5555
if err != nil {
5656
return nil, fmt.Errorf("ExtendedCommitStats: %w", err)
5757
}
@@ -66,11 +66,12 @@ func GetContributorStats(ctx context.Context, repo *repo_model.Repository) (map[
6666

6767
unknownUserAvatarLink := user_model.NewGhostUser().AvatarLink(ctx)
6868
contributors_commit_stats := make(map[string]*ContributorData)
69-
contributors_commit_stats[""] = &ContributorData{
69+
contributors_commit_stats["Total"] = &ContributorData{
7070
Name: "Total",
7171
AvatarLink: unknownUserAvatarLink,
7272
Weeks: CreateWeeks(sundays),
7373
}
74+
total, _ := contributors_commit_stats["Total"]
7475

7576
for _, v := range extended_commit_stats {
7677
if len(v.Author.Email) == 0 {
@@ -92,7 +93,6 @@ func GetContributorStats(ctx context.Context, repo *repo_model.Repository) (map[
9293
HomeLink: u.HomeLink(),
9394
Weeks: CreateWeeks(sundays),
9495
}
95-
9696
}
9797
}
9898
// Update user statistics
@@ -105,22 +105,14 @@ func GetContributorStats(ctx context.Context, repo *repo_model.Repository) (map[
105105
user.Weeks[idx].Additions += v.Stats.Additions
106106
user.Weeks[idx].Deletions += v.Stats.Deletions
107107
user.Weeks[idx].Commits++
108-
user.Total++
108+
user.TotalCommits++
109109

110110
// Update overall statistics
111-
total, _ := contributors_commit_stats[""]
112111
total.Weeks[idx].Additions += v.Stats.Additions
113112
total.Weeks[idx].Deletions += v.Stats.Deletions
114113
total.Weeks[idx].Commits++
115-
total.Total++
114+
total.TotalCommits++
116115
}
117116

118-
fmt.Printf("users are: %s", prettyPrint(contributors_commit_stats))
119-
120117
return contributors_commit_stats, nil
121118
}
122-
123-
func prettyPrint(i interface{}) string {
124-
s, _ := json.MarshalIndent(i, "", "\t")
125-
return string(s)
126-
}

modules/git/repo_commit.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,7 @@ type CommitsByFileAndRangeOptions struct {
225225
}
226226

227227
// ExtendedCommitStats return the list of *api.ExtendedCommitStats for the given revision
228-
func (repo *Repository) ExtendedCommitStats(revision string, limit int) ([]*api.ExtendedCommitStats, error) {
228+
func (repo *Repository) ExtendedCommitStats(revision string /*, limit int */) ([]*api.ExtendedCommitStats, error) {
229229
var baseCommit *Commit
230230
baseCommit, err := repo.GetCommit(revision)
231231
stdoutReader, stdoutWriter, err := os.Pipe()
@@ -276,7 +276,6 @@ func (repo *Repository) ExtendedCommitStats(revision string, limit int) ([]*api.
276276

277277
if strings.HasPrefix(type_, "insertion") {
278278
commit_stats.Additions = amount
279-
280279
} else {
281280
commit_stats.Deletions = amount
282281
}

routers/api/v1/repo/commits.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ func GetAllCommitsStats(ctx *context.APIContext) {
328328
sha = ctx.Repo.Repository.DefaultBranch
329329
}
330330

331-
commits, err = ctx.Repo.GitRepo.ExtendedCommitStats(sha, 6000)
331+
commits, err = ctx.Repo.GitRepo.ExtendedCommitStats(sha)
332332

333333
if err != nil {
334334
ctx.Error(http.StatusInternalServerError, "ContributorsCommitStats", err)

routers/web/repo/contributors.go

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ const (
1515

1616
// Contributors render the page to show repository contributors graph
1717
func Contributors(ctx *context.Context) {
18-
ctx.Data["Title"] = ctx.Tr("repo.activity")
18+
ctx.Data["Title"] = ctx.Tr("repo.contributors")
1919
ctx.Data["PageIsContributors"] = true
2020

2121
ctx.Data["ContributionType"] = ctx.Params("contribution_type")
@@ -30,13 +30,17 @@ func Contributors(ctx *context.Context) {
3030
ctx.ServerError("GetContributorStats", err)
3131
return
3232
} else {
33-
ctx.PageData["repoContributorsCommitStats"] = contributor_stats
33+
total_stats, ok := contributor_stats["Total"]
34+
if ok {
35+
delete(contributor_stats, "Total")
36+
}
37+
ctx.PageData["repoTotalStats"] = total_stats
38+
ctx.PageData["repoContributorsStats"] = contributor_stats
39+
40+
timeFrom := time.UnixMilli(total_stats.Weeks[0].Week)
3441
timeUntil := time.Now()
35-
timeFrom := time.UnixMilli(contributor_stats[""].Weeks[0].Week)
36-
3742
ctx.Data["DateFrom"] = timeFrom.UTC().Format(time.RFC3339)
3843
ctx.Data["DateUntil"] = timeUntil.UTC().Format(time.RFC3339)
39-
// contributor_stats[""].Weeks.(map[string]interface{})
4044
}
4145

4246
ctx.HTML(http.StatusOK, tplContributors)

0 commit comments

Comments
 (0)