From e0bd2cd8d7a25dd0a294ce4220ba6dd97c66e5cb Mon Sep 17 00:00:00 2001 From: yystopf Date: Mon, 12 Dec 2022 14:11:09 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E8=8E=B7=E5=8F=96?= =?UTF-8?q?=E4=BB=93=E5=BA=93commit=E6=80=BB=E6=95=B0=E3=80=81=E6=A0=87?= =?UTF-8?q?=E7=AD=BE=E6=80=BB=E6=95=B0=E3=80=81=E5=8F=91=E8=A1=8C=E7=89=88?= =?UTF-8?q?=E6=80=BB=E6=95=B0=E3=80=81=E5=88=86=E6=94=AF=E6=80=BB=E6=95=B0?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- routers/hat/hat.go | 3 ++ routers/hat/repo/repo.go | 61 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) diff --git a/routers/hat/hat.go b/routers/hat/hat.go index 63dc604..ddfa08b 100644 --- a/routers/hat/hat.go +++ b/routers/hat/hat.go @@ -97,6 +97,9 @@ func Routers() *web.Route { m.Group("/contributors", func() { m.Get("", repo.GetContributors) }) + m.Group("/count", func() { + m.Get("", context.ReferencesGitRepo(), repo.GetCommitCount) + }) }, repoAssignment()) }) m.Post("/orgs", reqToken(), bind(gitea_api.CreateOrgOption{}), org.Create) diff --git a/routers/hat/repo/repo.go b/routers/hat/repo/repo.go index 5a401ec..a0bb1fd 100644 --- a/routers/hat/repo/repo.go +++ b/routers/hat/repo/repo.go @@ -5,6 +5,7 @@ import ( "net/http" "strings" + "code.gitea.io/gitea/models" admin_model "code.gitea.io/gitea/models/admin" access_model "code.gitea.io/gitea/models/perm/access" repo_model "code.gitea.io/gitea/models/repo" @@ -489,3 +490,63 @@ func GetContributors(ctx *context.APIContext) { ctx.JSON(http.StatusOK, list) } + +type CountDTO struct { + Branch CountDTOBranch `json:"branch"` + ReleaseCount int64 `json:"release_count"` + TagCount int64 `json:"tag_count"` + BranchCount int64 `json:"branch_count"` +} + +type CountDTOBranch struct { + CommitCount int64 `json:"commit_count"` + BranchName string `json:"branch_name"` +} + +func GetCommitCount(ctx *context.APIContext) { + ref := ctx.FormString("ref") + if ref == "" { + ref = ctx.Repo.Repository.DefaultBranch + } + + commit, err := ctx.Repo.GitRepo.GetCommit(ref) + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetCommit", err) + return + } + + commitCount, err := commit.CommitsCount() + if err != nil { + ctx.Error(http.StatusInternalServerError, "CommitsCount", err) + return + } + + releaseCount, err := models.GetReleaseCountByRepoID(ctx.Repo.Repository.ID, models.FindReleasesOptions{IncludeDrafts: true, IncludeTags: true}) + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetReleaseCountByRepoID", err) + return + } + + branches, _, err := ctx.Repo.GitRepo.GetBranchNames(0, 0) + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetBranchNames", err) + return + } + + tags, err := ctx.Repo.GitRepo.GetTags(0, 0) + if err != nil { + ctx.Error(http.StatusInternalServerError, "GetTags", err) + return + } + + ctx.JSON(http.StatusOK, CountDTO{ + Branch: CountDTOBranch{ + CommitCount: commitCount, + BranchName: ref, + }, + BranchCount: int64(len(branches)), + TagCount: int64(len(tags)), + ReleaseCount: releaseCount, + }) + +}