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