gitea源码

issue_comment.go 4.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package convert
  4. import (
  5. "context"
  6. issues_model "code.gitea.io/gitea/models/issues"
  7. repo_model "code.gitea.io/gitea/models/repo"
  8. user_model "code.gitea.io/gitea/models/user"
  9. "code.gitea.io/gitea/modules/log"
  10. api "code.gitea.io/gitea/modules/structs"
  11. "code.gitea.io/gitea/modules/util"
  12. )
  13. // ToAPIComment converts a issues_model.Comment to the api.Comment format for API usage
  14. func ToAPIComment(ctx context.Context, repo *repo_model.Repository, c *issues_model.Comment) *api.Comment {
  15. return &api.Comment{
  16. ID: c.ID,
  17. Poster: ToUser(ctx, c.Poster, nil),
  18. HTMLURL: c.HTMLURL(ctx),
  19. IssueURL: c.IssueURL(ctx),
  20. PRURL: c.PRURL(ctx),
  21. Body: c.Content,
  22. Attachments: ToAPIAttachments(repo, c.Attachments),
  23. Created: c.CreatedUnix.AsTime(),
  24. Updated: c.UpdatedUnix.AsTime(),
  25. }
  26. }
  27. // ToTimelineComment converts a issues_model.Comment to the api.TimelineComment format
  28. func ToTimelineComment(ctx context.Context, repo *repo_model.Repository, c *issues_model.Comment, doer *user_model.User) *api.TimelineComment {
  29. err := c.LoadMilestone(ctx)
  30. if err != nil {
  31. log.Error("LoadMilestone: %v", err)
  32. return nil
  33. }
  34. err = c.LoadAssigneeUserAndTeam(ctx)
  35. if err != nil {
  36. log.Error("LoadAssigneeUserAndTeam: %v", err)
  37. return nil
  38. }
  39. err = c.LoadResolveDoer(ctx)
  40. if err != nil {
  41. log.Error("LoadResolveDoer: %v", err)
  42. return nil
  43. }
  44. err = c.LoadDepIssueDetails(ctx)
  45. if err != nil {
  46. log.Error("LoadDepIssueDetails: %v", err)
  47. return nil
  48. }
  49. err = c.LoadTime(ctx)
  50. if err != nil {
  51. log.Error("LoadTime: %v", err)
  52. return nil
  53. }
  54. err = c.LoadLabel(ctx)
  55. if err != nil {
  56. log.Error("LoadLabel: %v", err)
  57. return nil
  58. }
  59. if c.Content != "" {
  60. if (c.Type == issues_model.CommentTypeAddTimeManual ||
  61. c.Type == issues_model.CommentTypeStopTracking ||
  62. c.Type == issues_model.CommentTypeDeleteTimeManual) &&
  63. c.Content[0] == '|' {
  64. // TimeTracking Comments from v1.21 on store the seconds instead of an formatted string
  65. // so we check for the "|" delimiter and convert new to legacy format on demand
  66. c.Content = util.SecToHours(c.Content[1:])
  67. }
  68. if c.Type == issues_model.CommentTypeChangeTimeEstimate {
  69. timeSec, _ := util.ToInt64(c.Content)
  70. c.Content = util.TimeEstimateString(timeSec)
  71. }
  72. }
  73. comment := &api.TimelineComment{
  74. ID: c.ID,
  75. Type: c.Type.String(),
  76. Poster: ToUser(ctx, c.Poster, nil),
  77. HTMLURL: c.HTMLURL(ctx),
  78. IssueURL: c.IssueURL(ctx),
  79. PRURL: c.PRURL(ctx),
  80. Body: c.Content,
  81. Created: c.CreatedUnix.AsTime(),
  82. Updated: c.UpdatedUnix.AsTime(),
  83. OldProjectID: c.OldProjectID,
  84. ProjectID: c.ProjectID,
  85. OldTitle: c.OldTitle,
  86. NewTitle: c.NewTitle,
  87. OldRef: c.OldRef,
  88. NewRef: c.NewRef,
  89. RefAction: c.RefAction.String(),
  90. RefCommitSHA: c.CommitSHA,
  91. ReviewID: c.ReviewID,
  92. RemovedAssignee: c.RemovedAssignee,
  93. }
  94. if c.OldMilestone != nil {
  95. comment.OldMilestone = ToAPIMilestone(c.OldMilestone)
  96. }
  97. if c.Milestone != nil {
  98. comment.Milestone = ToAPIMilestone(c.Milestone)
  99. }
  100. if c.Time != nil {
  101. err = c.Time.LoadAttributes(ctx)
  102. if err != nil {
  103. log.Error("Time.LoadAttributes: %v", err)
  104. return nil
  105. }
  106. comment.TrackedTime = ToTrackedTime(ctx, doer, c.Time)
  107. }
  108. if c.RefIssueID != 0 {
  109. issue, err := issues_model.GetIssueByID(ctx, c.RefIssueID)
  110. if err != nil {
  111. log.Error("GetIssueByID(%d): %v", c.RefIssueID, err)
  112. return nil
  113. }
  114. comment.RefIssue = ToAPIIssue(ctx, doer, issue)
  115. }
  116. if c.RefCommentID != 0 {
  117. com, err := issues_model.GetCommentByID(ctx, c.RefCommentID)
  118. if err != nil {
  119. log.Error("GetCommentByID(%d): %v", c.RefCommentID, err)
  120. return nil
  121. }
  122. err = com.LoadPoster(ctx)
  123. if err != nil {
  124. log.Error("LoadPoster: %v", err)
  125. return nil
  126. }
  127. comment.RefComment = ToAPIComment(ctx, repo, com)
  128. }
  129. if c.Label != nil {
  130. var org *user_model.User
  131. var repo *repo_model.Repository
  132. if c.Label.BelongsToOrg() {
  133. var err error
  134. org, err = user_model.GetUserByID(ctx, c.Label.OrgID)
  135. if err != nil {
  136. log.Error("GetUserByID(%d): %v", c.Label.OrgID, err)
  137. return nil
  138. }
  139. }
  140. if c.Label.BelongsToRepo() {
  141. var err error
  142. repo, err = repo_model.GetRepositoryByID(ctx, c.Label.RepoID)
  143. if err != nil {
  144. log.Error("GetRepositoryByID(%d): %v", c.Label.RepoID, err)
  145. return nil
  146. }
  147. }
  148. comment.Label = ToLabel(c.Label, repo, org)
  149. }
  150. if c.Assignee != nil {
  151. comment.Assignee = ToUser(ctx, c.Assignee, nil)
  152. }
  153. if c.AssigneeTeam != nil {
  154. comment.AssigneeTeam, _ = ToTeam(ctx, c.AssigneeTeam)
  155. }
  156. if c.ResolveDoer != nil {
  157. comment.ResolveDoer = ToUser(ctx, c.ResolveDoer, nil)
  158. }
  159. if c.DependentIssue != nil {
  160. comment.DependentIssue = ToAPIIssue(ctx, doer, c.DependentIssue)
  161. }
  162. return comment
  163. }