You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

contributor.go 3.0 kB

2 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package repository
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/modules/git"
  5. "code.gitea.io/gitea/modules/log"
  6. "code.gitea.io/gitea/modules/redis/redis_client"
  7. "code.gitea.io/gitea/modules/redis/redis_key"
  8. "encoding/json"
  9. "github.com/patrickmn/go-cache"
  10. "time"
  11. )
  12. var repoContributorCache = cache.New(5*time.Minute, 1*time.Minute)
  13. type ContributorCacheVal struct {
  14. Contributors []*models.ContributorInfo
  15. Total int
  16. }
  17. func GetRepoTopNContributors(repo *models.Repository, N int) ([]*models.ContributorInfo, int) {
  18. val, _ := redis_client.Get(redis_key.RepoTopNContributors(repo.ID, N))
  19. if val != "" {
  20. log.Debug("Get RepoTopNContributors from redis,repo.ID = %d value = %v", repo.ID, val)
  21. temp := &ContributorCacheVal{}
  22. json.Unmarshal([]byte(val), temp)
  23. return temp.Contributors, temp.Total
  24. }
  25. contributorInfos, total := getRepoTopNContributorsFromDisk(repo, N)
  26. log.Debug("Get RepoTopNContributors from disk,repo.ID = %d ", repo.ID)
  27. jsonVal, err := json.Marshal(&ContributorCacheVal{Contributors: contributorInfos, Total: total})
  28. if err == nil {
  29. redis_client.Setex(redis_key.RepoTopNContributors(repo.ID, N), string(jsonVal), 2*time.Minute)
  30. }
  31. return contributorInfos, total
  32. }
  33. func getRepoTopNContributorsFromDisk(repo *models.Repository, N int) ([]*models.ContributorInfo, int) {
  34. contributorInfos := make([]*models.ContributorInfo, 0)
  35. branchName := GetDefaultBranchName(repo)
  36. if branchName == "" {
  37. return contributorInfos, 0
  38. }
  39. contributors, err := git.GetContributors(repo.RepoPath(), branchName)
  40. if err == nil && contributors != nil {
  41. contributorInfoHash := make(map[string]*models.ContributorInfo)
  42. for _, c := range contributors {
  43. if len(contributorInfos) >= N {
  44. break
  45. }
  46. if c.Email == "" {
  47. continue
  48. }
  49. // get user info from committer email
  50. user, err := models.GetUserByActivateEmail(c.Email)
  51. if err == nil {
  52. // committer is system user, get info through user's primary email
  53. if existedContributorInfo, ok := contributorInfoHash[user.Email]; ok {
  54. // existed: same primary email, different committer name
  55. existedContributorInfo.CommitCnt += c.CommitCnt
  56. } else {
  57. // new committer info
  58. var newContributor = &models.ContributorInfo{
  59. user.RelAvatarLink(), user.Name, user.Email, c.CommitCnt,
  60. }
  61. contributorInfos = append(contributorInfos, newContributor)
  62. contributorInfoHash[user.Email] = newContributor
  63. }
  64. } else {
  65. // committer is not system user
  66. if existedContributorInfo, ok := contributorInfoHash[c.Email]; ok {
  67. // existed: same primary email, different committer name
  68. existedContributorInfo.CommitCnt += c.CommitCnt
  69. } else {
  70. var newContributor = &models.ContributorInfo{
  71. "", "", c.Email, c.CommitCnt,
  72. }
  73. contributorInfos = append(contributorInfos, newContributor)
  74. contributorInfoHash[c.Email] = newContributor
  75. }
  76. }
  77. }
  78. }
  79. return contributorInfos, len(contributors)
  80. }