code.gitea.io/gitea@v1.21.7/services/issue/comments.go (about)

     1  // Copyright 2019 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package issue
     5  
     6  import (
     7  	"context"
     8  	"fmt"
     9  
    10  	"code.gitea.io/gitea/models/db"
    11  	issues_model "code.gitea.io/gitea/models/issues"
    12  	repo_model "code.gitea.io/gitea/models/repo"
    13  	user_model "code.gitea.io/gitea/models/user"
    14  	"code.gitea.io/gitea/modules/timeutil"
    15  	notify_service "code.gitea.io/gitea/services/notify"
    16  )
    17  
    18  // CreateRefComment creates a commit reference comment to issue.
    19  func CreateRefComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue, content, commitSHA string) error {
    20  	if len(commitSHA) == 0 {
    21  		return fmt.Errorf("cannot create reference with empty commit SHA")
    22  	}
    23  
    24  	// Check if same reference from same commit has already existed.
    25  	has, err := db.GetEngine(ctx).Get(&issues_model.Comment{
    26  		Type:      issues_model.CommentTypeCommitRef,
    27  		IssueID:   issue.ID,
    28  		CommitSHA: commitSHA,
    29  	})
    30  	if err != nil {
    31  		return fmt.Errorf("check reference comment: %w", err)
    32  	} else if has {
    33  		return nil
    34  	}
    35  
    36  	_, err = issues_model.CreateComment(ctx, &issues_model.CreateCommentOptions{
    37  		Type:      issues_model.CommentTypeCommitRef,
    38  		Doer:      doer,
    39  		Repo:      repo,
    40  		Issue:     issue,
    41  		CommitSHA: commitSHA,
    42  		Content:   content,
    43  	})
    44  	return err
    45  }
    46  
    47  // CreateIssueComment creates a plain issue comment.
    48  func CreateIssueComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue, content string, attachments []string) (*issues_model.Comment, error) {
    49  	comment, err := issues_model.CreateComment(ctx, &issues_model.CreateCommentOptions{
    50  		Type:        issues_model.CommentTypeComment,
    51  		Doer:        doer,
    52  		Repo:        repo,
    53  		Issue:       issue,
    54  		Content:     content,
    55  		Attachments: attachments,
    56  	})
    57  	if err != nil {
    58  		return nil, err
    59  	}
    60  
    61  	mentions, err := issues_model.FindAndUpdateIssueMentions(ctx, issue, doer, comment.Content)
    62  	if err != nil {
    63  		return nil, err
    64  	}
    65  
    66  	notify_service.CreateIssueComment(ctx, doer, repo, issue, comment, mentions)
    67  
    68  	return comment, nil
    69  }
    70  
    71  // UpdateComment updates information of comment.
    72  func UpdateComment(ctx context.Context, c *issues_model.Comment, doer *user_model.User, oldContent string) error {
    73  	needsContentHistory := c.Content != oldContent && c.Type.HasContentSupport()
    74  	if needsContentHistory {
    75  		hasContentHistory, err := issues_model.HasIssueContentHistory(ctx, c.IssueID, c.ID)
    76  		if err != nil {
    77  			return err
    78  		}
    79  		if !hasContentHistory {
    80  			if err = issues_model.SaveIssueContentHistory(ctx, c.PosterID, c.IssueID, c.ID,
    81  				c.CreatedUnix, oldContent, true); err != nil {
    82  				return err
    83  			}
    84  		}
    85  	}
    86  
    87  	if err := issues_model.UpdateComment(ctx, c, doer); err != nil {
    88  		return err
    89  	}
    90  
    91  	if needsContentHistory {
    92  		err := issues_model.SaveIssueContentHistory(ctx, doer.ID, c.IssueID, c.ID, timeutil.TimeStampNow(), c.Content, false)
    93  		if err != nil {
    94  			return err
    95  		}
    96  	}
    97  
    98  	notify_service.UpdateComment(ctx, doer, c, oldContent)
    99  
   100  	return nil
   101  }
   102  
   103  // DeleteComment deletes the comment
   104  func DeleteComment(ctx context.Context, doer *user_model.User, comment *issues_model.Comment) error {
   105  	err := db.WithTx(ctx, func(ctx context.Context) error {
   106  		return issues_model.DeleteComment(ctx, comment)
   107  	})
   108  	if err != nil {
   109  		return err
   110  	}
   111  
   112  	notify_service.DeleteComment(ctx, doer, comment)
   113  
   114  	return nil
   115  }