2018-08-06 07:43:22 +03:00
// 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 (
2020-04-18 16:50:25 +03:00
"fmt"
2019-11-14 05:57:36 +03:00
"strings"
2018-08-06 07:43:22 +03:00
2021-09-19 14:49:59 +03:00
"code.gitea.io/gitea/models/db"
2021-11-09 22:57:58 +03:00
"code.gitea.io/gitea/models/unit"
2020-10-12 22:55:13 +03:00
"code.gitea.io/gitea/modules/base"
2019-08-15 17:46:21 +03:00
"code.gitea.io/gitea/modules/timeutil"
2018-08-06 07:43:22 +03:00
2019-06-23 18:22:43 +03:00
"xorm.io/builder"
2018-08-06 07:43:22 +03:00
)
// 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
2020-04-06 19:33:34 +03:00
// ReviewTypeRequest request review from others
ReviewTypeRequest
2018-08-06 07:43:22 +03:00
)
// Icon returns the corresponding icon for the review type
func ( rt ReviewType ) Icon ( ) string {
switch rt {
case ReviewTypeApprove :
2020-04-03 08:12:42 +03:00
return "check"
2018-08-06 07:43:22 +03:00
case ReviewTypeReject :
2020-07-17 18:15:12 +03:00
return "diff"
2020-04-03 08:12:42 +03:00
case ReviewTypeComment :
2018-10-19 16:36:41 +03:00
return "comment"
2020-04-06 19:33:34 +03:00
case ReviewTypeRequest :
2020-07-17 18:15:12 +03:00
return "dot-fill"
2018-08-06 07:43:22 +03:00
default :
return "comment"
}
}
// Review represents collection of code comments giving feedback for a PR
type Review struct {
2020-01-23 20:28:15 +03:00
ID int64 ` xorm:"pk autoincr" `
Type ReviewType
Reviewer * User ` xorm:"-" `
ReviewerID int64 ` xorm:"index" `
2020-10-12 22:55:13 +03:00
ReviewerTeamID int64 ` xorm:"NOT NULL DEFAULT 0" `
ReviewerTeam * Team ` xorm:"-" `
2020-01-23 20:28:15 +03:00
OriginalAuthor string
OriginalAuthorID int64
Issue * Issue ` xorm:"-" `
IssueID int64 ` xorm:"index" `
Content string ` xorm:"TEXT" `
2019-12-04 04:08:56 +03:00
// Official is a review made by an assigned approver (counts towards approval)
2021-02-11 20:32:25 +03:00
Official bool ` xorm:"NOT NULL DEFAULT false" `
CommitID string ` xorm:"VARCHAR(40)" `
Stale bool ` xorm:"NOT NULL DEFAULT false" `
Dismissed bool ` xorm:"NOT NULL DEFAULT false" `
2018-08-06 07:43:22 +03:00
2019-08-15 17:46:21 +03:00
CreatedUnix timeutil . TimeStamp ` xorm:"INDEX created" `
UpdatedUnix timeutil . TimeStamp ` xorm:"INDEX updated" `
2018-08-06 07:43:22 +03:00
// CodeComments are the initial code comments of the review
CodeComments CodeComments ` xorm:"-" `
2020-01-23 20:28:15 +03:00
Comments [ ] * Comment ` xorm:"-" `
2018-08-06 07:43:22 +03:00
}
2021-09-19 14:49:59 +03:00
func init ( ) {
db . RegisterModel ( new ( Review ) )
}
func ( r * Review ) loadCodeComments ( e db . Engine ) ( err error ) {
2020-05-02 03:20:51 +03:00
if r . CodeComments != nil {
return
}
if err = r . loadIssue ( e ) ; err != nil {
return
2019-11-15 15:59:21 +03:00
}
2020-05-02 03:20:51 +03:00
r . CodeComments , err = fetchCodeCommentsByReview ( e , r . Issue , nil , r )
2018-08-06 07:43:22 +03:00
return
}
// LoadCodeComments loads CodeComments
func ( r * Review ) LoadCodeComments ( ) error {
2021-09-23 18:45:36 +03:00
return r . loadCodeComments ( db . GetEngine ( db . DefaultContext ) )
2018-08-06 07:43:22 +03:00
}
2021-09-19 14:49:59 +03:00
func ( r * Review ) loadIssue ( e db . Engine ) ( err error ) {
2020-05-02 03:20:51 +03:00
if r . Issue != nil {
return
}
2018-08-06 07:43:22 +03:00
r . Issue , err = getIssueByID ( e , r . IssueID )
return
}
2021-09-19 14:49:59 +03:00
func ( r * Review ) loadReviewer ( e db . Engine ) ( err error ) {
2020-10-12 22:55:13 +03:00
if r . ReviewerID == 0 || r . Reviewer != nil {
return
2018-08-06 07:43:22 +03:00
}
r . Reviewer , err = getUserByID ( e , r . ReviewerID )
return
}
2021-09-19 14:49:59 +03:00
func ( r * Review ) loadReviewerTeam ( e db . Engine ) ( err error ) {
2020-10-12 22:55:13 +03:00
if r . ReviewerTeamID == 0 || r . ReviewerTeam != nil {
return
}
r . ReviewerTeam , err = getTeamByID ( e , r . ReviewerTeamID )
return
}
2019-11-14 05:57:36 +03:00
// LoadReviewer loads reviewer
func ( r * Review ) LoadReviewer ( ) error {
2021-09-23 18:45:36 +03:00
return r . loadReviewer ( db . GetEngine ( db . DefaultContext ) )
2019-11-14 05:57:36 +03:00
}
2020-10-12 22:55:13 +03:00
// LoadReviewerTeam loads reviewer team
func ( r * Review ) LoadReviewerTeam ( ) error {
2021-09-23 18:45:36 +03:00
return r . loadReviewerTeam ( db . GetEngine ( db . DefaultContext ) )
2020-10-12 22:55:13 +03:00
}
2021-09-19 14:49:59 +03:00
func ( r * Review ) loadAttributes ( e db . Engine ) ( err error ) {
2020-05-02 03:20:51 +03:00
if err = r . loadIssue ( e ) ; err != nil {
2018-08-06 07:43:22 +03:00
return
}
2020-05-02 03:20:51 +03:00
if err = r . loadCodeComments ( e ) ; err != nil {
return
}
if err = r . loadReviewer ( e ) ; err != nil {
2018-08-06 07:43:22 +03:00
return
}
2020-10-12 22:55:13 +03:00
if err = r . loadReviewerTeam ( e ) ; err != nil {
return
}
2018-08-06 07:43:22 +03:00
return
}
// LoadAttributes loads all attributes except CodeComments
func ( r * Review ) LoadAttributes ( ) error {
2021-09-23 18:45:36 +03:00
return r . loadAttributes ( db . GetEngine ( db . DefaultContext ) )
2018-08-06 07:43:22 +03:00
}
2021-09-19 14:49:59 +03:00
func getReviewByID ( e db . Engine , id int64 ) ( * Review , error ) {
2018-08-06 07:43:22 +03:00
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 ) {
2021-09-23 18:45:36 +03:00
return getReviewByID ( db . GetEngine ( db . DefaultContext ) , id )
2018-08-06 07:43:22 +03:00
}
// FindReviewOptions represent possible filters to find reviews
type FindReviewOptions struct {
2021-09-24 14:32:56 +03:00
db . ListOptions
2019-12-31 02:34:11 +03:00
Type ReviewType
IssueID int64
ReviewerID int64
OfficialOnly bool
2018-08-06 07:43:22 +03:00
}
func ( opts * FindReviewOptions ) toCond ( ) builder . Cond {
2021-03-14 21:52:12 +03:00
cond := builder . NewCond ( )
2018-08-06 07:43:22 +03:00
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 } )
}
2019-12-31 02:34:11 +03:00
if opts . OfficialOnly {
cond = cond . And ( builder . Eq { "official" : true } )
}
2018-08-06 07:43:22 +03:00
return cond
}
2021-09-19 14:49:59 +03:00
func findReviews ( e db . Engine , opts FindReviewOptions ) ( [ ] * Review , error ) {
2018-08-06 07:43:22 +03:00
reviews := make ( [ ] * Review , 0 , 10 )
sess := e . Where ( opts . toCond ( ) )
2020-05-02 03:20:51 +03:00
if opts . Page > 0 {
2021-09-24 14:32:56 +03:00
sess = db . SetSessionPagination ( sess , & opts )
2020-05-02 03:20:51 +03:00
}
2018-08-06 07:43:22 +03:00
return reviews , sess .
Asc ( "created_unix" ) .
Asc ( "id" ) .
Find ( & reviews )
}
// FindReviews returns reviews passing FindReviewOptions
func FindReviews ( opts FindReviewOptions ) ( [ ] * Review , error ) {
2021-09-23 18:45:36 +03:00
return findReviews ( db . GetEngine ( db . DefaultContext ) , opts )
2018-08-06 07:43:22 +03:00
}
2021-08-12 15:43:08 +03:00
// CountReviews returns count of reviews passing FindReviewOptions
func CountReviews ( opts FindReviewOptions ) ( int64 , error ) {
2021-09-23 18:45:36 +03:00
return db . GetEngine ( db . DefaultContext ) . Where ( opts . toCond ( ) ) . Count ( & Review { } )
2021-08-12 15:43:08 +03:00
}
2018-08-06 07:43:22 +03:00
// CreateReviewOptions represent the options to create a review. Type, Issue and Reviewer are required.
type CreateReviewOptions struct {
2020-10-12 22:55:13 +03:00
Content string
Type ReviewType
Issue * Issue
Reviewer * User
ReviewerTeam * Team
Official bool
CommitID string
Stale bool
2019-12-04 04:08:56 +03:00
}
2020-10-12 22:55:13 +03:00
// IsOfficialReviewer check if at least one of the provided reviewers can make official reviews in issue (counts towards required approvals)
func IsOfficialReviewer ( issue * Issue , reviewers ... * User ) ( bool , error ) {
2021-09-23 18:45:36 +03:00
return isOfficialReviewer ( db . GetEngine ( db . DefaultContext ) , issue , reviewers ... )
2019-12-04 04:08:56 +03:00
}
2021-09-19 14:49:59 +03:00
func isOfficialReviewer ( e db . Engine , issue * Issue , reviewers ... * User ) ( bool , error ) {
2019-12-04 04:08:56 +03:00
pr , err := getPullRequestByIssueID ( e , issue . ID )
if err != nil {
return false , err
}
if err = pr . loadProtectedBranch ( e ) ; err != nil {
return false , err
}
if pr . ProtectedBranch == nil {
return false , nil
}
2020-10-12 22:55:13 +03:00
for _ , reviewer := range reviewers {
official , err := pr . ProtectedBranch . isUserOfficialReviewer ( e , reviewer )
if official || err != nil {
return official , err
}
}
return false , nil
}
// IsOfficialReviewerTeam check if reviewer in this team can make official reviews in issue (counts towards required approvals)
func IsOfficialReviewerTeam ( issue * Issue , team * Team ) ( bool , error ) {
2021-09-23 18:45:36 +03:00
return isOfficialReviewerTeam ( db . GetEngine ( db . DefaultContext ) , issue , team )
2020-10-12 22:55:13 +03:00
}
2021-09-19 14:49:59 +03:00
func isOfficialReviewerTeam ( e db . Engine , issue * Issue , team * Team ) ( bool , error ) {
2020-10-12 22:55:13 +03:00
pr , err := getPullRequestByIssueID ( e , issue . ID )
if err != nil {
return false , err
}
if err = pr . loadProtectedBranch ( e ) ; err != nil {
return false , err
}
if pr . ProtectedBranch == nil {
return false , nil
}
if ! pr . ProtectedBranch . EnableApprovalsWhitelist {
return team . Authorize >= AccessModeWrite , nil
}
return base . Int64sContains ( pr . ProtectedBranch . ApprovalsWhitelistTeamIDs , team . ID ) , nil
2018-08-06 07:43:22 +03:00
}
2021-09-19 14:49:59 +03:00
func createReview ( e db . Engine , opts CreateReviewOptions ) ( * Review , error ) {
2018-08-06 07:43:22 +03:00
review := & Review {
2020-10-12 22:55:13 +03:00
Type : opts . Type ,
Issue : opts . Issue ,
IssueID : opts . Issue . ID ,
Reviewer : opts . Reviewer ,
ReviewerTeam : opts . ReviewerTeam ,
Content : opts . Content ,
Official : opts . Official ,
CommitID : opts . CommitID ,
Stale : opts . Stale ,
}
if opts . Reviewer != nil {
review . ReviewerID = opts . Reviewer . ID
} else {
if review . Type != ReviewTypeRequest {
review . Type = ReviewTypeRequest
}
review . ReviewerTeamID = opts . ReviewerTeam . ID
2018-08-06 07:43:22 +03:00
}
if _ , err := e . Insert ( review ) ; err != nil {
return nil , err
}
2018-12-27 21:04:30 +03:00
2018-08-06 07:43:22 +03:00
return review , nil
}
// CreateReview creates a new review based on opts
func CreateReview ( opts CreateReviewOptions ) ( * Review , error ) {
2021-09-23 18:45:36 +03:00
return createReview ( db . GetEngine ( db . DefaultContext ) , opts )
2018-08-06 07:43:22 +03:00
}
2021-09-19 14:49:59 +03:00
func getCurrentReview ( e db . Engine , reviewer * User , issue * Issue ) ( * Review , error ) {
2018-08-06 07:43:22 +03:00
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 { }
}
2018-10-18 14:23:05 +03:00
reviews [ 0 ] . Reviewer = reviewer
reviews [ 0 ] . Issue = issue
2018-08-06 07:43:22 +03:00
return reviews [ 0 ] , nil
}
2019-11-24 08:46:16 +03:00
// ReviewExists returns whether a review exists for a particular line of code in the PR
func ReviewExists ( issue * Issue , treePath string , line int64 ) ( bool , error ) {
2021-09-23 18:45:36 +03:00
return db . GetEngine ( db . DefaultContext ) . Cols ( "id" ) . Exist ( & Comment { IssueID : issue . ID , TreePath : treePath , Line : line , Type : CommentTypeCode } )
2019-11-24 08:46:16 +03:00
}
2018-08-06 07:43:22 +03:00
// GetCurrentReview returns the current pending review of reviewer for given issue
func GetCurrentReview ( reviewer * User , issue * Issue ) ( * Review , error ) {
2021-09-23 18:45:36 +03:00
return getCurrentReview ( db . GetEngine ( db . DefaultContext ) , reviewer , issue )
2018-08-06 07:43:22 +03:00
}
2019-11-14 05:57:36 +03:00
// ContentEmptyErr represents an content empty error
2021-03-14 21:52:12 +03:00
type ContentEmptyErr struct { }
2019-11-14 05:57:36 +03:00
func ( ContentEmptyErr ) Error ( ) string {
return "Review content is empty"
}
// IsContentEmptyErr returns true if err is a ContentEmptyErr
func IsContentEmptyErr ( err error ) bool {
_ , ok := err . ( ContentEmptyErr )
return ok
}
// SubmitReview creates a review out of the existing pending review or creates a new one if no pending review exist
2021-06-15 04:12:33 +03:00
func SubmitReview ( doer * User , issue * Issue , reviewType ReviewType , content , commitID string , stale bool , attachmentUUIDs [ ] string ) ( * Review , * Comment , error ) {
2021-09-23 18:45:36 +03:00
sess := db . NewSession ( db . DefaultContext )
2019-11-14 05:57:36 +03:00
defer sess . Close ( )
if err := sess . Begin ( ) ; err != nil {
return nil , nil , err
}
2021-03-14 21:52:12 +03:00
official := false
2019-12-04 04:08:56 +03:00
2019-11-14 05:57:36 +03:00
review , err := getCurrentReview ( sess , doer , issue )
if err != nil {
if ! IsErrReviewNotExist ( err ) {
return nil , nil , err
}
2019-11-14 23:58:01 +03:00
if reviewType != ReviewTypeApprove && len ( strings . TrimSpace ( content ) ) == 0 {
2019-11-14 05:57:36 +03:00
return nil , nil , ContentEmptyErr { }
}
2019-12-04 04:08:56 +03:00
if reviewType == ReviewTypeApprove || reviewType == ReviewTypeReject {
// Only reviewers latest review of type approve and reject shall count as "official", so existing reviews needs to be cleared
if _ , err := sess . Exec ( "UPDATE `review` SET official=? WHERE issue_id=? AND reviewer_id=?" , false , issue . ID , doer . ID ) ; err != nil {
return nil , nil , err
}
2020-10-12 22:55:13 +03:00
if official , err = isOfficialReviewer ( sess , issue , doer ) ; err != nil {
2019-12-04 04:08:56 +03:00
return nil , nil , err
}
}
2019-11-14 05:57:36 +03:00
// No current review. Create a new one!
2020-10-12 22:55:13 +03:00
if review , err = createReview ( sess , CreateReviewOptions {
2019-11-14 05:57:36 +03:00
Type : reviewType ,
Issue : issue ,
Reviewer : doer ,
Content : content ,
2019-12-04 04:08:56 +03:00
Official : official ,
2020-01-09 04:47:45 +03:00
CommitID : commitID ,
Stale : stale ,
2020-10-12 22:55:13 +03:00
} ) ; err != nil {
2019-11-14 05:57:36 +03:00
return nil , nil , err
}
} else {
if err := review . loadCodeComments ( sess ) ; err != nil {
return nil , nil , err
}
2019-11-14 23:58:01 +03:00
if reviewType != ReviewTypeApprove && len ( review . CodeComments ) == 0 && len ( strings . TrimSpace ( content ) ) == 0 {
2019-11-14 05:57:36 +03:00
return nil , nil , ContentEmptyErr { }
}
2019-12-04 04:08:56 +03:00
if reviewType == ReviewTypeApprove || reviewType == ReviewTypeReject {
// Only reviewers latest review of type approve and reject shall count as "official", so existing reviews needs to be cleared
if _ , err := sess . Exec ( "UPDATE `review` SET official=? WHERE issue_id=? AND reviewer_id=?" , false , issue . ID , doer . ID ) ; err != nil {
return nil , nil , err
}
2020-10-12 22:55:13 +03:00
if official , err = isOfficialReviewer ( sess , issue , doer ) ; err != nil {
2019-12-04 04:08:56 +03:00
return nil , nil , err
}
}
review . Official = official
2019-11-14 05:57:36 +03:00
review . Issue = issue
review . Content = content
review . Type = reviewType
2020-01-09 04:47:45 +03:00
review . CommitID = commitID
review . Stale = stale
2019-12-04 04:08:56 +03:00
2020-01-09 04:47:45 +03:00
if _ , err := sess . ID ( review . ID ) . Cols ( "content, type, official, commit_id, stale" ) . Update ( review ) ; err != nil {
2019-11-14 05:57:36 +03:00
return nil , nil , err
}
2018-08-06 07:43:22 +03:00
}
2019-11-14 05:57:36 +03:00
2019-12-16 06:54:24 +03:00
comm , err := createComment ( sess , & CreateCommentOptions {
2021-06-15 04:12:33 +03:00
Type : CommentTypeReview ,
Doer : doer ,
Content : review . Content ,
Issue : issue ,
Repo : issue . Repo ,
ReviewID : review . ID ,
Attachments : attachmentUUIDs ,
2019-11-14 05:57:36 +03:00
} )
if err != nil || comm == nil {
return nil , nil , err
}
2020-10-12 22:55:13 +03:00
// try to remove team review request if need
if issue . Repo . Owner . IsOrganization ( ) && ( reviewType == ReviewTypeApprove || reviewType == ReviewTypeReject ) {
teamReviewRequests := make ( [ ] * Review , 0 , 10 )
2021-10-07 23:39:59 +03:00
if err := sess . SQL ( "SELECT * FROM review WHERE issue_id = ? AND reviewer_team_id > 0 AND type = ?" , issue . ID , ReviewTypeRequest ) . Find ( & teamReviewRequests ) ; err != nil {
2020-10-12 22:55:13 +03:00
return nil , nil , err
}
for _ , teamReviewRequest := range teamReviewRequests {
ok , err := isTeamMember ( sess , issue . Repo . OwnerID , teamReviewRequest . ReviewerTeamID , doer . ID )
if err != nil {
return nil , nil , err
} else if ! ok {
continue
}
if _ , err := sess . Delete ( teamReviewRequest ) ; err != nil {
return nil , nil , err
}
}
}
2019-11-14 05:57:36 +03:00
comm . Review = review
return review , comm , sess . Commit ( )
2018-08-06 07:43:22 +03:00
}
2018-11-22 16:17:36 +03:00
2019-12-04 04:08:56 +03:00
// GetReviewersByIssueID gets the latest review of each reviewer for a pull request
2020-10-12 22:55:13 +03:00
func GetReviewersByIssueID ( issueID int64 ) ( [ ] * Review , error ) {
reviews := make ( [ ] * Review , 0 , 10 )
2018-11-22 16:17:36 +03:00
2021-09-23 18:45:36 +03:00
sess := db . NewSession ( db . DefaultContext )
2019-12-04 04:08:56 +03:00
defer sess . Close ( )
if err := sess . Begin ( ) ; err != nil {
return nil , err
2018-12-11 04:09:46 +03:00
}
2018-11-22 16:17:36 +03:00
2021-07-08 14:38:13 +03:00
// Get latest review of each reviewer, sorted in order they were made
2021-02-11 20:32:25 +03:00
if err := sess . SQL ( "SELECT * FROM review WHERE id IN (SELECT max(id) as id FROM review WHERE issue_id = ? AND reviewer_team_id = 0 AND type in (?, ?, ?) AND dismissed = ? AND original_author_id = 0 GROUP BY issue_id, reviewer_id) ORDER BY review.updated_unix ASC" ,
issueID , ReviewTypeApprove , ReviewTypeReject , ReviewTypeRequest , false ) .
2020-10-12 22:55:13 +03:00
Find ( & reviews ) ; err != nil {
2019-12-04 04:08:56 +03:00
return nil , err
}
2020-10-12 22:55:13 +03:00
teamReviewRequests := make ( [ ] * Review , 0 , 5 )
if err := sess . SQL ( "SELECT * FROM review WHERE id IN (SELECT max(id) as id FROM review WHERE issue_id = ? AND reviewer_team_id <> 0 AND original_author_id = 0 GROUP BY issue_id, reviewer_team_id) ORDER BY review.updated_unix ASC" ,
issueID ) .
Find ( & teamReviewRequests ) ; err != nil {
return nil , err
}
if len ( teamReviewRequests ) > 0 {
reviews = append ( reviews , teamReviewRequests ... )
2018-11-22 16:17:36 +03:00
}
2019-12-04 04:08:56 +03:00
return reviews , nil
2018-11-22 16:17:36 +03:00
}
2020-01-09 04:47:45 +03:00
2020-10-14 15:11:11 +03:00
// GetReviewersFromOriginalAuthorsByIssueID gets the latest review of each original authors for a pull request
func GetReviewersFromOriginalAuthorsByIssueID ( issueID int64 ) ( [ ] * Review , error ) {
reviews := make ( [ ] * Review , 0 , 10 )
2021-07-08 14:38:13 +03:00
// Get latest review of each reviewer, sorted in order they were made
2021-09-23 18:45:36 +03:00
if err := db . GetEngine ( db . DefaultContext ) . SQL ( "SELECT * FROM review WHERE id IN (SELECT max(id) as id FROM review WHERE issue_id = ? AND reviewer_team_id = 0 AND type in (?, ?, ?) AND original_author_id <> 0 GROUP BY issue_id, original_author_id) ORDER BY review.updated_unix ASC" ,
2020-10-14 15:11:11 +03:00
issueID , ReviewTypeApprove , ReviewTypeReject , ReviewTypeRequest ) .
Find ( & reviews ) ; err != nil {
return nil , err
}
return reviews , nil
}
2020-10-12 22:55:13 +03:00
// GetReviewByIssueIDAndUserID get the latest review of reviewer for a pull request
func GetReviewByIssueIDAndUserID ( issueID , userID int64 ) ( * Review , error ) {
2021-09-23 18:45:36 +03:00
return getReviewByIssueIDAndUserID ( db . GetEngine ( db . DefaultContext ) , issueID , userID )
2020-04-11 07:44:50 +03:00
}
2021-09-19 14:49:59 +03:00
func getReviewByIssueIDAndUserID ( e db . Engine , issueID , userID int64 ) ( * Review , error ) {
2020-10-12 22:55:13 +03:00
review := new ( Review )
2020-04-06 19:33:34 +03:00
2020-10-12 22:55:13 +03:00
has , err := e . SQL ( "SELECT * FROM review WHERE id IN (SELECT max(id) as id FROM review WHERE issue_id = ? AND reviewer_id = ? AND original_author_id = 0 AND type in (?, ?, ?))" ,
2020-04-06 19:33:34 +03:00
issueID , userID , ReviewTypeApprove , ReviewTypeReject , ReviewTypeRequest ) .
2020-10-12 22:55:13 +03:00
Get ( review )
if err != nil {
return nil , err
}
if ! has {
return nil , ErrReviewNotExist { }
}
return review , nil
}
// GetTeamReviewerByIssueIDAndTeamID get the latest review requst of reviewer team for a pull request
func GetTeamReviewerByIssueIDAndTeamID ( issueID , teamID int64 ) ( review * Review , err error ) {
2021-09-23 18:45:36 +03:00
return getTeamReviewerByIssueIDAndTeamID ( db . GetEngine ( db . DefaultContext ) , issueID , teamID )
2020-10-12 22:55:13 +03:00
}
2021-09-19 14:49:59 +03:00
func getTeamReviewerByIssueIDAndTeamID ( e db . Engine , issueID , teamID int64 ) ( review * Review , err error ) {
2020-10-12 22:55:13 +03:00
review = new ( Review )
has := false
if has , err = e . SQL ( "SELECT * FROM review WHERE id IN (SELECT max(id) as id FROM review WHERE issue_id = ? AND reviewer_team_id = ?)" ,
issueID , teamID ) .
2020-04-06 19:33:34 +03:00
Get ( review ) ; err != nil {
return nil , err
}
2020-10-12 22:55:13 +03:00
if ! has {
return nil , ErrReviewNotExist { 0 }
}
2020-04-06 19:33:34 +03:00
return
}
2020-01-09 04:47:45 +03:00
// MarkReviewsAsStale marks existing reviews as stale
func MarkReviewsAsStale ( issueID int64 ) ( err error ) {
2021-09-23 18:45:36 +03:00
_ , err = db . GetEngine ( db . DefaultContext ) . Exec ( "UPDATE `review` SET stale=? WHERE issue_id=?" , true , issueID )
2020-01-09 04:47:45 +03:00
return
}
// MarkReviewsAsNotStale marks existing reviews as not stale for a giving commit SHA
func MarkReviewsAsNotStale ( issueID int64 , commitID string ) ( err error ) {
2021-09-23 18:45:36 +03:00
_ , err = db . GetEngine ( db . DefaultContext ) . Exec ( "UPDATE `review` SET stale=? WHERE issue_id=? AND commit_id=?" , false , issueID , commitID )
2020-01-09 04:47:45 +03:00
return
}
2020-01-23 20:28:15 +03:00
2021-02-11 20:32:25 +03:00
// DismissReview change the dismiss status of a review
func DismissReview ( review * Review , isDismiss bool ) ( err error ) {
if review . Dismissed == isDismiss || ( review . Type != ReviewTypeApprove && review . Type != ReviewTypeReject ) {
return nil
}
review . Dismissed = isDismiss
2021-04-15 13:03:11 +03:00
if review . ID == 0 {
return ErrReviewNotExist { }
}
2021-09-23 18:45:36 +03:00
_ , err = db . GetEngine ( db . DefaultContext ) . ID ( review . ID ) . Cols ( "dismissed" ) . Update ( review )
2021-02-11 20:32:25 +03:00
return
}
2020-01-23 20:28:15 +03:00
// InsertReviews inserts review and review comments
func InsertReviews ( reviews [ ] * Review ) error {
2021-09-23 18:45:36 +03:00
sess := db . NewSession ( db . DefaultContext )
2020-01-23 20:28:15 +03:00
defer sess . Close ( )
if err := sess . Begin ( ) ; err != nil {
return err
}
for _ , review := range reviews {
if _ , err := sess . NoAutoTime ( ) . Insert ( review ) ; err != nil {
return err
}
if _ , err := sess . NoAutoTime ( ) . Insert ( & Comment {
Type : CommentTypeReview ,
Content : review . Content ,
PosterID : review . ReviewerID ,
OriginalAuthor : review . OriginalAuthor ,
OriginalAuthorID : review . OriginalAuthorID ,
IssueID : review . IssueID ,
ReviewID : review . ID ,
CreatedUnix : review . CreatedUnix ,
UpdatedUnix : review . UpdatedUnix ,
} ) ; err != nil {
return err
}
for _ , c := range review . Comments {
c . ReviewID = review . ID
}
2020-04-20 06:04:08 +03:00
if len ( review . Comments ) > 0 {
if _ , err := sess . NoAutoTime ( ) . Insert ( review . Comments ) ; err != nil {
return err
}
2020-01-23 20:28:15 +03:00
}
}
return sess . Commit ( )
}
2020-04-06 19:33:34 +03:00
2020-04-30 23:24:08 +03:00
// AddReviewRequest add a review request from one reviewer
2020-10-12 22:55:13 +03:00
func AddReviewRequest ( issue * Issue , reviewer , doer * User ) ( * Comment , error ) {
2021-09-23 18:45:36 +03:00
sess := db . NewSession ( db . DefaultContext )
2020-04-06 19:33:34 +03:00
defer sess . Close ( )
if err := sess . Begin ( ) ; err != nil {
return nil , err
}
2020-10-12 22:55:13 +03:00
review , err := getReviewByIssueIDAndUserID ( sess , issue . ID , reviewer . ID )
if err != nil && ! IsErrReviewNotExist ( err ) {
2020-04-06 19:33:34 +03:00
return nil , err
}
2020-10-12 22:55:13 +03:00
// skip it when reviewer hase been request to review
if review != nil && review . Type == ReviewTypeRequest {
return nil , nil
2020-04-06 19:33:34 +03:00
}
2020-10-12 22:55:13 +03:00
official , err := isOfficialReviewer ( sess , issue , reviewer , doer )
if err != nil {
return nil , err
} else if official {
2020-04-06 19:33:34 +03:00
if _ , err := sess . Exec ( "UPDATE `review` SET official=? WHERE issue_id=? AND reviewer_id=?" , false , issue . ID , reviewer . ID ) ; err != nil {
return nil , err
}
}
2020-10-20 21:18:25 +03:00
review , err = createReview ( sess , CreateReviewOptions {
2020-04-06 19:33:34 +03:00
Type : ReviewTypeRequest ,
Issue : issue ,
Reviewer : reviewer ,
Official : official ,
Stale : false ,
2020-10-20 21:18:25 +03:00
} )
if err != nil {
2020-10-12 22:55:13 +03:00
return nil , err
2020-04-06 19:33:34 +03:00
}
2020-10-12 22:55:13 +03:00
comment , err := createComment ( sess , & CreateCommentOptions {
2020-04-06 19:33:34 +03:00
Type : CommentTypeReviewRequest ,
Doer : doer ,
Repo : issue . Repo ,
Issue : issue ,
RemovedAssignee : false , // Use RemovedAssignee as !isRequest
AssigneeID : reviewer . ID , // Use AssigneeID as reviewer ID
2020-10-20 21:18:25 +03:00
ReviewID : review . ID ,
2020-04-06 19:33:34 +03:00
} )
if err != nil {
return nil , err
}
return comment , sess . Commit ( )
}
2021-03-14 21:52:12 +03:00
// RemoveReviewRequest remove a review request from one reviewer
2020-10-12 22:55:13 +03:00
func RemoveReviewRequest ( issue * Issue , reviewer , doer * User ) ( * Comment , error ) {
2021-09-23 18:45:36 +03:00
sess := db . NewSession ( db . DefaultContext )
2020-10-12 22:55:13 +03:00
defer sess . Close ( )
if err := sess . Begin ( ) ; err != nil {
return nil , err
}
review , err := getReviewByIssueIDAndUserID ( sess , issue . ID , reviewer . ID )
if err != nil && ! IsErrReviewNotExist ( err ) {
return nil , err
2020-04-06 19:33:34 +03:00
}
2020-10-12 22:55:13 +03:00
if review == nil || review . Type != ReviewTypeRequest {
2020-04-06 19:33:34 +03:00
return nil , nil
}
2020-10-12 22:55:13 +03:00
if _ , err = sess . Delete ( review ) ; err != nil {
return nil , err
}
official , err := isOfficialReviewer ( sess , issue , reviewer )
if err != nil {
return nil , err
} else if official {
// recalculate the latest official review for reviewer
review , err := getReviewByIssueIDAndUserID ( sess , issue . ID , reviewer . ID )
if err != nil && ! IsErrReviewNotExist ( err ) {
return nil , err
}
if review != nil {
if _ , err := sess . Exec ( "UPDATE `review` SET official=? WHERE id=?" , true , review . ID ) ; err != nil {
return nil , err
}
}
}
comment , err := createComment ( sess , & CreateCommentOptions {
Type : CommentTypeReviewRequest ,
Doer : doer ,
Repo : issue . Repo ,
Issue : issue ,
RemovedAssignee : true , // Use RemovedAssignee as !isRequest
AssigneeID : reviewer . ID , // Use AssigneeID as reviewer ID
} )
if err != nil {
return nil , err
}
return comment , sess . Commit ( )
}
// AddTeamReviewRequest add a review request from one team
func AddTeamReviewRequest ( issue * Issue , reviewer * Team , doer * User ) ( * Comment , error ) {
2021-09-23 18:45:36 +03:00
sess := db . NewSession ( db . DefaultContext )
2020-04-06 19:33:34 +03:00
defer sess . Close ( )
if err := sess . Begin ( ) ; err != nil {
return nil , err
}
2020-10-12 22:55:13 +03:00
review , err := getTeamReviewerByIssueIDAndTeamID ( sess , issue . ID , reviewer . ID )
if err != nil && ! IsErrReviewNotExist ( err ) {
2020-04-06 19:33:34 +03:00
return nil , err
}
2020-10-12 22:55:13 +03:00
// This team already has been requested to review - therefore skip this.
if review != nil {
return nil , nil
}
official , err := isOfficialReviewerTeam ( sess , issue , reviewer )
2020-04-06 19:33:34 +03:00
if err != nil {
2020-10-12 22:55:13 +03:00
return nil , fmt . Errorf ( "isOfficialReviewerTeam(): %v" , err )
} else if ! official {
if official , err = isOfficialReviewer ( sess , issue , doer ) ; err != nil {
return nil , fmt . Errorf ( "isOfficialReviewer(): %v" , err )
}
}
2020-10-20 21:18:25 +03:00
if review , err = createReview ( sess , CreateReviewOptions {
2020-10-12 22:55:13 +03:00
Type : ReviewTypeRequest ,
Issue : issue ,
ReviewerTeam : reviewer ,
Official : official ,
Stale : false ,
} ) ; err != nil {
return nil , err
2020-04-06 19:33:34 +03:00
}
if official {
2020-10-12 22:55:13 +03:00
if _ , err := sess . Exec ( "UPDATE `review` SET official=? WHERE issue_id=? AND reviewer_team_id=?" , false , issue . ID , reviewer . ID ) ; err != nil {
return nil , err
}
}
comment , err := createComment ( sess , & CreateCommentOptions {
Type : CommentTypeReviewRequest ,
Doer : doer ,
Repo : issue . Repo ,
Issue : issue ,
RemovedAssignee : false , // Use RemovedAssignee as !isRequest
AssigneeTeamID : reviewer . ID , // Use AssigneeTeamID as reviewer team ID
2020-10-20 21:18:25 +03:00
ReviewID : review . ID ,
2020-10-12 22:55:13 +03:00
} )
if err != nil {
return nil , fmt . Errorf ( "createComment(): %v" , err )
}
return comment , sess . Commit ( )
}
2021-03-14 21:52:12 +03:00
// RemoveTeamReviewRequest remove a review request from one team
2020-10-12 22:55:13 +03:00
func RemoveTeamReviewRequest ( issue * Issue , reviewer * Team , doer * User ) ( * Comment , error ) {
2021-09-23 18:45:36 +03:00
sess := db . NewSession ( db . DefaultContext )
2020-10-12 22:55:13 +03:00
defer sess . Close ( )
if err := sess . Begin ( ) ; err != nil {
return nil , err
}
review , err := getTeamReviewerByIssueIDAndTeamID ( sess , issue . ID , reviewer . ID )
if err != nil && ! IsErrReviewNotExist ( err ) {
return nil , err
}
if review == nil {
return nil , nil
}
2020-04-06 19:33:34 +03:00
2020-10-12 22:55:13 +03:00
if _ , err = sess . Delete ( review ) ; err != nil {
return nil , err
}
official , err := isOfficialReviewerTeam ( sess , issue , reviewer )
if err != nil {
return nil , fmt . Errorf ( "isOfficialReviewerTeam(): %v" , err )
}
if official {
// recalculate which is the latest official review from that team
review , err := getReviewByIssueIDAndUserID ( sess , issue . ID , - reviewer . ID )
if err != nil && ! IsErrReviewNotExist ( err ) {
2020-04-06 19:33:34 +03:00
return nil , err
}
if review != nil {
if _ , err := sess . Exec ( "UPDATE `review` SET official=? WHERE id=?" , true , review . ID ) ; err != nil {
return nil , err
}
}
}
2020-10-12 22:55:13 +03:00
if doer == nil {
return nil , sess . Commit ( )
2020-04-06 19:33:34 +03:00
}
2020-10-12 22:55:13 +03:00
comment , err := createComment ( sess , & CreateCommentOptions {
2020-04-06 19:33:34 +03:00
Type : CommentTypeReviewRequest ,
Doer : doer ,
Repo : issue . Repo ,
Issue : issue ,
RemovedAssignee : true , // Use RemovedAssignee as !isRequest
2020-10-12 22:55:13 +03:00
AssigneeTeamID : reviewer . ID , // Use AssigneeTeamID as reviewer team ID
2020-04-06 19:33:34 +03:00
} )
if err != nil {
2020-10-12 22:55:13 +03:00
return nil , fmt . Errorf ( "createComment(): %v" , err )
2020-04-06 19:33:34 +03:00
}
return comment , sess . Commit ( )
}
2020-04-18 16:50:25 +03:00
// MarkConversation Add or remove Conversation mark for a code comment
func MarkConversation ( comment * Comment , doer * User , isResolve bool ) ( err error ) {
if comment . Type != CommentTypeCode {
return nil
}
if isResolve {
if comment . ResolveDoerID != 0 {
return nil
}
2021-09-23 18:45:36 +03:00
if _ , err = db . GetEngine ( db . DefaultContext ) . Exec ( "UPDATE `comment` SET resolve_doer_id=? WHERE id=?" , doer . ID , comment . ID ) ; err != nil {
2020-04-18 16:50:25 +03:00
return err
}
} else {
if comment . ResolveDoerID == 0 {
return nil
}
2021-09-23 18:45:36 +03:00
if _ , err = db . GetEngine ( db . DefaultContext ) . Exec ( "UPDATE `comment` SET resolve_doer_id=? WHERE id=?" , 0 , comment . ID ) ; err != nil {
2020-04-18 16:50:25 +03:00
return err
}
}
return nil
}
// CanMarkConversation Add or remove Conversation mark for a code comment permission check
// the PR writer , offfcial reviewer and poster can do it
func CanMarkConversation ( issue * Issue , doer * User ) ( permResult bool , err error ) {
if doer == nil || issue == nil {
return false , fmt . Errorf ( "issue or doer is nil" )
}
if doer . ID != issue . PosterID {
if err = issue . LoadRepo ( ) ; err != nil {
return false , err
}
perm , err := GetUserRepoPermission ( issue . Repo , doer )
if err != nil {
return false , err
}
2021-11-09 22:57:58 +03:00
permResult = perm . CanAccess ( AccessModeWrite , unit . TypePullRequests )
2020-04-18 16:50:25 +03:00
if ! permResult {
if permResult , err = IsOfficialReviewer ( issue , doer ) ; err != nil {
return false , err
}
}
if ! permResult {
return false , nil
}
}
return true , nil
}
2020-05-02 03:20:51 +03:00
// DeleteReview delete a review and it's code comments
func DeleteReview ( r * Review ) error {
2021-09-23 18:45:36 +03:00
sess := db . NewSession ( db . DefaultContext )
2020-05-02 03:20:51 +03:00
defer sess . Close ( )
if err := sess . Begin ( ) ; err != nil {
return err
}
if r . ID == 0 {
return fmt . Errorf ( "review is not allowed to be 0" )
}
2020-10-20 21:18:25 +03:00
if r . Type == ReviewTypeRequest {
return fmt . Errorf ( "review request can not be deleted using this method" )
}
2020-05-02 03:20:51 +03:00
opts := FindCommentsOptions {
Type : CommentTypeCode ,
IssueID : r . IssueID ,
ReviewID : r . ID ,
}
if _ , err := sess . Where ( opts . toConds ( ) ) . Delete ( new ( Comment ) ) ; err != nil {
return err
}
opts = FindCommentsOptions {
Type : CommentTypeReview ,
IssueID : r . IssueID ,
ReviewID : r . ID ,
}
if _ , err := sess . Where ( opts . toConds ( ) ) . Delete ( new ( Comment ) ) ; err != nil {
return err
}
if _ , err := sess . ID ( r . ID ) . Delete ( new ( Review ) ) ; err != nil {
return err
}
return sess . Commit ( )
}
// GetCodeCommentsCount return count of CodeComments a Review has
func ( r * Review ) GetCodeCommentsCount ( ) int {
opts := FindCommentsOptions {
Type : CommentTypeCode ,
IssueID : r . IssueID ,
ReviewID : r . ID ,
}
conds := opts . toConds ( )
if r . ID == 0 {
conds = conds . And ( builder . Eq { "invalidated" : false } )
}
2021-09-23 18:45:36 +03:00
count , err := db . GetEngine ( db . DefaultContext ) . Where ( conds ) . Count ( new ( Comment ) )
2020-05-02 03:20:51 +03:00
if err != nil {
return 0
}
return int ( count )
}
// HTMLURL formats a URL-string to the related review issue-comment
func ( r * Review ) HTMLURL ( ) string {
opts := FindCommentsOptions {
Type : CommentTypeReview ,
IssueID : r . IssueID ,
ReviewID : r . ID ,
}
comment := new ( Comment )
2021-09-23 18:45:36 +03:00
has , err := db . GetEngine ( db . DefaultContext ) . Where ( opts . toConds ( ) ) . Get ( comment )
2020-05-02 03:20:51 +03:00
if err != nil || ! has {
return ""
}
return comment . HTMLURL ( )
}