更改:贡献者获取规则

This commit is contained in:
yystopf 2023-02-23 11:31:57 +08:00
parent a1099096cb
commit 4df80116dc
4 changed files with 24 additions and 41 deletions

View File

@ -27,7 +27,7 @@ func GetContributors(repoID, userID int64) ([]Contributor, error) {
var result = make([]Contributor, 0)
sql :=
`select a.act_user_id as id ,
u.name as login,
u.name as name,
u.email as email,
count(act_user_id) as contributions
from action a

View File

@ -145,10 +145,9 @@ func GetCodeActivityStatsWithoutSince(repo *gitea_git.Repository, branch string)
}
// GetCodeActivityStats returns code statistics for activity page
func GetPaginateCodeActivityStatsWithoutSince(repo *gitea_git.Repository, branch string, page, pageSize int) (int64, *CodeActivityStats, error) {
func GetPaginateCodeAuthorsWithoutSince(repo *gitea_git.Repository, branch string, page, pageSize int) (int64, []*CodeActivityAuthor, error) {
var total int64
stats := &CodeActivityStats{}
var authors []*CodeActivityAuthor
authorCmd := gitea_git.NewCommand(repo.Ctx, "log", "--no-merges", "--format=%aN <%aE>", "--date=iso")
if len(branch) == 0 {
@ -189,20 +188,9 @@ func GetPaginateCodeActivityStatsWithoutSince(repo *gitea_git.Repository, branch
}
if filterAuthor == "" {
return total, &CodeActivityStats{Authors: make([]*CodeActivityAuthor, 0)}, nil
return total, authors, nil
}
stdout, _, runErr := gitea_git.NewCommand(repo.Ctx, "rev-list", "--count", "--no-merges", "--branches=*", "--date=iso", gitea_git.CmdArg(fmt.Sprintf("--author=%s", filterAuthor))).RunStdString(&gitea_git.RunOpts{Dir: repo.Path})
if runErr != nil {
return total, nil, runErr
}
c, err := strconv.ParseInt(strings.TrimSpace(stdout), 10, 64)
if err != nil {
return total, nil, err
}
stats.CommitCountInAllBranches = c
stdoutReader, stdoutWriter, err := os.Pipe()
if err != nil {
return total, nil, err
@ -228,10 +216,7 @@ func GetPaginateCodeActivityStatsWithoutSince(repo *gitea_git.Repository, branch
_ = stdoutWriter.Close()
scanner := bufio.NewScanner(stdoutReader)
scanner.Split(bufio.ScanLines)
stats.CommitCount = 0
stats.Additions = 0
stats.Deletions = 0
authors := make(map[string]*CodeActivityAuthor)
authorsMap := make(map[string]*CodeActivityAuthor)
files := make(container.Set[string])
var author string
var currentAuthor *CodeActivityAuthor
@ -251,27 +236,24 @@ func GetPaginateCodeActivityStatsWithoutSince(repo *gitea_git.Repository, branch
switch p {
case 1: // Separator
case 2: // Commit sha-1
stats.CommitCount++
case 3: // Author
author = l
case 4: // E-mail
email := strings.ToLower(l)
if _, ok := authors[email+author]; !ok {
authors[email+author] = &CodeActivityAuthor{Name: author, Email: email, Commits: 0}
if _, ok := authorsMap[email+author]; !ok {
authorsMap[email+author] = &CodeActivityAuthor{Name: author, Email: email, Commits: 0}
}
authors[email+author].Commits++
currentAuthor = authors[email+author]
authorsMap[email+author].Commits++
currentAuthor = authorsMap[email+author]
default: // Changed file
if parts := strings.Fields(l); len(parts) >= 3 {
if parts[0] != "-" {
if c, err := strconv.ParseInt(strings.TrimSpace(parts[0]), 10, 64); err == nil {
stats.Additions += c
currentAuthor.Additions += c
}
}
if parts[1] != "-" {
if c, err := strconv.ParseInt(strings.TrimSpace(parts[1]), 10, 64); err == nil {
stats.Deletions += c
currentAuthor.Deletions += c
}
}
@ -279,17 +261,10 @@ func GetPaginateCodeActivityStatsWithoutSince(repo *gitea_git.Repository, branch
}
}
}
a := make([]*CodeActivityAuthor, 0, len(authors))
for _, v := range authors {
a = append(a, v)
for _, v := range authorsMap {
authors = append(authors, 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 = a
_ = stdoutReader.Close()
return nil
},
@ -298,5 +273,5 @@ func GetPaginateCodeActivityStatsWithoutSince(repo *gitea_git.Repository, branch
return total, nil, fmt.Errorf("Failed to get GetCodeActivityStats for repository.\nError: %w\nStderr: %s", err, stderr)
}
return total, stats, nil
return total, authors, nil
}

View File

@ -117,7 +117,7 @@ func Routers(ctx gocontext.Context) *web.Route {
m.Get("/latest", context.ReferencesGitRepo(), repo.GetLatestRelease)
}, reqRepoReader(unit.TypeReleases))
m.Group("/contributors", func() {
m.Get("", repo.GetContributors)
m.Get("", context.ReferencesGitRepo(), repo.GetContributors)
})
m.Group("/count", func() {
m.Get("", context.ReferencesGitRepo(), repo.GetCommitCount)

View File

@ -15,8 +15,8 @@ import (
gitea_git "code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
"code.gitea.io/gitea/routers/api/v1/utils"
"code.gitea.io/gitea/services/gitdiff"
hat_activities_models "code.gitlink.org.cn/Gitlink/gitea_hat.git/models/activities"
hat_git "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/git"
)
@ -482,12 +482,20 @@ func SetDiffViewStyle(ctx *context.Context) {
}
func GetContributors(ctx *context.APIContext) {
list, err := hat_activities_models.GetContributors(ctx.Repo.Repository.ID, ctx.Repo.Owner.ID)
listOptions := utils.GetListOptions(ctx)
branch := ctx.FormString("branch")
total, list, err := hat_git.GetPaginateCodeAuthorsWithoutSince(ctx.Repo.GitRepo, branch, listOptions.Page, listOptions.PageSize)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetContributors", err)
return
}
ctx.SetLinkHeader(int(total), listOptions.PageSize)
ctx.RespHeader().Set("X-Total", fmt.Sprintf("%d", total))
ctx.RespHeader().Set("X-Total-Count", fmt.Sprintf("%d", total))
ctx.RespHeader().Set("Access-Control-Expose-Headers", "X-Total-Count, Link, X-Total")
ctx.JSON(http.StatusOK, list)
}