bilihub.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. package dao
  2. import (
  3. "context"
  4. "fmt"
  5. "net/http"
  6. "strconv"
  7. "go-common/app/admin/ep/merlin/model"
  8. "go-common/library/ecode"
  9. "go-common/library/log"
  10. )
  11. const (
  12. _projectURI = "/api/projects"
  13. _repositoryURI = "/api/repositories"
  14. _authHubURI = "/api/platformUsers?role=2&session="
  15. )
  16. // AuthHub Auth Hub.
  17. func (d *Dao) AuthHub(c context.Context, sessionID string) (err error) {
  18. var (
  19. req *http.Request
  20. res interface{}
  21. )
  22. url := d.c.BiliHub.Host + _authHubURI + sessionID
  23. if req, err = d.newRequest(http.MethodGet, url, nil); err != nil {
  24. return
  25. }
  26. req.SetBasicAuth(d.c.BiliHub.Username, d.c.BiliHub.Password)
  27. if err = d.httpClient.Do(c, req, &res); err != nil {
  28. err = ecode.MerlinNoHubAccount
  29. log.Error("d.AuthHub url(%s) err(%v)", url, err)
  30. }
  31. return
  32. }
  33. // HubProjects Get Hub Projects.
  34. func (d *Dao) HubProjects(c context.Context, projectName string) (projects []*model.HubProject, err error) {
  35. var req *http.Request
  36. url := d.c.BiliHub.Host + _projectURI + "?page=1&page_size=15&name=" + projectName
  37. if req, err = d.newRequest(http.MethodGet, url, nil); err != nil {
  38. return
  39. }
  40. req.SetBasicAuth(d.c.BiliHub.Username, d.c.BiliHub.Password)
  41. if err = d.httpClient.Do(c, req, &projects); err != nil {
  42. log.Error("d.HubProjects url(%s) err(%v)", url, err)
  43. err = ecode.MerlinHubRequestErr
  44. return
  45. }
  46. return
  47. }
  48. // ProjectRepoCount Get Project RepoCount.
  49. func (d *Dao) ProjectRepoCount(c context.Context, projectID int) (total int, err error) {
  50. var (
  51. req *http.Request
  52. res = &model.GetHubProjectDetailResponse{}
  53. )
  54. url := d.c.BiliHub.Host + _projectURI + "/" + strconv.Itoa(projectID)
  55. if req, err = d.newRequest(http.MethodGet, url, nil); err != nil {
  56. return
  57. }
  58. req.SetBasicAuth(d.c.BiliHub.Username, d.c.BiliHub.Password)
  59. if err = d.httpClient.Do(c, req, &res); err != nil {
  60. log.Error("d.ProjectRepoCount url(%s) err(%v)", url, err)
  61. err = ecode.MerlinHubRequestErr
  62. return
  63. }
  64. return res.RepoCount, err
  65. }
  66. // ProjectRepos Get Project Repos.
  67. func (d *Dao) ProjectRepos(c context.Context, projectID, pageNum, pageSize int, keyWord string) (projectRepos []*model.ProjectRepository, err error) {
  68. var req *http.Request
  69. host := d.c.BiliHub.Host + _repositoryURI + "?project_id=%s&page=%s&page_size=%s&q=%s"
  70. url := fmt.Sprintf(host, strconv.Itoa(projectID), strconv.Itoa(pageNum), strconv.Itoa(pageSize), keyWord)
  71. if req, err = d.newRequest(http.MethodGet, url, nil); err != nil {
  72. return
  73. }
  74. req.SetBasicAuth(d.c.BiliHub.Username, d.c.BiliHub.Password)
  75. if err = d.httpClient.Do(c, req, &projectRepos); err != nil {
  76. log.Error("d.ProjectRepos url(%s) err(%v)", url, err)
  77. err = ecode.MerlinHubRequestErr
  78. }
  79. return
  80. }
  81. // RepoTags Get Repo Tags.
  82. func (d *Dao) RepoTags(c context.Context, repoName string) (repoTags []*model.RepositoryTagResponse, err error) {
  83. var req *http.Request
  84. host := d.c.BiliHub.Host + _repositoryURI + "/%s/tags"
  85. url := fmt.Sprintf(host, repoName)
  86. if req, err = d.newRequest(http.MethodGet, url, nil); err != nil {
  87. return
  88. }
  89. req.SetBasicAuth(d.c.BiliHub.Username, d.c.BiliHub.Password)
  90. if err = d.httpClient.Do(c, req, &repoTags); err != nil {
  91. log.Error("d.RepoTags url(%s) err(%v)", url, err)
  92. err = ecode.MerlinHubRequestErr
  93. }
  94. return
  95. }
  96. // DeleteRepoTag Delete Repo Tag.
  97. func (d *Dao) DeleteRepoTag(c context.Context, repoName, tagName string) (err error) {
  98. var req *http.Request
  99. host := d.c.BiliHub.Host + _repositoryURI + "/%s/tags/%s"
  100. url := fmt.Sprintf(host, repoName, tagName)
  101. if req, err = d.newRequest(http.MethodDelete, url, nil); err != nil {
  102. return
  103. }
  104. req.SetBasicAuth(d.c.BiliHub.Username, d.c.BiliHub.Password)
  105. if err = d.httpClient.Do(c, req, nil); err != nil {
  106. log.Error("d.DeleteRepoTag url(%s) err(%v)", url, err)
  107. err = ecode.MerlinHubRequestErr
  108. }
  109. return
  110. }
  111. // DeleteRepo Delete Repo.
  112. func (d *Dao) DeleteRepo(c context.Context, repoName string) (err error) {
  113. var req *http.Request
  114. url := d.c.BiliHub.Host + _repositoryURI + "/" + repoName
  115. if req, err = d.newRequest(http.MethodDelete, url, nil); err != nil {
  116. return
  117. }
  118. req.SetBasicAuth(d.c.BiliHub.Username, d.c.BiliHub.Password)
  119. if err = d.httpClient.Do(c, req, nil); err != nil {
  120. log.Error("d.DeleteRepo url(%s) err(%v)", url, err)
  121. err = ecode.MerlinHubRequestErr
  122. }
  123. return
  124. }
  125. // ImageTask Image Task.
  126. func (d *Dao) ImageTask(imageTask func()) {
  127. d.hubCache.Do(context.Background(), func(c context.Context) {
  128. imageTask()
  129. })
  130. }