2018-08-06 04:43:22 +00:00
// Copyright 2018 The Gitea Authors. All rights reserved.
2022-11-27 18:20:29 +00:00
// SPDX-License-Identifier: MIT
2018-08-06 04:43:22 +00:00
2022-06-13 09:37:59 +00:00
package issues
2018-08-06 04:43:22 +00:00
import (
2021-12-10 01:27:50 +00:00
"context"
2020-04-18 13:50:25 +00:00
"fmt"
2019-11-14 02:57:36 +00:00
"strings"
2018-08-06 04:43:22 +00:00
2021-09-19 11:49:59 +00:00
"code.gitea.io/gitea/models/db"
2022-06-12 15:51:54 +00:00
git_model "code.gitea.io/gitea/models/git"
2022-03-29 06:29:02 +00:00
"code.gitea.io/gitea/models/organization"
2021-11-28 11:58:28 +00:00
"code.gitea.io/gitea/models/perm"
2022-05-11 10:09:36 +00:00
access_model "code.gitea.io/gitea/models/perm/access"
2021-11-09 19:57:58 +00:00
"code.gitea.io/gitea/models/unit"
2021-11-24 09:49:20 +00:00
user_model "code.gitea.io/gitea/models/user"
2020-10-12 19:55:13 +00:00
"code.gitea.io/gitea/modules/base"
2022-06-13 09:37:59 +00:00
"code.gitea.io/gitea/modules/structs"
2019-08-15 14:46:21 +00:00
"code.gitea.io/gitea/modules/timeutil"
2022-07-19 13:20:28 +00:00
"code.gitea.io/gitea/modules/util"
2018-08-06 04:43:22 +00:00
2019-06-23 15:22:43 +00:00
"xorm.io/builder"
2018-08-06 04:43:22 +00:00
)
2022-06-13 09:37:59 +00:00
// ErrReviewNotExist represents a "ReviewNotExist" kind of error.
type ErrReviewNotExist struct {
ID int64
}
// IsErrReviewNotExist checks if an error is a ErrReviewNotExist.
func IsErrReviewNotExist ( err error ) bool {
_ , ok := err . ( ErrReviewNotExist )
return ok
}
func ( err ErrReviewNotExist ) Error ( ) string {
return fmt . Sprintf ( "review does not exist [id: %d]" , err . ID )
}
2022-10-18 05:50:37 +00:00
func ( err ErrReviewNotExist ) Unwrap ( ) error {
return util . ErrNotExist
}
2022-06-13 09:37:59 +00:00
// ErrNotValidReviewRequest an not allowed review request modify
type ErrNotValidReviewRequest struct {
Reason string
UserID int64
RepoID int64
}
// IsErrNotValidReviewRequest checks if an error is a ErrNotValidReviewRequest.
func IsErrNotValidReviewRequest ( err error ) bool {
_ , ok := err . ( ErrNotValidReviewRequest )
return ok
}
func ( err ErrNotValidReviewRequest ) Error ( ) string {
return fmt . Sprintf ( "%s [user_id: %d, repo_id: %d]" ,
err . Reason ,
err . UserID ,
err . RepoID )
}
2022-10-18 05:50:37 +00:00
func ( err ErrNotValidReviewRequest ) Unwrap ( ) error {
return util . ErrInvalidArgument
}
2018-08-06 04:43:22 +00: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 16:33:34 +00:00
// ReviewTypeRequest request review from others
ReviewTypeRequest
2018-08-06 04:43:22 +00:00
)
// Icon returns the corresponding icon for the review type
func ( rt ReviewType ) Icon ( ) string {
switch rt {
case ReviewTypeApprove :
2020-04-03 05:12:42 +00:00
return "check"
2018-08-06 04:43:22 +00:00
case ReviewTypeReject :
2020-07-17 15:15:12 +00:00
return "diff"
2020-04-03 05:12:42 +00:00
case ReviewTypeComment :
2018-10-19 13:36:41 +00:00
return "comment"
2020-04-06 16:33:34 +00:00
case ReviewTypeRequest :
2020-07-17 15:15:12 +00:00
return "dot-fill"
2018-08-06 04:43:22 +00:00
default :
return "comment"
}
}
// Review represents collection of code comments giving feedback for a PR
type Review struct {
2020-01-23 17:28:15 +00:00
ID int64 ` xorm:"pk autoincr" `
Type ReviewType
2022-03-29 06:29:02 +00:00
Reviewer * user_model . User ` xorm:"-" `
ReviewerID int64 ` xorm:"index" `
ReviewerTeamID int64 ` xorm:"NOT NULL DEFAULT 0" `
ReviewerTeam * organization . Team ` xorm:"-" `
2020-01-23 17:28:15 +00:00
OriginalAuthor string
OriginalAuthorID int64
Issue * Issue ` xorm:"-" `
IssueID int64 ` xorm:"index" `
Content string ` xorm:"TEXT" `
2019-12-04 01:08:56 +00:00
// Official is a review made by an assigned approver (counts towards approval)
2021-02-11 17:32:25 +00: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 04:43:22 +00:00
2019-08-15 14:46:21 +00:00
CreatedUnix timeutil . TimeStamp ` xorm:"INDEX created" `
UpdatedUnix timeutil . TimeStamp ` xorm:"INDEX updated" `
2018-08-06 04:43:22 +00:00
// CodeComments are the initial code comments of the review
CodeComments CodeComments ` xorm:"-" `
2020-01-23 17:28:15 +00:00
Comments [ ] * Comment ` xorm:"-" `
2018-08-06 04:43:22 +00:00
}
2021-09-19 11:49:59 +00:00
func init ( ) {
db . RegisterModel ( new ( Review ) )
}
2022-01-19 23:26:57 +00:00
// LoadCodeComments loads CodeComments
func ( r * Review ) LoadCodeComments ( ctx context . Context ) ( err error ) {
2020-05-02 00:20:51 +00:00
if r . CodeComments != nil {
return
}
2022-05-20 14:08:52 +00:00
if err = r . loadIssue ( ctx ) ; err != nil {
2020-05-02 00:20:51 +00:00
return
2019-11-15 12:59:21 +00:00
}
2021-12-10 01:27:50 +00:00
r . CodeComments , err = fetchCodeCommentsByReview ( ctx , r . Issue , nil , r )
2022-06-20 10:02:49 +00:00
return err
2018-08-06 04:43:22 +00:00
}
2022-05-20 14:08:52 +00:00
func ( r * Review ) loadIssue ( ctx context . Context ) ( err error ) {
2020-05-02 00:20:51 +00:00
if r . Issue != nil {
return
}
2022-06-13 09:37:59 +00:00
r . Issue , err = GetIssueByID ( ctx , r . IssueID )
2022-06-20 10:02:49 +00:00
return err
2018-08-06 04:43:22 +00:00
}
2022-11-19 08:12:33 +00:00
// LoadReviewer loads reviewer
func ( r * Review ) LoadReviewer ( ctx context . Context ) ( err error ) {
2020-10-12 19:55:13 +00:00
if r . ReviewerID == 0 || r . Reviewer != nil {
return
2018-08-06 04:43:22 +00:00
}
2022-12-03 02:48:26 +00:00
r . Reviewer , err = user_model . GetUserByID ( ctx , r . ReviewerID )
2022-06-20 10:02:49 +00:00
return err
2018-08-06 04:43:22 +00:00
}
2022-11-19 08:12:33 +00:00
// LoadReviewerTeam loads reviewer team
func ( r * Review ) LoadReviewerTeam ( ctx context . Context ) ( err error ) {
2020-10-12 19:55:13 +00:00
if r . ReviewerTeamID == 0 || r . ReviewerTeam != nil {
return
}
2022-05-20 14:08:52 +00:00
r . ReviewerTeam , err = organization . GetTeamByID ( ctx , r . ReviewerTeamID )
2022-06-20 10:02:49 +00:00
return err
2020-10-12 19:55:13 +00:00
}
2022-01-19 23:26:57 +00:00
// LoadAttributes loads all attributes except CodeComments
func ( r * Review ) LoadAttributes ( ctx context . Context ) ( err error ) {
2022-05-20 14:08:52 +00:00
if err = r . loadIssue ( ctx ) ; err != nil {
2018-08-06 04:43:22 +00:00
return
}
2022-01-19 23:26:57 +00:00
if err = r . LoadCodeComments ( ctx ) ; err != nil {
2020-05-02 00:20:51 +00:00
return
}
2022-11-19 08:12:33 +00:00
if err = r . LoadReviewer ( ctx ) ; err != nil {
2018-08-06 04:43:22 +00:00
return
}
2022-11-19 08:12:33 +00:00
if err = r . LoadReviewerTeam ( ctx ) ; err != nil {
2020-10-12 19:55:13 +00:00
return
}
2022-06-20 10:02:49 +00:00
return err
2018-08-06 04:43:22 +00:00
}
2022-05-20 14:08:52 +00:00
// GetReviewByID returns the review by the given ID
func GetReviewByID ( ctx context . Context , id int64 ) ( * Review , error ) {
2018-08-06 04:43:22 +00:00
review := new ( Review )
2022-05-20 14:08:52 +00:00
if has , err := db . GetEngine ( ctx ) . ID ( id ) . Get ( review ) ; err != nil {
2018-08-06 04:43:22 +00:00
return nil , err
} else if ! has {
return nil , ErrReviewNotExist { ID : id }
} else {
return review , nil
}
}
// FindReviewOptions represent possible filters to find reviews
type FindReviewOptions struct {
2021-09-24 11:32:56 +00:00
db . ListOptions
2019-12-30 23:34:11 +00:00
Type ReviewType
IssueID int64
ReviewerID int64
OfficialOnly bool
2018-08-06 04:43:22 +00:00
}
func ( opts * FindReviewOptions ) toCond ( ) builder . Cond {
2021-03-14 18:52:12 +00:00
cond := builder . NewCond ( )
2018-08-06 04:43:22 +00: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-30 23:34:11 +00:00
if opts . OfficialOnly {
cond = cond . And ( builder . Eq { "official" : true } )
}
2018-08-06 04:43:22 +00:00
return cond
}
2022-05-20 14:08:52 +00:00
// FindReviews returns reviews passing FindReviewOptions
func FindReviews ( ctx context . Context , opts FindReviewOptions ) ( [ ] * Review , error ) {
2018-08-06 04:43:22 +00:00
reviews := make ( [ ] * Review , 0 , 10 )
2022-05-20 14:08:52 +00:00
sess := db . GetEngine ( ctx ) . Where ( opts . toCond ( ) )
2020-05-02 00:20:51 +00:00
if opts . Page > 0 {
2021-09-24 11:32:56 +00:00
sess = db . SetSessionPagination ( sess , & opts )
2020-05-02 00:20:51 +00:00
}
2018-08-06 04:43:22 +00:00
return reviews , sess .
Asc ( "created_unix" ) .
Asc ( "id" ) .
Find ( & reviews )
}
2021-08-12 12:43:08 +00:00
// CountReviews returns count of reviews passing FindReviewOptions
func CountReviews ( opts FindReviewOptions ) ( int64 , error ) {
2021-09-23 15:45:36 +00:00
return db . GetEngine ( db . DefaultContext ) . Where ( opts . toCond ( ) ) . Count ( & Review { } )
2021-08-12 12:43:08 +00:00
}
2018-08-06 04:43:22 +00:00
// CreateReviewOptions represent the options to create a review. Type, Issue and Reviewer are required.
type CreateReviewOptions struct {
2020-10-12 19:55:13 +00:00
Content string
Type ReviewType
Issue * Issue
2021-11-24 09:49:20 +00:00
Reviewer * user_model . User
2022-03-29 06:29:02 +00:00
ReviewerTeam * organization . Team
2020-10-12 19:55:13 +00:00
Official bool
CommitID string
Stale bool
2019-12-04 01:08:56 +00:00
}
2020-10-12 19:55:13 +00:00
// IsOfficialReviewer check if at least one of the provided reviewers can make official reviews in issue (counts towards required approvals)
2022-05-20 14:08:52 +00:00
func IsOfficialReviewer ( ctx context . Context , issue * Issue , reviewers ... * user_model . User ) ( bool , error ) {
pr , err := GetPullRequestByIssueID ( ctx , issue . ID )
2019-12-04 01:08:56 +00:00
if err != nil {
return false , err
}
2023-01-16 08:00:22 +00:00
rule , err := git_model . GetFirstMatchProtectedBranchRule ( ctx , pr . BaseRepoID , pr . BaseBranch )
if err != nil {
2019-12-04 01:08:56 +00:00
return false , err
}
2023-01-16 08:00:22 +00:00
if rule == nil {
2019-12-04 01:08:56 +00:00
return false , nil
}
2020-10-12 19:55:13 +00:00
for _ , reviewer := range reviewers {
2023-01-16 08:00:22 +00:00
official , err := git_model . IsUserOfficialReviewer ( ctx , rule , reviewer )
2020-10-12 19:55:13 +00:00
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)
2022-05-20 14:08:52 +00:00
func IsOfficialReviewerTeam ( ctx context . Context , issue * Issue , team * organization . Team ) ( bool , error ) {
pr , err := GetPullRequestByIssueID ( ctx , issue . ID )
2020-10-12 19:55:13 +00:00
if err != nil {
return false , err
}
2023-01-16 08:00:22 +00:00
pb , err := git_model . GetFirstMatchProtectedBranchRule ( ctx , pr . BaseRepoID , pr . BaseBranch )
if err != nil {
2020-10-12 19:55:13 +00:00
return false , err
}
2023-01-16 08:00:22 +00:00
if pb == nil {
2020-10-12 19:55:13 +00:00
return false , nil
}
2023-01-16 08:00:22 +00:00
if ! pb . EnableApprovalsWhitelist {
2022-12-10 02:46:31 +00:00
return team . UnitAccessMode ( ctx , unit . TypeCode ) >= perm . AccessModeWrite , nil
2020-10-12 19:55:13 +00:00
}
2023-01-16 08:00:22 +00:00
return base . Int64sContains ( pb . ApprovalsWhitelistTeamIDs , team . ID ) , nil
2018-08-06 04:43:22 +00:00
}
2022-05-20 14:08:52 +00:00
// CreateReview creates a new review based on opts
func CreateReview ( ctx context . Context , opts CreateReviewOptions ) ( * Review , error ) {
2018-08-06 04:43:22 +00:00
review := & Review {
2020-10-12 19:55:13 +00: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 04:43:22 +00:00
}
2022-05-20 14:08:52 +00:00
return review , db . Insert ( ctx , review )
2018-08-06 04:43:22 +00:00
}
2022-05-20 14:08:52 +00:00
// GetCurrentReview returns the current pending review of reviewer for given issue
func GetCurrentReview ( ctx context . Context , reviewer * user_model . User , issue * Issue ) ( * Review , error ) {
2018-08-06 04:43:22 +00:00
if reviewer == nil {
return nil , nil
}
2022-05-20 14:08:52 +00:00
reviews , err := FindReviews ( ctx , FindReviewOptions {
2018-08-06 04:43:22 +00:00
Type : ReviewTypePending ,
IssueID : issue . ID ,
ReviewerID : reviewer . ID ,
} )
if err != nil {
return nil , err
}
if len ( reviews ) == 0 {
return nil , ErrReviewNotExist { }
}
2018-10-18 11:23:05 +00:00
reviews [ 0 ] . Reviewer = reviewer
reviews [ 0 ] . Issue = issue
2018-08-06 04:43:22 +00:00
return reviews [ 0 ] , nil
}
2019-11-24 05:46:16 +00: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 15:45:36 +00:00
return db . GetEngine ( db . DefaultContext ) . Cols ( "id" ) . Exist ( & Comment { IssueID : issue . ID , TreePath : treePath , Line : line , Type : CommentTypeCode } )
2019-11-24 05:46:16 +00:00
}
2019-11-14 02:57:36 +00:00
// ContentEmptyErr represents an content empty error
2021-03-14 18:52:12 +00:00
type ContentEmptyErr struct { }
2019-11-14 02:57:36 +00: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-11-24 09:49:20 +00:00
func SubmitReview ( doer * user_model . User , issue * Issue , reviewType ReviewType , content , commitID string , stale bool , attachmentUUIDs [ ] string ) ( * Review , * Comment , error ) {
2022-11-12 20:18:50 +00:00
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-19 13:39:57 +00:00
if err != nil {
2019-11-14 02:57:36 +00:00
return nil , nil , err
}
2021-11-19 13:39:57 +00:00
defer committer . Close ( )
sess := db . GetEngine ( ctx )
2019-11-14 02:57:36 +00:00
2021-03-14 18:52:12 +00:00
official := false
2019-12-04 01:08:56 +00:00
2022-05-20 14:08:52 +00:00
review , err := GetCurrentReview ( ctx , doer , issue )
2019-11-14 02:57:36 +00:00
if err != nil {
if ! IsErrReviewNotExist ( err ) {
return nil , nil , err
}
2019-11-14 20:58:01 +00:00
if reviewType != ReviewTypeApprove && len ( strings . TrimSpace ( content ) ) == 0 {
2019-11-14 02:57:36 +00:00
return nil , nil , ContentEmptyErr { }
}
2019-12-04 01:08:56 +00: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
2022-05-20 14:08:52 +00:00
if _ , err := db . Exec ( ctx , "UPDATE `review` SET official=? WHERE issue_id=? AND reviewer_id=?" , false , issue . ID , doer . ID ) ; err != nil {
2019-12-04 01:08:56 +00:00
return nil , nil , err
}
2022-05-20 14:08:52 +00:00
if official , err = IsOfficialReviewer ( ctx , issue , doer ) ; err != nil {
2019-12-04 01:08:56 +00:00
return nil , nil , err
}
}
2019-11-14 02:57:36 +00:00
// No current review. Create a new one!
2022-05-20 14:08:52 +00:00
if review , err = CreateReview ( ctx , CreateReviewOptions {
2019-11-14 02:57:36 +00:00
Type : reviewType ,
Issue : issue ,
Reviewer : doer ,
Content : content ,
2019-12-04 01:08:56 +00:00
Official : official ,
2020-01-09 01:47:45 +00:00
CommitID : commitID ,
Stale : stale ,
2020-10-12 19:55:13 +00:00
} ) ; err != nil {
2019-11-14 02:57:36 +00:00
return nil , nil , err
}
} else {
2022-01-19 23:26:57 +00:00
if err := review . LoadCodeComments ( ctx ) ; err != nil {
2019-11-14 02:57:36 +00:00
return nil , nil , err
}
2019-11-14 20:58:01 +00:00
if reviewType != ReviewTypeApprove && len ( review . CodeComments ) == 0 && len ( strings . TrimSpace ( content ) ) == 0 {
2019-11-14 02:57:36 +00:00
return nil , nil , ContentEmptyErr { }
}
2019-12-04 01:08:56 +00: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
2022-05-20 14:08:52 +00:00
if _ , err := db . Exec ( ctx , "UPDATE `review` SET official=? WHERE issue_id=? AND reviewer_id=?" , false , issue . ID , doer . ID ) ; err != nil {
2019-12-04 01:08:56 +00:00
return nil , nil , err
}
2022-05-20 14:08:52 +00:00
if official , err = IsOfficialReviewer ( ctx , issue , doer ) ; err != nil {
2019-12-04 01:08:56 +00:00
return nil , nil , err
}
}
review . Official = official
2019-11-14 02:57:36 +00:00
review . Issue = issue
review . Content = content
review . Type = reviewType
2020-01-09 01:47:45 +00:00
review . CommitID = commitID
review . Stale = stale
2019-12-04 01:08:56 +00:00
2020-01-09 01:47:45 +00:00
if _ , err := sess . ID ( review . ID ) . Cols ( "content, type, official, commit_id, stale" ) . Update ( review ) ; err != nil {
2019-11-14 02:57:36 +00:00
return nil , nil , err
}
2018-08-06 04:43:22 +00:00
}
2019-11-14 02:57:36 +00:00
2022-12-10 02:46:31 +00:00
comm , err := CreateComment ( ctx , & CreateCommentOptions {
2021-06-15 01:12:33 +00:00
Type : CommentTypeReview ,
Doer : doer ,
Content : review . Content ,
Issue : issue ,
Repo : issue . Repo ,
ReviewID : review . ID ,
Attachments : attachmentUUIDs ,
2019-11-14 02:57:36 +00:00
} )
if err != nil || comm == nil {
return nil , nil , err
}
2020-10-12 19:55:13 +00: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 20:39:59 +00: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 19:55:13 +00:00
return nil , nil , err
}
for _ , teamReviewRequest := range teamReviewRequests {
2022-03-29 06:29:02 +00:00
ok , err := organization . IsTeamMember ( ctx , issue . Repo . OwnerID , teamReviewRequest . ReviewerTeamID , doer . ID )
2020-10-12 19:55:13 +00:00
if err != nil {
return nil , nil , err
} else if ! ok {
continue
}
2022-05-20 14:08:52 +00:00
if _ , err := sess . ID ( teamReviewRequest . ID ) . NoAutoCondition ( ) . Delete ( teamReviewRequest ) ; err != nil {
2020-10-12 19:55:13 +00:00
return nil , nil , err
}
}
}
2019-11-14 02:57:36 +00:00
comm . Review = review
2021-11-19 13:39:57 +00:00
return review , comm , committer . Commit ( )
2018-08-06 04:43:22 +00:00
}
2018-11-22 13:17:36 +00:00
2022-07-19 13:20:28 +00:00
// GetReviewOptions represent filter options for GetReviews
type GetReviewOptions struct {
IssueID int64
ReviewerID int64
Dismissed util . OptionalBool
}
// GetReviews return reviews based on GetReviewOptions
func GetReviews ( ctx context . Context , opts * GetReviewOptions ) ( [ ] * Review , error ) {
if opts == nil {
return nil , fmt . Errorf ( "opts are nil" )
}
sess := db . GetEngine ( ctx )
if opts . IssueID != 0 {
sess = sess . Where ( "issue_id=?" , opts . IssueID )
}
if opts . ReviewerID != 0 {
sess = sess . Where ( "reviewer_id=?" , opts . ReviewerID )
}
if ! opts . Dismissed . IsNone ( ) {
sess = sess . Where ( "dismissed=?" , opts . Dismissed . IsTrue ( ) )
}
reviews := make ( [ ] * Review , 0 , 4 )
return reviews , sess . Find ( & reviews )
}
2019-12-04 01:08:56 +00:00
// GetReviewersByIssueID gets the latest review of each reviewer for a pull request
2020-10-12 19:55:13 +00:00
func GetReviewersByIssueID ( issueID int64 ) ( [ ] * Review , error ) {
reviews := make ( [ ] * Review , 0 , 10 )
2018-11-22 13:17:36 +00:00
2021-11-21 15:41:00 +00:00
sess := db . GetEngine ( db . DefaultContext )
2018-11-22 13:17:36 +00:00
2021-07-08 11:38:13 +00:00
// Get latest review of each reviewer, sorted in order they were made
2021-02-11 17:32:25 +00: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 19:55:13 +00:00
Find ( & reviews ) ; err != nil {
2019-12-04 01:08:56 +00:00
return nil , err
}
2020-10-12 19:55:13 +00: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 13:17:36 +00:00
}
2019-12-04 01:08:56 +00:00
return reviews , nil
2018-11-22 13:17:36 +00:00
}
2020-01-09 01:47:45 +00:00
2020-10-14 12:11:11 +00: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 11:38:13 +00:00
// Get latest review of each reviewer, sorted in order they were made
2021-09-23 15:45:36 +00: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 12:11:11 +00:00
issueID , ReviewTypeApprove , ReviewTypeReject , ReviewTypeRequest ) .
Find ( & reviews ) ; err != nil {
return nil , err
}
return reviews , nil
}
2020-10-12 19:55:13 +00:00
// GetReviewByIssueIDAndUserID get the latest review of reviewer for a pull request
2022-05-20 14:08:52 +00:00
func GetReviewByIssueIDAndUserID ( ctx context . Context , issueID , userID int64 ) ( * Review , error ) {
2020-10-12 19:55:13 +00:00
review := new ( Review )
2020-04-06 16:33:34 +00:00
2022-05-20 14:08:52 +00:00
has , err := db . GetEngine ( ctx ) . 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 16:33:34 +00:00
issueID , userID , ReviewTypeApprove , ReviewTypeReject , ReviewTypeRequest ) .
2020-10-12 19:55:13 +00:00
Get ( review )
if err != nil {
return nil , err
}
if ! has {
return nil , ErrReviewNotExist { }
}
return review , nil
}
2022-01-10 09:32:37 +00:00
// GetTeamReviewerByIssueIDAndTeamID get the latest review request of reviewer team for a pull request
2022-05-20 14:08:52 +00:00
func GetTeamReviewerByIssueIDAndTeamID ( ctx context . Context , issueID , teamID int64 ) ( review * Review , err error ) {
2020-10-12 19:55:13 +00:00
review = new ( Review )
2022-06-20 10:02:49 +00:00
var has bool
2022-05-20 14:08:52 +00:00
if has , err = db . GetEngine ( ctx ) . SQL ( "SELECT * FROM review WHERE id IN (SELECT max(id) as id FROM review WHERE issue_id = ? AND reviewer_team_id = ?)" ,
2020-10-12 19:55:13 +00:00
issueID , teamID ) .
2020-04-06 16:33:34 +00:00
Get ( review ) ; err != nil {
return nil , err
}
2020-10-12 19:55:13 +00:00
if ! has {
return nil , ErrReviewNotExist { 0 }
}
2022-06-20 10:02:49 +00:00
return review , err
2020-04-06 16:33:34 +00:00
}
2020-01-09 01:47:45 +00:00
// MarkReviewsAsStale marks existing reviews as stale
func MarkReviewsAsStale ( issueID int64 ) ( err error ) {
2021-09-23 15:45:36 +00:00
_ , err = db . GetEngine ( db . DefaultContext ) . Exec ( "UPDATE `review` SET stale=? WHERE issue_id=?" , true , issueID )
2020-01-09 01:47:45 +00:00
2022-06-20 10:02:49 +00:00
return err
2020-01-09 01:47:45 +00:00
}
// MarkReviewsAsNotStale marks existing reviews as not stale for a giving commit SHA
func MarkReviewsAsNotStale ( issueID int64 , commitID string ) ( err error ) {
2021-09-23 15:45:36 +00:00
_ , err = db . GetEngine ( db . DefaultContext ) . Exec ( "UPDATE `review` SET stale=? WHERE issue_id=? AND commit_id=?" , false , issueID , commitID )
2020-01-09 01:47:45 +00:00
2022-06-20 10:02:49 +00:00
return err
2020-01-09 01:47:45 +00:00
}
2020-01-23 17:28:15 +00:00
2021-02-11 17:32:25 +00: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 10:03:11 +00:00
if review . ID == 0 {
return ErrReviewNotExist { }
}
2021-09-23 15:45:36 +00:00
_ , err = db . GetEngine ( db . DefaultContext ) . ID ( review . ID ) . Cols ( "dismissed" ) . Update ( review )
2021-02-11 17:32:25 +00:00
2022-06-20 10:02:49 +00:00
return err
2021-02-11 17:32:25 +00:00
}
2020-01-23 17:28:15 +00:00
// InsertReviews inserts review and review comments
func InsertReviews ( reviews [ ] * Review ) error {
2022-11-12 20:18:50 +00:00
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-21 15:41:00 +00:00
if err != nil {
2020-01-23 17:28:15 +00:00
return err
}
2021-11-21 15:41:00 +00:00
defer committer . Close ( )
sess := db . GetEngine ( ctx )
2020-01-23 17:28:15 +00:00
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 03:04:08 +00:00
if len ( review . Comments ) > 0 {
if _ , err := sess . NoAutoTime ( ) . Insert ( review . Comments ) ; err != nil {
return err
}
2020-01-23 17:28:15 +00:00
}
}
2021-11-21 15:41:00 +00:00
return committer . Commit ( )
2020-01-23 17:28:15 +00:00
}
2020-04-06 16:33:34 +00:00
2020-04-30 20:24:08 +00:00
// AddReviewRequest add a review request from one reviewer
2021-11-24 09:49:20 +00:00
func AddReviewRequest ( issue * Issue , reviewer , doer * user_model . User ) ( * Comment , error ) {
2022-11-12 20:18:50 +00:00
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-19 13:39:57 +00:00
if err != nil {
2020-04-06 16:33:34 +00:00
return nil , err
}
2021-11-19 13:39:57 +00:00
defer committer . Close ( )
sess := db . GetEngine ( ctx )
2020-04-06 16:33:34 +00:00
2022-05-20 14:08:52 +00:00
review , err := GetReviewByIssueIDAndUserID ( ctx , issue . ID , reviewer . ID )
2020-10-12 19:55:13 +00:00
if err != nil && ! IsErrReviewNotExist ( err ) {
2020-04-06 16:33:34 +00:00
return nil , err
}
2020-10-12 19:55:13 +00:00
// skip it when reviewer hase been request to review
if review != nil && review . Type == ReviewTypeRequest {
return nil , nil
2020-04-06 16:33:34 +00:00
}
2022-05-20 14:08:52 +00:00
official , err := IsOfficialReviewer ( ctx , issue , reviewer , doer )
2020-10-12 19:55:13 +00:00
if err != nil {
return nil , err
} else if official {
2020-04-06 16:33:34 +00: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
}
}
2022-05-20 14:08:52 +00:00
review , err = CreateReview ( ctx , CreateReviewOptions {
2020-04-06 16:33:34 +00:00
Type : ReviewTypeRequest ,
Issue : issue ,
Reviewer : reviewer ,
Official : official ,
Stale : false ,
2020-10-20 18:18:25 +00:00
} )
if err != nil {
2020-10-12 19:55:13 +00:00
return nil , err
2020-04-06 16:33:34 +00:00
}
2022-12-10 02:46:31 +00:00
comment , err := CreateComment ( ctx , & CreateCommentOptions {
2020-04-06 16:33:34 +00: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 18:18:25 +00:00
ReviewID : review . ID ,
2020-04-06 16:33:34 +00:00
} )
if err != nil {
return nil , err
}
2021-11-19 13:39:57 +00:00
return comment , committer . Commit ( )
2020-04-06 16:33:34 +00:00
}
2021-03-14 18:52:12 +00:00
// RemoveReviewRequest remove a review request from one reviewer
2021-11-24 09:49:20 +00:00
func RemoveReviewRequest ( issue * Issue , reviewer , doer * user_model . User ) ( * Comment , error ) {
2022-11-12 20:18:50 +00:00
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-19 13:39:57 +00:00
if err != nil {
2020-10-12 19:55:13 +00:00
return nil , err
}
2021-11-19 13:39:57 +00:00
defer committer . Close ( )
2020-10-12 19:55:13 +00:00
2022-05-20 14:08:52 +00:00
review , err := GetReviewByIssueIDAndUserID ( ctx , issue . ID , reviewer . ID )
2020-10-12 19:55:13 +00:00
if err != nil && ! IsErrReviewNotExist ( err ) {
return nil , err
2020-04-06 16:33:34 +00:00
}
2020-10-12 19:55:13 +00:00
if review == nil || review . Type != ReviewTypeRequest {
2020-04-06 16:33:34 +00:00
return nil , nil
}
2022-05-20 14:08:52 +00:00
if _ , err = db . DeleteByBean ( ctx , review ) ; err != nil {
2020-10-12 19:55:13 +00:00
return nil , err
}
2022-05-20 14:08:52 +00:00
official , err := IsOfficialReviewer ( ctx , issue , reviewer )
2020-10-12 19:55:13 +00:00
if err != nil {
return nil , err
} else if official {
2023-01-15 05:00:09 +00:00
if err := restoreLatestOfficialReview ( ctx , issue . ID , reviewer . ID ) ; err != nil {
2020-10-12 19:55:13 +00:00
return nil , err
}
}
2022-12-10 02:46:31 +00:00
comment , err := CreateComment ( ctx , & CreateCommentOptions {
2020-10-12 19:55:13 +00:00
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
}
2021-11-19 13:39:57 +00:00
return comment , committer . Commit ( )
2020-10-12 19:55:13 +00:00
}
2023-01-15 05:00:09 +00:00
// Recalculate the latest official review for reviewer
func restoreLatestOfficialReview ( ctx context . Context , issueID , reviewerID int64 ) error {
review , err := GetReviewByIssueIDAndUserID ( ctx , issueID , reviewerID )
if err != nil && ! IsErrReviewNotExist ( err ) {
return err
}
if review != nil {
if _ , err := db . Exec ( ctx , "UPDATE `review` SET official=? WHERE id=?" , true , review . ID ) ; err != nil {
return err
}
}
return nil
}
2020-10-12 19:55:13 +00:00
// AddTeamReviewRequest add a review request from one team
2022-03-29 06:29:02 +00:00
func AddTeamReviewRequest ( issue * Issue , reviewer * organization . Team , doer * user_model . User ) ( * Comment , error ) {
2022-11-12 20:18:50 +00:00
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-19 13:39:57 +00:00
if err != nil {
2020-04-06 16:33:34 +00:00
return nil , err
}
2021-11-19 13:39:57 +00:00
defer committer . Close ( )
2020-04-06 16:33:34 +00:00
2022-05-20 14:08:52 +00:00
review , err := GetTeamReviewerByIssueIDAndTeamID ( ctx , issue . ID , reviewer . ID )
2020-10-12 19:55:13 +00:00
if err != nil && ! IsErrReviewNotExist ( err ) {
2020-04-06 16:33:34 +00:00
return nil , err
}
2020-10-12 19:55:13 +00:00
// This team already has been requested to review - therefore skip this.
if review != nil {
return nil , nil
}
2022-05-20 14:08:52 +00:00
official , err := IsOfficialReviewerTeam ( ctx , issue , reviewer )
2020-04-06 16:33:34 +00:00
if err != nil {
2022-10-24 19:29:17 +00:00
return nil , fmt . Errorf ( "isOfficialReviewerTeam(): %w" , err )
2020-10-12 19:55:13 +00:00
} else if ! official {
2022-05-20 14:08:52 +00:00
if official , err = IsOfficialReviewer ( ctx , issue , doer ) ; err != nil {
2022-10-24 19:29:17 +00:00
return nil , fmt . Errorf ( "isOfficialReviewer(): %w" , err )
2020-10-12 19:55:13 +00:00
}
}
2022-05-20 14:08:52 +00:00
if review , err = CreateReview ( ctx , CreateReviewOptions {
2020-10-12 19:55:13 +00:00
Type : ReviewTypeRequest ,
Issue : issue ,
ReviewerTeam : reviewer ,
Official : official ,
Stale : false ,
} ) ; err != nil {
return nil , err
2020-04-06 16:33:34 +00:00
}
if official {
2022-05-20 14:08:52 +00:00
if _ , err := db . Exec ( ctx , "UPDATE `review` SET official=? WHERE issue_id=? AND reviewer_team_id=?" , false , issue . ID , reviewer . ID ) ; err != nil {
2020-10-12 19:55:13 +00:00
return nil , err
}
}
2022-12-10 02:46:31 +00:00
comment , err := CreateComment ( ctx , & CreateCommentOptions {
2020-10-12 19:55:13 +00:00
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 18:18:25 +00:00
ReviewID : review . ID ,
2020-10-12 19:55:13 +00:00
} )
if err != nil {
2022-12-10 02:46:31 +00:00
return nil , fmt . Errorf ( "CreateComment(): %w" , err )
2020-10-12 19:55:13 +00:00
}
2021-11-19 13:39:57 +00:00
return comment , committer . Commit ( )
2020-10-12 19:55:13 +00:00
}
2021-03-14 18:52:12 +00:00
// RemoveTeamReviewRequest remove a review request from one team
2022-03-29 06:29:02 +00:00
func RemoveTeamReviewRequest ( issue * Issue , reviewer * organization . Team , doer * user_model . User ) ( * Comment , error ) {
2022-11-12 20:18:50 +00:00
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-19 13:39:57 +00:00
if err != nil {
2020-10-12 19:55:13 +00:00
return nil , err
}
2021-11-19 13:39:57 +00:00
defer committer . Close ( )
2020-10-12 19:55:13 +00:00
2022-05-20 14:08:52 +00:00
review , err := GetTeamReviewerByIssueIDAndTeamID ( ctx , issue . ID , reviewer . ID )
2020-10-12 19:55:13 +00:00
if err != nil && ! IsErrReviewNotExist ( err ) {
return nil , err
}
if review == nil {
return nil , nil
}
2020-04-06 16:33:34 +00:00
2022-05-20 14:08:52 +00:00
if _ , err = db . DeleteByBean ( ctx , review ) ; err != nil {
2020-10-12 19:55:13 +00:00
return nil , err
}
2022-05-20 14:08:52 +00:00
official , err := IsOfficialReviewerTeam ( ctx , issue , reviewer )
2020-10-12 19:55:13 +00:00
if err != nil {
2022-10-24 19:29:17 +00:00
return nil , fmt . Errorf ( "isOfficialReviewerTeam(): %w" , err )
2020-10-12 19:55:13 +00:00
}
if official {
// recalculate which is the latest official review from that team
2022-05-20 14:08:52 +00:00
review , err := GetReviewByIssueIDAndUserID ( ctx , issue . ID , - reviewer . ID )
2020-10-12 19:55:13 +00:00
if err != nil && ! IsErrReviewNotExist ( err ) {
2020-04-06 16:33:34 +00:00
return nil , err
}
if review != nil {
2022-05-20 14:08:52 +00:00
if _ , err := db . Exec ( ctx , "UPDATE `review` SET official=? WHERE id=?" , true , review . ID ) ; err != nil {
2020-04-06 16:33:34 +00:00
return nil , err
}
}
}
2020-10-12 19:55:13 +00:00
if doer == nil {
2021-11-19 13:39:57 +00:00
return nil , committer . Commit ( )
2020-04-06 16:33:34 +00:00
}
2022-12-10 02:46:31 +00:00
comment , err := CreateComment ( ctx , & CreateCommentOptions {
2020-04-06 16:33:34 +00:00
Type : CommentTypeReviewRequest ,
Doer : doer ,
Repo : issue . Repo ,
Issue : issue ,
RemovedAssignee : true , // Use RemovedAssignee as !isRequest
2020-10-12 19:55:13 +00:00
AssigneeTeamID : reviewer . ID , // Use AssigneeTeamID as reviewer team ID
2020-04-06 16:33:34 +00:00
} )
if err != nil {
2022-12-10 02:46:31 +00:00
return nil , fmt . Errorf ( "CreateComment(): %w" , err )
2020-04-06 16:33:34 +00:00
}
2021-11-19 13:39:57 +00:00
return comment , committer . Commit ( )
2020-04-06 16:33:34 +00:00
}
2020-04-18 13:50:25 +00:00
// MarkConversation Add or remove Conversation mark for a code comment
2021-11-24 09:49:20 +00:00
func MarkConversation ( comment * Comment , doer * user_model . User , isResolve bool ) ( err error ) {
2020-04-18 13:50:25 +00:00
if comment . Type != CommentTypeCode {
return nil
}
if isResolve {
if comment . ResolveDoerID != 0 {
return nil
}
2021-09-23 15:45:36 +00: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 13:50:25 +00:00
return err
}
} else {
if comment . ResolveDoerID == 0 {
return nil
}
2021-09-23 15:45:36 +00:00
if _ , err = db . GetEngine ( db . DefaultContext ) . Exec ( "UPDATE `comment` SET resolve_doer_id=? WHERE id=?" , 0 , comment . ID ) ; err != nil {
2020-04-18 13:50:25 +00: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
2021-11-24 09:49:20 +00:00
func CanMarkConversation ( issue * Issue , doer * user_model . User ) ( permResult bool , err error ) {
2020-04-18 13:50:25 +00:00
if doer == nil || issue == nil {
return false , fmt . Errorf ( "issue or doer is nil" )
}
if doer . ID != issue . PosterID {
2022-04-08 09:11:15 +00:00
if err = issue . LoadRepo ( db . DefaultContext ) ; err != nil {
2020-04-18 13:50:25 +00:00
return false , err
}
2022-05-11 10:09:36 +00:00
p , err := access_model . GetUserRepoPermission ( db . DefaultContext , issue . Repo , doer )
2020-04-18 13:50:25 +00:00
if err != nil {
return false , err
}
2021-11-28 11:58:28 +00:00
permResult = p . CanAccess ( perm . AccessModeWrite , unit . TypePullRequests )
2020-04-18 13:50:25 +00:00
if ! permResult {
2022-05-20 14:08:52 +00:00
if permResult , err = IsOfficialReviewer ( db . DefaultContext , issue , doer ) ; err != nil {
2020-04-18 13:50:25 +00:00
return false , err
}
}
if ! permResult {
return false , nil
}
}
return true , nil
}
2020-05-02 00:20:51 +00:00
// DeleteReview delete a review and it's code comments
func DeleteReview ( r * Review ) error {
2022-11-12 20:18:50 +00:00
ctx , committer , err := db . TxContext ( db . DefaultContext )
2021-11-21 15:41:00 +00:00
if err != nil {
2020-05-02 00:20:51 +00:00
return err
}
2021-11-21 15:41:00 +00:00
defer committer . Close ( )
sess := db . GetEngine ( ctx )
2020-05-02 00:20:51 +00:00
if r . ID == 0 {
return fmt . Errorf ( "review is not allowed to be 0" )
}
2020-10-20 18:18:25 +00:00
if r . Type == ReviewTypeRequest {
return fmt . Errorf ( "review request can not be deleted using this method" )
}
2020-05-02 00:20:51 +00:00
opts := FindCommentsOptions {
Type : CommentTypeCode ,
IssueID : r . IssueID ,
ReviewID : r . ID ,
}
2023-01-17 21:03:44 +00:00
if _ , err := sess . Where ( opts . ToConds ( ) ) . Delete ( new ( Comment ) ) ; err != nil {
2020-05-02 00:20:51 +00:00
return err
}
opts = FindCommentsOptions {
Type : CommentTypeReview ,
IssueID : r . IssueID ,
ReviewID : r . ID ,
}
2023-01-17 21:03:44 +00:00
if _ , err := sess . Where ( opts . ToConds ( ) ) . Delete ( new ( Comment ) ) ; err != nil {
2020-05-02 00:20:51 +00:00
return err
}
if _ , err := sess . ID ( r . ID ) . Delete ( new ( Review ) ) ; err != nil {
return err
}
2023-01-15 05:00:09 +00:00
if r . Official {
if err := restoreLatestOfficialReview ( ctx , r . IssueID , r . ReviewerID ) ; err != nil {
return err
}
}
2021-11-21 15:41:00 +00:00
return committer . Commit ( )
2020-05-02 00:20:51 +00:00
}
// GetCodeCommentsCount return count of CodeComments a Review has
func ( r * Review ) GetCodeCommentsCount ( ) int {
opts := FindCommentsOptions {
Type : CommentTypeCode ,
IssueID : r . IssueID ,
ReviewID : r . ID ,
}
2023-01-17 21:03:44 +00:00
conds := opts . ToConds ( )
2020-05-02 00:20:51 +00:00
if r . ID == 0 {
conds = conds . And ( builder . Eq { "invalidated" : false } )
}
2021-09-23 15:45:36 +00:00
count , err := db . GetEngine ( db . DefaultContext ) . Where ( conds ) . Count ( new ( Comment ) )
2020-05-02 00:20:51 +00: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 )
2023-01-17 21:03:44 +00:00
has , err := db . GetEngine ( db . DefaultContext ) . Where ( opts . ToConds ( ) ) . Get ( comment )
2020-05-02 00:20:51 +00:00
if err != nil || ! has {
return ""
}
return comment . HTMLURL ( )
}
2022-02-01 18:20:28 +00:00
// RemapExternalUser ExternalUserRemappable interface
func ( r * Review ) RemapExternalUser ( externalName string , externalID , userID int64 ) error {
r . OriginalAuthor = externalName
r . OriginalAuthorID = externalID
r . ReviewerID = userID
return nil
}
// GetUserID ExternalUserRemappable interface
func ( r * Review ) GetUserID ( ) int64 { return r . ReviewerID }
// GetExternalName ExternalUserRemappable interface
func ( r * Review ) GetExternalName ( ) string { return r . OriginalAuthor }
// GetExternalID ExternalUserRemappable interface
func ( r * Review ) GetExternalID ( ) int64 { return r . OriginalAuthorID }
2022-06-13 09:37:59 +00:00
// UpdateReviewsMigrationsByType updates reviews' migrations information via given git service type and original id and poster id
func UpdateReviewsMigrationsByType ( tp structs . GitServiceType , originalAuthorID string , posterID int64 ) error {
_ , err := db . GetEngine ( db . DefaultContext ) . Table ( "review" ) .
Where ( "original_author_id = ?" , originalAuthorID ) .
And ( migratedIssueCond ( tp ) ) .
Update ( map [ string ] interface { } {
"reviewer_id" : posterID ,
"original_author" : "" ,
"original_author_id" : 0 ,
} )
return err
}