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
2 years ago
2 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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.ApplyYear = baseInfo.ApplyYear
  75. techRepoInfoAdmin.Province = baseInfo.Province
  76. techRepoInfoAdmin.Category = baseInfo.Category
  77. techRepoInfoAdmin.Owner = baseInfo.Owner
  78. techRepoInfoAdmin.Recommend = baseInfo.Recommend
  79. techRepoInfoAdmin.Phone = baseInfo.Phone
  80. techRepoInfoAdmin.Email = baseInfo.Email
  81. techRepoInfoAdmin.Contact = baseInfo.Contact
  82. techRepoInfoAdmin.ContactPhone = baseInfo.ContactPhone
  83. techRepoInfoAdmin.ContactEmail = baseInfo.ContactEmail
  84. techRepoInfoAdmin.ExecuteMonth = baseInfo.ExecuteMonth
  85. techRepoInfoAdmin.ExecutePeriod = baseInfo.ExecutePeriod
  86. techRepoInfoAdmin.Type = baseInfo.Type
  87. techRepoInfoAdmin.StartUp = baseInfo.StartUp
  88. techRepoInfoAdmin.NumberTopic = baseInfo.NumberTopic
  89. techRepoInfoAdmin.Topic1 = baseInfo.Topic1
  90. techRepoInfoAdmin.Topic2 = baseInfo.Topic2
  91. techRepoInfoAdmin.Topic3 = baseInfo.Topic3
  92. techRepoInfoAdmin.Topic4 = baseInfo.Topic4
  93. techRepoInfoAdmin.Topic5 = baseInfo.Topic5
  94. techRepoInfoAdmin.AllInstitution = baseInfo.AllInstitution
  95. }
  96. if info.Repo != nil {
  97. techRepoInfoAdmin.RepoName = info.Repo.Name
  98. techRepoInfoAdmin.RepoOwnerName = info.Repo.OwnerName
  99. }
  100. result = append(result, techRepoInfoAdmin)
  101. }
  102. return result, total, nil
  103. }
  104. func SearchRepoInfoWithInstitution(opt *models.SearchRepoOpt) ([]*models.RepoWithInstitution, int64, error) {
  105. infos, err := models.GetAvailableRepoConvergeInfo(opt)
  106. if err != nil {
  107. return nil, 0, err
  108. }
  109. if len(infos) == 0 {
  110. return []*models.RepoWithInstitution{}, 0, nil
  111. }
  112. repoIds, institutionMap := getRepoIdAndInstitutionMap(infos)
  113. result, err := repository.FindRepos(repository.FindReposOptions{
  114. ListOptions: models.ListOptions{Page: opt.Page, PageSize: opt.PageSize},
  115. Sort: opt.OrderBy,
  116. Keyword: opt.Q,
  117. Topic: opt.Topic,
  118. RepoIds: repoIds,
  119. })
  120. if err != nil {
  121. return nil, 0, err
  122. }
  123. repoResult := make([]*models.RepoWithInstitution, 0, len(result.Repos))
  124. for _, repo := range result.Repos {
  125. repoResult = append(repoResult, &models.RepoWithInstitution{
  126. ID: repo.ID,
  127. OwnerID: repo.OwnerID,
  128. OwnerName: repo.OwnerName,
  129. Name: repo.Name,
  130. Alias: repo.Alias,
  131. Topics: repo.Topics,
  132. Description: repo.Description,
  133. Institution: institutionMap[repo.ID],
  134. RelAvatarLink: repo.RelAvatarLink,
  135. UpdatedUnix: repo.UpdatedUnix,
  136. })
  137. }
  138. return repoResult, result.Total, nil
  139. }
  140. func getRepoIdAndInstitutionMap(infos []*models.RepoConvergeInfo) ([]int64, map[int64]string) {
  141. repoIds := make([]int64, 0, len(infos))
  142. institutionMap := make(map[int64]string, 0)
  143. // We only need the keys
  144. for _, info := range infos {
  145. repoIds = append(repoIds, info.RepoID)
  146. institutionMap[info.RepoID] = info.Institution
  147. }
  148. return repoIds, institutionMap
  149. }
  150. func IsValidRepoConvergeExists(repoId, baseInfoId int64) (bool, error) {
  151. list, err := models.GetRepoConverge(models.GetRepoConvergeOpts{
  152. Status: []int{models.TechHide, models.TechShow, models.TechMigrating},
  153. RepoId: repoId,
  154. BaseInfoId: baseInfoId,
  155. })
  156. if err != nil {
  157. return false, err
  158. }
  159. return len(list) > 0, nil
  160. }
  161. func UpdateTechStatus() {
  162. err := UpdateTechRepoDeleteStatus()
  163. if err != nil {
  164. log.Error("update delete status fail", err)
  165. }
  166. err = UpdateTechMigrateStatus()
  167. if err != nil {
  168. log.Error("update migrate status fail", err)
  169. }
  170. }
  171. func UpdateTechRepoDeleteStatus() error {
  172. needDeleteCheckRepos, err := models.GetRepoConverge(models.GetRepoConvergeOpts{
  173. Status: []int{models.TechShow, models.TechHide},
  174. })
  175. if err != nil {
  176. return err
  177. }
  178. for _, r := range needDeleteCheckRepos {
  179. _, err := models.GetRepositoryByID(r.RepoID)
  180. if err != nil && models.IsErrRepoNotExist(err) {
  181. models.UpdateRepoConvergeStatus(r.ID, models.TechNotExist)
  182. continue
  183. }
  184. }
  185. return nil
  186. }
  187. func UpdateTechMigrateStatus() error {
  188. migratingRepos, err := models.GetRepoConverge(models.GetRepoConvergeOpts{
  189. Status: []int{models.TechMigrating},
  190. })
  191. if err != nil {
  192. return err
  193. }
  194. for _, r := range migratingRepos {
  195. repo, err := models.GetRepositoryByID(r.RepoID)
  196. if err != nil {
  197. if models.IsErrRepoNotExist(err) {
  198. models.UpdateRepoConvergeStatus(r.ID, models.TechMigrateFailed)
  199. continue
  200. }
  201. continue
  202. }
  203. if repo.Status == models.RepositoryReady {
  204. models.UpdateRepoConvergeStatus(r.ID, models.DefaultTechApprovedStatus)
  205. continue
  206. }
  207. }
  208. return nil
  209. }