You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
604 lines
19 KiB
604 lines
19 KiB
8 years ago
|
// Copyright 2016 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.
|
||
|
|
||
3 years ago
|
package git
|
||
8 years ago
|
|
||
|
import (
|
||
5 years ago
|
"context"
|
||
8 years ago
|
"fmt"
|
||
5 years ago
|
"strings"
|
||
8 years ago
|
"time"
|
||
7 years ago
|
|
||
3 years ago
|
"code.gitea.io/gitea/models/db"
|
||
3 years ago
|
"code.gitea.io/gitea/models/organization"
|
||
3 years ago
|
"code.gitea.io/gitea/models/perm"
|
||
3 years ago
|
access_model "code.gitea.io/gitea/models/perm/access"
|
||
3 years ago
|
repo_model "code.gitea.io/gitea/models/repo"
|
||
3 years ago
|
"code.gitea.io/gitea/models/unit"
|
||
3 years ago
|
user_model "code.gitea.io/gitea/models/user"
|
||
7 years ago
|
"code.gitea.io/gitea/modules/base"
|
||
|
"code.gitea.io/gitea/modules/log"
|
||
5 years ago
|
"code.gitea.io/gitea/modules/timeutil"
|
||
7 years ago
|
"code.gitea.io/gitea/modules/util"
|
||
|
|
||
5 years ago
|
"github.com/gobwas/glob"
|
||
8 years ago
|
)
|
||
|
|
||
|
// ProtectedBranch struct
|
||
|
type ProtectedBranch struct {
|
||
4 years ago
|
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"`
|
||
3 years ago
|
UnprotectedFilePatterns string `xorm:"TEXT"`
|
||
5 years ago
|
|
||
|
CreatedUnix timeutil.TimeStamp `xorm:"created"`
|
||
|
UpdatedUnix timeutil.TimeStamp `xorm:"updated"`
|
||
7 years ago
|
}
|
||
|
|
||
3 years ago
|
func init() {
|
||
|
db.RegisterModel(new(ProtectedBranch))
|
||
|
db.RegisterModel(new(DeletedBranch))
|
||
3 years ago
|
db.RegisterModel(new(RenamedBranch))
|
||
3 years ago
|
}
|
||
|
|
||
7 years ago
|
// 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 {
|
||
5 years ago
|
if !protectBranch.CanPush {
|
||
7 years ago
|
return false
|
||
|
}
|
||
|
|
||
5 years ago
|
if !protectBranch.EnableWhitelist {
|
||
3 years ago
|
if user, err := user_model.GetUserByID(userID); err != nil {
|
||
5 years ago
|
log.Error("GetUserByID: %v", err)
|
||
|
return false
|
||
3 years ago
|
} else if repo, err := repo_model.GetRepositoryByID(protectBranch.RepoID); err != nil {
|
||
|
log.Error("repo_model.GetRepositoryByID: %v", err)
|
||
5 years ago
|
return false
|
||
3 years ago
|
} else if writeAccess, err := access_model.HasAccessUnit(db.DefaultContext, user, repo, unit.TypeCode, perm.AccessModeWrite); err != nil {
|
||
5 years ago
|
log.Error("HasAccessUnit: %v", err)
|
||
|
return false
|
||
|
} else {
|
||
|
return writeAccess
|
||
|
}
|
||
|
}
|
||
|
|
||
7 years ago
|
if base.Int64sContains(protectBranch.WhitelistUserIDs, userID) {
|
||
|
return true
|
||
|
}
|
||
|
|
||
|
if len(protectBranch.WhitelistTeamIDs) == 0 {
|
||
|
return false
|
||
|
}
|
||
|
|
||
3 years ago
|
in, err := organization.IsUserInTeams(db.DefaultContext, userID, protectBranch.WhitelistTeamIDs)
|
||
7 years ago
|
if err != nil {
|
||
6 years ago
|
log.Error("IsUserInTeams: %v", err)
|
||
7 years ago
|
return false
|
||
|
}
|
||
|
return in
|
||
8 years ago
|
}
|
||
|
|
||
5 years ago
|
// IsUserMergeWhitelisted checks if some user is whitelisted to merge to this branch
|
||
3 years ago
|
func IsUserMergeWhitelisted(ctx context.Context, protectBranch *ProtectedBranch, userID int64, permissionInRepo access_model.Permission) bool {
|
||
7 years ago
|
if !protectBranch.EnableMergeWhitelist {
|
||
4 years ago
|
// Then we need to fall back on whether the user has write permission
|
||
3 years ago
|
return permissionInRepo.CanWrite(unit.TypeCode)
|
||
7 years ago
|
}
|
||
|
|
||
|
if base.Int64sContains(protectBranch.MergeWhitelistUserIDs, userID) {
|
||
|
return true
|
||
|
}
|
||
|
|
||
7 years ago
|
if len(protectBranch.MergeWhitelistTeamIDs) == 0 {
|
||
7 years ago
|
return false
|
||
|
}
|
||
|
|
||
3 years ago
|
in, err := organization.IsUserInTeams(ctx, userID, protectBranch.MergeWhitelistTeamIDs)
|
||
7 years ago
|
if err != nil {
|
||
6 years ago
|
log.Error("IsUserInTeams: %v", err)
|
||
7 years ago
|
return false
|
||
|
}
|
||
|
return in
|
||
|
}
|
||
|
|
||
5 years ago
|
// IsUserOfficialReviewer check if user is official reviewer for the branch (counts towards required approvals)
|
||
3 years ago
|
func IsUserOfficialReviewer(protectBranch *ProtectedBranch, user *user_model.User) (bool, error) {
|
||
3 years ago
|
return IsUserOfficialReviewerCtx(db.DefaultContext, protectBranch, user)
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
// IsUserOfficialReviewerCtx check if user is official reviewer for the branch (counts towards required approvals)
|
||
|
func IsUserOfficialReviewerCtx(ctx context.Context, protectBranch *ProtectedBranch, user *user_model.User) (bool, error) {
|
||
3 years ago
|
repo, err := repo_model.GetRepositoryByIDCtx(ctx, protectBranch.RepoID)
|
||
5 years ago
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
|
||
|
if !protectBranch.EnableApprovalsWhitelist {
|
||
|
// Anyone with write access is considered official reviewer
|
||
3 years ago
|
writeAccess, err := access_model.HasAccessUnit(ctx, user, repo, unit.TypeCode, perm.AccessModeWrite)
|
||
5 years ago
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
return writeAccess, nil
|
||
|
}
|
||
|
|
||
|
if base.Int64sContains(protectBranch.ApprovalsWhitelistUserIDs, user.ID) {
|
||
|
return true, nil
|
||
|
}
|
||
|
|
||
3 years ago
|
inTeam, err := organization.IsUserInTeams(ctx, user.ID, protectBranch.ApprovalsWhitelistTeamIDs)
|
||
5 years ago
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
|
||
|
return inTeam, nil
|
||
|
}
|
||
|
|
||
5 years ago
|
// GetProtectedFilePatterns parses a semicolon separated list of protected file patterns and returns a glob.Glob slice
|
||
|
func (protectBranch *ProtectedBranch) GetProtectedFilePatterns() []glob.Glob {
|
||
3 years ago
|
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 {
|
||
5 years ago
|
extarr := make([]glob.Glob, 0, 10)
|
||
3 years ago
|
for _, expr := range strings.Split(strings.ToLower(filePatterns), ";") {
|
||
5 years ago
|
expr = strings.TrimSpace(expr)
|
||
|
if expr != "" {
|
||
|
if g, err := glob.Compile(expr, '.', '/'); err != nil {
|
||
4 years ago
|
log.Info("Invalid glob expression '%s' (skipped): %v", expr, err)
|
||
5 years ago
|
} else {
|
||
|
extarr = append(extarr, g)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return extarr
|
||
|
}
|
||
|
|
||
4 years ago
|
// MergeBlockedByProtectedFiles returns true if merge is blocked by protected files change
|
||
3 years ago
|
func (protectBranch *ProtectedBranch) MergeBlockedByProtectedFiles(changedProtectedFiles []string) bool {
|
||
4 years ago
|
glob := protectBranch.GetProtectedFilePatterns()
|
||
|
if len(glob) == 0 {
|
||
|
return false
|
||
|
}
|
||
|
|
||
3 years ago
|
return len(changedProtectedFiles) > 0
|
||
4 years ago
|
}
|
||
|
|
||
|
// 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
|
||
|
}
|
||
|
|
||
3 years ago
|
// 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
|
||
|
}
|
||
|
|
||
8 years ago
|
// GetProtectedBranchBy getting protected branch by ID/Name
|
||
3 years ago
|
func GetProtectedBranchBy(ctx context.Context, repoID int64, branchName string) (*ProtectedBranch, error) {
|
||
6 years ago
|
rel := &ProtectedBranch{RepoID: repoID, BranchName: branchName}
|
||
3 years ago
|
has, err := db.GetByBean(ctx, rel)
|
||
8 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if !has {
|
||
|
return nil, nil
|
||
|
}
|
||
|
return rel, nil
|
||
|
}
|
||
|
|
||
6 years ago
|
// 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
|
||
|
}
|
||
|
|
||
7 years ago
|
// 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.
|
||
3 years ago
|
func UpdateProtectBranch(ctx context.Context, repo *repo_model.Repository, protectBranch *ProtectedBranch, opts WhitelistOptions) (err error) {
|
||
|
if err = repo.GetOwner(ctx); err != nil {
|
||
7 years ago
|
return fmt.Errorf("GetOwner: %v", err)
|
||
|
}
|
||
|
|
||
3 years ago
|
whitelist, err := updateUserWhitelist(ctx, repo, protectBranch.WhitelistUserIDs, opts.UserIDs)
|
||
7 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
protectBranch.WhitelistUserIDs = whitelist
|
||
7 years ago
|
|
||
3 years ago
|
whitelist, err = updateUserWhitelist(ctx, repo, protectBranch.MergeWhitelistUserIDs, opts.MergeUserIDs)
|
||
7 years ago
|
if err != nil {
|
||
|
return err
|
||
7 years ago
|
}
|
||
7 years ago
|
protectBranch.MergeWhitelistUserIDs = whitelist
|
||
7 years ago
|
|
||
3 years ago
|
whitelist, err = updateApprovalWhitelist(ctx, repo, protectBranch.ApprovalsWhitelistUserIDs, opts.ApprovalsUserIDs)
|
||
6 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
protectBranch.ApprovalsWhitelistUserIDs = whitelist
|
||
|
|
||
7 years ago
|
// if the repo is in an organization
|
||
3 years ago
|
whitelist, err = updateTeamWhitelist(ctx, repo, protectBranch.WhitelistTeamIDs, opts.TeamIDs)
|
||
7 years ago
|
if err != nil {
|
||
|
return err
|
||
7 years ago
|
}
|
||
7 years ago
|
protectBranch.WhitelistTeamIDs = whitelist
|
||
|
|
||
3 years ago
|
whitelist, err = updateTeamWhitelist(ctx, repo, protectBranch.MergeWhitelistTeamIDs, opts.MergeTeamIDs)
|
||
7 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
protectBranch.MergeWhitelistTeamIDs = whitelist
|
||
7 years ago
|
|
||
3 years ago
|
whitelist, err = updateTeamWhitelist(ctx, repo, protectBranch.ApprovalsWhitelistTeamIDs, opts.ApprovalsTeamIDs)
|
||
6 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
protectBranch.ApprovalsWhitelistTeamIDs = whitelist
|
||
|
|
||
7 years ago
|
// Make sure protectBranch.ID is not 0 for whitelists
|
||
|
if protectBranch.ID == 0 {
|
||
3 years ago
|
if _, err = db.GetEngine(ctx).Insert(protectBranch); err != nil {
|
||
7 years ago
|
return fmt.Errorf("Insert: %v", err)
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
3 years ago
|
if _, err = db.GetEngine(ctx).ID(protectBranch.ID).AllCols().Update(protectBranch); err != nil {
|
||
7 years ago
|
return fmt.Errorf("Update: %v", err)
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
8 years ago
|
// GetProtectedBranches get all protected branches
|
||
3 years ago
|
func GetProtectedBranches(repoID int64) ([]*ProtectedBranch, error) {
|
||
8 years ago
|
protectedBranches := make([]*ProtectedBranch, 0)
|
||
3 years ago
|
return protectedBranches, db.GetEngine(db.DefaultContext).Find(&protectedBranches, &ProtectedBranch{RepoID: repoID})
|
||
5 years ago
|
}
|
||
|
|
||
8 years ago
|
// IsProtectedBranch checks if branch is protected
|
||
3 years ago
|
func IsProtectedBranch(repoID int64, branchName string) (bool, error) {
|
||
8 years ago
|
protectedBranch := &ProtectedBranch{
|
||
3 years ago
|
RepoID: repoID,
|
||
8 years ago
|
BranchName: branchName,
|
||
|
}
|
||
|
|
||
3 years ago
|
has, err := db.GetEngine(db.DefaultContext).Exist(protectedBranch)
|
||
6 years ago
|
if err != nil {
|
||
|
return true, err
|
||
|
}
|
||
|
return has, nil
|
||
|
}
|
||
|
|
||
5 years ago
|
// 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.
|
||
3 years ago
|
func updateApprovalWhitelist(ctx context.Context, repo *repo_model.Repository, currentWhitelist, newWhitelist []int64) (whitelist []int64, err error) {
|
||
5 years ago
|
hasUsersChanged := !util.IsSliceInt64Eq(currentWhitelist, newWhitelist)
|
||
|
if !hasUsersChanged {
|
||
|
return currentWhitelist, nil
|
||
|
}
|
||
|
|
||
|
whitelist = make([]int64, 0, len(newWhitelist))
|
||
|
for _, userID := range newWhitelist {
|
||
3 years ago
|
if reader, err := access_model.IsRepoReader(ctx, repo, userID); err != nil {
|
||
5 years ago
|
return nil, err
|
||
|
} else if !reader {
|
||
|
continue
|
||
|
}
|
||
|
whitelist = append(whitelist, userID)
|
||
|
}
|
||
|
|
||
|
return
|
||
|
}
|
||
|
|
||
7 years ago
|
// updateUserWhitelist checks whether the user whitelist changed and returns a whitelist with
|
||
|
// the users from newWhitelist which have write access to the repo.
|
||
3 years ago
|
func updateUserWhitelist(ctx context.Context, repo *repo_model.Repository, currentWhitelist, newWhitelist []int64) (whitelist []int64, err error) {
|
||
7 years ago
|
hasUsersChanged := !util.IsSliceInt64Eq(currentWhitelist, newWhitelist)
|
||
|
if !hasUsersChanged {
|
||
|
return currentWhitelist, nil
|
||
|
}
|
||
|
|
||
|
whitelist = make([]int64, 0, len(newWhitelist))
|
||
|
for _, userID := range newWhitelist {
|
||
3 years ago
|
user, err := user_model.GetUserByIDCtx(ctx, userID)
|
||
7 years ago
|
if err != nil {
|
||
6 years ago
|
return nil, fmt.Errorf("GetUserByID [user_id: %d, repo_id: %d]: %v", userID, repo.ID, err)
|
||
|
}
|
||
3 years ago
|
perm, err := access_model.GetUserRepoPermission(ctx, repo, user)
|
||
6 years ago
|
if err != nil {
|
||
|
return nil, fmt.Errorf("GetUserRepoPermission [user_id: %d, repo_id: %d]: %v", userID, repo.ID, err)
|
||
|
}
|
||
|
|
||
3 years ago
|
if !perm.CanWrite(unit.TypeCode) {
|
||
7 years ago
|
continue // Drop invalid user ID
|
||
|
}
|
||
|
|
||
|
whitelist = append(whitelist, userID)
|
||
|
}
|
||
|
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// updateTeamWhitelist checks whether the team whitelist changed and returns a whitelist with
|
||
|
// the teams from newWhitelist which have write access to the repo.
|
||
3 years ago
|
func updateTeamWhitelist(ctx context.Context, repo *repo_model.Repository, currentWhitelist, newWhitelist []int64) (whitelist []int64, err error) {
|
||
7 years ago
|
hasTeamsChanged := !util.IsSliceInt64Eq(currentWhitelist, newWhitelist)
|
||
|
if !hasTeamsChanged {
|
||
|
return currentWhitelist, nil
|
||
|
}
|
||
|
|
||
3 years ago
|
teams, err := organization.GetTeamsWithAccessToRepo(ctx, repo.OwnerID, repo.ID, perm.AccessModeRead)
|
||
7 years ago
|
if err != nil {
|
||
|
return nil, fmt.Errorf("GetTeamsWithAccessToRepo [org_id: %d, repo_id: %d]: %v", repo.OwnerID, repo.ID, err)
|
||
|
}
|
||
|
|
||
|
whitelist = make([]int64, 0, len(teams))
|
||
|
for i := range teams {
|
||
4 years ago
|
if util.IsInt64InSlice(teams[i].ID, newWhitelist) {
|
||
7 years ago
|
whitelist = append(whitelist, teams[i].ID)
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return
|
||
|
}
|
||
|
|
||
8 years ago
|
// DeleteProtectedBranch removes ProtectedBranch relation between the user and repository.
|
||
3 years ago
|
func DeleteProtectedBranch(repoID, id int64) (err error) {
|
||
8 years ago
|
protectedBranch := &ProtectedBranch{
|
||
3 years ago
|
RepoID: repoID,
|
||
8 years ago
|
ID: id,
|
||
|
}
|
||
|
|
||
3 years ago
|
if affected, err := db.GetEngine(db.DefaultContext).Delete(protectedBranch); err != nil {
|
||
8 years ago
|
return err
|
||
|
} else if affected != 1 {
|
||
|
return fmt.Errorf("delete protected branch ID(%v) failed", id)
|
||
|
}
|
||
|
|
||
3 years ago
|
return nil
|
||
8 years ago
|
}
|
||
7 years ago
|
|
||
|
// DeletedBranch struct
|
||
|
type DeletedBranch struct {
|
||
5 years ago
|
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"`
|
||
3 years ago
|
DeletedBy *user_model.User `xorm:"-"`
|
||
5 years ago
|
DeletedUnix timeutil.TimeStamp `xorm:"INDEX created"`
|
||
7 years ago
|
}
|
||
|
|
||
|
// AddDeletedBranch adds a deleted branch to the database
|
||
3 years ago
|
func AddDeletedBranch(repoID int64, branchName, commit string, deletedByID int64) error {
|
||
7 years ago
|
deletedBranch := &DeletedBranch{
|
||
3 years ago
|
RepoID: repoID,
|
||
7 years ago
|
Name: branchName,
|
||
|
Commit: commit,
|
||
|
DeletedByID: deletedByID,
|
||
|
}
|
||
|
|
||
3 years ago
|
_, err := db.GetEngine(db.DefaultContext).Insert(deletedBranch)
|
||
3 years ago
|
return err
|
||
7 years ago
|
}
|
||
|
|
||
|
// GetDeletedBranches returns all the deleted branches
|
||
3 years ago
|
func GetDeletedBranches(repoID int64) ([]*DeletedBranch, error) {
|
||
7 years ago
|
deletedBranches := make([]*DeletedBranch, 0)
|
||
3 years ago
|
return deletedBranches, db.GetEngine(db.DefaultContext).Where("repo_id = ?", repoID).Desc("deleted_unix").Find(&deletedBranches)
|
||
7 years ago
|
}
|
||
|
|
||
|
// GetDeletedBranchByID get a deleted branch by its ID
|
||
3 years ago
|
func GetDeletedBranchByID(repoID, id int64) (*DeletedBranch, error) {
|
||
5 years ago
|
deletedBranch := &DeletedBranch{}
|
||
3 years ago
|
has, err := db.GetEngine(db.DefaultContext).Where("repo_id = ?", repoID).And("id = ?", id).Get(deletedBranch)
|
||
7 years ago
|
if err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
if !has {
|
||
|
return nil, nil
|
||
|
}
|
||
|
return deletedBranch, nil
|
||
|
}
|
||
|
|
||
3 years ago
|
// RemoveDeletedBranchByID removes a deleted branch from the database
|
||
|
func RemoveDeletedBranchByID(repoID, id int64) (err error) {
|
||
7 years ago
|
deletedBranch := &DeletedBranch{
|
||
3 years ago
|
RepoID: repoID,
|
||
7 years ago
|
ID: id,
|
||
|
}
|
||
|
|
||
3 years ago
|
if affected, err := db.GetEngine(db.DefaultContext).Delete(deletedBranch); err != nil {
|
||
7 years ago
|
return err
|
||
|
} else if affected != 1 {
|
||
|
return fmt.Errorf("remove deleted branch ID(%v) failed", id)
|
||
|
}
|
||
|
|
||
3 years ago
|
return nil
|
||
7 years ago
|
}
|
||
|
|
||
|
// LoadUser loads the user that deleted the branch
|
||
3 years ago
|
// When there's no user found it returns a user_model.NewGhostUser
|
||
7 years ago
|
func (deletedBranch *DeletedBranch) LoadUser() {
|
||
3 years ago
|
user, err := user_model.GetUserByID(deletedBranch.DeletedByID)
|
||
7 years ago
|
if err != nil {
|
||
3 years ago
|
user = user_model.NewGhostUser()
|
||
7 years ago
|
}
|
||
|
deletedBranch.DeletedBy = user
|
||
|
}
|
||
|
|
||
3 years ago
|
// RemoveDeletedBranchByName removes all deleted branches
|
||
|
func RemoveDeletedBranchByName(repoID int64, branch string) error {
|
||
3 years ago
|
_, err := db.GetEngine(db.DefaultContext).Where("repo_id=? AND name=?", repoID, branch).Delete(new(DeletedBranch))
|
||
5 years ago
|
return err
|
||
|
}
|
||
|
|
||
7 years ago
|
// RemoveOldDeletedBranches removes old deleted branches
|
||
5 years ago
|
func RemoveOldDeletedBranches(ctx context.Context, olderThan time.Duration) {
|
||
5 years ago
|
// Nothing to do for shutdown or terminate
|
||
7 years ago
|
log.Trace("Doing: DeletedBranchesCleanup")
|
||
|
|
||
5 years ago
|
deleteBefore := time.Now().Add(-olderThan)
|
||
3 years ago
|
_, err := db.GetEngine(db.DefaultContext).Where("deleted_unix < ?", deleteBefore.Unix()).Delete(new(DeletedBranch))
|
||
7 years ago
|
if err != nil {
|
||
6 years ago
|
log.Error("DeletedBranchesCleanup: %v", err)
|
||
7 years ago
|
}
|
||
|
}
|
||
3 years ago
|
|
||
|
// 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)
|
||
|
|
||
|
return
|
||
|
}
|
||
|
|
||
|
// RenameBranch rename a branch
|
||
3 years ago
|
func RenameBranch(repo *repo_model.Repository, from, to string, gitAction func(isDefault bool) error) (err error) {
|
||
3 years ago
|
ctx, committer, err := db.TxContext()
|
||
|
if err != nil {
|
||
3 years ago
|
return err
|
||
|
}
|
||
3 years ago
|
defer committer.Close()
|
||
3 years ago
|
|
||
3 years ago
|
sess := db.GetEngine(ctx)
|
||
3 years ago
|
// 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
|
||
3 years ago
|
protectedBranch, err := GetProtectedBranchBy(ctx, repo.ID, from)
|
||
3 years ago
|
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
|
||
3 years ago
|
_, err = sess.Table("pull_request").Where("base_repo_id=? AND base_branch=? AND has_merged=?",
|
||
3 years ago
|
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,
|
||
|
}
|
||
3 years ago
|
err = db.Insert(ctx, renamedBranch)
|
||
3 years ago
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
3 years ago
|
return committer.Commit()
|
||
3 years ago
|
}
|