Browse Source

#3169

add contributors in repo search result
fix-1523
chenyifan01 2 years ago
parent
commit
e8e6cf6b6d
8 changed files with 160 additions and 35 deletions
  1. +2
    -3
      models/cloudbrain.go
  2. +60
    -17
      models/repo.go
  3. +0
    -2
      modules/setting/setting.go
  4. +4
    -4
      routers/repo/ai_model_manage.go
  5. +2
    -2
      routers/repo/attachment.go
  6. +55
    -0
      services/repository/contributor.go
  7. +32
    -6
      services/repository/repository.go
  8. +5
    -1
      services/repository/square.go

+ 2
- 3
models/cloudbrain.go View File

@@ -1861,7 +1861,7 @@ func CreateCloudbrain(cloudbrain *Cloudbrain) (err error) {
session.Commit()

go IncreaseDatasetUseCount(cloudbrain.Uuid)
go ResetRepoAITaskNum(cloudbrain.RepoID)
go OperateRepoAITaskNum(cloudbrain.RepoID, 1)
return nil
}

@@ -2030,7 +2030,7 @@ func updateAITaskNumWhenDeleteJob(job *Cloudbrain) {
}

if repoId > 0 {
ResetRepoAITaskNum(repoId)
go OperateRepoAITaskNum(repoId, -1)
}
}

@@ -2159,7 +2159,6 @@ func RestartCloudbrain(old *Cloudbrain, new *Cloudbrain) (err error) {
}

