summaryrefslogtreecommitdiffstats
path: root/models/migrations/v147.go
blob: a39b224039fae6876c56a23685e6d0990db294b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Copyright 2020 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.

package migrations

import (
	"code.gitea.io/gitea/modules/timeutil"

	"xorm.io/xorm"
)

func createReviewsForCodeComments(x *xorm.Engine) error {
	// Review
	type Review struct {
		ID               int64 `xorm:"pk autoincr"`
		Type             int
		ReviewerID       int64 `xorm:"index"`
		OriginalAuthor   string
		OriginalAuthorID int64
		IssueID          int64  `xorm:"index"`
		Content          string `xorm:"TEXT"`
		// Official is a review made by an assigned approver (counts towards approval)
		Official bool   `xorm:"NOT NULL DEFAULT false"`
		CommitID string `xorm:"VARCHAR(40)"`
		Stale    bool   `xorm:"NOT NULL DEFAULT false"`

		CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
		UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`
	}

	const ReviewTypeComment = 2

	// Comment represents a comment in commit and issue page.
	type Comment struct {
		ID               int64 `xorm:"pk autoincr"`
		Type             int   `xorm:"INDEX"`
		PosterID         int64 `xorm:"INDEX"`
		OriginalAuthor   string
		OriginalAuthorID int64
		IssueID          int64 `xorm:"INDEX"`
		LabelID          int64
		OldProjectID     int64
		ProjectID        int64
		OldMilestoneID   int64
		MilestoneID      int64
		AssigneeID       int64
		RemovedAssignee  bool
		ResolveDoerID    int64
		OldTitle         string
		NewTitle         string
		OldRef           string
		NewRef           string
		DependentIssueID int64

		CommitID int64
		Line     int64 // - previous line / + proposed line
		TreePath string
		Content  string `xorm:"TEXT"`

		// Path represents the 4 lines of code cemented by this comment
		PatchQuoted string `xorm:"TEXT patch"`

		CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
		UpdatedUnix timeutil.TimeStamp `xorm:"INDEX updated"`

		// Reference issue in commit message
		CommitSHA string `xorm:"VARCHAR(40)"`

		ReviewID    int64 `xorm:"index"`
		Invalidated bool

		// Reference an issue or pull from another comment, issue or PR
		// All information is about the origin of the reference
		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)
		RefAction    int   `xorm:"SMALLINT"` // What hapens if RefIssueID resolves
		RefIsPull    bool
	}

	if err := x.Sync2(new(Review), new(Comment)); err != nil {
		return err
	}

	var updateComment = func(comments []*Comment) error {
		sess := x.NewSession()
		defer sess.Close()
		if err := sess.Begin(); err != nil {
			return err
		}

		for _, comment := range comments {
			review := &Review{
				Type:             ReviewTypeComment,
				ReviewerID:       comment.PosterID,
				IssueID:          comment.IssueID,
				Official:         false,
				CommitID:         comment.CommitSHA,
				Stale:            comment.Invalidated,
				OriginalAuthor:   comment.OriginalAuthor,
				OriginalAuthorID: comment.OriginalAuthorID,
				CreatedUnix:      comment.CreatedUnix,
				UpdatedUnix:      comment.CreatedUnix,
			}
			if _, err := sess.NoAutoTime().Insert(review); err != nil {
				return err
			}

			reviewComment := &Comment{
				Type:             22,
				PosterID:         comment.PosterID,
				Content:          "",
				IssueID:          comment.IssueID,
				ReviewID:         review.ID,
				OriginalAuthor:   comment.OriginalAuthor,
				OriginalAuthorID: comment.OriginalAuthorID,
				CreatedUnix:      comment.CreatedUnix,
				UpdatedUnix:      comment.CreatedUnix,
			}
			if _, err := sess.NoAutoTime().Insert(reviewComment); err != nil {
				return err
			}

			comment.ReviewID = review.ID
			if _, err := sess.ID(comment.ID).Cols("review_id").NoAutoTime().Update(comment); err != nil {
				return err
			}
		}

		return sess.Commit()
	}

	var start = 0
	var batchSize = 100
	for {
		var comments = make([]*Comment, 0, batchSize)
		if err := x.Where("review_id = 0 and type = 21").Limit(batchSize, start).Find(&comments); err != nil {
			return err
		}

		if err := updateComment(comments); err != nil {
			return err
		}

		start += len(comments)

		if len(comments) < batchSize {
			break
		}
	}

	return nil
}