mirror of
				https://gitee.com/gitea/gitea
				synced 2025-11-04 08:30:25 +08:00 
			
		
		
		
	Add branch protection option to block merge on requested changes. (#9592)
* Add branch protection option to block merge on requested changes. * Add migration step * Fix check to correct negation * Apply suggestions from code review Language improvement. Co-Authored-By: John Olheiser <42128690+jolheiser@users.noreply.github.com> * Copyright year. Co-authored-by: John Olheiser <42128690+jolheiser@users.noreply.github.com> Co-authored-by: Lauris BH <lauris@nix.lv>
This commit is contained in:
		
				
					committed by
					
						
						Lauris BH
					
				
			
			
				
	
			
			
			
						parent
						
							b39fab41c8
						
					
				
				
					commit
					ea707f5a77
				
			@@ -44,6 +44,7 @@ type ProtectedBranch struct {
 | 
			
		||||
	ApprovalsWhitelistUserIDs []int64            `xorm:"JSON TEXT"`
 | 
			
		||||
	ApprovalsWhitelistTeamIDs []int64            `xorm:"JSON TEXT"`
 | 
			
		||||
	RequiredApprovals         int64              `xorm:"NOT NULL DEFAULT 0"`
 | 
			
		||||
	BlockOnRejectedReviews    bool               `xorm:"NOT NULL DEFAULT false"`
 | 
			
		||||
	CreatedUnix               timeutil.TimeStamp `xorm:"created"`
 | 
			
		||||
	UpdatedUnix               timeutil.TimeStamp `xorm:"updated"`
 | 
			
		||||
}
 | 
			
		||||
@@ -166,6 +167,23 @@ func (protectBranch *ProtectedBranch) GetGrantedApprovalsCount(pr *PullRequest)
 | 
			
		||||
	return approvals
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// MergeBlockedByRejectedReview returns true if merge is blocked by rejected reviews
 | 
			
		||||
func (protectBranch *ProtectedBranch) MergeBlockedByRejectedReview(pr *PullRequest) bool {
 | 
			
		||||
	if !protectBranch.BlockOnRejectedReviews {
 | 
			
		||||
		return false
 | 
			
		||||
	}
 | 
			
		||||
	rejectExist, err := x.Where("issue_id = ?", pr.IssueID).
 | 
			
		||||
		And("type = ?", ReviewTypeReject).
 | 
			
		||||
		And("official = ?", true).
 | 
			
		||||
		Exist(new(Review))
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		log.Error("MergeBlockedByRejectedReview: %v", err)
 | 
			
		||||
		return true
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return rejectExist
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
// GetProtectedBranchByRepoID getting protected branch by repo ID
 | 
			
		||||
func GetProtectedBranchByRepoID(repoID int64) ([]*ProtectedBranch, error) {
 | 
			
		||||
	protectedBranches := make([]*ProtectedBranch, 0)
 | 
			
		||||
@@ -340,7 +358,7 @@ func (repo *Repository) IsProtectedBranchForMerging(pr *PullRequest, branchName
 | 
			
		||||
	if err != nil {
 | 
			
		||||
		return true, err
 | 
			
		||||
	} else if has {
 | 
			
		||||
		return !protectedBranch.CanUserMerge(doer.ID) || !protectedBranch.HasEnoughApprovals(pr), nil
 | 
			
		||||
		return !protectedBranch.CanUserMerge(doer.ID) || !protectedBranch.HasEnoughApprovals(pr) || protectedBranch.MergeBlockedByRejectedReview(pr), nil
 | 
			
		||||
	}
 | 
			
		||||
 | 
			
		||||
	return false, nil
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user