go IncreaseDatasetUseCount(new.Uuid)
go ResetRepoAITaskNum(new.RepoID)
return nil
}
func CloudbrainAll(opts *CloudbrainsOptions) ([]*CloudbrainInfo, int64, error) {


+ 60
- 17
models/repo.go View File

@@ -244,23 +244,25 @@ type Repository struct {

// Repository4Card format for front display
type Repository4Card struct {
ID int64
OwnerID int64
OwnerName string
LowerName string
Name string
NumWatches int
NumStars int
NumForks int
Description string
Topics []string
AiTaskCnt int64
ModelCnt int64
DatasetCnt int64
CreatedUnix timeutil.TimeStamp
UpdatedUnix timeutil.TimeStamp
PrimaryLanguage *LanguageStat
RelAvatarLink string
ID int64
OwnerID int64
OwnerName string
LowerName string
Name string
NumWatches int
NumStars int
NumForks int
Description string
Topics []string
AiTaskCnt int64
ModelCnt int64
DatasetCnt int64
CreatedUnix timeutil.TimeStamp
UpdatedUnix timeutil.TimeStamp
PrimaryLanguage *LanguageStat
RelAvatarLink string
Contributors []*ContributorInfo
TotalContributorCount int
}

type RepositoryShow struct {
@@ -293,6 +295,14 @@ func (repo *Repository) ToCardFormat() *Repository4Card {
return result
}

type ContributorInfo struct {
UserInfo *User // nil for contributor who is not a registered user
RelAvatarLink string
UserName string
Email string
CommitCnt int
}

// SanitizedOriginalURL returns a sanitized OriginalURL
func (repo *Repository) SanitizedOriginalURL() string {
if repo.OriginalURL == "" {
@@ -2930,3 +2940,36 @@ func ResetRepoModelNum(repoId int64) error {
_, err = x.Cols("model_cnt").Where("id = ?", repoId).Update(&r)
return err
}

func operateRepoCol(repoId int64, colName string, amount int64, engines ...*xorm.Engine) error {
var err error

if amount == 0 {
return nil
}
var ee *xorm.Engine
if len(engines) == 0 {
ee = x
} else {
ee = engines[0]
}
if amount > 0 {
_, err = ee.Exec(fmt.Sprintf("update repository set %s = %s + ? where id = ?", colName, colName), amount, repoId)
} else {
_, err = ee.Exec(fmt.Sprintf("update repository set %s = %s - ? where id = ?", colName, colName), -1*amount, repoId)
}

return err
}

func OperateRepoDatasetNum(repoId int64, amount int64, engines ...*xorm.Engine) error {
return operateRepoCol(repoId, "dataset_cnt", amount, engines...)
}

func OperateRepoModelNum(repoId int64, amount int64, engines ...*xorm.Engine) error {
return operateRepoCol(repoId, "model_cnt", amount, engines...)
}

func OperateRepoAITaskNum(repoId int64, amount int64, engines ...*xorm.Engine) error {
return operateRepoCol(repoId, "ai_task_cnt", amount, engines...)
}

+ 0
- 2
modules/setting/setting.go View File

@@ -666,7 +666,6 @@ var (

//repo square config
IncubationSourceOrgId int64
PaperSourceOwnerId int64

//nginx proxy
PROXYURL string
@@ -1563,7 +1562,6 @@ func NewContext() {

sec = Cfg.Section("repo-square")
IncubationSourceOrgId = sec.Key("INCUBATION_ORG_ID").MustInt64(9)
PaperSourceOwnerId = sec.Key("PAPER_OWNER_ID").MustInt64(36008)

sec = Cfg.Section("point")
CloudBrainPaySwitch = sec.Key("CLOUDBRAIN_PAY_SWITCH").MustBool(false)


+ 4
- 4
routers/repo/ai_model_manage.go View File

@@ -122,7 +122,7 @@ func saveModelByParameters(jobId string, versionName string, name string, versio
return "", err
}
if modelSize > 0 {
go repository.ResetRepoModelNum(aiTask.RepoID)
go repository.IncreaseRepoModelNum(aiTask.RepoID)
}
if len(lastNewModelId) > 0 {
//udpate status and version count
@@ -330,7 +330,7 @@ func UpdateModelSize(modeluuid string) {
}
}
if model.Size == 0 && size > 0 {
repository.ResetRepoModelNum(model.RepoId)
go repository.IncreaseRepoModelNum(model.RepoId)
}
} else {
log.Info("not found model,uuid=" + modeluuid)
@@ -491,7 +491,7 @@ func DeleteModelFile(ctx *context.Context) {
}
}
if (model.Size - totalSize) <= 0 {
repository.ResetRepoModelNum(model.RepoId)
go repository.DecreaseRepoModelNum(model.RepoId)
}
}
ctx.JSON(200, map[string]string{
@@ -562,7 +562,7 @@ func deleteModelByID(ctx *context.Context, id string) error {
}
}
if model.Size > 0 {
go repository.ResetRepoModelNum(model.RepoId)
go repository.DecreaseRepoModelNum(model.RepoId)
}
}
}


+ 2
- 2
routers/repo/attachment.go View File

@@ -181,7 +181,7 @@ func DeleteAttachment(ctx *context.Context) {
ctx.Error(500, fmt.Sprintf("DeleteAttachment: %v", err))
return
}
go repo_service.ResetRepoDatasetNumByDatasetId(attach.DatasetID)
go repo_service.DecreaseRepoDatasetNum(attach.DatasetID)

attachjson, _ := json.Marshal(attach)
labelmsg.SendDeleteAttachToLabelSys(string(attachjson))
@@ -896,7 +896,7 @@ func CompleteMultipart(ctx *context.Context) {
return
}
attachment.UpdateDatasetUpdateUnix()
go repo_service.ResetRepoDatasetNumByDatasetId(dataset.ID)
go repo_service.IncreaseRepoDatasetNum(dataset.ID)
repository, _ := models.GetRepositoryByID(dataset.RepoID)
notification.NotifyOtherTask(ctx.User, repository, fmt.Sprint(repository.IsPrivate, attachment.IsPrivate), attachment.Name, models.ActionUploadAttachment)
if attachment.DatasetID != 0 {


+ 55
- 0
services/repository/contributor.go View File

@@ -0,0 +1,55 @@
package repository

import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
)

func GetRepoTopNContributors(repo *models.Repository, N int) ([]*models.ContributorInfo, int) {
var contributorInfos []*models.ContributorInfo
branchName := GetDefaultBranchName(repo)
if branchName == "" {
return contributorInfos, 0
}
contributors, err := git.GetContributors(repo.RepoPath(), branchName)
if err == nil && contributors != nil {
contributorInfoHash := make(map[string]*models.ContributorInfo)
for _, c := range contributors {
if len(contributorInfos) >= N {
break
}
if c.Email == "" {
continue
}
// get user info from committer email
user, err := models.GetUserByActivateEmail(c.Email)
if err == nil {
// committer is system user, get info through user's primary email
if existedContributorInfo, ok := contributorInfoHash[user.Email]; ok {
// existed: same primary email, different committer name
existedContributorInfo.CommitCnt += c.CommitCnt
} else {
// new committer info
var newContributor = &models.ContributorInfo{
user, user.RelAvatarLink(), user.Name, user.Email, c.CommitCnt,
}
contributorInfos = append(contributorInfos, newContributor)
contributorInfoHash[user.Email] = newContributor
}
} else {
// committer is not system user
if existedContributorInfo, ok := contributorInfoHash[c.Email]; ok {
// existed: same primary email, different committer name
existedContributorInfo.CommitCnt += c.CommitCnt
} else {
var newContributor = &models.ContributorInfo{
user, "", "", c.Email, c.CommitCnt,
}
contributorInfos = append(contributorInfos, newContributor)
contributorInfoHash[c.Email] = newContributor
}
}
}
}
return contributorInfos, len(contributors)
}

+ 32
- 6
services/repository/repository.go View File

@@ -6,6 +6,7 @@ package repository

import (
"code.gitea.io/gitea/models"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/notification"
repo_module "code.gitea.io/gitea/modules/repository"
@@ -16,6 +17,7 @@ import (
"net/http"
"os"
"strings"
"xorm.io/xorm"
)

const SHELL_FLAG_ON = 1
@@ -332,18 +334,42 @@ func IsUploadFileInvalidErr(err error) bool {
return ok
}

func ResetRepoDatasetNumByDatasetId(datasetID int64) error {
func IncreaseRepoDatasetNum(datasetID int64, engines ...*xorm.Engine) error {
dataset, err := models.GetDatasetByID(datasetID)
if err != nil {
return err
}
return models.ResetRepoDatasetNum(dataset.RepoID)
return models.OperateRepoDatasetNum(dataset.RepoID, 1, engines...)
}

func ResetRepoModelNum(repoId int64) error {
return models.ResetRepoModelNum(repoId)
func IncreaseRepoModelNum(repoId int64, engines ...*xorm.Engine) error {
return models.OperateRepoModelNum(repoId, 1, engines...)
}

func ResetRepoAITaskNum(repoId int64) error {
return models.ResetRepoAITaskNum(repoId)
func DecreaseRepoDatasetNum(datasetID int64, engines ...*xorm.Engine) error {
dataset, err := models.GetDatasetByID(datasetID)
if err != nil {
return err
}
return models.OperateRepoDatasetNum(dataset.RepoID, -1, engines...)
}

func DecreaseRepoModelNum(repoId int64, engines ...*xorm.Engine) error {
return models.OperateRepoModelNum(repoId, -1, engines...)
}

func GetDefaultBranchName(repo *models.Repository) string {
gitRepo, err := git.OpenRepository(repo.RepoPath())
if err != nil {
return ""
}
defer gitRepo.Close()
if len(repo.DefaultBranch) > 0 && gitRepo.IsBranchExist(repo.DefaultBranch) {
return repo.DefaultBranch
}
brs, _, err := gitRepo.GetBranches(0, 0)
if len(brs) > 0 {
return brs[0]
}
return ""
}

+ 5
- 1
services/repository/square.go View File

@@ -134,7 +134,11 @@ func FindRepos(opts FindReposOptions) (*models.FindReposResponse, error) {
}
result := make([]*models.Repository4Card, len(repos))
for i, r := range repos {
result[i] = r.ToCardFormat()
t := r.ToCardFormat()
contributors, n := GetRepoTopNContributors(r, 6)
t.Contributors = contributors
t.TotalContributorCount = n
result[i] = t
}

return &models.FindReposResponse{


Loading…
Cancel
Save