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