summaryrefslogtreecommitdiffstats
path: root/models/review.go
blob: 5f856fbd8973bf54f3457442dc364edefdd27161 (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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
// Copyright 2018 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 models

import (
	"fmt"

	"code.gitea.io/gitea/modules/log"
	api "code.gitea.io/gitea/modules/structs"
	"code.gitea.io/gitea/modules/util"

	"github.com/go-xorm/builder"
	"github.com/go-xorm/core"
	"github.com/go-xorm/xorm"
)

// ReviewType defines the sort of feedback a review gives
type ReviewType int

// ReviewTypeUnknown unknown review type
const ReviewTypeUnknown ReviewType = -1

const (
	// ReviewTypePending is a review which is not published yet
	ReviewTypePending ReviewType = iota
	// ReviewTypeApprove approves changes
	ReviewTypeApprove
	// ReviewTypeComment gives general feedback
	ReviewTypeComment
	// ReviewTypeReject gives feedback blocking merge
	ReviewTypeReject
)

// Icon returns the corresponding icon for the review type
func (rt ReviewType) Icon() string {
	switch rt {
	case ReviewTypeApprove:
		return "eye"
	case ReviewTypeReject:
		return "x"
	case ReviewTypeComment, ReviewTypeUnknown:
		return "comment"
	default:
		return "comment"
	}
}

// Review represents collection of code comments giving feedback for a PR
type Review struct {
	ID         int64 `xorm:"pk autoincr"`
	Type       ReviewType
	Reviewer   *User  `xorm:"-"`
	ReviewerID int64  `xorm:"index"`
	Issue      *Issue `xorm:"-"`
	IssueID    int64  `xorm:"index"`
	Content    string

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

	// CodeComments are the initial code comments of the review
	CodeComments CodeComments `xorm:"-"`
}

func (r *Review) loadCodeComments(e Engine) (err error) {
	r.CodeComments, err = fetchCodeCommentsByReview(e, r.Issue, nil, r)
	return
}

// LoadCodeComments loads CodeComments
func (r *Review) LoadCodeComments() error {
	return r.loadCodeComments(x)
}

func (r *Review) loadIssue(e Engine) (err error) {
	r.Issue, err = getIssueByID(e, r.IssueID)
	return
}

func (r *Review) loadReviewer(e Engine) (err error) {
	if r.ReviewerID == 0 {
		return nil
	}
	r.Reviewer, err = getUserByID(e, r.ReviewerID)
	return
}

func (r *Review) loadAttributes(e Engine) (err error) {
	if err = r.loadReviewer(e); err != nil {
		return
	}
	if err = r.loadIssue(e); err != nil {
		return
	}
	return
}

// LoadAttributes loads all attributes except CodeComments
func (r *Review) LoadAttributes() error {
	return r.loadAttributes(x)
}

// Publish will send notifications / actions to participants for all code comments; parts are concurrent
func (r *Review) Publish() error {
	return r.publish(x)
}

func (r *Review) publish(e *xorm.Engine) error {
	if r.Type == ReviewTypePending || r.Type == ReviewTypeUnknown {
		return fmt.Errorf("review cannot be published if type is pending or unknown")
	}
	if r.Issue == nil {
		if err := r.loadIssue(e); err != nil {
			return err
		}
	}
	if err := r.Issue.loadRepo(e); err != nil {
		return err
	}
	if len(r.CodeComments) == 0 {
		if err := r.loadCodeComments(e); err != nil {
			return err
		}
	}
	for _, lines := range r.CodeComments {
		for _, comments := range lines {
			for _, comment := range comments {
				go func(en *xorm.Engine, review *Review, comm *Comment) {
					sess := en.NewSession()
					defer sess.Close()
					if err := sendCreateCommentAction(sess, &CreateCommentOptions{
						Doer:    comm.Poster,
						Issue:   review.Issue,
						Repo:    review.Issue.Repo,
						Type:    comm.Type,
						Content: comm.Content,
					}, comm); err != nil {
						log.Warn("sendCreateCommentAction: %v", err)
					}
				}(e, r, comment)
			}
		}
	}
	return nil
}

func getReviewByID(e Engine, id int64) (*Review, error) {
	review := new(Review)
	if has, err := e.ID(id).Get(review); err != nil {
		return nil, err
	} else if !has {
		return nil, ErrReviewNotExist{ID: id}
	} else {
		return review, nil
	}
}

// GetReviewByID returns the review by the given ID
func GetReviewByID(id int64) (*Review, error) {
	return getReviewByID(x, id)
}

func getUniqueApprovalsByPullRequestID(e Engine, prID int64) (reviews []*Review, err error) {
	reviews = make([]*Review, 0)
	if err := e.
		Where("issue_id = ? AND type = ?", prID, ReviewTypeApprove).
		OrderBy("updated_unix").
		GroupBy("reviewer_id").
		Find(&reviews); err != nil {
		return nil, err
	}
	return
}

// GetUniqueApprovalsByPullRequestID returns all reviews submitted for a specific pull request
func GetUniqueApprovalsByPullRequestID(prID int64) ([]*Review, error) {
	return getUniqueApprovalsByPullRequestID(x, prID)
}

// FindReviewOptions represent possible filters to find reviews
type FindReviewOptions struct {
	Type       ReviewType
	IssueID    int64
	ReviewerID int64
}

func (opts *FindReviewOptions) toCond() builder.Cond {
	var cond = builder.NewCond()
	if opts.IssueID > 0 {
		cond = cond.And(builder.Eq{"issue_id": opts.IssueID})
	}
	if opts.ReviewerID > 0 {
		cond = cond.And(builder.Eq{"reviewer_id": opts.ReviewerID})
	}
	if opts.Type != ReviewTypeUnknown {
		cond = cond.And(builder.Eq{"type": opts.Type})
	}
	return cond
}

func findReviews(e Engine, opts FindReviewOptions) ([]*Review, error) {
	reviews := make([]*Review, 0, 10)
	sess := e.Where(opts.toCond())
	return reviews, sess.
		Asc("created_unix").
		Asc("id").
		Find(&reviews)
}

// FindReviews returns reviews passing FindReviewOptions
func FindReviews(opts FindReviewOptions) ([]*Review, error) {
	return findReviews(x, opts)
}

// CreateReviewOptions represent the options to create a review. Type, Issue and Reviewer are required.
type CreateReviewOptions struct {
	Content  string
	Type     ReviewType
	Issue    *Issue
	Reviewer *User
}

func createReview(e Engine, opts CreateReviewOptions) (*Review, error) {
	review := &Review{
		Type:       opts.Type,
		Issue:      opts.Issue,
		IssueID:    opts.Issue.ID,
		Reviewer:   opts.Reviewer,
		ReviewerID: opts.Reviewer.ID,
		Content:    opts.Content,
	}
	if _, err := e.Insert(review); err != nil {
		return nil, err
	}

	var reviewHookType HookEventType

	switch opts.Type {
	case ReviewTypeApprove:
		reviewHookType = HookEventPullRequestApproved
	case ReviewTypeComment:
		reviewHookType = HookEventPullRequestComment
	case ReviewTypeReject:
		reviewHookType = HookEventPullRequestRejected
	default:
		// unsupported review webhook type here
		return review, nil
	}

	pr := opts.Issue.PullRequest

	if err := pr.LoadIssue(); err != nil {
		return nil, err
	}

	mode, err := AccessLevel(opts.Issue.Poster, opts.Issue.Repo)
	if err != nil {
		return nil, err
	}

	if err := PrepareWebhooks(opts.Issue.Repo, reviewHookType, &api.PullRequestPayload{
		Action:      api.HookIssueSynchronized,
		Index:       opts.Issue.Index,
		PullRequest: pr.APIFormat(),
		Repository:  opts.Issue.Repo.APIFormat(mode),
		Sender:      opts.Reviewer.APIFormat(),
	}); err != nil {
		return nil, err
	}
	go HookQueue.Add(opts.Issue.Repo.ID)

	return review, nil
}

// CreateReview creates a new review based on opts
func CreateReview(opts CreateReviewOptions) (*Review, error) {
	return createReview(x, opts)
}

func getCurrentReview(e Engine, reviewer *User, issue *Issue) (*Review, error) {
	if reviewer == nil {
		return nil, nil
	}
	reviews, err := findReviews(e, FindReviewOptions{
		Type:       ReviewTypePending,
		IssueID:    issue.ID,
		ReviewerID: reviewer.ID,
	})
	if err != nil {
		return nil, err
	}
	if len(reviews) == 0 {
		return nil, ErrReviewNotExist{}
	}
	reviews[0].Reviewer = reviewer
	reviews[0].Issue = issue
	return reviews[0], nil
}

// GetCurrentReview returns the current pending review of reviewer for given issue
func GetCurrentReview(reviewer *User, issue *Issue) (*Review, error) {
	return getCurrentReview(x, reviewer, issue)
}

// UpdateReview will update all cols of the given review in db
func UpdateReview(r *Review) error {
	if _, err := x.ID(r.ID).AllCols().Update(r); err != nil {
		return err
	}
	return nil
}

// PullReviewersWithType represents the type used to display a review overview
type PullReviewersWithType struct {
	User              `xorm:"extends"`
	Type              ReviewType
	ReviewUpdatedUnix util.TimeStamp `xorm:"review_updated_unix"`
}

// GetReviewersByPullID gets all reviewers for a pull request with the statuses
func GetReviewersByPullID(pullID int64) (issueReviewers []*PullReviewersWithType, err error) {
	irs := []*PullReviewersWithType{}
	if x.Dialect().DBType() == core.MSSQL {
		err = x.SQL(`SELECT [user].*, review.type, review.review_updated_unix FROM
(SELECT review.id, review.type, review.reviewer_id, max(review.updated_unix) as review_updated_unix
FROM review WHERE review.issue_id=? AND (review.type = ? OR review.type = ?)
GROUP BY review.id, review.type, review.reviewer_id) as review
INNER JOIN [user] ON review.reviewer_id = [user].id ORDER BY review_updated_unix DESC`,
			pullID, ReviewTypeApprove, ReviewTypeReject).
			Find(&irs)
	} else {
		err = x.Select("`user`.*, review.type, max(review.updated_unix) as review_updated_unix").
			Table("review").
			Join("INNER", "`user`", "review.reviewer_id = `user`.id").
			Where("review.issue_id = ? AND (review.type = ? OR review.type = ?)",
				pullID, ReviewTypeApprove, ReviewTypeReject).
			GroupBy("`user`.id, review.type").
			OrderBy("review_updated_unix DESC").
			Find(&irs)
	}

	// We need to group our results by user id _and_ review type, otherwise the query fails when using postgresql.
	// But becaus we're doing this, we need to manually filter out multiple reviews of different types by the
	// same person because we only want to show the newest review grouped by user. Thats why we're using a map here.
	issueReviewers = []*PullReviewersWithType{}
	usersInArray := make(map[int64]bool)
	for _, ir := range irs {
		if !usersInArray[ir.ID] {
			issueReviewers = append(issueReviewers, ir)
			usersInArray[ir.ID] = true
		}
	}

	return
}
m_and_yarn/vitest/coverage-v8-2.1.1 Nextcloud server, a safe home for all your data: https://github.com/nextcloud/serverwww-data
summaryrefslogtreecommitdiffstats
path: root/ocs/v1.php
blob: d69904fc4954c8f898f1e945e2d3d1d97c297d93 (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
<?php

/**
* ownCloud
*
* @author Frank Karlitschek
* @copyright 2012 Frank Karlitschek frank@owncloud.org
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
* License as published by the Free Software Foundation; either
* version 3 of the License, or any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
* GNU AFFERO GENERAL PUBLIC LICENSE for more details.
*
* You should have received a copy of the GNU Affero General Public
* License along with this library.  If not, see <http://www.gnu.org/licenses/>.
*
*/

require_once '../lib/base.php';

use Symfony\Component\Routing\Exception\ResourceNotFoundException;
use Symfony\Component\Routing\Exception\MethodNotAllowedException;

try {
	// load all apps to get all api routes properly setup
	OC_App::loadApps();

	// api calls always will return English
	\OC_L10N::forceLanguage('en');

	OC::$server->getRouter()->match('/ocs'.OC_Request::getRawPathInfo());
} catch (ResourceNotFoundException $e) {
	OC_API::setContentType();
	OC_OCS::notFound();
} catch (MethodNotAllowedException $e) {
	OC_API::setContentType();
	OC_Response::setStatus(405);
}