aboutsummaryrefslogtreecommitdiffstats
path: root/services/pull
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2023-01-18 05:03:44 +0800
committerGitHub <noreply@github.com>2023-01-17 15:03:44 -0600
commitdb2286bbb69f5453f5b184a16a9dca999f3f3eb8 (patch)
tree3d425b4e184233b8f776762a0b46e49a12ec1532 /services/pull
parent60c4725cc2e908bedcbece00cd1efa0be9b7d540 (diff)
downloadgitea-db2286bbb69f5453f5b184a16a9dca999f3f3eb8.tar.gz
gitea-db2286bbb69f5453f5b184a16a9dca999f3f3eb8.zip
some refactor about code comments (#20821)
Diffstat (limited to 'services/pull')
-rw-r--r--services/pull/pull.go2
-rw-r--r--services/pull/review.go49
2 files changed, 48 insertions, 3 deletions
diff --git a/services/pull/pull.go b/services/pull/pull.go
index afb0fa2442..08f70a5e4e 100644
--- a/services/pull/pull.go
+++ b/services/pull/pull.go
@@ -240,7 +240,7 @@ func checkForInvalidation(ctx context.Context, requests issues_model.PullRequest
}
go func() {
// FIXME: graceful: We need to tell the manager we're doing something...
- err := requests.InvalidateCodeComments(ctx, doer, gitRepo, branch)
+ err := InvalidateCodeComments(ctx, requests, doer, gitRepo, branch)
if err != nil {
log.Error("PullRequestList.InvalidateCodeComments: %v", err)
}
diff --git a/services/pull/review.go b/services/pull/review.go
index 67a10d7aad..ca386ca6b0 100644
--- a/services/pull/review.go
+++ b/services/pull/review.go
@@ -23,6 +23,53 @@ import (
issue_service "code.gitea.io/gitea/services/issue"
)
+var notEnoughLines = regexp.MustCompile(`fatal: file .* has only \d+ lines?`)
+
+// 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.
+func checkInvalidation(ctx context.Context, c *issues_model.Comment, doer *user_model.User, repo *git.Repository, branch string) error {
+ // FIXME differentiate between previous and proposed line
+ commit, err := repo.LineBlame(branch, repo.Path, c.TreePath, uint(c.UnsignedLine()))
+ if err != nil && (strings.Contains(err.Error(), "fatal: no such path") || notEnoughLines.MatchString(err.Error())) {
+ c.Invalidated = true
+ return issues_model.UpdateCommentInvalidate(ctx, c)
+ }
+ if err != nil {
+ return err
+ }
+ if c.CommitSHA != "" && c.CommitSHA != commit.ID.String() {
+ c.Invalidated = true
+ return issues_model.UpdateCommentInvalidate(ctx, c)
+ }
+ return nil
+}
+
+// InvalidateCodeComments will lookup the prs for code comments which got invalidated by change
+func InvalidateCodeComments(ctx context.Context, prs issues_model.PullRequestList, doer *user_model.User, repo *git.Repository, branch string) error {
+ if len(prs) == 0 {
+ return nil
+ }
+ issueIDs := prs.GetIssueIDs()
+ var codeComments []*issues_model.Comment
+
+ if err := db.Find(ctx, &issues_model.FindCommentsOptions{
+ ListOptions: db.ListOptions{
+ ListAll: true,
+ },
+ Type: issues_model.CommentTypeCode,
+ Invalidated: util.OptionalBoolFalse,
+ IssueIDs: issueIDs,
+ }, &codeComments); err != nil {
+ return fmt.Errorf("find code comments: %v", err)
+ }
+ for _, comment := range codeComments {
+ if err := checkInvalidation(ctx, comment, doer, repo, branch); err != nil {
+ return err
+ }
+ }
+ return nil
+}
+
// CreateCodeComment creates a comment on the code line
func CreateCodeComment(ctx context.Context, doer *user_model.User, gitRepo *git.Repository, issue *issues_model.Issue, line int64, content, treePath string, isReview bool, replyReviewID int64, latestCommitID string) (*issues_model.Comment, error) {
var (
@@ -114,8 +161,6 @@ func CreateCodeComment(ctx context.Context, doer *user_model.User, gitRepo *git.
return comment, nil
}
-var notEnoughLines = regexp.MustCompile(`exit status 128 - fatal: file .* has only \d+ lines?`)
-
// createCodeComment creates a plain code comment at the specified line / path
func createCodeComment(ctx context.Context, doer *user_model.User, repo *repo_model.Repository, issue *issues_model.Issue, content, treePath string, line, reviewID int64) (*issues_model.Comment, error) {
var commitID, patch string