2017-02-21 20:32:10 +05:30
|
|
|
// Copyright 2016 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2017-02-21 20:32:10 +05:30
|
|
|
|
2022-06-12 21:21:54 +05:30
|
|
|
package git
|
2017-02-21 20:32:10 +05:30
|
|
|
|
|
|
|
import (
|
2019-12-15 15:21:28 +05:30
|
|
|
"context"
|
2017-02-21 20:32:10 +05:30
|
|
|
"fmt"
|
2020-03-27 03:56:34 +05:30
|
|
|
"strings"
|
2017-02-21 20:32:10 +05:30
|
|
|
"time"
|
2017-09-14 13:46:22 +05:30
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-03-29 11:59:02 +05:30
|
|
|
"code.gitea.io/gitea/models/organization"
|
2021-11-28 17:28:28 +05:30
|
|
|
"code.gitea.io/gitea/models/perm"
|
2022-05-11 15:39:36 +05:30
|
|
|
access_model "code.gitea.io/gitea/models/perm/access"
|
2021-12-10 06:57:50 +05:30
|
|
|
repo_model "code.gitea.io/gitea/models/repo"
|
2021-11-10 01:27:58 +05:30
|
|
|
"code.gitea.io/gitea/models/unit"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2017-09-14 13:46:22 +05:30
|
|
|
"code.gitea.io/gitea/modules/base"
|
|
|
|
"code.gitea.io/gitea/modules/log"
|
2019-08-15 20:16:21 +05:30
|
|
|
"code.gitea.io/gitea/modules/timeutil"
|
2017-09-14 13:46:22 +05:30
|
|
|
"code.gitea.io/gitea/modules/util"
|
|
|
|
|
2020-03-27 03:56:34 +05:30
|
|
|
"github.com/gobwas/glob"
|
2017-02-21 20:32:10 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
// ProtectedBranch struct
|
|
|
|
type ProtectedBranch struct {
|
2020-11-29 01:00:46 +05:30
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
RepoID int64 `xorm:"UNIQUE(s)"`
|
|
|
|
BranchName string `xorm:"UNIQUE(s)"`
|
|
|
|
CanPush bool `xorm:"NOT NULL DEFAULT false"`
|
|
|
|
EnableWhitelist bool
|
|
|
|
WhitelistUserIDs []int64 `xorm:"JSON TEXT"`
|
|
|
|
WhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
|
|
|
|
EnableMergeWhitelist bool `xorm:"NOT NULL DEFAULT false"`
|
|
|
|
WhitelistDeployKeys bool `xorm:"NOT NULL DEFAULT false"`
|
|
|
|
MergeWhitelistUserIDs []int64 `xorm:"JSON TEXT"`
|
|
|
|
MergeWhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
|
|
|
|
EnableStatusCheck bool `xorm:"NOT NULL DEFAULT false"`
|
|
|
|
StatusCheckContexts []string `xorm:"JSON TEXT"`
|
|
|
|
EnableApprovalsWhitelist bool `xorm:"NOT NULL DEFAULT false"`
|
|
|
|
ApprovalsWhitelistUserIDs []int64 `xorm:"JSON TEXT"`
|
|
|
|
ApprovalsWhitelistTeamIDs []int64 `xorm:"JSON TEXT"`
|
|
|
|
RequiredApprovals int64 `xorm:"NOT NULL DEFAULT 0"`
|
|
|
|
BlockOnRejectedReviews bool `xorm:"NOT NULL DEFAULT false"`
|
|
|
|
BlockOnOfficialReviewRequests bool `xorm:"NOT NULL DEFAULT false"`
|
|
|
|
BlockOnOutdatedBranch bool `xorm:"NOT NULL DEFAULT false"`
|
|
|
|
DismissStaleApprovals bool `xorm:"NOT NULL DEFAULT false"`
|
|
|
|
RequireSignedCommits bool `xorm:"NOT NULL DEFAULT false"`
|
|
|
|
ProtectedFilePatterns string `xorm:"TEXT"`
|
2021-09-11 19:51:17 +05:30
|
|
|
UnprotectedFilePatterns string `xorm:"TEXT"`
|
2020-01-09 07:17:45 +05:30
|
|
|
|
|
|
|
CreatedUnix timeutil.TimeStamp `xorm:"created"`
|
|
|
|
UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
|
2017-09-14 13:46:22 +05:30
|
|
|
}
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
func init() {
|
|
|
|
db.RegisterModel(new(ProtectedBranch))
|
|
|
|
db.RegisterModel(new(DeletedBranch))
|
2021-10-08 22:33:04 +05:30
|
|
|
db.RegisterModel(new(RenamedBranch))
|
2021-09-19 17:19:59 +05:30
|
|
|
}
|
|
|
|
|
2017-09-14 13:46:22 +05:30
|
|
|
// IsProtected returns if the branch is protected
|
|
|
|
func (protectBranch *ProtectedBranch) IsProtected() bool {
|
|
|
|
return protectBranch.ID > 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// CanUserPush returns if some user could push to this protected branch
|
|
|
|
func (protectBranch *ProtectedBranch) CanUserPush(userID int64) bool {
|
2019-12-04 06:38:56 +05:30
|
|
|
if !protectBranch.CanPush {
|
2017-09-14 13:46:22 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-12-04 06:38:56 +05:30
|
|
|
if !protectBranch.EnableWhitelist {
|
2022-12-03 08:18:26 +05:30
|
|
|
if user, err := user_model.GetUserByID(db.DefaultContext, userID); err != nil {
|
2019-12-04 06:38:56 +05:30
|
|
|
log.Error("GetUserByID: %v", err)
|
|
|
|
return false
|
2022-12-03 08:18:26 +05:30
|
|
|
} else if repo, err := repo_model.GetRepositoryByID(db.DefaultContext, protectBranch.RepoID); err != nil {
|
2021-12-10 06:57:50 +05:30
|
|
|
log.Error("repo_model.GetRepositoryByID: %v", err)
|
2019-12-04 06:38:56 +05:30
|
|
|
return false
|
2022-05-11 15:39:36 +05:30
|
|
|
} else if writeAccess, err := access_model.HasAccessUnit(db.DefaultContext, user, repo, unit.TypeCode, perm.AccessModeWrite); err != nil {
|
2019-12-04 06:38:56 +05:30
|
|
|
log.Error("HasAccessUnit: %v", err)
|
|
|
|
return false
|
|
|
|
} else {
|
|
|
|
return writeAccess
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-14 13:46:22 +05:30
|
|
|
if base.Int64sContains(protectBranch.WhitelistUserIDs, userID) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(protectBranch.WhitelistTeamIDs) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-03-29 11:59:02 +05:30
|
|
|
in, err := organization.IsUserInTeams(db.DefaultContext, userID, protectBranch.WhitelistTeamIDs)
|
2017-09-14 13:46:22 +05:30
|
|
|
if err != nil {
|
2019-04-02 13:18:31 +05:30
|
|
|
log.Error("IsUserInTeams: %v", err)
|
2017-09-14 13:46:22 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
return in
|
2017-02-21 20:32:10 +05:30
|
|
|
}
|
|
|
|
|
2020-01-11 12:59:34 +05:30
|
|
|
// IsUserMergeWhitelisted checks if some user is whitelisted to merge to this branch
|
2022-05-11 15:39:36 +05:30
|
|
|
func IsUserMergeWhitelisted(ctx context.Context, protectBranch *ProtectedBranch, userID int64, permissionInRepo access_model.Permission) bool {
|
2018-03-25 15:31:32 +05:30
|
|
|
if !protectBranch.EnableMergeWhitelist {
|
2020-08-20 13:18:40 +05:30
|
|
|
// Then we need to fall back on whether the user has write permission
|
2021-11-10 01:27:58 +05:30
|
|
|
return permissionInRepo.CanWrite(unit.TypeCode)
|
2018-03-25 15:31:32 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
if base.Int64sContains(protectBranch.MergeWhitelistUserIDs, userID) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-07-28 00:41:24 +05:30
|
|
|
if len(protectBranch.MergeWhitelistTeamIDs) == 0 {
|
2018-03-25 15:31:32 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-05-04 01:16:28 +05:30
|
|
|
in, err := organization.IsUserInTeams(ctx, userID, protectBranch.MergeWhitelistTeamIDs)
|
2018-03-25 15:31:32 +05:30
|
|
|
if err != nil {
|
2019-04-02 13:18:31 +05:30
|
|
|
log.Error("IsUserInTeams: %v", err)
|
2018-03-25 15:31:32 +05:30
|
|
|
return false
|
|
|
|
}
|
|
|
|
return in
|
|
|
|
}
|
|
|
|
|
2019-12-04 06:38:56 +05:30
|
|
|
// IsUserOfficialReviewer check if user is official reviewer for the branch (counts towards required approvals)
|
2022-12-03 08:18:26 +05:30
|
|
|
func IsUserOfficialReviewer(ctx context.Context, protectBranch *ProtectedBranch, user *user_model.User) (bool, error) {
|
|
|
|
repo, err := repo_model.GetRepositoryByID(ctx, protectBranch.RepoID)
|
2019-12-04 06:38:56 +05:30
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !protectBranch.EnableApprovalsWhitelist {
|
|
|
|
// Anyone with write access is considered official reviewer
|
2022-05-11 15:39:36 +05:30
|
|
|
writeAccess, err := access_model.HasAccessUnit(ctx, user, repo, unit.TypeCode, perm.AccessModeWrite)
|
2019-12-04 06:38:56 +05:30
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return writeAccess, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if base.Int64sContains(protectBranch.ApprovalsWhitelistUserIDs, user.ID) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
2022-03-29 11:59:02 +05:30
|
|
|
inTeam, err := organization.IsUserInTeams(ctx, user.ID, protectBranch.ApprovalsWhitelistTeamIDs)
|
2019-12-04 06:38:56 +05:30
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return inTeam, nil
|
|
|
|
}
|
|
|
|
|
2020-03-27 03:56:34 +05:30
|
|
|
// GetProtectedFilePatterns parses a semicolon separated list of protected file patterns and returns a glob.Glob slice
|
|
|
|
func (protectBranch *ProtectedBranch) GetProtectedFilePatterns() []glob.Glob {
|
2021-09-11 19:51:17 +05:30
|
|
|
return getFilePatterns(protectBranch.ProtectedFilePatterns)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetUnprotectedFilePatterns parses a semicolon separated list of unprotected file patterns and returns a glob.Glob slice
|
|
|
|
func (protectBranch *ProtectedBranch) GetUnprotectedFilePatterns() []glob.Glob {
|
|
|
|
return getFilePatterns(protectBranch.UnprotectedFilePatterns)
|
|
|
|
}
|
|
|
|
|
|
|
|
func getFilePatterns(filePatterns string) []glob.Glob {
|
2020-03-27 03:56:34 +05:30
|
|
|
extarr := make([]glob.Glob, 0, 10)
|
2021-09-11 19:51:17 +05:30
|
|
|
for _, expr := range strings.Split(strings.ToLower(filePatterns), ";") {
|
2020-03-27 03:56:34 +05:30
|
|
|
expr = strings.TrimSpace(expr)
|
|
|
|
if expr != "" {
|
|
|
|
if g, err := glob.Compile(expr, '.', '/'); err != nil {
|
2021-07-08 17:08:13 +05:30
|
|
|
log.Info("Invalid glob expression '%s' (skipped): %v", expr, err)
|
2020-03-27 03:56:34 +05:30
|
|
|
} else {
|
|
|
|
extarr = append(extarr, g)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return extarr
|
|
|
|
}
|
|
|
|
|
2020-10-14 00:20:57 +05:30
|
|
|
// MergeBlockedByProtectedFiles returns true if merge is blocked by protected files change
|
2022-06-12 21:21:54 +05:30
|
|
|
func (protectBranch *ProtectedBranch) MergeBlockedByProtectedFiles(changedProtectedFiles []string) bool {
|
2020-10-14 00:20:57 +05:30
|
|
|
glob := protectBranch.GetProtectedFilePatterns()
|
|
|
|
if len(glob) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-06-12 21:21:54 +05:30
|
|
|
return len(changedProtectedFiles) > 0
|
2020-10-14 00:20:57 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// IsProtectedFile return if path is protected
|
|
|
|
func (protectBranch *ProtectedBranch) IsProtectedFile(patterns []glob.Glob, path string) bool {
|
|
|
|
if len(patterns) == 0 {
|
|
|
|
patterns = protectBranch.GetProtectedFilePatterns()
|
|
|
|
if len(patterns) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lpath := strings.ToLower(strings.TrimSpace(path))
|
|
|
|
|
|
|
|
r := false
|
|
|
|
for _, pat := range patterns {
|
|
|
|
if pat.Match(lpath) {
|
|
|
|
r = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2021-09-11 19:51:17 +05:30
|
|
|
// IsUnprotectedFile return if path is unprotected
|
|
|
|
func (protectBranch *ProtectedBranch) IsUnprotectedFile(patterns []glob.Glob, path string) bool {
|
|
|
|
if len(patterns) == 0 {
|
|
|
|
patterns = protectBranch.GetUnprotectedFilePatterns()
|
|
|
|
if len(patterns) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lpath := strings.ToLower(strings.TrimSpace(path))
|
|
|
|
|
|
|
|
r := false
|
|
|
|
for _, pat := range patterns {
|
|
|
|
if pat.Match(lpath) {
|
|
|
|
r = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2017-02-21 20:32:10 +05:30
|
|
|
// GetProtectedBranchBy getting protected branch by ID/Name
|
2022-05-20 19:38:52 +05:30
|
|
|
func GetProtectedBranchBy(ctx context.Context, repoID int64, branchName string) (*ProtectedBranch, error) {
|
2019-06-13 01:11:28 +05:30
|
|
|
rel := &ProtectedBranch{RepoID: repoID, BranchName: branchName}
|
2022-05-20 19:38:52 +05:30
|
|
|
has, err := db.GetByBean(ctx, rel)
|
2017-02-21 20:32:10 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return rel, nil
|
|
|
|
}
|
|
|
|
|
2018-12-11 16:58:37 +05:30
|
|
|
// WhitelistOptions represent all sorts of whitelists used for protected branches
|
|
|
|
type WhitelistOptions struct {
|
|
|
|
UserIDs []int64
|
|
|
|
TeamIDs []int64
|
|
|
|
|
|
|
|
MergeUserIDs []int64
|
|
|
|
MergeTeamIDs []int64
|
|
|
|
|
|
|
|
ApprovalsUserIDs []int64
|
|
|
|
ApprovalsTeamIDs []int64
|
|
|
|
}
|
|
|
|
|
2017-09-14 13:46:22 +05:30
|
|
|
// UpdateProtectBranch saves branch protection options of repository.
|
|
|
|
// If ID is 0, it creates a new record. Otherwise, updates existing record.
|
|
|
|
// This function also performs check if whitelist user and team's IDs have been changed
|
|
|
|
// to avoid unnecessary whitelist delete and regenerate.
|
2022-04-28 17:18:48 +05:30
|
|
|
func UpdateProtectBranch(ctx context.Context, repo *repo_model.Repository, protectBranch *ProtectedBranch, opts WhitelistOptions) (err error) {
|
|
|
|
if err = repo.GetOwner(ctx); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("GetOwner: %w", err)
|
2017-09-14 13:46:22 +05:30
|
|
|
}
|
|
|
|
|
2022-04-28 17:18:48 +05:30
|
|
|
whitelist, err := updateUserWhitelist(ctx, repo, protectBranch.WhitelistUserIDs, opts.UserIDs)
|
2018-03-25 15:31:32 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
protectBranch.WhitelistUserIDs = whitelist
|
2017-09-14 13:46:22 +05:30
|
|
|
|
2022-04-28 17:18:48 +05:30
|
|
|
whitelist, err = updateUserWhitelist(ctx, repo, protectBranch.MergeWhitelistUserIDs, opts.MergeUserIDs)
|
2018-03-25 15:31:32 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-09-14 13:46:22 +05:30
|
|
|
}
|
2018-03-25 15:31:32 +05:30
|
|
|
protectBranch.MergeWhitelistUserIDs = whitelist
|
2017-09-14 13:46:22 +05:30
|
|
|
|
2022-04-28 17:18:48 +05:30
|
|
|
whitelist, err = updateApprovalWhitelist(ctx, repo, protectBranch.ApprovalsWhitelistUserIDs, opts.ApprovalsUserIDs)
|
2018-12-11 16:58:37 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
protectBranch.ApprovalsWhitelistUserIDs = whitelist
|
|
|
|
|
2018-03-25 15:31:32 +05:30
|
|
|
// if the repo is in an organization
|
2022-04-28 17:18:48 +05:30
|
|
|
whitelist, err = updateTeamWhitelist(ctx, repo, protectBranch.WhitelistTeamIDs, opts.TeamIDs)
|
2018-03-25 15:31:32 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
2017-09-14 13:46:22 +05:30
|
|
|
}
|
2018-03-25 15:31:32 +05:30
|
|
|
protectBranch.WhitelistTeamIDs = whitelist
|
|
|
|
|
2022-04-28 17:18:48 +05:30
|
|
|
whitelist, err = updateTeamWhitelist(ctx, repo, protectBranch.MergeWhitelistTeamIDs, opts.MergeTeamIDs)
|
2018-03-25 15:31:32 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
protectBranch.MergeWhitelistTeamIDs = whitelist
|
2017-09-14 13:46:22 +05:30
|
|
|
|
2022-04-28 17:18:48 +05:30
|
|
|
whitelist, err = updateTeamWhitelist(ctx, repo, protectBranch.ApprovalsWhitelistTeamIDs, opts.ApprovalsTeamIDs)
|
2018-12-11 16:58:37 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
protectBranch.ApprovalsWhitelistTeamIDs = whitelist
|
|
|
|
|
2017-09-14 13:46:22 +05:30
|
|
|
// Make sure protectBranch.ID is not 0 for whitelists
|
|
|
|
if protectBranch.ID == 0 {
|
2022-04-28 17:18:48 +05:30
|
|
|
if _, err = db.GetEngine(ctx).Insert(protectBranch); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("Insert: %w", err)
|
2017-09-14 13:46:22 +05:30
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-28 17:18:48 +05:30
|
|
|
if _, err = db.GetEngine(ctx).ID(protectBranch.ID).AllCols().Update(protectBranch); err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return fmt.Errorf("Update: %w", err)
|
2017-09-14 13:46:22 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-15 06:22:01 +05:30
|
|
|
// GetProtectedBranches get all protected branches
|
2021-12-10 06:57:50 +05:30
|
|
|
func GetProtectedBranches(repoID int64) ([]*ProtectedBranch, error) {
|
2017-02-21 20:32:10 +05:30
|
|
|
protectedBranches := make([]*ProtectedBranch, 0)
|
2021-12-10 06:57:50 +05:30
|
|
|
return protectedBranches, db.GetEngine(db.DefaultContext).Find(&protectedBranches, &ProtectedBranch{RepoID: repoID})
|
2019-11-17 01:09:18 +05:30
|
|
|
}
|
|
|
|
|
2017-05-02 06:19:55 +05:30
|
|
|
// IsProtectedBranch checks if branch is protected
|
2021-12-10 06:57:50 +05:30
|
|
|
func IsProtectedBranch(repoID int64, branchName string) (bool, error) {
|
2017-05-02 06:19:55 +05:30
|
|
|
protectedBranch := &ProtectedBranch{
|
2021-12-10 06:57:50 +05:30
|
|
|
RepoID: repoID,
|
2017-05-02 06:19:55 +05:30
|
|
|
BranchName: branchName,
|
|
|
|
}
|
|
|
|
|
2021-09-23 21:15:36 +05:30
|
|
|
has, err := db.GetEngine(db.DefaultContext).Exist(protectedBranch)
|
2018-08-08 08:47:11 +05:30
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
return has, nil
|
|
|
|
}
|
|
|
|
|
2019-10-09 00:48:17 +05:30
|
|
|
// updateApprovalWhitelist checks whether the user whitelist changed and returns a whitelist with
|
|
|
|
// the users from newWhitelist which have explicit read or write access to the repo.
|
2022-04-28 17:18:48 +05:30
|
|
|
func updateApprovalWhitelist(ctx context.Context, repo *repo_model.Repository, currentWhitelist, newWhitelist []int64) (whitelist []int64, err error) {
|
2019-10-09 00:48:17 +05:30
|
|
|
hasUsersChanged := !util.IsSliceInt64Eq(currentWhitelist, newWhitelist)
|
|
|
|
if !hasUsersChanged {
|
|
|
|
return currentWhitelist, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
whitelist = make([]int64, 0, len(newWhitelist))
|
|
|
|
for _, userID := range newWhitelist {
|
2022-05-11 15:39:36 +05:30
|
|
|
if reader, err := access_model.IsRepoReader(ctx, repo, userID); err != nil {
|
2019-10-09 00:48:17 +05:30
|
|
|
return nil, err
|
|
|
|
} else if !reader {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
whitelist = append(whitelist, userID)
|
|
|
|
}
|
|
|
|
|
2022-06-20 15:32:49 +05:30
|
|
|
return whitelist, err
|
2019-10-09 00:48:17 +05:30
|
|
|
}
|
|
|
|
|
2018-03-25 15:31:32 +05:30
|
|
|
// updateUserWhitelist checks whether the user whitelist changed and returns a whitelist with
|
|
|
|
// the users from newWhitelist which have write access to the repo.
|
2022-04-28 17:18:48 +05:30
|
|
|
func updateUserWhitelist(ctx context.Context, repo *repo_model.Repository, currentWhitelist, newWhitelist []int64) (whitelist []int64, err error) {
|
2018-03-25 15:31:32 +05:30
|
|
|
hasUsersChanged := !util.IsSliceInt64Eq(currentWhitelist, newWhitelist)
|
|
|
|
if !hasUsersChanged {
|
|
|
|
return currentWhitelist, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
whitelist = make([]int64, 0, len(newWhitelist))
|
|
|
|
for _, userID := range newWhitelist {
|
2022-12-03 08:18:26 +05:30
|
|
|
user, err := user_model.GetUserByID(ctx, userID)
|
2018-03-25 15:31:32 +05:30
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("GetUserByID [user_id: %d, repo_id: %d]: %w", userID, repo.ID, err)
|
2018-11-28 16:56:14 +05:30
|
|
|
}
|
2022-05-11 15:39:36 +05:30
|
|
|
perm, err := access_model.GetUserRepoPermission(ctx, repo, user)
|
2018-11-28 16:56:14 +05:30
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("GetUserRepoPermission [user_id: %d, repo_id: %d]: %w", userID, repo.ID, err)
|
2018-11-28 16:56:14 +05:30
|
|
|
}
|
|
|
|
|
2021-11-10 01:27:58 +05:30
|
|
|
if !perm.CanWrite(unit.TypeCode) {
|
2018-03-25 15:31:32 +05:30
|
|
|
continue // Drop invalid user ID
|
|
|
|
}
|
|
|
|
|
|
|
|
whitelist = append(whitelist, userID)
|
|
|
|
}
|
|
|
|
|
2022-06-20 15:32:49 +05:30
|
|
|
return whitelist, err
|
2018-03-25 15:31:32 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// updateTeamWhitelist checks whether the team whitelist changed and returns a whitelist with
|
|
|
|
// the teams from newWhitelist which have write access to the repo.
|
2022-04-28 17:18:48 +05:30
|
|
|
func updateTeamWhitelist(ctx context.Context, repo *repo_model.Repository, currentWhitelist, newWhitelist []int64) (whitelist []int64, err error) {
|
2018-03-25 15:31:32 +05:30
|
|
|
hasTeamsChanged := !util.IsSliceInt64Eq(currentWhitelist, newWhitelist)
|
|
|
|
if !hasTeamsChanged {
|
|
|
|
return currentWhitelist, nil
|
|
|
|
}
|
|
|
|
|
2022-04-28 17:18:48 +05:30
|
|
|
teams, err := organization.GetTeamsWithAccessToRepo(ctx, repo.OwnerID, repo.ID, perm.AccessModeRead)
|
2018-03-25 15:31:32 +05:30
|
|
|
if err != nil {
|
2022-10-25 00:59:17 +05:30
|
|
|
return nil, fmt.Errorf("GetTeamsWithAccessToRepo [org_id: %d, repo_id: %d]: %w", repo.OwnerID, repo.ID, err)
|
2018-03-25 15:31:32 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
whitelist = make([]int64, 0, len(teams))
|
|
|
|
for i := range teams {
|
2020-12-25 15:29:32 +05:30
|
|
|
if util.IsInt64InSlice(teams[i].ID, newWhitelist) {
|
2018-03-25 15:31:32 +05:30
|
|
|
whitelist = append(whitelist, teams[i].ID)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-20 15:32:49 +05:30
|
|
|
return whitelist, err
|
2018-03-25 15:31:32 +05:30
|
|
|
}
|
|
|
|
|
2017-02-21 20:32:10 +05:30
|
|
|
// DeleteProtectedBranch removes ProtectedBranch relation between the user and repository.
|
2021-12-10 06:57:50 +05:30
|
|
|
func DeleteProtectedBranch(repoID, id int64) (err error) {
|
2017-02-21 20:32:10 +05:30
|
|
|
protectedBranch := &ProtectedBranch{
|
2021-12-10 06:57:50 +05:30
|
|
|
RepoID: repoID,
|
2017-02-21 20:32:10 +05:30
|
|
|
ID: id,
|
|
|
|
}
|
|
|
|
|
2021-09-23 21:15:36 +05:30
|
|
|
if affected, err := db.GetEngine(db.DefaultContext).Delete(protectedBranch); err != nil {
|
2017-02-21 20:32:10 +05:30
|
|
|
return err
|
|
|
|
} else if affected != 1 {
|
|
|
|
return fmt.Errorf("delete protected branch ID(%v) failed", id)
|
|
|
|
}
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
return nil
|
2017-02-21 20:32:10 +05:30
|
|
|
}
|
2017-10-26 06:19:16 +05:30
|
|
|
|
|
|
|
// DeletedBranch struct
|
|
|
|
type DeletedBranch struct {
|
2019-08-15 20:16:21 +05:30
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
|
|
|
|
Name string `xorm:"UNIQUE(s) NOT NULL"`
|
|
|
|
Commit string `xorm:"UNIQUE(s) NOT NULL"`
|
|
|
|
DeletedByID int64 `xorm:"INDEX"`
|
2021-11-24 15:19:20 +05:30
|
|
|
DeletedBy *user_model.User `xorm:"-"`
|
2019-08-15 20:16:21 +05:30
|
|
|
DeletedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
2017-10-26 06:19:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// AddDeletedBranch adds a deleted branch to the database
|
2021-12-10 06:57:50 +05:30
|
|
|
func AddDeletedBranch(repoID int64, branchName, commit string, deletedByID int64) error {
|
2017-10-26 06:19:16 +05:30
|
|
|
deletedBranch := &DeletedBranch{
|
2021-12-10 06:57:50 +05:30
|
|
|
RepoID: repoID,
|
2017-10-26 06:19:16 +05:30
|
|
|
Name: branchName,
|
|
|
|
Commit: commit,
|
|
|
|
DeletedByID: deletedByID,
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
_, err := db.GetEngine(db.DefaultContext).Insert(deletedBranch)
|
2021-09-19 17:19:59 +05:30
|
|
|
return err
|
2017-10-26 06:19:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// GetDeletedBranches returns all the deleted branches
|
2021-12-10 06:57:50 +05:30
|
|
|
func GetDeletedBranches(repoID int64) ([]*DeletedBranch, error) {
|
2017-10-26 06:19:16 +05:30
|
|
|
deletedBranches := make([]*DeletedBranch, 0)
|
2021-12-10 06:57:50 +05:30
|
|
|
return deletedBranches, db.GetEngine(db.DefaultContext).Where("repo_id = ?", repoID).Desc("deleted_unix").Find(&deletedBranches)
|
2017-10-26 06:19:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// GetDeletedBranchByID get a deleted branch by its ID
|
2021-12-10 06:57:50 +05:30
|
|
|
func GetDeletedBranchByID(repoID, id int64) (*DeletedBranch, error) {
|
2020-06-17 23:20:11 +05:30
|
|
|
deletedBranch := &DeletedBranch{}
|
2021-12-10 06:57:50 +05:30
|
|
|
has, err := db.GetEngine(db.DefaultContext).Where("repo_id = ?", repoID).And("id = ?", id).Get(deletedBranch)
|
2017-10-26 06:19:16 +05:30
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if !has {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
return deletedBranch, nil
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
// RemoveDeletedBranchByID removes a deleted branch from the database
|
|
|
|
func RemoveDeletedBranchByID(repoID, id int64) (err error) {
|
2017-10-26 06:19:16 +05:30
|
|
|
deletedBranch := &DeletedBranch{
|
2021-12-10 06:57:50 +05:30
|
|
|
RepoID: repoID,
|
2017-10-26 06:19:16 +05:30
|
|
|
ID: id,
|
|
|
|
}
|
|
|
|
|
2021-09-23 21:15:36 +05:30
|
|
|
if affected, err := db.GetEngine(db.DefaultContext).Delete(deletedBranch); err != nil {
|
2017-10-26 06:19:16 +05:30
|
|
|
return err
|
|
|
|
} else if affected != 1 {
|
|
|
|
return fmt.Errorf("remove deleted branch ID(%v) failed", id)
|
|
|
|
}
|
|
|
|
|
2021-09-19 17:19:59 +05:30
|
|
|
return nil
|
2017-10-26 06:19:16 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// LoadUser loads the user that deleted the branch
|
2021-11-24 15:19:20 +05:30
|
|
|
// When there's no user found it returns a user_model.NewGhostUser
|
2022-12-03 08:18:26 +05:30
|
|
|
func (deletedBranch *DeletedBranch) LoadUser(ctx context.Context) {
|
|
|
|
user, err := user_model.GetUserByID(ctx, deletedBranch.DeletedByID)
|
2017-10-26 06:19:16 +05:30
|
|
|
if err != nil {
|
2021-11-24 15:19:20 +05:30
|
|
|
user = user_model.NewGhostUser()
|
2017-10-26 06:19:16 +05:30
|
|
|
}
|
|
|
|
deletedBranch.DeletedBy = user
|
|
|
|
}
|
|
|
|
|
2021-12-10 06:57:50 +05:30
|
|
|
// RemoveDeletedBranchByName removes all deleted branches
|
|
|
|
func RemoveDeletedBranchByName(repoID int64, branch string) error {
|
2021-09-23 21:15:36 +05:30
|
|
|
_, err := db.GetEngine(db.DefaultContext).Where("repo_id=? AND name=?", repoID, branch).Delete(new(DeletedBranch))
|
2019-12-28 00:47:37 +05:30
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-10-26 06:19:16 +05:30
|
|
|
// RemoveOldDeletedBranches removes old deleted branches
|
2020-05-17 05:01:38 +05:30
|
|
|
func RemoveOldDeletedBranches(ctx context.Context, olderThan time.Duration) {
|
2019-12-15 15:21:28 +05:30
|
|
|
// Nothing to do for shutdown or terminate
|
2017-10-26 06:19:16 +05:30
|
|
|
log.Trace("Doing: DeletedBranchesCleanup")
|
|
|
|
|
2020-05-17 05:01:38 +05:30
|
|
|
deleteBefore := time.Now().Add(-olderThan)
|
2021-09-23 21:15:36 +05:30
|
|
|
_, err := db.GetEngine(db.DefaultContext).Where("deleted_unix < ?", deleteBefore.Unix()).Delete(new(DeletedBranch))
|
2017-10-26 06:19:16 +05:30
|
|
|
if err != nil {
|
2019-04-02 13:18:31 +05:30
|
|
|
log.Error("DeletedBranchesCleanup: %v", err)
|
2017-10-26 06:19:16 +05:30
|
|
|
}
|
|
|
|
}
|
2021-10-08 22:33:04 +05:30
|
|
|
|
|
|
|
// RenamedBranch provide renamed branch log
|
|
|
|
// will check it when a branch can't be found
|
|
|
|
type RenamedBranch struct {
|
|
|
|
ID int64 `xorm:"pk autoincr"`
|
|
|
|
RepoID int64 `xorm:"INDEX NOT NULL"`
|
|
|
|
From string
|
|
|
|
To string
|
|
|
|
CreatedUnix timeutil.TimeStamp `xorm:"created"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindRenamedBranch check if a branch was renamed
|
|
|
|
func FindRenamedBranch(repoID int64, from string) (branch *RenamedBranch, exist bool, err error) {
|
|
|
|
branch = &RenamedBranch{
|
|
|
|
RepoID: repoID,
|
|
|
|
From: from,
|
|
|
|
}
|
|
|
|
exist, err = db.GetEngine(db.DefaultContext).Get(branch)
|
|
|
|
|
2022-06-20 15:32:49 +05:30
|
|
|
return branch, exist, err
|
2021-10-08 22:33:04 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
// RenameBranch rename a branch
|
2021-12-10 06:57:50 +05:30
|
|
|
func RenameBranch(repo *repo_model.Repository, from, to string, gitAction func(isDefault bool) error) (err error) {
|
2022-11-13 01:48:50 +05:30
|
|
|
ctx, committer, err := db.TxContext(db.DefaultContext)
|
2021-11-21 21:11:00 +05:30
|
|
|
if err != nil {
|
2021-10-08 22:33:04 +05:30
|
|
|
return err
|
|
|
|
}
|
2021-11-21 21:11:00 +05:30
|
|
|
defer committer.Close()
|
2021-10-08 22:33:04 +05:30
|
|
|
|
2021-11-21 21:11:00 +05:30
|
|
|
sess := db.GetEngine(ctx)
|
2021-10-08 22:33:04 +05:30
|
|
|
// 1. update default branch if needed
|
|
|
|
isDefault := repo.DefaultBranch == from
|
|
|
|
if isDefault {
|
|
|
|
repo.DefaultBranch = to
|
|
|
|
_, err = sess.ID(repo.ID).Cols("default_branch").Update(repo)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Update protected branch if needed
|
2022-05-20 19:38:52 +05:30
|
|
|
protectedBranch, err := GetProtectedBranchBy(ctx, repo.ID, from)
|
2021-10-08 22:33:04 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if protectedBranch != nil {
|
|
|
|
protectedBranch.BranchName = to
|
|
|
|
_, err = sess.ID(protectedBranch.ID).Cols("branch_name").Update(protectedBranch)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 3. Update all not merged pull request base branch name
|
2022-06-12 21:21:54 +05:30
|
|
|
_, err = sess.Table("pull_request").Where("base_repo_id=? AND base_branch=? AND has_merged=?",
|
2021-10-08 22:33:04 +05:30
|
|
|
repo.ID, from, false).
|
|
|
|
Update(map[string]interface{}{"base_branch": to})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 4. do git action
|
|
|
|
if err = gitAction(isDefault); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 5. insert renamed branch record
|
|
|
|
renamedBranch := &RenamedBranch{
|
|
|
|
RepoID: repo.ID,
|
|
|
|
From: from,
|
|
|
|
To: to,
|
|
|
|
}
|
2021-11-21 21:11:00 +05:30
|
|
|
err = db.Insert(ctx, renamedBranch)
|
2021-10-08 22:33:04 +05:30
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-11-21 21:11:00 +05:30
|
|
|
return committer.Commit()
|
2021-10-08 22:33:04 +05:30
|
|
|
}
|