tapd.go 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764
  1. package service
  2. import (
  3. "context"
  4. "fmt"
  5. "strconv"
  6. "strings"
  7. "sync"
  8. "time"
  9. "go-common/app/admin/ep/marthe/model"
  10. "go-common/library/ecode"
  11. "go-common/library/log"
  12. )
  13. // UpdateTapdBugTpl Update Tapd Bug Tpl.
  14. func (s *Service) UpdateTapdBugTpl(c context.Context, username string, req *model.UpdateTapdBugTplRequest) (rep map[string]interface{}, err error) {
  15. //check if access to workspace
  16. if !s.AccessToWorkspace(req.WorkspaceID, username) {
  17. err = ecode.AccessDenied
  18. return
  19. }
  20. //check sql
  21. if _, err = s.CheckTapdBugTplSQL(c, req.IssueFilterSQL); err != nil {
  22. return
  23. }
  24. tapdBugTemplate := &model.TapdBugTemplate{
  25. ID: req.ID,
  26. WorkspaceID: req.WorkspaceID,
  27. BuglyProjectId: req.BuglyProjectId,
  28. IssueFilterSQL: req.IssueFilterSQL,
  29. SeverityKey: req.SeverityKey,
  30. UpdateBy: username,
  31. TapdProperty: req.TapdProperty,
  32. }
  33. if req.ID > 0 {
  34. err = s.dao.UpdateTapdBugTemplate(tapdBugTemplate)
  35. } else {
  36. // add new
  37. err = s.dao.InsertTapdBugTemplate(tapdBugTemplate)
  38. }
  39. rep = make(map[string]interface{})
  40. rep["template_id"] = tapdBugTemplate.ID
  41. return
  42. }
  43. // QueryTapdBugTpl Query Tapd Bug Tpl
  44. func (s *Service) QueryTapdBugTpl(c context.Context, req *model.QueryTapdBugTemplateRequest) (rep *model.PaginateTapdBugTemplates, err error) {
  45. var (
  46. total int64
  47. tapdBugTemplates []*model.TapdBugTemplate
  48. tapdBugTemplateWithProjectNames []*model.TapdBugTemplateWithProjectName
  49. )
  50. if total, tapdBugTemplates, err = s.dao.FindTapdBugTemplates(req); err != nil {
  51. return
  52. }
  53. for _, tapdBugTemplate := range tapdBugTemplates {
  54. var buglyProject *model.BuglyProject
  55. if buglyProject, err = s.dao.QueryBuglyProject(tapdBugTemplate.BuglyProjectId); err != nil {
  56. return
  57. }
  58. if req.ProjectName == "" || strings.Contains(buglyProject.ProjectName, req.ProjectName) {
  59. tapdBugTemplateWithProjectName := &model.TapdBugTemplateWithProjectName{
  60. TapdBugTemplate: tapdBugTemplate,
  61. ProjectName: buglyProject.ProjectName,
  62. }
  63. tapdBugTemplateWithProjectNames = append(tapdBugTemplateWithProjectNames, tapdBugTemplateWithProjectName)
  64. }
  65. }
  66. rep = &model.PaginateTapdBugTemplates{
  67. PaginationRep: model.PaginationRep{
  68. Total: total,
  69. PageSize: req.PageSize,
  70. PageNum: req.PageNum,
  71. },
  72. TapdBugTemplateWithProjectNames: tapdBugTemplateWithProjectNames,
  73. }
  74. return
  75. }
  76. // QueryAllTapdBugTpl Query All Tapd Bug Tpl
  77. func (s *Service) QueryAllTapdBugTpl(c context.Context) (rep []*model.TapdBugTemplateShortResponse, err error) {
  78. var tapdBugTemplates []*model.TapdBugTemplate
  79. if tapdBugTemplates, err = s.dao.QueryAllTapdBugTemplates(); err != nil {
  80. return
  81. }
  82. for _, tapdBugTemplate := range tapdBugTemplates {
  83. var buglyProject *model.BuglyProject
  84. if buglyProject, err = s.dao.QueryBuglyProject(tapdBugTemplate.BuglyProjectId); err != nil {
  85. return
  86. }
  87. ret := &model.TapdBugTemplateShortResponse{
  88. ID: tapdBugTemplate.ID,
  89. WorkspaceID: tapdBugTemplate.WorkspaceID,
  90. BuglyProjectId: tapdBugTemplate.BuglyProjectId,
  91. BuglyProjectName: buglyProject.ProjectName,
  92. }
  93. rep = append(rep, ret)
  94. }
  95. return
  96. }
  97. // UpdateTapdBugVersionTpl Update Tapd Bug Version Tpl.
  98. func (s *Service) UpdateTapdBugVersionTpl(c context.Context, username string, req *model.UpdateTapdBugVersionTplRequest) (rep map[string]interface{}, err error) {
  99. // check sql
  100. if req.IssueFilterSQL != "" {
  101. if _, err = s.CheckTapdBugTplSQL(c, req.IssueFilterSQL); err != nil {
  102. return
  103. }
  104. }
  105. //check project id
  106. var tmp *model.TapdBugTemplate
  107. if tmp, err = s.dao.QueryTapdBugTemplate(req.ProjectTemplateID); err != nil {
  108. return
  109. }
  110. if tmp.ID == 0 {
  111. err = ecode.NothingFound
  112. return
  113. }
  114. //check if access to workspace
  115. if !s.AccessToWorkspace(tmp.WorkspaceID, username) {
  116. err = ecode.AccessDenied
  117. return
  118. }
  119. tapdBugVersionTemplate := &model.TapdBugVersionTemplate{
  120. ID: req.ID,
  121. Version: req.Version,
  122. ProjectTemplateID: req.ProjectTemplateID,
  123. IssueFilterSQL: req.IssueFilterSQL,
  124. SeverityKey: req.SeverityKey,
  125. UpdateBy: username,
  126. TapdProperty: req.TapdProperty,
  127. }
  128. if req.ID > 0 {
  129. err = s.dao.UpdateTapdBugVersionTemplate(tapdBugVersionTemplate)
  130. } else {
  131. // add new
  132. err = s.dao.InsertTapdBugVersionTemplate(tapdBugVersionTemplate)
  133. }
  134. rep = make(map[string]interface{})
  135. rep["version_template_id"] = tapdBugVersionTemplate.ID
  136. return
  137. }
  138. // QueryTapdBugVersionTpl Query Tapd Bug Version Tpl
  139. func (s *Service) QueryTapdBugVersionTpl(c context.Context, req *model.QueryTapdBugVersionTemplateRequest) (rep *model.PaginateTapdBugVersionTemplates, err error) {
  140. var (
  141. total int64
  142. tapdBugVersionTemplates []*model.TapdBugVersionTemplate
  143. )
  144. if total, tapdBugVersionTemplates, err = s.dao.FindTapdBugVersionTemplates(req); err != nil {
  145. return
  146. }
  147. rep = &model.PaginateTapdBugVersionTemplates{
  148. PaginationRep: model.PaginationRep{
  149. Total: total,
  150. PageSize: req.PageSize,
  151. PageNum: req.PageNum,
  152. },
  153. TapdBugVersionTemplates: tapdBugVersionTemplates,
  154. }
  155. return
  156. }
  157. // CheckTapdBugTplSQL Check Tapd Bug Tpl SQL.
  158. func (s *Service) CheckTapdBugTplSQL(c context.Context, sql string) (rep map[string]interface{}, err error) {
  159. var buglyIssues []*model.BuglyIssue
  160. if buglyIssues, err = s.getBuglyIssuesByFilterSQLWithNoTapdBug(sql); err != nil {
  161. err = ecode.MartheFilterSqlError
  162. return
  163. }
  164. rep = make(map[string]interface{})
  165. rep["count"] = len(buglyIssues)
  166. return
  167. }
  168. // BugInsertTapdWithProject Bug Insert Tapd With Project.
  169. func (s *Service) BugInsertTapdWithProject(c context.Context, id int64, username string) (rep map[string]interface{}, err error) {
  170. var count int
  171. count, err = s.bugInsertTapd(id, 0, username)
  172. rep = make(map[string]interface{})
  173. rep["tapd_bug_count"] = count
  174. return
  175. }
  176. // BugInsertTapdWithVersion Bug Insert Tapd With Version.
  177. func (s *Service) BugInsertTapdWithVersion(c context.Context, id int64, username string) (rep map[string]interface{}, err error) {
  178. var count int
  179. count, err = s.bugInsertTapd(0, id, username)
  180. rep = make(map[string]interface{})
  181. rep["tapd_bug_count"] = count
  182. return
  183. }
  184. func (s *Service) bugInsertTapd(projectTemplateId, versionTemplateId int64, username string) (count int, err error) {
  185. var (
  186. tapdBugTemplate *model.TapdBugTemplate
  187. tapdBugVersionTemplate *model.TapdBugVersionTemplate
  188. buglyIssues []*model.BuglyIssue
  189. tapdBugRecords []*model.TapdBugRecord
  190. )
  191. if (projectTemplateId == 0 && versionTemplateId == 0) || (projectTemplateId > 0 && versionTemplateId > 0) {
  192. return
  193. }
  194. if projectTemplateId > 0 {
  195. if tapdBugTemplate, err = s.dao.QueryTapdBugTemplate(projectTemplateId); err != nil {
  196. return
  197. }
  198. if tapdBugTemplate.ID == 0 {
  199. err = ecode.NothingFound
  200. return
  201. }
  202. } else if versionTemplateId > 0 {
  203. if tapdBugVersionTemplate, err = s.dao.QueryTapdBugVersionTemplate(versionTemplateId); err != nil {
  204. return
  205. }
  206. if tapdBugVersionTemplate.ID == 0 {
  207. err = ecode.NothingFound
  208. return
  209. }
  210. projectTemplateId = tapdBugVersionTemplate.ProjectTemplateID
  211. if tapdBugTemplate, err = s.dao.QueryTapdBugTemplate(projectTemplateId); err != nil {
  212. return
  213. }
  214. if tapdBugTemplate.ID == 0 {
  215. err = ecode.NothingFound
  216. return
  217. }
  218. if err = s.UpdateTplAsVersionTpl(tapdBugVersionTemplate, tapdBugTemplate); err != nil {
  219. return
  220. }
  221. }
  222. // get map lock, every project tpl 串行
  223. insertLock := s.getTapdBugInsertLock(projectTemplateId)
  224. insertLock.Lock()
  225. defer insertLock.Unlock()
  226. //check if access to workspace
  227. if !s.AccessToWorkspace(tapdBugTemplate.WorkspaceID, username) {
  228. err = ecode.AccessDenied
  229. return
  230. }
  231. if tapdBugRecords, err = s.dao.QueryTapdBugRecordByProjectIDAndStatus(projectTemplateId, model.InsertBugStatusRunning); err != nil {
  232. return
  233. }
  234. if len(tapdBugRecords) > 0 {
  235. err = ecode.MartheTaskInRunning
  236. return
  237. }
  238. if buglyIssues, err = s.getBuglyIssuesByFilterSQLWithNoTapdBug(tapdBugTemplate.IssueFilterSQL); err != nil {
  239. return
  240. }
  241. count = len(buglyIssues)
  242. if count > 0 {
  243. tapdBugInsertLog := &model.TapdBugRecord{
  244. ProjectTemplateID: projectTemplateId,
  245. VersionTemplateID: versionTemplateId,
  246. Operator: username,
  247. Count: count,
  248. IssueFilterSQL: tapdBugTemplate.IssueFilterSQL,
  249. Status: model.InsertBugStatusRunning,
  250. }
  251. if err = s.dao.InsertTapdBugRecord(tapdBugInsertLog); err != nil {
  252. return
  253. }
  254. s.tapdBugCache.Do(context.TODO(), func(ctx context.Context) {
  255. defer func() {
  256. if err != nil {
  257. tapdBugInsertLog.Status = model.InsertBugStatusFailed
  258. } else {
  259. tapdBugInsertLog.Status = model.InsertBugStatusDone
  260. }
  261. err = s.dao.UpdateTapdBugRecord(tapdBugInsertLog)
  262. }()
  263. for _, buglyIssue := range buglyIssues {
  264. var (
  265. bug *model.Bug
  266. bugID string
  267. )
  268. if bug, err = s.getBugModel(buglyIssue, tapdBugTemplate); err != nil {
  269. log.Error("getLiveIOSBugModel error (%v)", err)
  270. continue
  271. }
  272. if bugID, err = s.dao.CreateBug(bug); err != nil && bugID == "" {
  273. log.Error("CreateBug error (%v)", err)
  274. continue
  275. }
  276. log.Info("insert issue no: [%s], bug number [%s]", buglyIssue.IssueNo, bugID)
  277. if err = s.dao.UpdateBuglyIssueTapdBugID(buglyIssue.ID, bugID); err != nil {
  278. log.Error("UpdateIssueRecordTapdBugID error (%v)", err)
  279. continue
  280. }
  281. buglyIssue.TapdBugID = bugID
  282. if err = s.updateBugInTapd(tapdBugTemplate, buglyIssue, time.Now()); err != nil {
  283. continue
  284. }
  285. log.Info("update issue no [%s], bug number [%s]", buglyIssue.IssueNo, buglyIssue.TapdBugID)
  286. }
  287. })
  288. }
  289. return
  290. }
  291. func (s *Service) getBugModel(bugIssue *model.BuglyIssue, bugTemplate *model.TapdBugTemplate) (bug *model.Bug, err error) {
  292. title := fmt.Sprintf(bugTemplate.Title, bugIssue.IssueNo, bugIssue.Version, bugIssue.Title, strconv.Itoa(bugIssue.HappenTimes), strconv.Itoa(bugIssue.UserTimes))
  293. description := fmt.Sprintf(bugTemplate.Description, title, bugIssue.KeyStack,
  294. bugIssue.IssueLink, bugIssue.IssueLink, bugIssue.IssueLink,
  295. bugIssue.Detail)
  296. bug = &model.Bug{
  297. Title: title,
  298. Description: description,
  299. Priority: bugTemplate.Priority,
  300. Severity: bugTemplate.Severity,
  301. Module: bugTemplate.Module,
  302. Status: bugTemplate.Status,
  303. Reporter: bugTemplate.Reporter,
  304. BugType: bugTemplate.BugType,
  305. CurrentOwner: bugTemplate.CurrentOwner,
  306. Source: bugTemplate.Source,
  307. OriginPhase: bugTemplate.OriginPhase,
  308. Platform: bugTemplate.Platform,
  309. ReleaseID: bugTemplate.ReleaseID,
  310. CustomFieldThree: bugTemplate.CustomFieldThree,
  311. CustomFieldFour: bugTemplate.CustomFieldFour,
  312. WorkspaceID: bugTemplate.WorkspaceID,
  313. IterationID: bugTemplate.IterationID,
  314. }
  315. return
  316. }
  317. // UpdateTplAsVersionTpl Update Tpl As Version Tpl.
  318. func (s *Service) UpdateTplAsVersionTpl(tapdBugVersionTemplate *model.TapdBugVersionTemplate, tapdBugTemplate *model.TapdBugTemplate) (err error) {
  319. if tapdBugVersionTemplate.ProjectTemplateID != tapdBugTemplate.ID {
  320. log.Error("projectID should be [%s],actual [%s]", tapdBugVersionTemplate.ProjectTemplateID, tapdBugTemplate.ID)
  321. return
  322. }
  323. if strings.TrimSpace(tapdBugVersionTemplate.Title) != "" {
  324. tapdBugTemplate.Title = tapdBugVersionTemplate.Title
  325. }
  326. if strings.TrimSpace(tapdBugVersionTemplate.Description) != "" {
  327. tapdBugTemplate.Description = tapdBugVersionTemplate.Description
  328. }
  329. if strings.TrimSpace(tapdBugVersionTemplate.CurrentOwner) != "" {
  330. tapdBugTemplate.CurrentOwner = tapdBugVersionTemplate.CurrentOwner
  331. }
  332. if strings.TrimSpace(tapdBugVersionTemplate.Platform) != "" {
  333. tapdBugTemplate.Platform = tapdBugVersionTemplate.Platform
  334. }
  335. if strings.TrimSpace(tapdBugVersionTemplate.Module) != "" {
  336. tapdBugTemplate.Module = tapdBugVersionTemplate.Module
  337. }
  338. if strings.TrimSpace(tapdBugVersionTemplate.IterationID) != "" {
  339. tapdBugTemplate.IterationID = tapdBugVersionTemplate.IterationID
  340. }
  341. if strings.TrimSpace(tapdBugVersionTemplate.ReleaseID) != "" {
  342. tapdBugTemplate.ReleaseID = tapdBugVersionTemplate.ReleaseID
  343. }
  344. if strings.TrimSpace(tapdBugVersionTemplate.Priority) != "" {
  345. tapdBugTemplate.Priority = tapdBugVersionTemplate.Priority
  346. }
  347. if strings.TrimSpace(tapdBugVersionTemplate.Severity) != "" {
  348. tapdBugTemplate.Severity = tapdBugVersionTemplate.Severity
  349. }
  350. if strings.TrimSpace(tapdBugVersionTemplate.Source) != "" {
  351. tapdBugTemplate.Source = tapdBugVersionTemplate.Source
  352. }
  353. if strings.TrimSpace(tapdBugVersionTemplate.CustomFieldFour) != "" {
  354. tapdBugTemplate.CustomFieldFour = tapdBugVersionTemplate.CustomFieldFour
  355. }
  356. if strings.TrimSpace(tapdBugVersionTemplate.BugType) != "" {
  357. tapdBugTemplate.BugType = tapdBugVersionTemplate.BugType
  358. }
  359. if strings.TrimSpace(tapdBugVersionTemplate.OriginPhase) != "" {
  360. tapdBugTemplate.OriginPhase = tapdBugVersionTemplate.OriginPhase
  361. }
  362. if strings.TrimSpace(tapdBugVersionTemplate.CustomFieldThree) != "" {
  363. tapdBugTemplate.CustomFieldThree = tapdBugVersionTemplate.CustomFieldThree
  364. }
  365. if strings.TrimSpace(tapdBugVersionTemplate.Reporter) != "" {
  366. tapdBugTemplate.Reporter = tapdBugVersionTemplate.Reporter
  367. }
  368. if strings.TrimSpace(tapdBugVersionTemplate.Status) != "" {
  369. tapdBugTemplate.Status = tapdBugVersionTemplate.Status
  370. }
  371. if strings.TrimSpace(tapdBugVersionTemplate.IssueFilterSQL) != "" {
  372. tapdBugTemplate.IssueFilterSQL = tapdBugVersionTemplate.IssueFilterSQL
  373. }
  374. if strings.TrimSpace(tapdBugVersionTemplate.SeverityKey) != "" {
  375. tapdBugTemplate.SeverityKey = tapdBugVersionTemplate.SeverityKey
  376. }
  377. return
  378. }
  379. // BatchRunUpdateBugInTapd Batch Run Update Bug In Tapd.
  380. func (s *Service) BatchRunUpdateBugInTapd() (err error) {
  381. var buglyIssues []*model.BuglyIssue
  382. if buglyIssues, err = s.dao.GetBuglyIssuesHasInTapd(); err != nil {
  383. return
  384. }
  385. for _, buglyIssue := range buglyIssues {
  386. var (
  387. tapdBugTemplate *model.TapdBugTemplate
  388. tapdBugVersionTemplate *model.TapdBugVersionTemplate
  389. )
  390. if tapdBugTemplate, err = s.dao.QueryTapdBugTemplateByProjectID(buglyIssue.ProjectID); err != nil {
  391. continue
  392. }
  393. if tapdBugTemplate.ID == 0 {
  394. continue
  395. }
  396. if tapdBugVersionTemplate, err = s.dao.QueryTapdBugVersionTemplateByVersion(buglyIssue.Version); err != nil {
  397. continue
  398. }
  399. if tapdBugVersionTemplate.ID > 0 {
  400. if err = s.UpdateTplAsVersionTpl(tapdBugVersionTemplate, tapdBugTemplate); err != nil {
  401. continue
  402. }
  403. }
  404. err = s.updateBugInTapd(tapdBugTemplate, buglyIssue, time.Now())
  405. }
  406. return
  407. }
  408. func (s *Service) updateBugInTapd(tapdBugTemplate *model.TapdBugTemplate, bugIssue *model.BuglyIssue, timeNow time.Time) (err error) {
  409. var bug *model.Bug
  410. // update title
  411. title := fmt.Sprintf(tapdBugTemplate.Title, bugIssue.IssueNo, bugIssue.Version, bugIssue.Title, strconv.Itoa(bugIssue.HappenTimes), strconv.Itoa(bugIssue.UserTimes))
  412. if bug, err = s.dao.BugPre(tapdBugTemplate.WorkspaceID, bugIssue.TapdBugID); err != nil {
  413. log.Error("BugPre projectId %s, error (%v)", bugIssue.ProjectID, err)
  414. return
  415. }
  416. bug.Title = title
  417. // update priority
  418. var (
  419. tapdBugPriorityConfs []*model.TapdBugPriorityConf
  420. )
  421. if tapdBugPriorityConfs, err = s.dao.QueryTapdBugPriorityConfsByProjectTemplateIdAndStatus(tapdBugTemplate.ID, model.TapdBugPriorityConfEnable); err != nil {
  422. return
  423. }
  424. for _, tmpTapdBugPriorityConf := range tapdBugPriorityConfs {
  425. if timeNow.After(tmpTapdBugPriorityConf.StartTime) && timeNow.Before(tmpTapdBugPriorityConf.EndTime) {
  426. if bugIssue.UserTimes >= tmpTapdBugPriorityConf.Urgent || bugIssue.HappenTimes >= tmpTapdBugPriorityConf.Urgent {
  427. bug.Priority = "urgent"
  428. } else if bugIssue.UserTimes >= tmpTapdBugPriorityConf.High || bugIssue.HappenTimes >= tmpTapdBugPriorityConf.High {
  429. bug.Priority = "high"
  430. } else if bugIssue.UserTimes >= tmpTapdBugPriorityConf.Medium || bugIssue.HappenTimes >= tmpTapdBugPriorityConf.Medium {
  431. bug.Priority = "medium"
  432. }
  433. break
  434. }
  435. }
  436. // update serious
  437. if strings.TrimSpace(tapdBugTemplate.SeverityKey) != "" {
  438. keys := strings.Split(tapdBugTemplate.SeverityKey, ",")
  439. for _, key := range keys {
  440. if strings.Contains(bug.Description, key) {
  441. bug.Severity = "serious"
  442. break
  443. }
  444. }
  445. }
  446. updateBug := &model.UpdateBug{
  447. Bug: bug,
  448. CurrentUser: bug.CurrentOwner,
  449. }
  450. if err := s.dao.UpdateBug(updateBug); err != nil {
  451. log.Error("UpdateBug bugid %s, error (%v)", bug.ID, err)
  452. }
  453. return
  454. }
  455. // QueryBugRecords Query Bug Records
  456. func (s *Service) QueryBugRecords(c context.Context, req *model.QueryBugRecordsRequest) (rep *model.PaginateBugRecords, err error) {
  457. var (
  458. total int64
  459. tapdBugRecords []*model.TapdBugRecord
  460. )
  461. if total, tapdBugRecords, err = s.dao.FindBugRecords(req); err != nil {
  462. return
  463. }
  464. rep = &model.PaginateBugRecords{
  465. PaginationRep: model.PaginationRep{
  466. Total: total,
  467. PageSize: req.PageSize,
  468. PageNum: req.PageNum,
  469. },
  470. TapdBugRecords: tapdBugRecords,
  471. }
  472. return
  473. }
  474. // QueryTapdBugPriorityConfsRequest Query Tapd Bug Priority Confs Request
  475. func (s *Service) QueryTapdBugPriorityConfsRequest(c context.Context, req *model.QueryTapdBugPriorityConfsRequest) (rep *model.PaginateTapdBugPriorityConfs, err error) {
  476. var (
  477. total int64
  478. tapdBugPriorityConfs []*model.TapdBugPriorityConf
  479. )
  480. if total, tapdBugPriorityConfs, err = s.dao.FindTapdBugPriorityConfs(req); err != nil {
  481. return
  482. }
  483. rep = &model.PaginateTapdBugPriorityConfs{
  484. PaginationRep: model.PaginationRep{
  485. Total: total,
  486. PageSize: req.PageSize,
  487. PageNum: req.PageNum,
  488. },
  489. TapdBugPriorityConfs: tapdBugPriorityConfs,
  490. }
  491. return
  492. }
  493. // UpdateTapdBugPriorityConf Update Tapd Bug Priority Conf.
  494. func (s *Service) UpdateTapdBugPriorityConf(c context.Context, username string, req *model.UpdateTapdBugPriorityConfRequest) (rep map[string]interface{}, err error) {
  495. var (
  496. startTime time.Time
  497. endTime time.Time
  498. tapdBugPriorityConfsInDB []*model.TapdBugPriorityConf
  499. tapdBugTemplate *model.TapdBugTemplate
  500. )
  501. //check project id
  502. if tapdBugTemplate, err = s.dao.QueryTapdBugTemplate(req.ProjectTemplateID); err != nil {
  503. return
  504. }
  505. if tapdBugTemplate.ID == 0 {
  506. err = ecode.NothingFound
  507. return
  508. }
  509. //check if access to workspace
  510. if !s.AccessToWorkspace(tapdBugTemplate.WorkspaceID, username) {
  511. err = ecode.AccessDenied
  512. return
  513. }
  514. if startTime, err = time.ParseInLocation(model.TimeLayout, req.StartTime, time.Local); err != nil {
  515. return
  516. }
  517. if endTime, err = time.ParseInLocation(model.TimeLayout, req.EndTime, time.Local); err != nil {
  518. return
  519. }
  520. // when status == enable, check time if conflict
  521. if req.Status == model.TapdBugPriorityConfEnable {
  522. if tapdBugPriorityConfsInDB, err = s.dao.QueryTapdBugPriorityConfsByProjectTemplateIdAndStatus(req.ProjectTemplateID, model.TapdBugPriorityConfEnable); err != nil {
  523. return
  524. }
  525. for _, tapdBugPriorityConfInDB := range tapdBugPriorityConfsInDB {
  526. isStartTimeInDuration := tapdBugPriorityConfInDB.StartTime.After(startTime) && tapdBugPriorityConfInDB.StartTime.Before(endTime)
  527. isEndTimeInDuration := tapdBugPriorityConfInDB.EndTime.After(startTime) && tapdBugPriorityConfInDB.EndTime.Before(endTime)
  528. if isStartTimeInDuration || isEndTimeInDuration {
  529. err = ecode.MartheTimeConflictError
  530. return
  531. }
  532. }
  533. }
  534. tapdBugPriorityConf := &model.TapdBugPriorityConf{
  535. ID: req.ID,
  536. ProjectTemplateID: req.ProjectTemplateID,
  537. Urgent: req.Urgent,
  538. High: req.High,
  539. Medium: req.Medium,
  540. UpdateBy: username,
  541. Status: req.Status,
  542. StartTime: startTime,
  543. EndTime: endTime,
  544. }
  545. if req.ID > 0 {
  546. err = s.dao.UpdateTapdBugPriorityConf(tapdBugPriorityConf)
  547. } else {
  548. // add new
  549. err = s.dao.InsertTapdBugPriorityConf(tapdBugPriorityConf)
  550. }
  551. rep = make(map[string]interface{})
  552. rep["tapd_bug_priority_conf"] = tapdBugPriorityConf.ID
  553. return
  554. }
  555. // AccessToWorkspace Access To Workspace.
  556. func (s *Service) AccessToWorkspace(workspaceID, username string) (isAccess bool) {
  557. if !s.c.Tapd.BugOperateAuth {
  558. return true
  559. }
  560. var (
  561. usernames []string
  562. contactInfo *model.ContactInfo
  563. err error
  564. )
  565. if contactInfo, err = s.dao.QueryContactInfoByUsername(username); err != nil {
  566. return
  567. }
  568. if contactInfo.ID == 0 {
  569. err = ecode.NothingFound
  570. return
  571. }
  572. if usernames, err = s.dao.WorkspaceUser(workspaceID); err != nil {
  573. return
  574. }
  575. for _, u := range usernames {
  576. if u == contactInfo.NickName {
  577. return true
  578. }
  579. }
  580. return
  581. }
  582. // HttpAccessToWorkspace Access To Workspace.
  583. func (s *Service) HttpAccessToWorkspace(c context.Context, workspaceID, username string) (rep map[string]interface{}, err error) {
  584. var (
  585. usernames []string
  586. contactInfo *model.ContactInfo
  587. isAccess bool
  588. )
  589. if contactInfo, err = s.dao.QueryContactInfoByUsername(username); err != nil {
  590. return
  591. }
  592. if contactInfo.ID == 0 {
  593. err = ecode.NothingFound
  594. return
  595. }
  596. if usernames, err = s.dao.WorkspaceUser(workspaceID); err != nil {
  597. return
  598. }
  599. for _, u := range usernames {
  600. if u == contactInfo.NickName {
  601. isAccess = true
  602. break
  603. }
  604. }
  605. rep = make(map[string]interface{})
  606. rep["is_access"] = isAccess
  607. rep["names"] = usernames
  608. return
  609. }
  610. func (s *Service) getBuglyIssuesByFilterSQLWithNoTapdBug(issueFilterSQL string) (buglyIssues []*model.BuglyIssue, err error) {
  611. var tmpBuglyIssues []*model.BuglyIssue
  612. if tmpBuglyIssues, err = s.dao.GetBuglyIssuesByFilterSQL(issueFilterSQL); err != nil {
  613. return
  614. }
  615. for _, buglyIssue := range tmpBuglyIssues {
  616. if buglyIssue.TapdBugID == "" {
  617. buglyIssues = append(buglyIssues, buglyIssue)
  618. }
  619. }
  620. return
  621. }
  622. func (s *Service) getTapdBugInsertLock(projectTplId int64) (tapdBugInsertLock *sync.Mutex) {
  623. s.syncTapdBugInsertLock.Lock()
  624. defer s.syncTapdBugInsertLock.Unlock()
  625. var ok bool
  626. if tapdBugInsertLock, ok = s.mapTapdBugInsertLocks[projectTplId]; !ok {
  627. tapdBugInsertLock = new(sync.Mutex)
  628. s.mapTapdBugInsertLocks[projectTplId] = tapdBugInsertLock
  629. }
  630. return
  631. }