tapd.go 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. package dao
  2. import (
  3. "context"
  4. "encoding/json"
  5. "fmt"
  6. "net/http"
  7. "go-common/app/admin/ep/marthe/model"
  8. "go-common/library/cache/memcache"
  9. "go-common/library/ecode"
  10. "go-common/library/log"
  11. )
  12. const (
  13. _userName = "bilibilinik"
  14. _password = "0989D4F0-AF9D-949F-C950-E22A3F891NIK"
  15. _successCode = 1
  16. _releaseURL = "https://api.tapd.cn/releases?workspace_id=%s&id=%s"
  17. _bugPreURL = "https://api.tapd.cn/bugs?workspace_id=%s&id=%s"
  18. _createBugURL = "https://api.tapd.cn/bugs"
  19. _workspaceUserURL = "https://api.tapd.cn/workspaces/users?workspace_id=%s&fields=user"
  20. )
  21. //Bug Bug
  22. func (d *Dao) Bug(bugURL string) (res *model.BugResponse, err error) {
  23. var req *http.Request
  24. res = &model.BugResponse{}
  25. if req, err = d.newTapdRequest(http.MethodGet, bugURL, nil); err != nil {
  26. log.Error("Bug url(%s) res(%v) err(%v)", bugURL, res, err)
  27. return nil, ecode.MartheTapdErr
  28. }
  29. if err = d.httpClient.Do(context.TODO(), req, res); err != nil {
  30. log.Error("WorkspaceUser url(%s) res(%v) err(%v)", bugURL, res, err)
  31. err = ecode.MartheTapdResDataErr
  32. return
  33. }
  34. if res.Status != _successCode {
  35. log.Error("WorkspaceUser url(%s) res(%v) err(%v)", bugURL, res, err)
  36. err = ecode.MartheTapdResDataErr
  37. }
  38. return
  39. }
  40. //BugPre Bug Pre
  41. func (d *Dao) BugPre(workSpaceID, bugID string) (bug *model.Bug, err error) {
  42. var (
  43. req *http.Request
  44. res = &model.BugSingleResponse{}
  45. )
  46. url := fmt.Sprintf(_bugPreURL, workSpaceID, bugID)
  47. if req, err = d.newTapdRequest(http.MethodGet, url, nil); err != nil {
  48. log.Error("BugPre url(%s) res(%v) err(%v)", url, res, err)
  49. err = ecode.MartheTapdErr
  50. return
  51. }
  52. if err = d.httpClient.Do(context.TODO(), req, res); err != nil || res.Data == nil {
  53. log.Error("BugPre url(%s) res(%v) err(%v)", url, res, err)
  54. err = ecode.MartheTapdErr
  55. return
  56. }
  57. if res.Status != _successCode || res.Data == nil {
  58. log.Error("BugPre url(%s) res(%v) err(%v)", url, res, err)
  59. err = ecode.MartheTapdResDataErr
  60. return
  61. }
  62. bug = res.Data.Bug
  63. return
  64. }
  65. //Release Release
  66. func (d *Dao) Release(workSpaceID, releaseID string) (release *model.Release, err error) {
  67. var (
  68. req *http.Request
  69. res = &model.ReleaseResponse{}
  70. )
  71. releaseURL := fmt.Sprintf(_releaseURL, workSpaceID, releaseID)
  72. if req, err = d.newTapdRequest(http.MethodGet, releaseURL, nil); err != nil {
  73. log.Error("Release url(%s) res(%v) err(%v)", _releaseURL, res, err)
  74. err = ecode.MartheTapdErr
  75. return
  76. }
  77. if err = d.httpClient.Do(context.TODO(), req, res); err != nil || res.Data == nil {
  78. log.Error("Release url(%s) res(%v) err(%v)", _releaseURL, res, err)
  79. err = ecode.MartheTapdErr
  80. return
  81. }
  82. if res.Status != _successCode || res.Data == nil {
  83. log.Error("Release url(%s) res(%v) err(%v)", _releaseURL, res, err)
  84. err = ecode.MartheTapdResDataErr
  85. return
  86. }
  87. release = res.Data.Release
  88. return
  89. }
  90. // ReleaseName ReleaseName
  91. func (d *Dao) ReleaseName(workspaceID, releaseID string) (releaseName string, err error) {
  92. var (
  93. item *memcache.Item
  94. conn = d.mc.Get(context.Background())
  95. release *model.Release
  96. )
  97. defer conn.Close()
  98. if item, err = conn.Get(workspaceID + releaseID); err == nil {
  99. if err = json.Unmarshal(item.Value, &releaseName); err != nil {
  100. log.Error("Json unmarshal err(%v)", err)
  101. }
  102. return
  103. }
  104. if release, err = d.Release(workspaceID, releaseID); err != nil {
  105. return
  106. }
  107. releaseName = release.Name
  108. item = &memcache.Item{Key: workspaceID + releaseID, Object: release.Name, Flags: memcache.FlagJSON, Expiration: d.expire}
  109. d.cacheSave(context.Background(), item)
  110. return
  111. }
  112. //newTapdRequest new tapd request
  113. func (d *Dao) newTapdRequest(method, url string, v interface{}) (req *http.Request, err error) {
  114. req, err = d.newRequest(method, url, v)
  115. req.SetBasicAuth(_userName, _password)
  116. return
  117. }
  118. // CreateBug Create Bug.
  119. func (d *Dao) CreateBug(bug *model.Bug) (bugID string, err error) {
  120. var (
  121. req *http.Request
  122. res = &model.BugSingleResponse{}
  123. )
  124. if req, err = d.newTapdRequest(http.MethodPost, _createBugURL, bug); err != nil {
  125. log.Error("CreateBug url(%s) res(%v) err(%v)", _createBugURL, res, err)
  126. err = ecode.MartheTapdErr
  127. return
  128. }
  129. if err = d.httpClient.Do(context.TODO(), req, res); err != nil {
  130. log.Error("CreateBug url(%s) res(%v) err(%v)", _createBugURL, res, err)
  131. err = ecode.MartheTapdErr
  132. return
  133. }
  134. if res.Status != _successCode {
  135. log.Error("CreateBug url(%s) res(%v) err(%v)", _createBugURL, res, err)
  136. err = ecode.MartheTapdResDataErr
  137. }
  138. bugID = res.Data.Bug.ID
  139. return
  140. }
  141. // UpdateBug Update Bug.
  142. func (d *Dao) UpdateBug(bug *model.UpdateBug) (err error) {
  143. var (
  144. req *http.Request
  145. res = &model.BugSingleResponse{}
  146. )
  147. if req, err = d.newTapdRequest(http.MethodPost, _createBugURL, bug); err != nil {
  148. log.Error("UpdateBug url(%s) res(%v) err(%v)", _createBugURL, res, err)
  149. err = ecode.MartheTapdErr
  150. return
  151. }
  152. if err = d.httpClient.Do(context.TODO(), req, res); err != nil {
  153. log.Error("UpdateBug url(%s) res(%v) err(%v)", _createBugURL, res, err)
  154. err = ecode.MartheTapdErr
  155. return
  156. }
  157. if res.Status != _successCode {
  158. log.Error("UpdateBug url(%s) res(%v) err(%v)", _createBugURL, res, err)
  159. err = ecode.MartheTapdResDataErr
  160. }
  161. return
  162. }
  163. // WorkspaceUser Workspace User
  164. func (d *Dao) WorkspaceUser(workspaceID string) (usernames []string, err error) {
  165. var (
  166. req *http.Request
  167. res = &model.WorkspaceUserResponse{}
  168. )
  169. workspaceUserURL := fmt.Sprintf(_workspaceUserURL, workspaceID)
  170. if req, err = d.newTapdRequest(http.MethodGet, workspaceUserURL, nil); err != nil {
  171. log.Error("WorkspaceUser url(%s) res(%v) err(%v)", workspaceUserURL, res, err)
  172. err = ecode.MartheTapdErr
  173. return
  174. }
  175. if err = d.httpClient.Do(context.TODO(), req, res); err != nil {
  176. log.Error("WorkspaceUser url(%s) res(%v) err(%v)", workspaceUserURL, res, err)
  177. err = ecode.MartheTapdErr
  178. return
  179. }
  180. if res.Status != _successCode || res.Data == nil {
  181. log.Error("WorkspaceUser url(%s) res(%v) err(%v)", workspaceUserURL, res, err)
  182. err = ecode.MartheTapdResDataErr
  183. return
  184. }
  185. for _, workspaceUser := range res.Data {
  186. usernames = append(usernames, workspaceUser.UserWrapper.User)
  187. }
  188. return
  189. }