2020-10-17 07:23:08 +03:00
// Copyright 2020 The Gitea Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2020-10-17 07:23:08 +03:00
package convert
import (
2022-11-19 11:12:33 +03:00
"context"
2022-06-13 12:37:59 +03:00
issues_model "code.gitea.io/gitea/models/issues"
2022-01-01 17:12:25 +03:00
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/log"
2020-10-17 07:23:08 +03:00
api "code.gitea.io/gitea/modules/structs"
)
2022-06-13 12:37:59 +03:00
// ToComment converts a issues_model.Comment to the api.Comment format
func ToComment ( c * issues_model . Comment ) * api . Comment {
2020-10-17 07:23:08 +03:00
return & api . Comment {
ID : c . ID ,
2021-03-27 19:45:26 +03:00
Poster : ToUser ( c . Poster , nil ) ,
2020-10-17 07:23:08 +03:00
HTMLURL : c . HTMLURL ( ) ,
IssueURL : c . IssueURL ( ) ,
PRURL : c . PRURL ( ) ,
Body : c . Content ,
Created : c . CreatedUnix . AsTime ( ) ,
Updated : c . UpdatedUnix . AsTime ( ) ,
}
}
2022-01-01 17:12:25 +03:00
2022-06-13 12:37:59 +03:00
// ToTimelineComment converts a issues_model.Comment to the api.TimelineComment format
2022-11-19 11:12:33 +03:00
func ToTimelineComment ( ctx context . Context , c * issues_model . Comment , doer * user_model . User ) * api . TimelineComment {
err := c . LoadMilestone ( ctx )
2022-01-01 17:12:25 +03:00
if err != nil {
log . Error ( "LoadMilestone: %v" , err )
return nil
}
err = c . LoadAssigneeUserAndTeam ( )
if err != nil {
log . Error ( "LoadAssigneeUserAndTeam: %v" , err )
return nil
}
err = c . LoadResolveDoer ( )
if err != nil {
log . Error ( "LoadResolveDoer: %v" , err )
return nil
}
err = c . LoadDepIssueDetails ( )
if err != nil {
log . Error ( "LoadDepIssueDetails: %v" , err )
return nil
}
err = c . LoadTime ( )
if err != nil {
log . Error ( "LoadTime: %v" , err )
return nil
}
err = c . LoadLabel ( )
if err != nil {
log . Error ( "LoadLabel: %v" , err )
return nil
}
comment := & api . TimelineComment {
ID : c . ID ,
Type : c . Type . String ( ) ,
Poster : ToUser ( c . Poster , nil ) ,
HTMLURL : c . HTMLURL ( ) ,
IssueURL : c . IssueURL ( ) ,
PRURL : c . PRURL ( ) ,
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 {
2022-09-04 17:01:23 +03:00
err = c . Time . LoadAttributes ( )
if err != nil {
log . Error ( "Time.LoadAttributes: %v" , err )
return nil
}
2022-11-19 11:12:33 +03:00
comment . TrackedTime = ToTrackedTime ( ctx , c . Time )
2022-01-01 17:12:25 +03:00
}
if c . RefIssueID != 0 {
2022-11-19 11:12:33 +03:00
issue , err := issues_model . GetIssueByID ( ctx , c . RefIssueID )
2022-01-01 17:12:25 +03:00
if err != nil {
log . Error ( "GetIssueByID(%d): %v" , c . RefIssueID , err )
return nil
}
2022-11-19 11:12:33 +03:00
comment . RefIssue = ToAPIIssue ( ctx , issue )
2022-01-01 17:12:25 +03:00
}
if c . RefCommentID != 0 {
2022-11-19 11:12:33 +03:00
com , err := issues_model . GetCommentByID ( ctx , c . RefCommentID )
2022-01-01 17:12:25 +03:00
if err != nil {
log . Error ( "GetCommentByID(%d): %v" , c . RefCommentID , err )
return nil
}
2022-11-19 11:12:33 +03:00
err = com . LoadPoster ( ctx )
2022-01-01 17:12:25 +03:00
if err != nil {
log . Error ( "LoadPoster: %v" , err )
return nil
}
comment . RefComment = ToComment ( com )
}
if c . Label != nil {
var org * user_model . User
var repo * repo_model . Repository
if c . Label . BelongsToOrg ( ) {
var err error
2022-12-03 05:48:26 +03:00
org , err = user_model . GetUserByID ( ctx , c . Label . OrgID )
2022-01-01 17:12:25 +03:00
if err != nil {
2022-12-03 05:48:26 +03:00
log . Error ( "GetUserByID(%d): %v" , c . Label . OrgID , err )
2022-01-01 17:12:25 +03:00
return nil
}
}
if c . Label . BelongsToRepo ( ) {
var err error
2022-12-03 05:48:26 +03:00
repo , err = repo_model . GetRepositoryByID ( ctx , c . Label . RepoID )
2022-01-01 17:12:25 +03:00
if err != nil {
2022-11-19 11:12:33 +03:00
log . Error ( "GetRepositoryByIDCtx(%d): %v" , c . Label . RepoID , err )
2022-01-01 17:12:25 +03:00
return nil
}
}
comment . Label = ToLabel ( c . Label , repo , org )
}
if c . Assignee != nil {
comment . Assignee = ToUser ( c . Assignee , nil )
}
if c . AssigneeTeam != nil {
2022-05-13 20:27:58 +03:00
comment . AssigneeTeam , _ = ToTeam ( c . AssigneeTeam )
2022-01-01 17:12:25 +03:00
}
if c . ResolveDoer != nil {
comment . ResolveDoer = ToUser ( c . ResolveDoer , nil )
}
if c . DependentIssue != nil {
2022-11-19 11:12:33 +03:00
comment . DependentIssue = ToAPIIssue ( ctx , c . DependentIssue )
2022-01-01 17:12:25 +03:00
}
return comment
}