Browse Source

cmd: update repo commit_cnt

tags/v1.21.12.1
lewis 4 years ago
parent
commit
b1347c2594
4 changed files with 54 additions and 2 deletions
  1. +18
    -0
      models/repo.go
  2. +33
    -0
      routers/private/cmd.go
  3. +2
    -2
      routers/private/hook.go
  4. +1
    -0
      routers/private/internal.go

+ 18
- 0
models/repo.go View File

@@ -1412,6 +1412,15 @@ func GetRepositoriesByForkID(forkID int64) ([]*Repository, error) {
return getRepositoriesByForkID(x, forkID)
}

func getALLRepositories(e Engine) ([]*Repository, error) {
repos := make([]*Repository, 0, 1000)
return repos, e.Find(&repos)
}

func GetAllRepositories() ([]*Repository, error) {
return getALLRepositories(x)
}

func updateRepository(e Engine, repo *Repository, visibilityChanged bool) (err error) {
repo.LowerName = strings.ToLower(repo.Name)

@@ -2414,6 +2423,7 @@ func updateRepositoryCols(e Engine, repo *Repository, cols ...string) error {
}

// UpdateRepositoryCols updates repository's columns
// Notice: it will update the updated_unix automatically, be careful to use this
func UpdateRepositoryCols(repo *Repository, cols ...string) error {
return updateRepositoryCols(x, repo, cols...)
}
@@ -2443,3 +2453,11 @@ func (repo *Repository) IncreaseCloneCnt() {

return
}

func UpdateRepositoryCommitNum(repo *Repository) error {
if _,err := x.Exec("UPDATE `repository` SET num_commit = ? where id = ?", repo.NumCommit, repo.ID); err != nil {
return err
}

return nil
}

+ 33
- 0
routers/private/cmd.go View File

@@ -0,0 +1,33 @@
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package private

import (
"gitea.com/macaron/macaron"
"net/http"

"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/log"
)

func UpdateAllRepoCommitCnt(ctx *macaron.Context) {
repos,err := models.GetAllRepositories()
if err != nil {
log.Error("GetAllRepositories failed:%v", err.Error(), ctx.Data["MsgID"])
ctx.JSON(http.StatusInternalServerError, map[string]string{
"error_msg": "GetAllRepositories failed",
})
return
}

for _, repo := range repos {
log.Info("begin updateRepoCommitCnt(id = %u, name = %s)", repo.ID, repo.Name)
if err = updateRepoCommitCnt(ctx, repo); err != nil {
log.Error("updateRepoCommitCnt(id = %u, name = %s) failed:%v", repo.ID, repo.Name, err.Error(), ctx.Data["MsgID"])
break
}
log.Info("finish updateRepoCommitCnt(id = %u, name = %s)", repo.ID, repo.Name)
}
}

+ 2
- 2
routers/private/hook.go View File

@@ -546,8 +546,8 @@ func updateRepoCommitCnt(ctx *macaron.Context, repo *models.Repository) error {
}

repo.NumCommit = count
if err = models.UpdateRepositoryCols(repo, "num_commit"); err != nil {
log.Error("UpdateRepositoryCols failed:%v", err.Error(), ctx.Data["MsgID"])
if err = models.UpdateRepositoryCommitNum(repo); err != nil {
log.Error("UpdateRepositoryCommitNum failed:%v", err.Error(), ctx.Data["MsgID"])
return err
}



+ 1
- 0
routers/private/internal.go View File

@@ -42,6 +42,7 @@ func RegisterRoutes(m *macaron.Macaron) {
m.Post("/manager/shutdown", Shutdown)
m.Post("/manager/restart", Restart)
m.Post("/manager/flush-queues", bind(private.FlushOptions{}), FlushQueues)
m.Post("/cmd/update_all_repo_commit_cnt", UpdateAllRepoCommitCnt)

}, CheckInternalToken)
}

Loading…
Cancel
Save