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.

tech.go 6.1 kB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package tech
  2. import (
  3. "code.gitea.io/gitea/models"
  4. "code.gitea.io/gitea/services/repository"
  5. )
  6. func FindTech(opt models.FindTechOpt) ([]*models.TechConvergeBrief, error) {
  7. techList, err := models.FindTech(opt)
  8. if err != nil {
  9. return nil, err
  10. }
  11. r := make([]*models.TechConvergeBrief, len(techList))
  12. for i := 0; i < len(techList); i++ {
  13. r[i] = techList[i].Brief()
  14. }
  15. return r, nil
  16. }
  17. func GetMyRepoInfo(opts *models.SearchUserRepoOpt) ([]*models.TechRepoInfoUser, int64, error) {
  18. infos, total, err := models.GetTechRepoInfoForUser(opts)
  19. if err != nil {
  20. return nil, total, err
  21. }
  22. result := make([]*models.TechRepoInfoUser, 0, total)
  23. for _, info := range infos {
  24. techRepoInfoUser := &models.TechRepoInfoUser{
  25. ID: info.ID,
  26. Url: info.Url,
  27. Status: info.Status,
  28. ContributionInstitution: info.Institution,
  29. CreatedUnix: info.CreatedUnix,
  30. UpdatedUnix: info.UpdatedUnix,
  31. }
  32. if info.User != nil {
  33. techRepoInfoUser.UserName = info.User.Name
  34. }
  35. if info.BaseInfo != nil {
  36. baseInfo := info.BaseInfo
  37. techRepoInfoUser.ProjectNumber = baseInfo.ProjectNumber
  38. techRepoInfoUser.ProjectName = baseInfo.ProjectName
  39. techRepoInfoUser.Institution = baseInfo.Institution
  40. techRepoInfoUser.AllInstitution = baseInfo.AllInstitution
  41. }
  42. if info.Repo != nil {
  43. techRepoInfoUser.RepoName = info.Repo.Name
  44. techRepoInfoUser.RepoOwnerName = info.Repo.OwnerName
  45. }
  46. result = append(result, techRepoInfoUser)
  47. }
  48. return result, total, nil
  49. }
  50. func GetAdminRepoInfo(opts *models.SearchUserRepoOpt) ([]*models.TechRepoInfoAdmin, int64, error) {
  51. infos, total, err := models.GetTechRepoInfoForUser(opts)
  52. if err != nil {
  53. return nil, total, err
  54. }
  55. result := make([]*models.TechRepoInfoAdmin, 0, total)
  56. for _, info := range infos {
  57. techRepoInfoAdmin := &models.TechRepoInfoAdmin{
  58. ID: info.ID,
  59. Url: info.Url,
  60. Status: info.Status,
  61. ContributionInstitution: info.Institution,
  62. CreatedUnix: info.CreatedUnix,
  63. UpdatedUnix: info.UpdatedUnix,
  64. }
  65. if info.User != nil {
  66. techRepoInfoAdmin.UserName = info.User.Name
  67. }
  68. if info.BaseInfo != nil {
  69. baseInfo := info.BaseInfo
  70. techRepoInfoAdmin.ProjectNumber = baseInfo.ProjectNumber
  71. techRepoInfoAdmin.ProjectName = baseInfo.ProjectName
  72. techRepoInfoAdmin.Institution = baseInfo.Institution
  73. techRepoInfoAdmin.Province = baseInfo.Province
  74. techRepoInfoAdmin.Category = baseInfo.Category
  75. techRepoInfoAdmin.Owner = baseInfo.Owner
  76. techRepoInfoAdmin.Recommend = baseInfo.Recommend
  77. techRepoInfoAdmin.Phone = baseInfo.Phone
  78. techRepoInfoAdmin.Email = baseInfo.Email
  79. techRepoInfoAdmin.Contact = baseInfo.Contact
  80. techRepoInfoAdmin.ContactPhone = baseInfo.ContactPhone
  81. techRepoInfoAdmin.ContactEmail = baseInfo.ContactEmail
  82. techRepoInfoAdmin.ExecuteMonth = baseInfo.ExecuteMonth
  83. techRepoInfoAdmin.ExecutePeriod = baseInfo.ExecutePeriod
  84. techRepoInfoAdmin.Type = baseInfo.Type
  85. techRepoInfoAdmin.StartUp = baseInfo.StartUp
  86. techRepoInfoAdmin.NumberTopic = baseInfo.NumberTopic
  87. techRepoInfoAdmin.Topic1 = baseInfo.Topic1
  88. techRepoInfoAdmin.Topic2 = baseInfo.Topic2
  89. techRepoInfoAdmin.Topic3 = baseInfo.Topic3
  90. techRepoInfoAdmin.Topic4 = baseInfo.Topic4
  91. techRepoInfoAdmin.Topic5 = baseInfo.Topic5
  92. techRepoInfoAdmin.AllInstitution = baseInfo.AllInstitution
  93. }
  94. if info.Repo != nil {
  95. techRepoInfoAdmin.RepoName = info.Repo.Name
  96. techRepoInfoAdmin.RepoOwnerName = info.Repo.OwnerName
  97. }
  98. result = append(result, techRepoInfoAdmin)
  99. }
  100. return result, total, nil
  101. }
  102. func SearchRepoInfoWithInstitution(opt *models.SearchRepoOpt) ([]*models.RepoWithInstitution, int64, error) {
  103. infos, err := models.GetAvailableRepoConvergeInfo(opt)
  104. if err != nil {
  105. return nil, 0, err
  106. }
  107. repoIds, institutionMap := getRepoIdAndInstitutionMap(infos)
  108. result, err := repository.FindRepos(repository.FindReposOptions{
  109. ListOptions: models.ListOptions{Page: opt.Page, PageSize: opt.PageSize},
  110. Sort: opt.OrderBy,
  111. Keyword: opt.Q,
  112. Topic: opt.Topic,
  113. RepoIds: repoIds,
  114. })
  115. if err != nil {
  116. return nil, 0, err
  117. }
  118. repoResult := make([]*models.RepoWithInstitution, 0, len(result.Repos))
  119. for _, repo := range result.Repos {
  120. repoResult = append(repoResult, &models.RepoWithInstitution{
  121. ID: repo.ID,
  122. OwnerID: repo.OwnerID,
  123. OwnerName: repo.OwnerName,
  124. Name: repo.Name,
  125. Alias: repo.Alias,
  126. Topics: repo.Topics,
  127. Description: repo.Description,
  128. Institution: institutionMap[repo.ID],
  129. RelAvatarLink: repo.RelAvatarLink,
  130. UpdatedUnix: repo.UpdatedUnix,
  131. })
  132. }
  133. return repoResult, result.Total, nil
  134. }
  135. func getRepoIdAndInstitutionMap(infos []*models.RepoConvergeInfo) ([]int64, map[int64]string) {
  136. repoIds := make([]int64, 0, len(infos))
  137. institutionMap := make(map[int64]string, 0)
  138. // We only need the keys
  139. for _, info := range infos {
  140. repoIds = append(repoIds, info.RepoID)
  141. institutionMap[info.RepoID] = info.Institution
  142. }
  143. return repoIds, institutionMap
  144. }
  145. func IsValidRepoConvergeExists(repoId, baseInfoId int64) (bool, error) {
  146. list, err := models.GetRepoConverge(models.GetRepoConvergeOpts{
  147. Status: []int{models.TechHide, models.TechShow, models.TechMigrating},
  148. RepoId: repoId,
  149. BaseInfoId: baseInfoId,
  150. })
  151. if err != nil {
  152. return false, err
  153. }
  154. return len(list) > 0, nil
  155. }
  156. func UpdateTechMigrateStatus() error {
  157. migratingRepos, err := models.GetRepoConverge(models.GetRepoConvergeOpts{
  158. Status: []int{models.TechMigrating},
  159. })
  160. if err != nil {
  161. return err
  162. }
  163. for _, r := range migratingRepos {
  164. repo, err := models.GetRepositoryByID(r.RepoID)
  165. if err != nil {
  166. if models.IsErrRepoNotExist(err) {
  167. models.UpdateRepoConvergeStatus(r.ID, models.TechMigrateFailed)
  168. continue
  169. }
  170. continue
  171. }
  172. if repo.Status == models.RepositoryReady {
  173. models.UpdateRepoConvergeStatus(r.ID, models.DefaultTechApprovedStatus)
  174. continue
  175. }
  176. }
  177. return nil
  178. }