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, + }) + +}