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.9 kB

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