新增:贡献者代码行接口

This commit is contained in:
yystopf 2023-04-17 10:38:18 +08:00
parent c1d4bdbab0
commit f513d462e5
3 changed files with 32 additions and 3 deletions

View File

@ -8,6 +8,7 @@ import (
"sort"
"strconv"
"strings"
"time"
"code.gitea.io/gitea/modules/container"
gitea_git "code.gitea.io/gitea/modules/git"
@ -145,11 +146,12 @@ func GetCodeActivityStatsWithoutSince(repo *gitea_git.Repository, branch string)
}
// GetCodeActivityStats returns code statistics for activity page
func GetPaginateCodeAuthorsWithoutSince(repo *gitea_git.Repository, branch string, page, pageSize int) (int64, []*CodeActivityAuthor, error) {
func GetPaginateCodeAuthors(repo *gitea_git.Repository, fromTime time.Time, branch string, page, pageSize int) (int64, []*CodeActivityAuthor, error) {
var total int64
var authors []*CodeActivityAuthor
since := fromTime.Format(time.RFC3339)
authorCmd := gitea_git.NewCommand(repo.Ctx, "log", "--no-merges", "--format=%aN <%aE>", "--date=iso")
authorCmd := gitea_git.NewCommand(repo.Ctx, "log", "--no-merges", "--format=%aN <%aE>", "--date=iso").AddDynamicArguments("--since='%s'", since)
if len(branch) == 0 {
authorCmd.AddArguments("--branches=*")
} else {
@ -199,7 +201,7 @@ func GetPaginateCodeAuthorsWithoutSince(repo *gitea_git.Repository, branch strin
_ = stdoutReader.Close()
_ = stdoutWriter.Close()
}()
gitCmd := gitea_git.NewCommand(repo.Ctx, "log", "--numstat", "--no-merges", "--pretty=format:---%n%h%n%aN%n%aE%n", "--date=iso", gitea_git.CmdArg(fmt.Sprintf("--author=%s", filterAuthor)))
gitCmd := gitea_git.NewCommand(repo.Ctx, "log", "--numstat", "--no-merges", "--pretty=format:---%n%h%n%aN%n%aE%n", "--date=iso", gitea_git.CmdArg(fmt.Sprintf("--author=%s", filterAuthor))).AddDynamicArguments("--since='%s'", since)
if len(branch) == 0 {
gitCmd.AddArguments("--branches=*")
} else {

View File

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

View File

@ -4,6 +4,7 @@ import (
"fmt"
"net/http"
"strings"
"time"
access_model "code.gitea.io/gitea/models/perm/access"
repo_model "code.gitea.io/gitea/models/repo"
@ -498,6 +499,31 @@ func GetContributors(ctx *context.APIContext) {
ctx.JSON(http.StatusOK, list)
}
func GetContributorStat(ctx *context.APIContext) {
listOptions := utils.GetListOptions(ctx)
ref := ctx.FormString("ref")
year := ctx.FormInt("pass_year")
var timeFrom time.Time = time.Now().AddDate(-1, 0, 0) // 默认近一年
if year > 0 {
timeFrom = time.Now().AddDate(-year, 0, 0) // 近几年
}
total, list, err := hat_git.GetPaginateCodeAuthors(ctx.Repo.GitRepo, timeFrom, ref, listOptions.Page, listOptions.PageSize)
if err != nil {
ctx.Error(http.StatusInternalServerError, "GetPaginateCodeAuthors", 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)
}
type CountDTO struct {
Branch CountDTOBranch `json:"branch"`
ReleaseCount int64 `json:"release_count"`