2019-12-04 01:08:56 +00:00
// Copyright 2019 The Gitea Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package migrations
import (
"code.gitea.io/gitea/models"
"xorm.io/xorm"
)
func addBranchProtectionCanPushAndEnableWhitelist ( x * xorm . Engine ) error {
type ProtectedBranch struct {
CanPush bool ` xorm:"NOT NULL DEFAULT false" `
EnableApprovalsWhitelist bool ` xorm:"NOT NULL DEFAULT false" `
RequiredApprovals int64 ` xorm:"NOT NULL DEFAULT 0" `
}
type Review struct {
ID int64 ` xorm:"pk autoincr" `
Official bool ` xorm:"NOT NULL DEFAULT false" `
}
2020-09-16 07:37:04 +00:00
if err := x . Sync2 ( new ( ProtectedBranch ) ) ; err != nil {
2019-12-04 01:08:56 +00:00
return err
}
2020-09-16 07:37:04 +00:00
if err := x . Sync2 ( new ( Review ) ) ; err != nil {
2019-12-04 01:08:56 +00:00
return err
}
2020-09-16 07:37:04 +00:00
if _ , err := x . Exec ( "UPDATE `protected_branch` SET `enable_whitelist` = ? WHERE enable_whitelist IS NULL" , false ) ; err != nil {
2019-12-21 10:12:39 +00:00
return err
}
2020-09-16 07:37:04 +00:00
if _ , err := x . Exec ( "UPDATE `protected_branch` SET `can_push` = `enable_whitelist`" ) ; err != nil {
2019-12-04 01:08:56 +00:00
return err
}
2020-09-16 07:37:04 +00:00
if _ , err := x . Exec ( "UPDATE `protected_branch` SET `enable_approvals_whitelist` = ? WHERE `required_approvals` > ?" , true , 0 ) ; err != nil {
2019-12-04 01:08:56 +00:00
return err
}
var pageSize int64 = 20
2020-09-16 07:37:04 +00:00
qresult , err := x . QueryInterface ( "SELECT max(id) as max_id FROM issue" )
2019-12-04 01:08:56 +00:00
if err != nil {
return err
}
var totalIssues int64
totalIssues , ok := qresult [ 0 ] [ "max_id" ] . ( int64 )
if ! ok {
// If there are no issues at all we ignore it
return nil
}
totalPages := totalIssues / pageSize
2020-09-16 07:37:04 +00:00
sess := x . NewSession ( )
defer sess . Close ( )
if err := sess . Begin ( ) ; err != nil {
return err
}
2019-12-04 01:08:56 +00:00
// Find latest review of each user in each pull request, and set official field if appropriate
2020-09-16 07:37:04 +00:00
2019-12-04 01:08:56 +00:00
var page int64
2020-09-16 07:37:04 +00:00
var count int
2019-12-04 01:08:56 +00:00
for page = 0 ; page <= totalPages ; page ++ {
2020-09-16 07:37:04 +00:00
reviews := [ ] * models . Review { }
2019-12-04 01:08:56 +00:00
if err := sess . SQL ( "SELECT * FROM review WHERE id IN (SELECT max(id) as id FROM review WHERE issue_id > ? AND issue_id <= ? AND type in (?, ?) GROUP BY issue_id, reviewer_id)" ,
page * pageSize , ( page + 1 ) * pageSize , models . ReviewTypeApprove , models . ReviewTypeReject ) .
Find ( & reviews ) ; err != nil {
return err
}
for _ , review := range reviews {
2020-09-16 07:37:04 +00:00
if err := review . LoadAttributesX ( sess ) ; err != nil {
2019-12-04 01:08:56 +00:00
// Error might occur if user or issue doesn't exist, ignore it.
continue
}
2020-09-16 07:37:04 +00:00
official , err := models . IsOfficialReviewerX ( sess , review . Issue , review . Reviewer )
2019-12-04 01:08:56 +00:00
if err != nil {
// Branch might not be proteced or other error, ignore it.
continue
}
review . Official = official
2020-09-16 07:37:04 +00:00
count ++
2019-12-04 01:08:56 +00:00
if _ , err := sess . ID ( review . ID ) . Cols ( "official" ) . Update ( review ) ; err != nil {
return err
}
2020-09-16 07:37:04 +00:00
if count == 100 {
if err := sess . Commit ( ) ; err != nil {
return err
}
count = 0
if err := sess . Begin ( ) ; err != nil {
return err
}
}
}
2019-12-04 01:08:56 +00:00
}
2020-09-16 07:37:04 +00:00
if count > 0 {
return sess . Commit ( )
}
return nil
2019-12-04 01:08:56 +00:00
}