From f513d462e52cf7ec89d59fdc6b955a0addd42ce9 Mon Sep 17 00:00:00 2001 From: yystopf Date: Mon, 17 Apr 2023 10:38:18 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E8=B4=A1=E7=8C=AE?= =?UTF-8?q?=E8=80=85=E4=BB=A3=E7=A0=81=E8=A1=8C=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/git/repo_stats.go | 8 +++++--- routers/hat/hat.go | 1 + routers/hat/repo/repo.go | 26 ++++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 3 deletions(-) diff --git a/modules/git/repo_stats.go b/modules/git/repo_stats.go index d348b26..fa54183 100644 --- a/modules/git/repo_stats.go +++ b/modules/git/repo_stats.go @@ -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 { diff --git a/routers/hat/hat.go b/routers/hat/hat.go index 1e247b4..539d108 100644 --- a/routers/hat/hat.go +++ b/routers/hat/hat.go @@ -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) diff --git a/routers/hat/repo/repo.go b/routers/hat/repo/repo.go index 8e206ba..685fde1 100644 --- a/routers/hat/repo/repo.go +++ b/routers/hat/repo/repo.go @@ -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"`