hook.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. package service
  2. import (
  3. "context"
  4. "encoding/json"
  5. "io"
  6. "io/ioutil"
  7. "strconv"
  8. "time"
  9. "go-common/app/admin/ep/tapd/model"
  10. "go-common/library/ecode"
  11. )
  12. // TapdCallBack Tapd Call Back.
  13. func (s *Service) TapdCallBack(c context.Context, body io.ReadCloser) (err error) {
  14. var (
  15. URLs []string
  16. jsonByte []byte
  17. eventRequest *model.EventRequest
  18. eventInterface = make(map[string]interface{})
  19. createdTime time.Time
  20. workspaceID int
  21. eventID int
  22. )
  23. if jsonByte, err = ioutil.ReadAll(body); err != nil {
  24. return
  25. }
  26. //get event
  27. if err = json.Unmarshal(jsonByte, &eventRequest); err != nil {
  28. return
  29. }
  30. if eventRequest.Secret != s.c.Tapd.CallbackToken {
  31. err = ecode.Unauthorized
  32. return
  33. }
  34. if err = json.Unmarshal(jsonByte, &eventInterface); err != nil {
  35. return
  36. }
  37. // add log
  38. workspaceID, _ = strconv.Atoi(eventRequest.WorkspaceID)
  39. eventID, _ = strconv.Atoi(eventRequest.EventID)
  40. eventLog := &model.EventLog{
  41. Event: string(eventRequest.Event),
  42. WorkspaceID: workspaceID,
  43. EventID: eventID,
  44. }
  45. if err = s.dao.AddEventLog(eventLog); err != nil {
  46. return
  47. }
  48. //handle special param
  49. if createdTime, err = time.Parse("2006-01-02 15:04:05", eventRequest.Created); err != nil {
  50. return
  51. }
  52. eventInterface["id"] = eventRequest.EventID
  53. eventInterface["created"] = createdTime.Unix()
  54. if URLs, err = s.GetEnableHookURL(c, eventRequest.Event, workspaceID); err != nil {
  55. return
  56. }
  57. for _, URL := range URLs {
  58. s.transferChan.Do(context.Background(), func(c context.Context) {
  59. s.dao.CallHookUrlAsForm(context.Background(), URL, eventInterface)
  60. })
  61. }
  62. return
  63. }
  64. // GetEnableHookURL Get Enable Hook URL.
  65. func (s *Service) GetEnableHookURL(c context.Context, eventType model.Event, workspaceID int) (URLs []string, err error) {
  66. var hookURLs []*model.HookUrl
  67. if s.c.Tapd.UseCache {
  68. if hookURLs, err = s.dao.GetEnableHookURLFromCacheAndSaveIfNot(eventType); err != nil {
  69. return
  70. }
  71. } else {
  72. if hookURLs, err = s.dao.GetEnableHookURLFromDB(eventType); err != nil {
  73. return
  74. }
  75. }
  76. for _, hookURL := range hookURLs {
  77. if hookURL.ID > 0 && hookURL.Status == model.HookURLStatusEnable {
  78. if hookURL.WorkspaceID == 0 {
  79. URLs = append(URLs, hookURL.URL)
  80. } else {
  81. if workspaceID == hookURL.WorkspaceID {
  82. URLs = append(URLs, hookURL.URL)
  83. }
  84. }
  85. }
  86. }
  87. return
  88. }
  89. // SaveEnableHookURL Save Enable Hook URL.
  90. func (s *Service) SaveEnableHookURL(c context.Context) (eventMap map[string][]*model.HookUrl, err error) {
  91. return s.dao.SaveEnableHookURLToCache()
  92. }
  93. // QueryEnableHookURLInCache query Enable Hook URL In Cache.
  94. func (s *Service) QueryEnableHookURLInCache(c context.Context) (hookURLs []*model.HookUrl, err error) {
  95. return s.dao.GetEnableHookURLFromCache(model.StoryCreate)
  96. }
  97. // UpdateHookURL Update Hook URL
  98. func (s *Service) UpdateHookURL(c context.Context, username string, req *model.HookURLUpdateReq) (rep map[string]interface{}, err error) {
  99. var repID int64
  100. if req.ID > 0 {
  101. // update
  102. var (
  103. urlEvents []*model.UrlEvent
  104. urlEventsInDB []*model.UrlEvent
  105. hookUrlInDB *model.HookUrl
  106. )
  107. if hookUrlInDB, err = s.dao.QueryHookURLByID(req.ID); err != nil {
  108. return
  109. }
  110. if hookUrlInDB.ID == 0 {
  111. err = ecode.NothingFound
  112. return
  113. }
  114. hookUrl := &model.HookUrl{
  115. ID: req.ID,
  116. URL: req.URL,
  117. WorkspaceID: req.WorkspaceID,
  118. Status: req.Status,
  119. UpdateBy: username,
  120. }
  121. if urlEventsInDB, err = s.dao.QueryURLEventByUrl(req.ID); err != nil {
  122. return
  123. }
  124. //新增 或修改为可用
  125. for _, eventStr := range req.Events {
  126. urlEvent := &model.UrlEvent{
  127. Event: eventStr,
  128. Status: model.HookEventStatusEnable,
  129. UrlID: req.ID,
  130. }
  131. for _, urlEventInDB := range urlEventsInDB {
  132. if urlEventInDB.Event == eventStr {
  133. urlEvent.ID = urlEventInDB.ID
  134. break
  135. }
  136. }
  137. urlEvents = append(urlEvents, urlEvent)
  138. }
  139. // 未传值,视为disable
  140. for _, urlEventInDB := range urlEventsInDB {
  141. if urlEventInDB.Status == model.HookEventStatusEnable {
  142. var isEnable bool
  143. for _, eventStr := range req.Events {
  144. if eventStr == urlEventInDB.Event {
  145. isEnable = true
  146. break
  147. }
  148. }
  149. if !isEnable {
  150. urlEvent := &model.UrlEvent{
  151. Event: urlEventInDB.Event,
  152. Status: model.HookEventStatusDisable,
  153. ID: urlEventInDB.ID,
  154. UrlID: urlEventInDB.UrlID,
  155. }
  156. urlEvents = append(urlEvents, urlEvent)
  157. }
  158. }
  159. }
  160. if err = s.dao.UpdateHookURLandEvent(hookUrl, urlEvents); err != nil {
  161. return
  162. }
  163. repID = req.ID
  164. } else {
  165. // add
  166. var urlEvents []*model.UrlEvent
  167. hookUrl := &model.HookUrl{
  168. URL: req.URL,
  169. WorkspaceID: req.WorkspaceID,
  170. Status: req.Status,
  171. UpdateBy: username,
  172. }
  173. for _, eventStr := range req.Events {
  174. urlEvent := &model.UrlEvent{
  175. Event: eventStr,
  176. Status: model.HookEventStatusEnable,
  177. }
  178. urlEvents = append(urlEvents, urlEvent)
  179. }
  180. if err = s.dao.AddHookURLandEvent(hookUrl, urlEvents); err != nil {
  181. return
  182. }
  183. repID = hookUrl.ID
  184. }
  185. rep = make(map[string]interface{})
  186. rep["url_id"] = repID
  187. return
  188. }
  189. // QueryHookURL Query Hook URL.
  190. func (s *Service) QueryHookURL(c context.Context, req *model.QueryHookURLReq) (rep *model.QueryHookURLRep, err error) {
  191. var (
  192. total int64
  193. hookUrls []*model.HookUrl
  194. )
  195. if total, hookUrls, err = s.dao.FindHookURLs(req); err != nil {
  196. return
  197. }
  198. rep = &model.QueryHookURLRep{
  199. Pagination: model.Pagination{
  200. PageNum: req.PageNum,
  201. PageSize: req.PageSize,
  202. },
  203. Total: total,
  204. HookUrls: hookUrls,
  205. }
  206. return
  207. }
  208. // QueryURLEvent Query URL Event.
  209. func (s *Service) QueryURLEvent(c context.Context, urlID int64) (rep []*model.UrlEvent, err error) {
  210. var tmpUrlEvents []*model.UrlEvent
  211. if tmpUrlEvents, err = s.dao.QueryURLEventByUrl(urlID); err != nil {
  212. return
  213. }
  214. for _, tmpUrlEvent := range tmpUrlEvents {
  215. if tmpUrlEvent.Status == model.HookEventStatusEnable {
  216. rep = append(rep, tmpUrlEvent)
  217. }
  218. }
  219. return
  220. }
  221. // QueryEventLog Query Event Log.
  222. func (s *Service) QueryEventLog(c context.Context, req *model.QueryEventLogReq) (rep *model.QueryEventLogRep, err error) {
  223. var (
  224. total int64
  225. eventLogs []*model.EventLog
  226. )
  227. if total, eventLogs, err = s.dao.FindEventLogs(req); err != nil {
  228. return
  229. }
  230. rep = &model.QueryEventLogRep{
  231. Pagination: model.Pagination{
  232. PageNum: req.PageNum,
  233. PageSize: req.PageSize,
  234. },
  235. Total: total,
  236. EventLogs: eventLogs,
  237. }
  238. return
  239. }