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.

repo_activity_custom.go 3.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package models
  2. import (
  3. "fmt"
  4. "strings"
  5. "time"
  6. "code.gitea.io/gitea/modules/git"
  7. )
  8. func GetRepoKPIStats(repo *Repository) (*git.RepoKPIStats, error) {
  9. wikiPath := ""
  10. if repo.HasWiki() {
  11. wikiPath = repo.WikiPath()
  12. }
  13. return getRepoKPIStats(repo.RepoPath(), wikiPath)
  14. }
  15. func getRepoKPIStats(repoPath string, wikiPath string) (*git.RepoKPIStats, error) {
  16. stats := &git.RepoKPIStats{}
  17. contributors, err := git.GetContributors(repoPath)
  18. if err != nil {
  19. return nil, err
  20. }
  21. timeUntil := time.Now()
  22. fourMonthAgo := timeUntil.AddDate(0, -4, 0)
  23. recentlyContributors, err := git.GetContributorsDetail(repoPath, fourMonthAgo)
  24. newContributersDict := make(map[string]struct{})
  25. if err != nil {
  26. return nil, err
  27. }
  28. if contributors != nil {
  29. contributorDistinctDict := make(map[string]int, 0)
  30. keyContributorsDict := make(map[string]struct{}, 0)
  31. for _, contributor := range contributors {
  32. if strings.Compare(contributor.Email, "") == 0 {
  33. continue
  34. }
  35. user, err := GetUserByActivateEmail(contributor.Email)
  36. if err == nil {
  37. value, ok := contributorDistinctDict[user.Email]
  38. if !ok {
  39. contributorDistinctDict[user.Email] = contributor.CommitCnt
  40. } else {
  41. contributorDistinctDict[user.Email] = value + contributor.CommitCnt
  42. }
  43. setKeyContributerDict(contributorDistinctDict, user.Email, keyContributorsDict)
  44. } else {
  45. value, ok := contributorDistinctDict[contributor.Email]
  46. if !ok {
  47. contributorDistinctDict[contributor.Email] = contributor.CommitCnt
  48. } else {
  49. contributorDistinctDict[contributor.Email] = value + contributor.CommitCnt
  50. }
  51. setKeyContributerDict(contributorDistinctDict, contributor.Email, keyContributorsDict)
  52. }
  53. }
  54. if recentlyContributors != nil {
  55. for _, recentlyContributor := range recentlyContributors {
  56. user, err := GetUserByActivateEmail(recentlyContributor.Email)
  57. var ok bool
  58. if err == nil {
  59. _, ok = contributorDistinctDict[user.Email]
  60. } else {
  61. _, ok = contributorDistinctDict[recentlyContributor.Email]
  62. }
  63. if !ok {
  64. stats.ContributorsAdded++
  65. newContributersDict[recentlyContributor.Email] = struct{}{}
  66. }
  67. }
  68. }
  69. stats.Contributors = int64(len(contributorDistinctDict))
  70. stats.KeyContributors = int64(len(keyContributorsDict))
  71. }
  72. err = git.SetDevelopAge(repoPath, stats)
  73. if err != nil {
  74. return nil, fmt.Errorf("FillFromGit: %v", err)
  75. }
  76. err = git.SetRepoKPIStats(repoPath, fourMonthAgo, stats, newContributersDict)
  77. if err != nil {
  78. return nil, fmt.Errorf("FillFromGit: %v", err)
  79. }
  80. git.SetWikiPages(wikiPath, stats)
  81. return stats, nil
  82. }
  83. func setKeyContributerDict(contributorDistinctDict map[string]int, email string, keyContributorsDict map[string]struct{}) {
  84. if contributorDistinctDict[email] >= 3 {
  85. _, ok := keyContributorsDict[email]
  86. if !ok {
  87. keyContributorsDict[email] = struct{}{}
  88. }
  89. }
  90. }
  91. func GetAllUserKPIStats() (map[string]*git.UserKPIStats, error) {
  92. authors := make(map[string]*git.UserKPIStats)
  93. repositorys, err := GetAllRepositoriesByFilterCols("owner_name", "name")
  94. if err != nil {
  95. return nil, err
  96. }
  97. for _, repository := range repositorys {
  98. authorsOneRepo, err1 := git.GetUserKPIStats(repository.RepoPath())
  99. if err1 != nil {
  100. return nil, err
  101. }
  102. for key, value := range authorsOneRepo {
  103. if _, ok := authors[key]; !ok {
  104. authors[key] = &git.UserKPIStats{
  105. Name: value.Name,
  106. Email: value.Email,
  107. Commits: 0,
  108. CommitLines: 0,
  109. }
  110. }
  111. authors[key].Commits += value.Commits
  112. authors[key].CommitLines += value.CommitLines
  113. }
  114. }
  115. return authors, nil
  116. }