code.gitea.io/gitea@v1.21.7/models/migrations/v1_13/v147.go (about)

     1  // Copyright 2020 The Gitea Authors. All rights reserved.
     2  // SPDX-License-Identifier: MIT
     3  
     4  package v1_13 //nolint
     5  
     6  import (
     7  	"code.gitea.io/gitea/modules/timeutil"
     8  
     9  	"xorm.io/xorm"
    10  )
    11  
    12  func CreateReviewsForCodeComments(x *xorm.Engine) error {
    13  	// Review
    14  	type Review struct {
    15  		ID               int64 `xorm:"pk autoincr"`
    16  		Type             int
    17  		ReviewerID       int64 `xorm:"index"`
    18  		OriginalAuthor   string
    19  		OriginalAuthorID int64
    20  		IssueID          int64  `xorm:"index"`
    21  		Content          string `xorm:"TEXT"`
    22  		// Official is a review made by an assigned approver (counts towards approval)
    23  		Official bool   `xorm:"NOT NULL DEFAULT false"`
    24  		CommitID string `xorm:"VARCHAR(40)"`
    25  		Stale    bool   `xorm:"NOT NULL DEFAULT false"`
    26  
    27  		CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
    28  		UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
    29  	}
    30  
    31  	const ReviewTypeComment = 2
    32  
    33  	// Comment represents a comment in commit and issue page.
    34  	type Comment struct {
    35  		ID               int64 `xorm:"pk autoincr"`
    36  		Type             int   `xorm:"INDEX"`
    37  		PosterID         int64 `xorm:"INDEX"`
    38  		OriginalAuthor   string
    39  		OriginalAuthorID int64
    40  		IssueID          int64 `xorm:"INDEX"`
    41  		LabelID          int64
    42  		OldProjectID     int64
    43  		ProjectID        int64
    44  		OldMilestoneID   int64
    45  		MilestoneID      int64
    46  		AssigneeID       int64
    47  		RemovedAssignee  bool
    48  		ResolveDoerID    int64
    49  		OldTitle         string
    50  		NewTitle         string
    51  		OldRef           string
    52  		NewRef           string
    53  		DependentIssueID int64
    54  
    55  		CommitID int64
    56  		Line     int64 // - previous line / + proposed line
    57  		TreePath string
    58  		Content  string `xorm:"TEXT"`
    59  
    60  		// Path represents the 4 lines of code cemented by this comment
    61  		PatchQuoted string `xorm:"TEXT patch"`
    62  
    63  		CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
    64  		UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
    65  
    66  		// Reference issue in commit message
    67  		CommitSHA string `xorm:"VARCHAR(40)"`
    68  
    69  		ReviewID    int64 `xorm:"index"`
    70  		Invalidated bool
    71  
    72  		// Reference an issue or pull from another comment, issue or PR
    73  		// All information is about the origin of the reference
    74  		RefRepoID    int64 `xorm:"index"` // Repo where the referencing
    75  		RefIssueID   int64 `xorm:"index"`
    76  		RefCommentID int64 `xorm:"index"`    // 0 if origin is Issue title or content (or PR's)
    77  		RefAction    int   `xorm:"SMALLINT"` // What happens if RefIssueID resolves
    78  		RefIsPull    bool
    79  	}
    80  
    81  	if err := x.Sync(new(Review), new(Comment)); err != nil {
    82  		return err
    83  	}
    84  
    85  	updateComment := func(comments []*Comment) error {
    86  		sess := x.NewSession()
    87  		defer sess.Close()
    88  		if err := sess.Begin(); err != nil {
    89  			return err
    90  		}
    91  
    92  		for _, comment := range comments {
    93  			review := &Review{
    94  				Type:             ReviewTypeComment,
    95  				ReviewerID:       comment.PosterID,
    96  				IssueID:          comment.IssueID,
    97  				Official:         false,
    98  				CommitID:         comment.CommitSHA,
    99  				Stale:            comment.Invalidated,
   100  				OriginalAuthor:   comment.OriginalAuthor,
   101  				OriginalAuthorID: comment.OriginalAuthorID,
   102  				CreatedUnix:      comment.CreatedUnix,
   103  				UpdatedUnix:      comment.CreatedUnix,
   104  			}
   105  			if _, err := sess.NoAutoTime().Insert(review); err != nil {
   106  				return err
   107  			}
   108  
   109  			reviewComment := &Comment{
   110  				Type:             22,
   111  				PosterID:         comment.PosterID,
   112  				Content:          "",
   113  				IssueID:          comment.IssueID,
   114  				ReviewID:         review.ID,
   115  				OriginalAuthor:   comment.OriginalAuthor,
   116  				OriginalAuthorID: comment.OriginalAuthorID,
   117  				CreatedUnix:      comment.CreatedUnix,
   118  				UpdatedUnix:      comment.CreatedUnix,
   119  			}
   120  			if _, err := sess.NoAutoTime().Insert(reviewComment); err != nil {
   121  				return err
   122  			}
   123  
   124  			comment.ReviewID = review.ID
   125  			if _, err := sess.ID(comment.ID).Cols("review_id").NoAutoTime().Update(comment); err != nil {
   126  				return err
   127  			}
   128  		}
   129  
   130  		return sess.Commit()
   131  	}
   132  
   133  	start := 0
   134  	batchSize := 100
   135  	for {
   136  		comments := make([]*Comment, 0, batchSize)
   137  		if err := x.Where("review_id = 0 and type = 21").Limit(batchSize, start).Find(&comments); err != nil {
   138  			return err
   139  		}
   140  
   141  		if err := updateComment(comments); err != nil {
   142  			return err
   143  		}
   144  
   145  		start += len(comments)
   146  
   147  		if len(comments) < batchSize {
   148  			break
   149  		}
   150  	}
   151  
   152  	return nil
   153  }