2018-08-06 10:13:22 +05:30
// Copyright 2018 The Gitea Authors.
// Copyright 2016 The Gogs Authors.
// All rights reserved.
2016-03-05 23:28:51 +05:30
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
2022-06-13 15:07:59 +05:30
package issues
2016-03-05 23:28:51 +05:30
import (
2021-11-19 19:09:57 +05:30
"context"
2016-03-05 23:28:51 +05:30
"fmt"
2020-06-14 19:25:20 +05:30
"regexp"
2020-06-18 19:37:09 +05:30
"strconv"
2016-03-05 23:28:51 +05:30
"strings"
2020-06-18 19:37:09 +05:30
"unicode/utf8"
2016-03-05 23:28:51 +05:30
2021-09-19 17:19:59 +05:30
"code.gitea.io/gitea/models/db"
2022-06-12 21:21:54 +05:30
git_model "code.gitea.io/gitea/models/git"
2022-03-29 11:59:02 +05:30
"code.gitea.io/gitea/models/organization"
2022-03-29 19:46:31 +05:30
project_model "code.gitea.io/gitea/models/project"
2021-11-19 19:09:57 +05:30
repo_model "code.gitea.io/gitea/models/repo"
2021-11-24 15:19:20 +05:30
user_model "code.gitea.io/gitea/models/user"
2019-03-27 15:03:00 +05:30
"code.gitea.io/gitea/modules/git"
2021-07-24 21:33:58 +05:30
"code.gitea.io/gitea/modules/json"
2019-08-15 20:16:21 +05:30
"code.gitea.io/gitea/modules/log"
2021-04-20 03:55:08 +05:30
"code.gitea.io/gitea/modules/markup"
2018-08-06 10:13:22 +05:30
"code.gitea.io/gitea/modules/markup/markdown"
2019-10-14 03:59:10 +05:30
"code.gitea.io/gitea/modules/references"
2019-10-14 11:40:42 +05:30
"code.gitea.io/gitea/modules/structs"
2019-08-15 20:16:21 +05:30
"code.gitea.io/gitea/modules/timeutil"
2022-10-18 11:20:37 +05:30
"code.gitea.io/gitea/modules/util"
2019-08-15 20:16:21 +05:30
2019-06-23 20:52:43 +05:30
"xorm.io/builder"
2019-10-17 14:56:49 +05:30
"xorm.io/xorm"
2016-03-05 23:28:51 +05:30
)
2022-06-13 15:07:59 +05:30
// ErrCommentNotExist represents a "CommentNotExist" kind of error.
type ErrCommentNotExist struct {
ID int64
IssueID int64
}
// IsErrCommentNotExist checks if an error is a ErrCommentNotExist.
func IsErrCommentNotExist ( err error ) bool {
_ , ok := err . ( ErrCommentNotExist )
return ok
}
func ( err ErrCommentNotExist ) Error ( ) string {
return fmt . Sprintf ( "comment does not exist [id: %d, issue_id: %d]" , err . ID , err . IssueID )
}
2022-10-18 11:20:37 +05:30
func ( err ErrCommentNotExist ) Unwrap ( ) error {
return util . ErrNotExist
}
2016-03-05 23:28:51 +05:30
// CommentType defines whether a comment is just a simple comment, an action (like close) or a reference.
type CommentType int
2017-06-21 06:30:44 +05:30
// define unknown comment type
const (
CommentTypeUnknown CommentType = - 1
)
2016-11-28 19:03:09 +05:30
// Enumerate all the comment types
2016-03-05 23:28:51 +05:30
const (
2021-02-10 09:27:30 +05:30
// 0 Plain comment, can be associated with a commit (CommitID > 0) and a line (LineNum > 0)
2016-11-07 22:00:04 +05:30
CommentTypeComment CommentType = iota
2021-02-10 09:27:30 +05:30
CommentTypeReopen // 1
CommentTypeClose // 2
2016-03-05 23:28:51 +05:30
2021-02-10 09:27:30 +05:30
// 3 References.
2016-11-07 22:05:34 +05:30
CommentTypeIssueRef
2021-02-10 09:27:30 +05:30
// 4 Reference from a commit (not part of a pull request)
2016-11-07 22:05:34 +05:30
CommentTypeCommitRef
2021-02-10 09:27:30 +05:30
// 5 Reference from a comment
2016-11-08 02:28:22 +05:30
CommentTypeCommentRef
2021-02-10 09:27:30 +05:30
// 6 Reference from a pull request
2016-11-07 22:05:34 +05:30
CommentTypePullRef
2021-02-10 09:27:30 +05:30
// 7 Labels changed
2017-01-30 18:16:45 +05:30
CommentTypeLabel
2021-02-10 09:27:30 +05:30
// 8 Milestone changed
2017-02-01 08:06:08 +05:30
CommentTypeMilestone
2021-02-10 09:27:30 +05:30
// 9 Assignees changed
2017-02-03 20:39:10 +05:30
CommentTypeAssignees
2021-02-10 09:27:30 +05:30
// 10 Change Title
2017-02-05 20:06:00 +05:30
CommentTypeChangeTitle
2021-02-10 09:27:30 +05:30
// 11 Delete Branch
2017-02-11 09:30:29 +05:30
CommentTypeDeleteBranch
2021-02-10 09:27:30 +05:30
// 12 Start a stopwatch for time tracking
2017-09-12 12:18:13 +05:30
CommentTypeStartTracking
2021-02-10 09:27:30 +05:30
// 13 Stop a stopwatch for time tracking
2017-09-12 12:18:13 +05:30
CommentTypeStopTracking
2021-02-10 09:27:30 +05:30
// 14 Add time manual for time tracking
2017-09-12 12:18:13 +05:30
CommentTypeAddTimeManual
2021-02-10 09:27:30 +05:30
// 15 Cancel a stopwatch for time tracking
2017-09-12 12:18:13 +05:30
CommentTypeCancelTracking
2021-02-10 09:27:30 +05:30
// 16 Added a due date
2018-05-02 00:35:28 +05:30
CommentTypeAddedDeadline
2021-02-10 09:27:30 +05:30
// 17 Modified the due date
2018-05-02 00:35:28 +05:30
CommentTypeModifiedDeadline
2021-02-10 09:27:30 +05:30
// 18 Removed a due date
2018-05-02 00:35:28 +05:30
CommentTypeRemovedDeadline
2021-02-10 09:27:30 +05:30
// 19 Dependency added
2018-07-18 02:53:58 +05:30
CommentTypeAddDependency
2021-02-10 09:27:30 +05:30
// 20 Dependency removed
2018-07-18 02:53:58 +05:30
CommentTypeRemoveDependency
2021-02-10 09:27:30 +05:30
// 21 Comment a line of code
2018-08-06 10:13:22 +05:30
CommentTypeCode
2021-02-10 09:27:30 +05:30
// 22 Reviews a pull request by giving general feedback
2018-08-06 10:13:22 +05:30
CommentTypeReview
2021-02-10 09:27:30 +05:30
// 23 Lock an issue, giving only collaborators access
2019-02-19 02:25:04 +05:30
CommentTypeLock
2021-02-10 09:27:30 +05:30
// 24 Unlocks a previously locked issue
2019-02-19 02:25:04 +05:30
CommentTypeUnlock
2021-02-10 09:27:30 +05:30
// 25 Change pull request's target branch
2019-12-16 11:50:25 +05:30
CommentTypeChangeTargetBranch
2021-02-10 09:27:30 +05:30
// 26 Delete time manual for time tracking
2019-12-28 02:00:58 +05:30
CommentTypeDeleteTimeManual
2021-02-10 09:27:30 +05:30
// 27 add or remove Request from one
2020-04-06 22:03:34 +05:30
CommentTypeReviewRequest
2021-02-10 09:27:30 +05:30
// 28 merge pull request
2020-04-14 06:36:23 +05:30
CommentTypeMergePull
2021-02-10 09:27:30 +05:30
// 29 push to PR head branch
2022-01-21 23:29:26 +05:30
CommentTypePullRequestPush
2021-02-10 09:27:30 +05:30
// 30 Project changed
2020-08-17 08:37:38 +05:30
CommentTypeProject
2021-02-10 09:27:30 +05:30
// 31 Project board changed
2020-08-17 08:37:38 +05:30
CommentTypeProjectBoard
2021-11-19 15:24:31 +05:30
// 32 Dismiss Review
2021-02-11 23:02:25 +05:30
CommentTypeDismissReview
2021-11-19 15:24:31 +05:30
// 33 Change issue ref
CommentTypeChangeIssueRef
2022-05-07 22:35:52 +05:30
// 34 pr was scheduled to auto merge when checks succeed
CommentTypePRScheduledToAutoMerge
// 35 pr was un scheduled to auto merge when checks succeed
CommentTypePRUnScheduledToAutoMerge
2016-03-05 23:28:51 +05:30
)
2022-01-01 19:42:25 +05:30
var commentStrings = [ ] string {
"comment" ,
"reopen" ,
"close" ,
"issue_ref" ,
"commit_ref" ,
"comment_ref" ,
"pull_ref" ,
"label" ,
"milestone" ,
"assignees" ,
"change_title" ,
"delete_branch" ,
"start_tracking" ,
"stop_tracking" ,
"add_time_manual" ,
"cancel_tracking" ,
"added_deadline" ,
"modified_deadline" ,
"removed_deadline" ,
"add_dependency" ,
"remove_dependency" ,
"code" ,
"review" ,
"lock" ,
"unlock" ,
"change_target_branch" ,
"delete_time_manual" ,
"review_request" ,
"merge_pull" ,
"pull_push" ,
"project" ,
"project_board" ,
"dismiss_review" ,
"change_issue_ref" ,
2022-05-07 22:35:52 +05:30
"pull_scheduled_merge" ,
"pull_cancel_scheduled_merge" ,
2022-01-01 19:42:25 +05:30
}
func ( t CommentType ) String ( ) string {
return commentStrings [ t ]
}
2021-11-11 11:59:30 +05:30
// RoleDescriptor defines comment tag type
type RoleDescriptor int
2016-03-05 23:28:51 +05:30
2021-11-11 11:59:30 +05:30
// Enumerate all the role tags.
2016-03-05 23:28:51 +05:30
const (
2021-11-11 11:59:30 +05:30
RoleDescriptorNone RoleDescriptor = iota
RoleDescriptorPoster
RoleDescriptorWriter
RoleDescriptorOwner
2016-03-05 23:28:51 +05:30
)
2021-11-11 11:59:30 +05:30
// WithRole enable a specific tag on the RoleDescriptor.
func ( rd RoleDescriptor ) WithRole ( role RoleDescriptor ) RoleDescriptor {
2021-12-26 20:26:14 +05:30
return rd | ( 1 << role )
2021-11-11 11:59:30 +05:30
}
func stringToRoleDescriptor ( role string ) RoleDescriptor {
switch role {
case "Poster" :
return RoleDescriptorPoster
case "Writer" :
return RoleDescriptorWriter
case "Owner" :
return RoleDescriptorOwner
default :
return RoleDescriptorNone
}
}
// HasRole returns if a certain role is enabled on the RoleDescriptor.
func ( rd RoleDescriptor ) HasRole ( role string ) bool {
roleDescriptor := stringToRoleDescriptor ( role )
bitValue := rd & ( 1 << roleDescriptor )
return ( bitValue > 0 )
}
2016-03-05 23:28:51 +05:30
// Comment represents a comment in commit and issue page.
type Comment struct {
2021-11-24 15:19:20 +05:30
ID int64 ` xorm:"pk autoincr" `
Type CommentType ` xorm:"INDEX" `
PosterID int64 ` xorm:"INDEX" `
Poster * user_model . User ` xorm:"-" `
2019-07-08 07:44:12 +05:30
OriginalAuthor string
OriginalAuthorID int64
2018-07-18 02:53:58 +05:30
IssueID int64 ` xorm:"INDEX" `
Issue * Issue ` xorm:"-" `
LabelID int64
2020-10-26 03:19:48 +05:30
Label * Label ` xorm:"-" `
AddedLabels [ ] * Label ` xorm:"-" `
RemovedLabels [ ] * Label ` xorm:"-" `
2020-08-17 08:37:38 +05:30
OldProjectID int64
ProjectID int64
2022-03-29 19:46:31 +05:30
OldProject * project_model . Project ` xorm:"-" `
Project * project_model . Project ` xorm:"-" `
2018-07-18 02:53:58 +05:30
OldMilestoneID int64
MilestoneID int64
2022-06-13 15:07:59 +05:30
OldMilestone * Milestone ` xorm:"-" `
Milestone * Milestone ` xorm:"-" `
2021-02-19 16:22:11 +05:30
TimeID int64
Time * TrackedTime ` xorm:"-" `
2018-07-18 02:53:58 +05:30
AssigneeID int64
RemovedAssignee bool
2022-03-29 11:59:02 +05:30
Assignee * user_model . User ` xorm:"-" `
AssigneeTeamID int64 ` xorm:"NOT NULL DEFAULT 0" `
AssigneeTeam * organization . Team ` xorm:"-" `
2020-04-18 19:20:25 +05:30
ResolveDoerID int64
2021-11-24 15:19:20 +05:30
ResolveDoer * user_model . User ` xorm:"-" `
2018-07-18 02:53:58 +05:30
OldTitle string
NewTitle string
2019-12-16 11:50:25 +05:30
OldRef string
NewRef string
2018-07-18 02:53:58 +05:30
DependentIssueID int64
DependentIssue * Issue ` xorm:"-" `
2017-02-03 20:39:10 +05:30
2017-02-01 08:06:08 +05:30
CommitID int64
2018-08-06 10:13:22 +05:30
Line int64 // - previous line / + proposed line
TreePath string
2021-08-22 21:03:05 +05:30
Content string ` xorm:"LONGTEXT" `
2016-03-10 06:23:30 +05:30
RenderedContent string ` xorm:"-" `
2018-08-06 10:13:22 +05:30
// Path represents the 4 lines of code cemented by this comment
2020-06-18 19:37:09 +05:30
Patch string ` xorm:"-" `
2021-08-22 21:03:05 +05:30
PatchQuoted string ` xorm:"LONGTEXT patch" `
2018-08-06 10:13:22 +05:30
2019-08-15 20:16:21 +05:30
CreatedUnix timeutil . TimeStamp ` xorm:"INDEX created" `
UpdatedUnix timeutil . TimeStamp ` xorm:"INDEX updated" `
2016-03-05 23:28:51 +05:30
// Reference issue in commit message
CommitSHA string ` xorm:"VARCHAR(40)" `
2022-06-13 15:07:59 +05:30
Attachments [ ] * repo_model . Attachment ` xorm:"-" `
Reactions ReactionList ` xorm:"-" `
2016-03-05 23:28:51 +05:30
// For view issue page.
2021-11-11 11:59:30 +05:30
ShowRole RoleDescriptor ` xorm:"-" `
2018-08-06 10:13:22 +05:30
Review * Review ` xorm:"-" `
2019-08-05 19:59:40 +05:30
ReviewID int64 ` xorm:"index" `
2018-08-06 10:13:22 +05:30
Invalidated bool
2019-09-20 11:15:38 +05:30
// Reference an issue or pull from another comment, issue or PR
// All information is about the origin of the reference
2019-10-14 03:59:10 +05:30
RefRepoID int64 ` xorm:"index" ` // Repo where the referencing
RefIssueID int64 ` xorm:"index" `
RefCommentID int64 ` xorm:"index" ` // 0 if origin is Issue title or content (or PR's)
2021-07-08 17:08:13 +05:30
RefAction references . XRefAction ` xorm:"SMALLINT" ` // What happens if RefIssueID resolves
2019-09-20 11:15:38 +05:30
RefIsPull bool
2021-12-10 06:57:50 +05:30
RefRepo * repo_model . Repository ` xorm:"-" `
RefIssue * Issue ` xorm:"-" `
RefComment * Comment ` xorm:"-" `
2020-05-20 18:17:24 +05:30
2022-06-12 21:21:54 +05:30
Commits [ ] * git_model . SignCommitWithStatuses ` xorm:"-" `
OldCommit string ` xorm:"-" `
NewCommit string ` xorm:"-" `
CommitsNum int64 ` xorm:"-" `
IsForcePush bool ` xorm:"-" `
2020-05-20 18:17:24 +05:30
}
2021-09-19 17:19:59 +05:30
func init ( ) {
db . RegisterModel ( new ( Comment ) )
}
2020-05-20 18:17:24 +05:30
// PushActionContent is content of push pull comment
type PushActionContent struct {
IsForcePush bool ` json:"is_force_push" `
CommitIDs [ ] string ` json:"commit_ids" `
2016-03-05 23:28:51 +05:30
}
2018-05-16 19:31:55 +05:30
// LoadIssue loads issue from database
func ( c * Comment ) LoadIssue ( ) ( err error ) {
2022-05-04 01:16:28 +05:30
return c . LoadIssueCtx ( db . DefaultContext )
2019-09-20 11:15:38 +05:30
}
2022-05-04 01:16:28 +05:30
// LoadIssueCtx loads issue from database
func ( c * Comment ) LoadIssueCtx ( ctx context . Context ) ( err error ) {
2018-05-16 19:31:55 +05:30
if c . Issue != nil {
return nil
}
2022-06-13 15:07:59 +05:30
c . Issue , err = GetIssueByID ( ctx , c . IssueID )
2022-06-20 15:32:49 +05:30
return err
2018-05-16 19:31:55 +05:30
}
2020-06-18 19:37:09 +05:30
// BeforeInsert will be invoked by XORM before inserting a record
func ( c * Comment ) BeforeInsert ( ) {
c . PatchQuoted = c . Patch
if ! utf8 . ValidString ( c . Patch ) {
c . PatchQuoted = strconv . Quote ( c . Patch )
}
}
// BeforeUpdate will be invoked by XORM before updating a record
func ( c * Comment ) BeforeUpdate ( ) {
c . PatchQuoted = c . Patch
if ! utf8 . ValidString ( c . Patch ) {
c . PatchQuoted = strconv . Quote ( c . Patch )
}
}
// AfterLoad is invoked from XORM after setting the values of all fields of this object.
func ( c * Comment ) AfterLoad ( session * xorm . Session ) {
c . Patch = c . PatchQuoted
if len ( c . PatchQuoted ) > 0 && c . PatchQuoted [ 0 ] == '"' {
unquoted , err := strconv . Unquote ( c . PatchQuoted )
if err == nil {
c . Patch = unquoted
}
}
}
2022-05-20 19:38:52 +05:30
func ( c * Comment ) loadPoster ( ctx context . Context ) ( err error ) {
2019-09-24 23:09:50 +05:30
if c . PosterID <= 0 || c . Poster != nil {
2019-04-18 10:30:03 +05:30
return nil
}
2022-05-20 19:38:52 +05:30
c . Poster , err = user_model . GetUserByIDCtx ( ctx , c . PosterID )
2019-04-18 10:30:03 +05:30
if err != nil {
2021-11-24 15:19:20 +05:30
if user_model . IsErrUserNotExist ( err ) {
2019-04-18 10:30:03 +05:30
c . PosterID = - 1
2021-11-24 15:19:20 +05:30
c . Poster = user_model . NewGhostUser ( )
2019-04-18 10:30:03 +05:30
} else {
log . Error ( "getUserByID[%d]: %v" , c . ID , err )
}
}
return err
}
2016-11-28 19:03:09 +05:30
// AfterDelete is invoked from XORM after the object is deleted.
2016-03-05 23:28:51 +05:30
func ( c * Comment ) AfterDelete ( ) {
2018-06-12 04:24:30 +05:30
if c . ID <= 0 {
return
}
2021-11-19 19:09:57 +05:30
_ , err := repo_model . DeleteAttachmentsByComment ( c . ID , true )
2016-03-05 23:28:51 +05:30
if err != nil {
log . Info ( "Could not delete files for comment %d on issue #%d: %s" , c . ID , c . IssueID , err )
}
}
2016-12-22 13:59:26 +05:30
// HTMLURL formats a URL-string to the issue-comment
func ( c * Comment ) HTMLURL ( ) string {
2018-05-16 19:31:55 +05:30
err := c . LoadIssue ( )
2016-12-22 13:59:26 +05:30
if err != nil { // Silently dropping errors :unamused:
2019-04-02 13:18:31 +05:30
log . Error ( "LoadIssue(%d): %v" , c . IssueID , err )
2016-12-22 13:59:26 +05:30
return ""
}
2022-04-08 14:41:15 +05:30
err = c . Issue . LoadRepo ( db . DefaultContext )
2018-12-13 21:25:43 +05:30
if err != nil { // Silently dropping errors :unamused:
2019-04-02 13:18:31 +05:30
log . Error ( "loadRepo(%d): %v" , c . Issue . RepoID , err )
2018-12-13 21:25:43 +05:30
return ""
}
2018-08-06 10:13:22 +05:30
if c . Type == CommentTypeCode {
if c . ReviewID == 0 {
return fmt . Sprintf ( "%s/files#%s" , c . Issue . HTMLURL ( ) , c . HashTag ( ) )
}
if c . Review == nil {
if err := c . LoadReview ( ) ; err != nil {
log . Warn ( "LoadReview(%d): %v" , c . ReviewID , err )
return fmt . Sprintf ( "%s/files#%s" , c . Issue . HTMLURL ( ) , c . HashTag ( ) )
}
}
if c . Review . Type <= ReviewTypePending {
return fmt . Sprintf ( "%s/files#%s" , c . Issue . HTMLURL ( ) , c . HashTag ( ) )
}
}
2018-05-16 19:31:55 +05:30
return fmt . Sprintf ( "%s#%s" , c . Issue . HTMLURL ( ) , c . HashTag ( ) )
2016-12-22 13:59:26 +05:30
}
2020-01-09 17:26:32 +05:30
// APIURL formats a API-string to the issue-comment
func ( c * Comment ) APIURL ( ) string {
err := c . LoadIssue ( )
if err != nil { // Silently dropping errors :unamused:
log . Error ( "LoadIssue(%d): %v" , c . IssueID , err )
return ""
}
2022-04-08 14:41:15 +05:30
err = c . Issue . LoadRepo ( db . DefaultContext )
2020-01-09 17:26:32 +05:30
if err != nil { // Silently dropping errors :unamused:
log . Error ( "loadRepo(%d): %v" , c . Issue . RepoID , err )
return ""
}
2020-01-14 21:07:19 +05:30
return fmt . Sprintf ( "%s/issues/comments/%d" , c . Issue . Repo . APIURL ( ) , c . ID )
2020-01-09 17:26:32 +05:30
}
2016-12-22 13:59:26 +05:30
// IssueURL formats a URL-string to the issue
func ( c * Comment ) IssueURL ( ) string {
2018-05-16 19:31:55 +05:30
err := c . LoadIssue ( )
2016-12-22 13:59:26 +05:30
if err != nil { // Silently dropping errors :unamused:
2019-04-02 13:18:31 +05:30
log . Error ( "LoadIssue(%d): %v" , c . IssueID , err )
2016-12-22 13:59:26 +05:30
return ""
}
2018-05-16 19:31:55 +05:30
if c . Issue . IsPull {
2016-12-22 13:59:26 +05:30
return ""
}
2018-12-13 21:25:43 +05:30
2022-04-08 14:41:15 +05:30
err = c . Issue . LoadRepo ( db . DefaultContext )
2018-12-13 21:25:43 +05:30
if err != nil { // Silently dropping errors :unamused:
2019-04-02 13:18:31 +05:30
log . Error ( "loadRepo(%d): %v" , c . Issue . RepoID , err )
2018-12-13 21:25:43 +05:30
return ""
}
2018-05-16 19:31:55 +05:30
return c . Issue . HTMLURL ( )
2016-12-22 13:59:26 +05:30
}
// PRURL formats a URL-string to the pull-request
func ( c * Comment ) PRURL ( ) string {
2018-05-16 19:31:55 +05:30
err := c . LoadIssue ( )
2016-12-22 13:59:26 +05:30
if err != nil { // Silently dropping errors :unamused:
2019-04-02 13:18:31 +05:30
log . Error ( "LoadIssue(%d): %v" , c . IssueID , err )
2016-12-22 13:59:26 +05:30
return ""
}
2022-04-08 14:41:15 +05:30
err = c . Issue . LoadRepo ( db . DefaultContext )
2018-12-13 21:25:43 +05:30
if err != nil { // Silently dropping errors :unamused:
2019-04-02 13:18:31 +05:30
log . Error ( "loadRepo(%d): %v" , c . Issue . RepoID , err )
2018-12-13 21:25:43 +05:30
return ""
}
2018-05-16 19:31:55 +05:30
if ! c . Issue . IsPull {
2016-12-22 13:59:26 +05:30
return ""
}
2018-05-16 19:31:55 +05:30
return c . Issue . HTMLURL ( )
2016-12-22 13:59:26 +05:30
}
2018-05-16 19:31:55 +05:30
// CommentHashTag returns unique hash tag for comment id.
func CommentHashTag ( id int64 ) string {
return fmt . Sprintf ( "issuecomment-%d" , id )
}
2016-03-05 23:28:51 +05:30
// HashTag returns unique hash tag for comment.
func ( c * Comment ) HashTag ( ) string {
2018-05-16 19:31:55 +05:30
return CommentHashTag ( c . ID )
2016-03-05 23:28:51 +05:30
}
// EventTag returns unique event hash tag for comment.
func ( c * Comment ) EventTag ( ) string {
2020-12-25 15:29:32 +05:30
return fmt . Sprintf ( "event-%d" , c . ID )
2016-03-05 23:28:51 +05:30
}
2017-01-30 18:16:45 +05:30
// LoadLabel if comment.Type is CommentTypeLabel, then load Label
func ( c * Comment ) LoadLabel ( ) error {
var label Label
2021-09-23 21:15:36 +05:30
has , err := db . GetEngine ( db . DefaultContext ) . ID ( c . LabelID ) . Get ( & label )
2017-01-30 18:16:45 +05:30
if err != nil {
return err
2017-02-11 18:26:57 +05:30
} else if has {
c . Label = & label
} else {
// Ignore Label is deleted, but not clear this table
log . Warn ( "Commit %d cannot load label %d" , c . ID , c . LabelID )
2017-01-30 18:16:45 +05:30
}
2017-02-11 18:26:57 +05:30
2017-01-30 18:16:45 +05:30
return nil
}
2020-08-17 08:37:38 +05:30
// LoadProject if comment.Type is CommentTypeProject, then load project.
func ( c * Comment ) LoadProject ( ) error {
if c . OldProjectID > 0 {
2022-03-29 19:46:31 +05:30
var oldProject project_model . Project
2021-09-23 21:15:36 +05:30
has , err := db . GetEngine ( db . DefaultContext ) . ID ( c . OldProjectID ) . Get ( & oldProject )
2020-08-17 08:37:38 +05:30
if err != nil {
return err
} else if has {
c . OldProject = & oldProject
}
}
if c . ProjectID > 0 {
2022-03-29 19:46:31 +05:30
var project project_model . Project
2021-09-23 21:15:36 +05:30
has , err := db . GetEngine ( db . DefaultContext ) . ID ( c . ProjectID ) . Get ( & project )
2020-08-17 08:37:38 +05:30
if err != nil {
return err
} else if has {
c . Project = & project
}
}
return nil
}
2017-02-01 08:06:08 +05:30
// LoadMilestone if comment.Type is CommentTypeMilestone, then load milestone
func ( c * Comment ) LoadMilestone ( ) error {
if c . OldMilestoneID > 0 {
2022-06-13 15:07:59 +05:30
var oldMilestone Milestone
2021-09-23 21:15:36 +05:30
has , err := db . GetEngine ( db . DefaultContext ) . ID ( c . OldMilestoneID ) . Get ( & oldMilestone )
2017-02-01 08:06:08 +05:30
if err != nil {
return err
2017-06-17 10:21:28 +05:30
} else if has {
c . OldMilestone = & oldMilestone
2017-02-01 08:06:08 +05:30
}
}
if c . MilestoneID > 0 {
2022-06-13 15:07:59 +05:30
var milestone Milestone
2021-09-23 21:15:36 +05:30
has , err := db . GetEngine ( db . DefaultContext ) . ID ( c . MilestoneID ) . Get ( & milestone )
2017-02-01 08:06:08 +05:30
if err != nil {
return err
2017-06-17 10:21:28 +05:30
} else if has {
c . Milestone = & milestone
2017-02-01 08:06:08 +05:30
}
}
return nil
}
2018-12-13 21:25:43 +05:30
// LoadPoster loads comment poster
func ( c * Comment ) LoadPoster ( ) error {
2022-05-20 19:38:52 +05:30
return c . loadPoster ( db . DefaultContext )
2018-12-13 21:25:43 +05:30
}
2022-01-18 22:58:38 +05:30
// LoadAttachments loads attachments (it never returns error, the error during `GetAttachmentsByCommentIDCtx` is ignored)
2018-12-13 21:25:43 +05:30
func ( c * Comment ) LoadAttachments ( ) error {
if len ( c . Attachments ) > 0 {
return nil
}
var err error
2022-05-20 19:38:52 +05:30
c . Attachments , err = repo_model . GetAttachmentsByCommentID ( db . DefaultContext , c . ID )
2018-12-13 21:25:43 +05:30
if err != nil {
2019-04-02 13:18:31 +05:30
log . Error ( "getAttachmentsByCommentID[%d]: %v" , c . ID , err )
2018-12-13 21:25:43 +05:30
}
return nil
}
2019-10-15 17:49:32 +05:30
// UpdateAttachments update attachments by UUIDs for the comment
func ( c * Comment ) UpdateAttachments ( uuids [ ] string ) error {
2021-11-19 19:09:57 +05:30
ctx , committer , err := db . TxContext ( )
if err != nil {
2019-10-15 17:49:32 +05:30
return err
}
2021-11-19 19:09:57 +05:30
defer committer . Close ( )
attachments , err := repo_model . GetAttachmentsByUUIDs ( ctx , uuids )
2019-10-15 17:49:32 +05:30
if err != nil {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "getAttachmentsByUUIDs [uuids: %v]: %w" , uuids , err )
2019-10-15 17:49:32 +05:30
}
for i := 0 ; i < len ( attachments ) ; i ++ {
attachments [ i ] . IssueID = c . IssueID
attachments [ i ] . CommentID = c . ID
2022-05-20 19:38:52 +05:30
if err := repo_model . UpdateAttachment ( ctx , attachments [ i ] ) ; err != nil {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "update attachment [id: %d]: %w" , attachments [ i ] . ID , err )
2019-10-15 17:49:32 +05:30
}
}
2021-11-19 19:09:57 +05:30
return committer . Commit ( )
2019-10-15 17:49:32 +05:30
}
2020-10-13 01:25:13 +05:30
// LoadAssigneeUserAndTeam if comment.Type is CommentTypeAssignees, then load assignees
func ( c * Comment ) LoadAssigneeUserAndTeam ( ) error {
2017-02-03 20:39:10 +05:30
var err error
2020-10-13 01:25:13 +05:30
if c . AssigneeID > 0 && c . Assignee == nil {
2021-11-24 15:19:20 +05:30
c . Assignee , err = user_model . GetUserByIDCtx ( db . DefaultContext , c . AssigneeID )
2017-02-03 20:39:10 +05:30
if err != nil {
2021-11-24 15:19:20 +05:30
if ! user_model . IsErrUserNotExist ( err ) {
2018-01-07 14:43:10 +05:30
return err
}
2021-11-24 15:19:20 +05:30
c . Assignee = user_model . NewGhostUser ( )
2017-02-03 20:39:10 +05:30
}
2020-10-13 01:25:13 +05:30
} else if c . AssigneeTeamID > 0 && c . AssigneeTeam == nil {
if err = c . LoadIssue ( ) ; err != nil {
return err
}
2022-04-08 14:41:15 +05:30
if err = c . Issue . LoadRepo ( db . DefaultContext ) ; err != nil {
2020-10-13 01:25:13 +05:30
return err
}
2021-12-10 06:57:50 +05:30
if err = c . Issue . Repo . GetOwner ( db . DefaultContext ) ; err != nil {
2020-10-13 01:25:13 +05:30
return err
}
if c . Issue . Repo . Owner . IsOrganization ( ) {
2022-05-20 19:38:52 +05:30
c . AssigneeTeam , err = organization . GetTeamByID ( db . DefaultContext , c . AssigneeTeamID )
2022-03-29 11:59:02 +05:30
if err != nil && ! organization . IsErrTeamNotExist ( err ) {
2020-10-13 01:25:13 +05:30
return err
}
}
2017-02-03 20:39:10 +05:30
}
return nil
}
2020-04-18 19:20:25 +05:30
// LoadResolveDoer if comment.Type is CommentTypeCode and ResolveDoerID not zero, then load resolveDoer
func ( c * Comment ) LoadResolveDoer ( ) ( err error ) {
if c . ResolveDoerID == 0 || c . Type != CommentTypeCode {
return nil
}
2021-11-24 15:19:20 +05:30
c . ResolveDoer , err = user_model . GetUserByIDCtx ( db . DefaultContext , c . ResolveDoerID )
2020-04-18 19:20:25 +05:30
if err != nil {
2021-11-24 15:19:20 +05:30
if user_model . IsErrUserNotExist ( err ) {
c . ResolveDoer = user_model . NewGhostUser ( )
2020-04-18 19:20:25 +05:30
err = nil
}
}
2022-06-20 15:32:49 +05:30
return err
2020-04-18 19:20:25 +05:30
}
// IsResolved check if an code comment is resolved
func ( c * Comment ) IsResolved ( ) bool {
return c . ResolveDoerID != 0 && c . Type == CommentTypeCode
}
2018-07-18 02:53:58 +05:30
// LoadDepIssueDetails loads Dependent Issue Details
func ( c * Comment ) LoadDepIssueDetails ( ) ( err error ) {
if c . DependentIssueID <= 0 || c . DependentIssue != nil {
return nil
}
2022-06-13 15:07:59 +05:30
c . DependentIssue , err = GetIssueByID ( db . DefaultContext , c . DependentIssueID )
2018-07-18 02:53:58 +05:30
return err
}
2021-02-19 16:22:11 +05:30
// LoadTime loads the associated time for a CommentTypeAddTimeManual
func ( c * Comment ) LoadTime ( ) error {
if c . Time != nil || c . TimeID == 0 {
return nil
}
var err error
c . Time , err = GetTrackedTimeByID ( c . TimeID )
return err
}
2022-03-31 14:50:39 +05:30
func ( c * Comment ) loadReactions ( ctx context . Context , repo * repo_model . Repository ) ( err error ) {
2017-12-04 04:44:26 +05:30
if c . Reactions != nil {
return nil
}
2022-06-13 15:07:59 +05:30
c . Reactions , _ , err = FindReactions ( ctx , FindReactionsOptions {
2017-12-04 04:44:26 +05:30
IssueID : c . IssueID ,
CommentID : c . ID ,
} )
if err != nil {
return err
}
// Load reaction user data
2022-03-31 14:50:39 +05:30
if _ , err := c . Reactions . LoadUsers ( ctx , repo ) ; err != nil {
2017-12-04 04:44:26 +05:30
return err
}
return nil
}
// LoadReactions loads comment reactions
2021-12-10 06:57:50 +05:30
func ( c * Comment ) LoadReactions ( repo * repo_model . Repository ) error {
2022-03-31 14:50:39 +05:30
return c . loadReactions ( db . DefaultContext , repo )
2017-12-04 04:44:26 +05:30
}
2022-05-20 19:38:52 +05:30
func ( c * Comment ) loadReview ( ctx context . Context ) ( err error ) {
2018-12-13 21:25:43 +05:30
if c . Review == nil {
2022-05-20 19:38:52 +05:30
if c . Review , err = GetReviewByID ( ctx , c . ReviewID ) ; err != nil {
2018-12-13 21:25:43 +05:30
return err
}
2018-08-06 10:13:22 +05:30
}
2019-05-06 17:39:31 +05:30
c . Review . Issue = c . Issue
2018-08-06 10:13:22 +05:30
return nil
}
// LoadReview loads the associated review
func ( c * Comment ) LoadReview ( ) error {
2022-05-20 19:38:52 +05:30
return c . loadReview ( db . DefaultContext )
2018-08-06 10:13:22 +05:30
}
2020-06-14 19:25:20 +05:30
var notEnoughLines = regexp . MustCompile ( ` fatal: file .* has only \d+ lines? ` )
2021-11-24 15:19:20 +05:30
func ( c * Comment ) checkInvalidation ( doer * user_model . User , repo * git . Repository , branch string ) error {
2018-08-06 10:13:22 +05:30
// FIXME differentiate between previous and proposed line
commit , err := repo . LineBlame ( branch , repo . Path , c . TreePath , uint ( c . UnsignedLine ( ) ) )
2020-06-14 19:25:20 +05:30
if err != nil && ( strings . Contains ( err . Error ( ) , "fatal: no such path" ) || notEnoughLines . MatchString ( err . Error ( ) ) ) {
2019-10-31 23:29:36 +05:30
c . Invalidated = true
return UpdateComment ( c , doer )
}
2018-08-06 10:13:22 +05:30
if err != nil {
return err
}
2018-09-17 20:29:49 +05:30
if c . CommitSHA != "" && c . CommitSHA != commit . ID . String ( ) {
2018-08-06 10:13:22 +05:30
c . Invalidated = true
2019-09-24 23:09:50 +05:30
return UpdateComment ( c , doer )
2018-08-06 10:13:22 +05:30
}
return nil
}
// CheckInvalidation checks if the line of code comment got changed by another commit.
// If the line got changed the comment is going to be invalidated.
2021-11-24 15:19:20 +05:30
func ( c * Comment ) CheckInvalidation ( repo * git . Repository , doer * user_model . User , branch string ) error {
2019-06-13 01:11:28 +05:30
return c . checkInvalidation ( doer , repo , branch )
2018-08-06 10:13:22 +05:30
}
// DiffSide returns "previous" if Comment.Line is a LOC of the previous changes and "proposed" if it is a LOC of the proposed changes.
func ( c * Comment ) DiffSide ( ) string {
if c . Line < 0 {
return "previous"
}
return "proposed"
}
// UnsignedLine returns the LOC of the code comment without + or -
func ( c * Comment ) UnsignedLine ( ) uint64 {
if c . Line < 0 {
return uint64 ( c . Line * - 1 )
}
return uint64 ( c . Line )
}
// CodeCommentURL returns the url to a comment in code
func ( c * Comment ) CodeCommentURL ( ) string {
err := c . LoadIssue ( )
if err != nil { // Silently dropping errors :unamused:
2019-04-02 13:18:31 +05:30
log . Error ( "LoadIssue(%d): %v" , c . IssueID , err )
2018-08-06 10:13:22 +05:30
return ""
}
2022-04-08 14:41:15 +05:30
err = c . Issue . LoadRepo ( db . DefaultContext )
2018-12-13 21:25:43 +05:30
if err != nil { // Silently dropping errors :unamused:
2019-04-02 13:18:31 +05:30
log . Error ( "loadRepo(%d): %v" , c . Issue . RepoID , err )
2018-12-13 21:25:43 +05:30
return ""
}
2018-08-06 10:13:22 +05:30
return fmt . Sprintf ( "%s/files#%s" , c . Issue . HTMLURL ( ) , c . HashTag ( ) )
}
2020-05-20 18:17:24 +05:30
// LoadPushCommits Load push commits
2022-01-20 04:56:57 +05:30
func ( c * Comment ) LoadPushCommits ( ctx context . Context ) ( err error ) {
2022-01-21 23:29:26 +05:30
if c . Content == "" || c . Commits != nil || c . Type != CommentTypePullRequestPush {
2020-05-20 18:17:24 +05:30
return nil
}
var data PushActionContent
err = json . Unmarshal ( [ ] byte ( c . Content ) , & data )
if err != nil {
return
}
c . IsForcePush = data . IsForcePush
if c . IsForcePush {
if len ( data . CommitIDs ) != 2 {
return nil
}
c . OldCommit = data . CommitIDs [ 0 ]
c . NewCommit = data . CommitIDs [ 1 ]
} else {
repoPath := c . Issue . Repo . RepoPath ( )
2022-01-20 04:56:57 +05:30
gitRepo , closer , err := git . RepositoryFromContextOrOpen ( ctx , repoPath )
2020-05-20 18:17:24 +05:30
if err != nil {
return err
}
2022-01-20 04:56:57 +05:30
defer closer . Close ( )
2020-05-20 18:17:24 +05:30
2022-06-12 21:21:54 +05:30
c . Commits = git_model . ConvertFromGitCommit ( gitRepo . GetCommitsFromIDs ( data . CommitIDs ) , c . Issue . Repo )
2021-08-09 23:38:51 +05:30
c . CommitsNum = int64 ( len ( c . Commits ) )
2020-05-20 18:17:24 +05:30
}
return err
}
2022-04-08 14:41:15 +05:30
// CreateCommentCtx creates comment with context
func CreateCommentCtx ( ctx context . Context , opts * CreateCommentOptions ) ( _ * Comment , err error ) {
2021-11-19 19:09:57 +05:30
e := db . GetEngine ( ctx )
2017-01-30 18:16:45 +05:30
var LabelID int64
if opts . Label != nil {
LabelID = opts . Label . ID
}
2018-07-18 02:53:58 +05:30
2016-03-05 23:28:51 +05:30
comment := & Comment {
2018-07-18 02:53:58 +05:30
Type : opts . Type ,
PosterID : opts . Doer . ID ,
Poster : opts . Doer ,
IssueID : opts . Issue . ID ,
LabelID : LabelID ,
OldMilestoneID : opts . OldMilestoneID ,
MilestoneID : opts . MilestoneID ,
2020-08-17 08:37:38 +05:30
OldProjectID : opts . OldProjectID ,
ProjectID : opts . ProjectID ,
2021-02-19 16:22:11 +05:30
TimeID : opts . TimeID ,
2018-07-18 02:53:58 +05:30
RemovedAssignee : opts . RemovedAssignee ,
AssigneeID : opts . AssigneeID ,
2020-10-13 01:25:13 +05:30
AssigneeTeamID : opts . AssigneeTeamID ,
2018-07-18 02:53:58 +05:30
CommitID : opts . CommitID ,
CommitSHA : opts . CommitSHA ,
Line : opts . LineNum ,
Content : opts . Content ,
OldTitle : opts . OldTitle ,
NewTitle : opts . NewTitle ,
2019-12-16 11:50:25 +05:30
OldRef : opts . OldRef ,
NewRef : opts . NewRef ,
2018-07-18 02:53:58 +05:30
DependentIssueID : opts . DependentIssueID ,
2018-08-06 10:13:22 +05:30
TreePath : opts . TreePath ,
ReviewID : opts . ReviewID ,
Patch : opts . Patch ,
2019-09-20 11:15:38 +05:30
RefRepoID : opts . RefRepoID ,
RefIssueID : opts . RefIssueID ,
RefCommentID : opts . RefCommentID ,
RefAction : opts . RefAction ,
RefIsPull : opts . RefIsPull ,
2020-05-20 18:17:24 +05:30
IsForcePush : opts . IsForcePush ,
2020-11-09 11:45:09 +05:30
Invalidated : opts . Invalidated ,
2016-03-05 23:28:51 +05:30
}
if _ , err = e . Insert ( comment ) ; err != nil {
return nil , err
}
2021-12-10 06:57:50 +05:30
if err = opts . Repo . GetOwner ( ctx ) ; err != nil {
2017-01-30 18:16:45 +05:30
return nil , err
}
2021-11-19 19:09:57 +05:30
if err = updateCommentInfos ( ctx , opts , comment ) ; err != nil {
2019-11-06 19:09:29 +05:30
return nil , err
}
2022-06-13 15:07:59 +05:30
if err = comment . AddCrossReferences ( ctx , opts . Doer , false ) ; err != nil {
2019-09-20 11:15:38 +05:30
return nil , err
}
2018-08-06 10:13:22 +05:30
return comment , nil
}
2021-11-19 19:09:57 +05:30
func updateCommentInfos ( ctx context . Context , opts * CreateCommentOptions , comment * Comment ) ( err error ) {
2016-03-05 23:28:51 +05:30
// Check comment type.
switch opts . Type {
2018-08-06 10:13:22 +05:30
case CommentTypeCode :
if comment . ReviewID != 0 {
if comment . Review == nil {
2022-05-20 19:38:52 +05:30
if err := comment . loadReview ( ctx ) ; err != nil {
2018-08-06 10:13:22 +05:30
return err
}
}
if comment . Review . Type <= ReviewTypePending {
return nil
}
}
fallthrough
2016-11-07 22:00:04 +05:30
case CommentTypeComment :
2022-05-20 19:38:52 +05:30
if _ , err = db . Exec ( ctx , "UPDATE `issue` SET num_comments=num_comments+1 WHERE id=?" , opts . Issue . ID ) ; err != nil {
2018-08-06 10:13:22 +05:30
return err
2016-03-05 23:28:51 +05:30
}
2022-01-13 22:20:43 +05:30
fallthrough
case CommentTypeReview :
2016-03-05 23:28:51 +05:30
// Check attachments
2021-11-19 19:09:57 +05:30
attachments , err := repo_model . GetAttachmentsByUUIDs ( ctx , opts . Attachments )
2019-12-11 05:31:52 +05:30
if err != nil {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "getAttachmentsByUUIDs [uuids: %v]: %w" , opts . Attachments , err )
2016-03-05 23:28:51 +05:30
}
for i := range attachments {
attachments [ i ] . IssueID = opts . Issue . ID
attachments [ i ] . CommentID = comment . ID
// No assign value could be 0, so ignore AllCols().
2022-05-20 19:38:52 +05:30
if _ , err = db . GetEngine ( ctx ) . ID ( attachments [ i ] . ID ) . Update ( attachments [ i ] ) ; err != nil {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "update attachment [%d]: %w" , attachments [ i ] . ID , err )
2016-03-05 23:28:51 +05:30
}
}
2019-11-06 19:09:29 +05:30
case CommentTypeReopen , CommentTypeClose :
2022-10-25 18:17:46 +05:30
if err = repo_model . UpdateRepoIssueNumbers ( ctx , opts . Issue . RepoID , opts . Issue . IsPull , true ) ; err != nil {
2019-11-06 19:09:29 +05:30
return err
}
}
// update the issue's updated_unix column
2022-04-08 14:41:15 +05:30
return UpdateIssueCols ( ctx , opts . Issue , "updated_unix" )
2019-11-06 19:09:29 +05:30
}
2016-03-05 23:28:51 +05:30
2021-11-24 15:19:20 +05:30
func createDeadlineComment ( ctx context . Context , doer * user_model . User , issue * Issue , newDeadlineUnix timeutil . TimeStamp ) ( * Comment , error ) {
2018-05-02 00:35:28 +05:30
var content string
var commentType CommentType
// newDeadline = 0 means deleting
if newDeadlineUnix == 0 {
commentType = CommentTypeRemovedDeadline
content = issue . DeadlineUnix . Format ( "2006-01-02" )
} else if issue . DeadlineUnix == 0 {
// Check if the new date was added or modified
// If the actual deadline is 0 => deadline added
commentType = CommentTypeAddedDeadline
content = newDeadlineUnix . Format ( "2006-01-02" )
} else { // Otherwise modified
commentType = CommentTypeModifiedDeadline
content = newDeadlineUnix . Format ( "2006-01-02" ) + "|" + issue . DeadlineUnix . Format ( "2006-01-02" )
}
2022-04-08 14:41:15 +05:30
if err := issue . LoadRepo ( ctx ) ; err != nil {
2018-12-27 20:32:43 +05:30
return nil , err
}
2021-03-15 00:22:12 +05:30
opts := & CreateCommentOptions {
2018-05-02 00:35:28 +05:30
Type : commentType ,
Doer : doer ,
Repo : issue . Repo ,
Issue : issue ,
Content : content ,
2019-12-01 08:14:39 +05:30
}
2022-04-08 14:41:15 +05:30
comment , err := CreateCommentCtx ( ctx , opts )
2019-12-01 08:14:39 +05:30
if err != nil {
return nil , err
}
2019-12-02 20:13:39 +05:30
return comment , nil
2018-05-02 00:35:28 +05:30
}
2018-07-18 02:53:58 +05:30
// Creates issue dependency comment
2021-11-24 15:19:20 +05:30
func createIssueDependencyComment ( ctx context . Context , doer * user_model . User , issue , dependentIssue * Issue , add bool ) ( err error ) {
2018-07-18 02:53:58 +05:30
cType := CommentTypeAddDependency
if ! add {
cType = CommentTypeRemoveDependency
}
2022-04-08 14:41:15 +05:30
if err = issue . LoadRepo ( ctx ) ; err != nil {
2019-01-27 17:01:40 +05:30
return
}
2018-07-18 02:53:58 +05:30
// Make two comments, one in each issue
2021-03-15 00:22:12 +05:30
opts := & CreateCommentOptions {
2018-07-18 02:53:58 +05:30
Type : cType ,
Doer : doer ,
Repo : issue . Repo ,
Issue : issue ,
DependentIssueID : dependentIssue . ID ,
2019-12-01 08:14:39 +05:30
}
2022-04-08 14:41:15 +05:30
if _ , err = CreateCommentCtx ( ctx , opts ) ; err != nil {
2018-07-18 02:53:58 +05:30
return
}
2019-12-01 08:14:39 +05:30
opts = & CreateCommentOptions {
2018-07-18 02:53:58 +05:30
Type : cType ,
Doer : doer ,
Repo : issue . Repo ,
Issue : dependentIssue ,
DependentIssueID : issue . ID ,
2019-12-01 08:14:39 +05:30
}
2022-04-08 14:41:15 +05:30
_ , err = CreateCommentCtx ( ctx , opts )
2022-06-20 15:32:49 +05:30
return err
2018-07-18 02:53:58 +05:30
}
2016-11-28 19:03:09 +05:30
// CreateCommentOptions defines options for creating comment
2016-03-05 23:28:51 +05:30
type CreateCommentOptions struct {
Type CommentType
2021-11-24 15:19:20 +05:30
Doer * user_model . User
2021-12-10 06:57:50 +05:30
Repo * repo_model . Repository
2016-03-05 23:28:51 +05:30
Issue * Issue
2017-01-30 18:16:45 +05:30
Label * Label
2016-03-05 23:28:51 +05:30
2018-07-18 02:53:58 +05:30
DependentIssueID int64
OldMilestoneID int64
MilestoneID int64
2020-08-17 08:37:38 +05:30
OldProjectID int64
ProjectID int64
2021-02-19 16:22:11 +05:30
TimeID int64
2018-07-18 02:53:58 +05:30
AssigneeID int64
2020-10-13 01:25:13 +05:30
AssigneeTeamID int64
2018-07-18 02:53:58 +05:30
RemovedAssignee bool
OldTitle string
NewTitle string
2019-12-16 11:50:25 +05:30
OldRef string
NewRef string
2018-07-18 02:53:58 +05:30
CommitID int64
CommitSHA string
2018-08-06 10:13:22 +05:30
Patch string
2018-07-18 02:53:58 +05:30
LineNum int64
2018-08-06 10:13:22 +05:30
TreePath string
ReviewID int64
2018-07-18 02:53:58 +05:30
Content string
Attachments [ ] string // UUIDs of attachments
2019-09-20 11:15:38 +05:30
RefRepoID int64
RefIssueID int64
RefCommentID int64
2019-10-14 03:59:10 +05:30
RefAction references . XRefAction
2019-09-20 11:15:38 +05:30
RefIsPull bool
2020-05-20 18:17:24 +05:30
IsForcePush bool
2020-11-09 11:45:09 +05:30
Invalidated bool
2016-03-05 23:28:51 +05:30
}
// CreateComment creates comment of issue or commit.
func CreateComment ( opts * CreateCommentOptions ) ( comment * Comment , err error ) {
2021-11-19 19:09:57 +05:30
ctx , committer , err := db . TxContext ( )
if err != nil {
2016-03-05 23:28:51 +05:30
return nil , err
}
2021-11-19 19:09:57 +05:30
defer committer . Close ( )
2016-03-05 23:28:51 +05:30
2022-04-08 14:41:15 +05:30
comment , err = CreateCommentCtx ( ctx , opts )
2016-03-05 23:28:51 +05:30
if err != nil {
return nil , err
}
2021-11-19 19:09:57 +05:30
if err = committer . Commit ( ) ; err != nil {
2017-09-17 01:46:21 +05:30
return nil , err
}
return comment , nil
2016-03-05 23:28:51 +05:30
}
// CreateRefComment creates a commit reference comment to issue.
2021-12-10 06:57:50 +05:30
func CreateRefComment ( doer * user_model . User , repo * repo_model . Repository , issue * Issue , content , commitSHA string ) error {
2016-03-05 23:28:51 +05:30
if len ( commitSHA ) == 0 {
return fmt . Errorf ( "cannot create reference with empty commit SHA" )
}
// Check if same reference from same commit has already existed.
2021-09-23 21:15:36 +05:30
has , err := db . GetEngine ( db . DefaultContext ) . Get ( & Comment {
2016-11-07 22:05:34 +05:30
Type : CommentTypeCommitRef ,
2016-03-05 23:28:51 +05:30
IssueID : issue . ID ,
CommitSHA : commitSHA ,
} )
if err != nil {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "check reference comment: %w" , err )
2016-03-05 23:28:51 +05:30
} else if has {
return nil
}
_ , err = CreateComment ( & CreateCommentOptions {
2016-11-07 22:05:34 +05:30
Type : CommentTypeCommitRef ,
2016-03-05 23:28:51 +05:30
Doer : doer ,
Repo : repo ,
Issue : issue ,
CommitSHA : commitSHA ,
Content : content ,
} )
return err
}
// GetCommentByID returns the comment by given ID.
2022-05-20 19:38:52 +05:30
func GetCommentByID ( ctx context . Context , id int64 ) ( * Comment , error ) {
2016-03-05 23:28:51 +05:30
c := new ( Comment )
2022-05-20 19:38:52 +05:30
has , err := db . GetEngine ( ctx ) . ID ( id ) . Get ( c )
2016-03-05 23:28:51 +05:30
if err != nil {
return nil , err
} else if ! has {
2016-08-27 02:10:53 +05:30
return nil , ErrCommentNotExist { id , 0 }
2016-03-05 23:28:51 +05:30
}
return c , nil
}
2017-06-21 06:30:44 +05:30
// FindCommentsOptions describes the conditions to Find comments
type FindCommentsOptions struct {
2021-09-24 17:02:56 +05:30
db . ListOptions
2018-08-06 10:13:22 +05:30
RepoID int64
IssueID int64
ReviewID int64
Since int64
2020-01-13 21:32:24 +05:30
Before int64
2020-11-09 11:45:09 +05:30
Line int64
TreePath string
2018-08-06 10:13:22 +05:30
Type CommentType
2017-06-21 06:30:44 +05:30
}
func ( opts * FindCommentsOptions ) toConds ( ) builder . Cond {
2021-03-15 00:22:12 +05:30
cond := builder . NewCond ( )
2017-06-21 06:30:44 +05:30
if opts . RepoID > 0 {
cond = cond . And ( builder . Eq { "issue.repo_id" : opts . RepoID } )
2016-08-27 02:10:53 +05:30
}
2017-06-21 06:30:44 +05:30
if opts . IssueID > 0 {
cond = cond . And ( builder . Eq { "comment.issue_id" : opts . IssueID } )
}
2018-08-06 10:13:22 +05:30
if opts . ReviewID > 0 {
cond = cond . And ( builder . Eq { "comment.review_id" : opts . ReviewID } )
}
2017-06-21 06:30:44 +05:30
if opts . Since > 0 {
cond = cond . And ( builder . Gte { "comment.updated_unix" : opts . Since } )
}
2020-01-13 21:32:24 +05:30
if opts . Before > 0 {
cond = cond . And ( builder . Lte { "comment.updated_unix" : opts . Before } )
}
2017-06-21 06:30:44 +05:30
if opts . Type != CommentTypeUnknown {
cond = cond . And ( builder . Eq { "comment.type" : opts . Type } )
}
2021-01-09 03:19:55 +05:30
if opts . Line != 0 {
2020-11-09 11:45:09 +05:30
cond = cond . And ( builder . Eq { "comment.line" : opts . Line } )
}
if len ( opts . TreePath ) > 0 {
cond = cond . And ( builder . Eq { "comment.tree_path" : opts . TreePath } )
}
2017-06-21 06:30:44 +05:30
return cond
2016-08-27 02:10:53 +05:30
}
2022-05-20 19:38:52 +05:30
// FindComments returns all comments according options
func FindComments ( ctx context . Context , opts * FindCommentsOptions ) ( [ ] * Comment , error ) {
2016-12-22 13:59:26 +05:30
comments := make ( [ ] * Comment , 0 , 10 )
2022-05-20 19:38:52 +05:30
sess := db . GetEngine ( ctx ) . Where ( opts . toConds ( ) )
2017-06-21 06:30:44 +05:30
if opts . RepoID > 0 {
sess . Join ( "INNER" , "issue" , "issue.id = comment.issue_id" )
2016-12-22 13:59:26 +05:30
}
2020-01-25 00:30:29 +05:30
if opts . Page != 0 {
2021-09-24 17:02:56 +05:30
sess = db . SetSessionPagination ( sess , opts )
2020-01-25 00:30:29 +05:30
}
2020-11-09 11:45:09 +05:30
// WARNING: If you change this order you will need to fix createCodeComment
2017-06-21 06:30:44 +05:30
return comments , sess .
Asc ( "comment.created_unix" ) .
2017-11-03 08:41:42 +05:30
Asc ( "comment.id" ) .
2017-06-21 06:30:44 +05:30
Find ( & comments )
2016-12-22 13:59:26 +05:30
}
2021-08-12 18:13:08 +05:30
// CountComments count all comments according options by ignoring pagination
func CountComments ( opts * FindCommentsOptions ) ( int64 , error ) {
2021-09-23 21:15:36 +05:30
sess := db . GetEngine ( db . DefaultContext ) . Where ( opts . toConds ( ) )
2021-08-12 18:13:08 +05:30
if opts . RepoID > 0 {
sess . Join ( "INNER" , "issue" , "issue.id = comment.issue_id" )
}
return sess . Count ( & Comment { } )
}
2016-03-05 23:28:51 +05:30
// UpdateComment updates information of comment.
2021-11-24 15:19:20 +05:30
func UpdateComment ( c * Comment , doer * user_model . User ) error {
2021-11-19 19:09:57 +05:30
ctx , committer , err := db . TxContext ( )
if err != nil {
2017-09-17 01:46:21 +05:30
return err
}
2021-11-19 19:09:57 +05:30
defer committer . Close ( )
sess := db . GetEngine ( ctx )
2018-05-16 19:31:55 +05:30
2019-09-20 11:15:38 +05:30
if _ , err := sess . ID ( c . ID ) . AllCols ( ) . Update ( c ) ; err != nil {
2018-12-13 21:25:43 +05:30
return err
}
2022-05-04 01:16:28 +05:30
if err := c . LoadIssueCtx ( ctx ) ; err != nil {
2018-05-16 19:31:55 +05:30
return err
}
2022-06-13 15:07:59 +05:30
if err := c . AddCrossReferences ( ctx , doer , true ) ; err != nil {
2019-09-20 11:15:38 +05:30
return err
}
2021-11-19 19:09:57 +05:30
if err := committer . Commit ( ) ; err != nil {
2022-10-25 00:59:17 +05:30
return fmt . Errorf ( "Commit: %w" , err )
2019-09-20 11:15:38 +05:30
}
2018-05-16 19:31:55 +05:30
2017-09-17 01:46:21 +05:30
return nil
2016-03-05 23:28:51 +05:30
}
2016-07-26 00:18:17 +05:30
2017-01-25 08:13:02 +05:30
// DeleteComment deletes the comment
2022-06-13 15:07:59 +05:30
func DeleteComment ( ctx context . Context , comment * Comment ) error {
2022-03-31 14:50:39 +05:30
e := db . GetEngine ( ctx )
2022-03-01 05:50:15 +05:30
if _ , err := e . ID ( comment . ID ) . NoAutoCondition ( ) . Delete ( comment ) ; err != nil {
2016-07-26 00:18:17 +05:30
return err
}
2022-06-13 15:07:59 +05:30
if _ , err := db . DeleteByBean ( ctx , & ContentHistory {
2021-10-11 04:10:03 +05:30
CommentID : comment . ID ,
} ) ; err != nil {
return err
}
2016-11-07 22:00:04 +05:30
if comment . Type == CommentTypeComment {
2022-03-18 03:34:09 +05:30
if _ , err := e . ID ( comment . IssueID ) . Decr ( "num_comments" ) . Update ( new ( Issue ) ) ; err != nil {
2016-07-26 00:18:17 +05:30
return err
}
}
2022-06-13 15:07:59 +05:30
if _ , err := e . Table ( "action" ) .
Where ( "comment_id = ?" , comment . ID ) .
Update ( map [ string ] interface { } {
"is_deleted" : true ,
} ) ; err != nil {
2017-07-04 07:00:41 +05:30
return err
}
2016-07-26 00:18:17 +05:30
2022-05-20 19:38:52 +05:30
if err := comment . neuterCrossReferences ( ctx ) ; err != nil {
2019-09-20 11:15:38 +05:30
return err
}
2022-06-13 15:07:59 +05:30
return DeleteReaction ( ctx , & ReactionOptions { CommentID : comment . ID } )
2016-07-26 00:18:17 +05:30
}
2018-08-06 10:13:22 +05:30
// CodeComments represents comments on code by using this structure: FILENAME -> LINE (+ == proposed; - == previous) -> COMMENTS
type CodeComments map [ string ] map [ int64 ] [ ] * Comment
2022-05-20 19:38:52 +05:30
// FetchCodeComments will return a 2d-map: ["Path"]["Line"] = Comments at line
func FetchCodeComments ( ctx context . Context , issue * Issue , currentUser * user_model . User ) ( CodeComments , error ) {
2021-12-10 06:57:50 +05:30
return fetchCodeCommentsByReview ( ctx , issue , currentUser , nil )
2018-08-06 10:13:22 +05:30
}
2021-12-10 06:57:50 +05:30
func fetchCodeCommentsByReview ( ctx context . Context , issue * Issue , currentUser * user_model . User , review * Review ) ( CodeComments , error ) {
2018-08-06 10:13:22 +05:30
pathToLineToComment := make ( CodeComments )
if review == nil {
review = & Review { ID : 0 }
}
opts := FindCommentsOptions {
Type : CommentTypeCode ,
IssueID : issue . ID ,
ReviewID : review . ID ,
}
2021-01-09 03:19:55 +05:30
2021-12-10 06:57:50 +05:30
comments , err := findCodeComments ( ctx , opts , issue , currentUser , review )
2021-01-09 03:19:55 +05:30
if err != nil {
return nil , err
}
for _ , comment := range comments {
if pathToLineToComment [ comment . TreePath ] == nil {
pathToLineToComment [ comment . TreePath ] = make ( map [ int64 ] [ ] * Comment )
}
pathToLineToComment [ comment . TreePath ] [ comment . Line ] = append ( pathToLineToComment [ comment . TreePath ] [ comment . Line ] , comment )
}
return pathToLineToComment , nil
}
2021-12-10 06:57:50 +05:30
func findCodeComments ( ctx context . Context , opts FindCommentsOptions , issue * Issue , currentUser * user_model . User , review * Review ) ( [ ] * Comment , error ) {
2021-01-09 03:19:55 +05:30
var comments [ ] * Comment
if review == nil {
review = & Review { ID : 0 }
}
2018-08-06 10:13:22 +05:30
conds := opts . toConds ( )
if review . ID == 0 {
2018-10-05 21:19:30 +05:30
conds = conds . And ( builder . Eq { "invalidated" : false } )
2018-08-06 10:13:22 +05:30
}
2021-12-10 06:57:50 +05:30
e := db . GetEngine ( ctx )
2018-08-06 10:13:22 +05:30
if err := e . Where ( conds ) .
Asc ( "comment.created_unix" ) .
Asc ( "comment.id" ) .
Find ( & comments ) ; err != nil {
return nil , err
}
2022-04-08 14:41:15 +05:30
if err := issue . LoadRepo ( ctx ) ; err != nil {
2018-08-06 10:13:22 +05:30
return nil , err
}
2019-04-18 10:30:03 +05:30
2022-05-20 19:38:52 +05:30
if err := CommentList ( comments ) . loadPosters ( ctx ) ; err != nil {
2019-04-18 10:30:03 +05:30
return nil , err
}
2018-08-06 10:13:22 +05:30
// Find all reviews by ReviewID
reviews := make ( map [ int64 ] * Review )
2021-03-15 00:22:12 +05:30
ids := make ( [ ] int64 , 0 , len ( comments ) )
2018-08-06 10:13:22 +05:30
for _ , comment := range comments {
if comment . ReviewID != 0 {
ids = append ( ids , comment . ReviewID )
}
}
if err := e . In ( "id" , ids ) . Find ( & reviews ) ; err != nil {
return nil , err
}
2020-04-18 19:20:25 +05:30
2021-01-09 03:19:55 +05:30
n := 0
2018-08-06 10:13:22 +05:30
for _ , comment := range comments {
2021-01-09 03:19:55 +05:30
if re , ok := reviews [ comment . ReviewID ] ; ok && re != nil {
// If the review is pending only the author can see the comments (except if the review is set)
if review . ID == 0 && re . Type == ReviewTypePending &&
( currentUser == nil || currentUser . ID != re . ReviewerID ) {
continue
}
comment . Review = re
}
comments [ n ] = comment
n ++
2020-04-18 19:20:25 +05:30
if err := comment . LoadResolveDoer ( ) ; err != nil {
return nil , err
}
2020-11-02 14:26:02 +05:30
if err := comment . LoadReactions ( issue . Repo ) ; err != nil {
return nil , err
}
2021-04-20 03:55:08 +05:30
var err error
if comment . RenderedContent , err = markdown . RenderString ( & markup . RenderContext {
2022-01-20 04:56:57 +05:30
Ctx : ctx ,
2021-04-20 03:55:08 +05:30
URLPrefix : issue . Repo . Link ( ) ,
Metas : issue . Repo . ComposeMetas ( ) ,
} , comment . Content ) ; err != nil {
return nil , err
}
2018-08-06 10:13:22 +05:30
}
2021-01-09 03:19:55 +05:30
return comments [ : n ] , nil
}
// FetchCodeCommentsByLine fetches the code comments for a given treePath and line number
2022-01-20 04:56:57 +05:30
func FetchCodeCommentsByLine ( ctx context . Context , issue * Issue , currentUser * user_model . User , treePath string , line int64 ) ( [ ] * Comment , error ) {
2021-01-09 03:19:55 +05:30
opts := FindCommentsOptions {
Type : CommentTypeCode ,
IssueID : issue . ID ,
TreePath : treePath ,
Line : line ,
}
2022-01-20 04:56:57 +05:30
return findCodeComments ( ctx , opts , issue , currentUser , nil )
2018-08-06 10:13:22 +05:30
}
2019-10-14 11:40:42 +05:30
// UpdateCommentsMigrationsByType updates comments' migrations information via given git service type and original id and poster id
2019-10-17 07:36:28 +05:30
func UpdateCommentsMigrationsByType ( tp structs . GitServiceType , originalAuthorID string , posterID int64 ) error {
2021-09-23 21:15:36 +05:30
_ , err := db . GetEngine ( db . DefaultContext ) . Table ( "comment" ) .
2019-10-14 11:40:42 +05:30
Where ( builder . In ( "issue_id" ,
builder . Select ( "issue.id" ) .
From ( "issue" ) .
InnerJoin ( "repository" , "issue.repo_id = repository.id" ) .
Where ( builder . Eq {
"repository.original_service_type" : tp ,
} ) ,
) ) .
And ( "comment.original_author_id = ?" , originalAuthorID ) .
Update ( map [ string ] interface { } {
"poster_id" : posterID ,
"original_author" : "" ,
"original_author_id" : 0 ,
} )
return err
}
2020-05-20 18:17:24 +05:30
2021-07-08 17:08:13 +05:30
// CreatePushPullComment create push code to pull base comment
2022-01-20 04:56:57 +05:30
func CreatePushPullComment ( ctx context . Context , pusher * user_model . User , pr * PullRequest , oldCommitID , newCommitID string ) ( comment * Comment , err error ) {
2020-05-20 18:17:24 +05:30
if pr . HasMerged || oldCommitID == "" || newCommitID == "" {
return nil , nil
}
ops := & CreateCommentOptions {
2022-01-21 23:29:26 +05:30
Type : CommentTypePullRequestPush ,
2020-05-20 18:17:24 +05:30
Doer : pusher ,
Repo : pr . BaseRepo ,
}
var data PushActionContent
2022-01-20 04:56:57 +05:30
data . CommitIDs , data . IsForcePush , err = getCommitIDsFromRepo ( ctx , pr . BaseRepo , oldCommitID , newCommitID , pr . BaseBranch )
2020-05-20 18:17:24 +05:30
if err != nil {
return nil , err
}
ops . Issue = pr . Issue
2021-03-02 02:38:10 +05:30
2020-05-20 18:17:24 +05:30
dataJSON , err := json . Marshal ( data )
if err != nil {
return nil , err
}
ops . Content = string ( dataJSON )
comment , err = CreateComment ( ops )
2022-06-20 15:32:49 +05:30
return comment , err
2020-05-20 18:17:24 +05:30
}
2022-05-08 19:16:34 +05:30
// CreateAutoMergeComment is a internal function, only use it for CommentTypePRScheduledToAutoMerge and CommentTypePRUnScheduledToAutoMerge CommentTypes
func CreateAutoMergeComment ( ctx context . Context , typ CommentType , pr * PullRequest , doer * user_model . User ) ( comment * Comment , err error ) {
if typ != CommentTypePRScheduledToAutoMerge && typ != CommentTypePRUnScheduledToAutoMerge {
return nil , fmt . Errorf ( "comment type %d cannot be used to create an auto merge comment" , typ )
}
if err = pr . LoadIssueCtx ( ctx ) ; err != nil {
return
}
if err = pr . LoadBaseRepoCtx ( ctx ) ; err != nil {
return
}
comment , err = CreateCommentCtx ( ctx , & CreateCommentOptions {
Type : typ ,
Doer : doer ,
Repo : pr . BaseRepo ,
Issue : pr . Issue ,
} )
2022-06-20 15:32:49 +05:30
return comment , err
2022-05-08 19:16:34 +05:30
}
2021-07-08 17:08:13 +05:30
// getCommitsFromRepo get commit IDs from repo in between oldCommitID and newCommitID
2020-05-20 18:17:24 +05:30
// isForcePush will be true if oldCommit isn't on the branch
// Commit on baseBranch will skip
2022-01-20 04:56:57 +05:30
func getCommitIDsFromRepo ( ctx context . Context , repo * repo_model . Repository , oldCommitID , newCommitID , baseBranch string ) ( commitIDs [ ] string , isForcePush bool , err error ) {
2020-05-20 18:17:24 +05:30
repoPath := repo . RepoPath ( )
2022-01-20 04:56:57 +05:30
gitRepo , closer , err := git . RepositoryFromContextOrOpen ( ctx , repoPath )
2020-05-20 18:17:24 +05:30
if err != nil {
return nil , false , err
}
2022-01-20 04:56:57 +05:30
defer closer . Close ( )
2020-05-20 18:17:24 +05:30
oldCommit , err := gitRepo . GetCommit ( oldCommitID )
if err != nil {
return nil , false , err
}
2020-06-25 01:10:52 +05:30
if err = oldCommit . LoadBranchName ( ) ; err != nil {
2020-05-20 18:17:24 +05:30
return nil , false , err
}
2020-06-25 01:10:52 +05:30
if len ( oldCommit . Branch ) == 0 {
2020-05-20 18:17:24 +05:30
commitIDs = make ( [ ] string , 2 )
commitIDs [ 0 ] = oldCommitID
commitIDs [ 1 ] = newCommitID
return commitIDs , true , err
}
newCommit , err := gitRepo . GetCommit ( newCommitID )
if err != nil {
return nil , false , err
}
2021-08-09 23:38:51 +05:30
commits , err := newCommit . CommitsBeforeUntil ( oldCommitID )
2020-05-20 18:17:24 +05:30
if err != nil {
return nil , false , err
}
2021-08-09 23:38:51 +05:30
commitIDs = make ( [ ] string , 0 , len ( commits ) )
commitChecks := make ( map [ string ] * commitBranchCheckItem )
2020-05-20 18:17:24 +05:30
2021-08-09 23:38:51 +05:30
for _ , commit := range commits {
commitChecks [ commit . ID . String ( ) ] = & commitBranchCheckItem {
Commit : commit ,
2020-06-25 01:10:52 +05:30
Checked : false ,
2020-05-20 18:17:24 +05:30
}
2020-06-25 01:10:52 +05:30
}
2020-05-20 18:17:24 +05:30
2020-06-25 01:10:52 +05:30
if err = commitBranchCheck ( gitRepo , newCommit , oldCommitID , baseBranch , commitChecks ) ; err != nil {
return
}
2021-08-09 23:38:51 +05:30
for i := len ( commits ) - 1 ; i >= 0 ; i -- {
commitID := commits [ i ] . ID . String ( )
2020-06-25 01:10:52 +05:30
if item , ok := commitChecks [ commitID ] ; ok && item . Checked {
commitIDs = append ( commitIDs , commitID )
2020-05-20 18:17:24 +05:30
}
}
2022-06-20 15:32:49 +05:30
return commitIDs , isForcePush , err
2020-05-20 18:17:24 +05:30
}
2020-06-25 01:10:52 +05:30
type commitBranchCheckItem struct {
Commit * git . Commit
Checked bool
}
2021-08-09 23:38:51 +05:30
func commitBranchCheck ( gitRepo * git . Repository , startCommit * git . Commit , endCommitID , baseBranch string , commitList map [ string ] * commitBranchCheckItem ) error {
2020-06-25 01:10:52 +05:30
if startCommit . ID . String ( ) == endCommitID {
2021-08-09 23:38:51 +05:30
return nil
2020-06-25 01:10:52 +05:30
}
2021-08-09 23:38:51 +05:30
checkStack := make ( [ ] string , 0 , 10 )
checkStack = append ( checkStack , startCommit . ID . String ( ) )
2020-06-25 01:10:52 +05:30
2021-08-09 23:38:51 +05:30
for len ( checkStack ) > 0 {
commitID := checkStack [ 0 ]
checkStack = checkStack [ 1 : ]
2020-06-25 01:10:52 +05:30
2021-08-09 23:38:51 +05:30
item , ok := commitList [ commitID ]
if ! ok {
2020-06-25 01:10:52 +05:30
continue
}
if item . Commit . ID . String ( ) == endCommitID {
continue
}
2021-08-09 23:38:51 +05:30
if err := item . Commit . LoadBranchName ( ) ; err != nil {
return err
2020-06-25 01:10:52 +05:30
}
if item . Commit . Branch == baseBranch {
continue
}
if item . Checked {
continue
}
item . Checked = true
parentNum := item . Commit . ParentCount ( )
for i := 0 ; i < parentNum ; i ++ {
2021-08-09 23:38:51 +05:30
parentCommit , err := item . Commit . Parent ( i )
2020-06-25 01:10:52 +05:30
if err != nil {
2021-08-09 23:38:51 +05:30
return err
2020-06-25 01:10:52 +05:30
}
2021-08-09 23:38:51 +05:30
checkStack = append ( checkStack , parentCommit . ID . String ( ) )
2020-06-25 01:10:52 +05:30
}
}
return nil
}
2022-02-01 23:50:28 +05:30
// RemapExternalUser ExternalUserRemappable interface
func ( c * Comment ) RemapExternalUser ( externalName string , externalID , userID int64 ) error {
c . OriginalAuthor = externalName
c . OriginalAuthorID = externalID
c . PosterID = userID
return nil
}
// GetUserID ExternalUserRemappable interface
func ( c * Comment ) GetUserID ( ) int64 { return c . PosterID }
// GetExternalName ExternalUserRemappable interface
func ( c * Comment ) GetExternalName ( ) string { return c . OriginalAuthor }
// GetExternalID ExternalUserRemappable interface
func ( c * Comment ) GetExternalID ( ) int64 { return c . OriginalAuthorID }
2022-06-13 15:07:59 +05:30
// CountCommentTypeLabelWithEmptyLabel count label comments with empty label
func CountCommentTypeLabelWithEmptyLabel ( ) ( int64 , error ) {
return db . GetEngine ( db . DefaultContext ) . Where ( builder . Eq { "type" : CommentTypeLabel , "label_id" : 0 } ) . Count ( new ( Comment ) )
}
// FixCommentTypeLabelWithEmptyLabel count label comments with empty label
func FixCommentTypeLabelWithEmptyLabel ( ) ( int64 , error ) {
return db . GetEngine ( db . DefaultContext ) . Where ( builder . Eq { "type" : CommentTypeLabel , "label_id" : 0 } ) . Delete ( new ( Comment ) )
}
// CountCommentTypeLabelWithOutsideLabels count label comments with outside label
func CountCommentTypeLabelWithOutsideLabels ( ) ( int64 , error ) {
return db . GetEngine ( db . DefaultContext ) . Where ( "comment.type = ? AND ((label.org_id = 0 AND issue.repo_id != label.repo_id) OR (label.repo_id = 0 AND label.org_id != repository.owner_id))" , CommentTypeLabel ) .
Table ( "comment" ) .
Join ( "inner" , "label" , "label.id = comment.label_id" ) .
Join ( "inner" , "issue" , "issue.id = comment.issue_id " ) .
Join ( "inner" , "repository" , "issue.repo_id = repository.id" ) .
Count ( )
}
// FixCommentTypeLabelWithOutsideLabels count label comments with outside label
func FixCommentTypeLabelWithOutsideLabels ( ) ( int64 , error ) {
res , err := db . GetEngine ( db . DefaultContext ) . Exec ( ` DELETE FROM comment WHERE comment . id IN (
SELECT il_too . id FROM (
SELECT com . id
FROM comment AS com
INNER JOIN label ON com . label_id = label . id
INNER JOIN issue on issue . id = com . issue_id
INNER JOIN repository ON issue . repo_id = repository . id
WHERE
com . type = ? AND ( ( label . org_id = 0 AND issue . repo_id != label . repo_id ) OR ( label . repo_id = 0 AND label . org_id != repository . owner_id ) )
) AS il_too ) ` , CommentTypeLabel )
if err != nil {
return 0 , err
}
return res . RowsAffected ( )
}