2021-03-01 00:47:30 +00:00
// Copyright 2021 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 models
import (
"fmt"
"os"
2021-09-19 11:49:59 +00:00
"code.gitea.io/gitea/models/db"
2021-03-01 00:47:30 +00:00
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
)
// RepoTransfer is used to manage repository transfers
type RepoTransfer struct {
ID int64 ` xorm:"pk autoincr" `
DoerID int64
Doer * User ` xorm:"-" `
RecipientID int64
Recipient * User ` xorm:"-" `
RepoID int64
TeamIDs [ ] int64
Teams [ ] * Team ` xorm:"-" `
CreatedUnix timeutil . TimeStamp ` xorm:"INDEX NOT NULL created" `
UpdatedUnix timeutil . TimeStamp ` xorm:"INDEX NOT NULL updated" `
}
2021-09-19 11:49:59 +00:00
func init ( ) {
db . RegisterModel ( new ( RepoTransfer ) )
}
2021-03-01 00:47:30 +00:00
// LoadAttributes fetches the transfer recipient from the database
func ( r * RepoTransfer ) LoadAttributes ( ) error {
if r . Recipient == nil {
u , err := GetUserByID ( r . RecipientID )
if err != nil {
return err
}
r . Recipient = u
}
if r . Recipient . IsOrganization ( ) && len ( r . TeamIDs ) != len ( r . Teams ) {
for _ , v := range r . TeamIDs {
team , err := GetTeamByID ( v )
if err != nil {
return err
}
if team . OrgID != r . Recipient . ID {
return fmt . Errorf ( "team %d belongs not to org %d" , v , r . Recipient . ID )
}
r . Teams = append ( r . Teams , team )
}
}
if r . Doer == nil {
u , err := GetUserByID ( r . DoerID )
if err != nil {
return err
}
r . Doer = u
}
return nil
}
// CanUserAcceptTransfer checks if the user has the rights to accept/decline a repo transfer.
// For user, it checks if it's himself
// For organizations, it checks if the user is able to create repos
func ( r * RepoTransfer ) CanUserAcceptTransfer ( u * User ) bool {
if err := r . LoadAttributes ( ) ; err != nil {
log . Error ( "LoadAttributes: %v" , err )
return false
}
if ! r . Recipient . IsOrganization ( ) {
return r . RecipientID == u . ID
}
allowed , err := CanCreateOrgRepo ( r . RecipientID , u . ID )
if err != nil {
log . Error ( "CanCreateOrgRepo: %v" , err )
return false
}
return allowed
}
// GetPendingRepositoryTransfer fetches the most recent and ongoing transfer
// process for the repository
func GetPendingRepositoryTransfer ( repo * Repository ) ( * RepoTransfer , error ) {
2021-03-14 18:52:12 +00:00
transfer := new ( RepoTransfer )
2021-03-01 00:47:30 +00:00
2021-09-23 15:45:36 +00:00
has , err := db . GetEngine ( db . DefaultContext ) . Where ( "repo_id = ? " , repo . ID ) . Get ( transfer )
2021-03-01 00:47:30 +00:00
if err != nil {
return nil , err
}
if ! has {
return nil , ErrNoPendingRepoTransfer { RepoID : repo . ID }
}
return transfer , nil
}
2021-09-19 11:49:59 +00:00
func deleteRepositoryTransfer ( e db . Engine , repoID int64 ) error {
2021-03-01 00:47:30 +00:00
_ , err := e . Where ( "repo_id = ?" , repoID ) . Delete ( & RepoTransfer { } )
return err
}
// CancelRepositoryTransfer marks the repository as ready and remove pending transfer entry,
// thus cancel the transfer process.
func CancelRepositoryTransfer ( repo * Repository ) error {
2021-09-23 15:45:36 +00:00
sess := db . NewSession ( db . DefaultContext )
2021-03-01 00:47:30 +00:00
defer sess . Close ( )
if err := sess . Begin ( ) ; err != nil {
return err
}
repo . Status = RepositoryReady
if err := updateRepositoryCols ( sess , repo , "status" ) ; err != nil {
return err
}
if err := deleteRepositoryTransfer ( sess , repo . ID ) ; err != nil {
return err
}
return sess . Commit ( )
}
// TestRepositoryReadyForTransfer make sure repo is ready to transfer
func TestRepositoryReadyForTransfer ( status RepositoryStatus ) error {
switch status {
case RepositoryBeingMigrated :
return fmt . Errorf ( "repo is not ready, currently migrating" )
case RepositoryPendingTransfer :
return ErrRepoTransferInProgress { }
}
return nil
}
// CreatePendingRepositoryTransfer transfer a repo from one owner to a new one.
// it marks the repository transfer as "pending"
func CreatePendingRepositoryTransfer ( doer , newOwner * User , repoID int64 , teams [ ] * Team ) error {
2021-09-23 15:45:36 +00:00
sess := db . NewSession ( db . DefaultContext )
2021-03-01 00:47:30 +00:00
defer sess . Close ( )
if err := sess . Begin ( ) ; err != nil {
return err
}
repo , err := getRepositoryByID ( sess , repoID )
if err != nil {
return err
}
// Make sure repo is ready to transfer
if err := TestRepositoryReadyForTransfer ( repo . Status ) ; err != nil {
return err
}
repo . Status = RepositoryPendingTransfer
if err := updateRepositoryCols ( sess , repo , "status" ) ; err != nil {
return err
}
// Check if new owner has repository with same name.
if has , err := isRepositoryExist ( sess , newOwner , repo . Name ) ; err != nil {
return fmt . Errorf ( "IsRepositoryExist: %v" , err )
} else if has {
return ErrRepoAlreadyExist { newOwner . LowerName , repo . Name }
}
transfer := & RepoTransfer {
RepoID : repo . ID ,
RecipientID : newOwner . ID ,
CreatedUnix : timeutil . TimeStampNow ( ) ,
UpdatedUnix : timeutil . TimeStampNow ( ) ,
DoerID : doer . ID ,
TeamIDs : make ( [ ] int64 , 0 , len ( teams ) ) ,
}
for k := range teams {
transfer . TeamIDs = append ( transfer . TeamIDs , teams [ k ] . ID )
}
if _ , err := sess . Insert ( transfer ) ; err != nil {
return err
}
return sess . Commit ( )
}
// TransferOwnership transfers all corresponding repository items from old user to new one.
2021-03-05 02:28:52 +00:00
func TransferOwnership ( doer * User , newOwnerName string , repo * Repository ) ( err error ) {
repoRenamed := false
wikiRenamed := false
oldOwnerName := doer . Name
defer func ( ) {
if ! repoRenamed && ! wikiRenamed {
return
}
recoverErr := recover ( )
if err == nil && recoverErr == nil {
return
}
if repoRenamed {
2021-07-15 15:46:07 +00:00
if err := util . Rename ( RepoPath ( newOwnerName , repo . Name ) , RepoPath ( oldOwnerName , repo . Name ) ) ; err != nil {
2021-03-05 02:28:52 +00:00
log . Critical ( "Unable to move repository %s/%s directory from %s back to correct place %s: %v" , oldOwnerName , repo . Name , RepoPath ( newOwnerName , repo . Name ) , RepoPath ( oldOwnerName , repo . Name ) , err )
}
}
if wikiRenamed {
2021-07-15 15:46:07 +00:00
if err := util . Rename ( WikiPath ( newOwnerName , repo . Name ) , WikiPath ( oldOwnerName , repo . Name ) ) ; err != nil {
2021-03-05 02:28:52 +00:00
log . Critical ( "Unable to move wiki for repository %s/%s directory from %s back to correct place %s: %v" , oldOwnerName , repo . Name , WikiPath ( newOwnerName , repo . Name ) , WikiPath ( oldOwnerName , repo . Name ) , err )
}
}
if recoverErr != nil {
log . Error ( "Panic within TransferOwnership: %v\n%s" , recoverErr , log . Stack ( 2 ) )
panic ( recoverErr )
}
} ( )
2021-09-23 15:45:36 +00:00
sess := db . NewSession ( db . DefaultContext )
2021-03-01 00:47:30 +00:00
defer sess . Close ( )
if err := sess . Begin ( ) ; err != nil {
return fmt . Errorf ( "sess.Begin: %v" , err )
}
newOwner , err := getUserByName ( sess , newOwnerName )
if err != nil {
return fmt . Errorf ( "get new owner '%s': %v" , newOwnerName , err )
}
2021-03-05 02:28:52 +00:00
newOwnerName = newOwner . Name // ensure capitalisation matches
2021-03-01 00:47:30 +00:00
// Check if new owner has repository with same name.
if has , err := isRepositoryExist ( sess , newOwner , repo . Name ) ; err != nil {
return fmt . Errorf ( "IsRepositoryExist: %v" , err )
} else if has {
return ErrRepoAlreadyExist { newOwnerName , repo . Name }
}
oldOwner := repo . Owner
2021-03-05 02:28:52 +00:00
oldOwnerName = oldOwner . Name
2021-03-01 00:47:30 +00:00
// Note: we have to set value here to make sure recalculate accesses is based on
// new owner.
repo . OwnerID = newOwner . ID
repo . Owner = newOwner
repo . OwnerName = newOwner . Name
// Update repository.
if _ , err := sess . ID ( repo . ID ) . Update ( repo ) ; err != nil {
return fmt . Errorf ( "update owner: %v" , err )
}
// Remove redundant collaborators.
2021-09-24 11:32:56 +00:00
collaborators , err := repo . getCollaborators ( sess , db . ListOptions { } )
2021-03-01 00:47:30 +00:00
if err != nil {
return fmt . Errorf ( "getCollaborators: %v" , err )
}
// Dummy object.
collaboration := & Collaboration { RepoID : repo . ID }
for _ , c := range collaborators {
2021-09-27 18:07:19 +00:00
if c . IsGhost ( ) {
collaboration . ID = c . Collaboration . ID
if _ , err := sess . Delete ( collaboration ) ; err != nil {
return fmt . Errorf ( "remove collaborator '%d': %v" , c . ID , err )
}
collaboration . ID = 0
}
2021-03-01 00:47:30 +00:00
if c . ID != newOwner . ID {
isMember , err := isOrganizationMember ( sess , newOwner . ID , c . ID )
if err != nil {
return fmt . Errorf ( "IsOrgMember: %v" , err )
} else if ! isMember {
continue
}
}
collaboration . UserID = c . ID
if _ , err := sess . Delete ( collaboration ) ; err != nil {
return fmt . Errorf ( "remove collaborator '%d': %v" , c . ID , err )
}
2021-09-27 18:07:19 +00:00
collaboration . UserID = 0
2021-03-01 00:47:30 +00:00
}
// Remove old team-repository relations.
if oldOwner . IsOrganization ( ) {
2021-11-19 11:41:40 +00:00
if err := OrgFromUser ( oldOwner ) . removeOrgRepo ( sess , repo . ID ) ; err != nil {
2021-03-01 00:47:30 +00:00
return fmt . Errorf ( "removeOrgRepo: %v" , err )
}
}
if newOwner . IsOrganization ( ) {
2021-11-19 11:41:40 +00:00
teams , err := OrgFromUser ( newOwner ) . loadTeams ( sess )
if err != nil {
2021-08-12 12:43:08 +00:00
return fmt . Errorf ( "LoadTeams: %v" , err )
2021-03-01 00:47:30 +00:00
}
2021-11-19 11:41:40 +00:00
for _ , t := range teams {
2021-03-01 00:47:30 +00:00
if t . IncludesAllRepositories {
if err := t . addRepository ( sess , repo ) ; err != nil {
return fmt . Errorf ( "addRepository: %v" , err )
}
}
}
} else if err := repo . recalculateAccesses ( sess ) ; err != nil {
// Organization called this in addRepository method.
return fmt . Errorf ( "recalculateAccesses: %v" , err )
}
// Update repository count.
if _ , err := sess . Exec ( "UPDATE `user` SET num_repos=num_repos+1 WHERE id=?" , newOwner . ID ) ; err != nil {
return fmt . Errorf ( "increase new owner repository count: %v" , err )
} else if _ , err := sess . Exec ( "UPDATE `user` SET num_repos=num_repos-1 WHERE id=?" , oldOwner . ID ) ; err != nil {
return fmt . Errorf ( "decrease old owner repository count: %v" , err )
}
if err := watchRepo ( sess , doer . ID , repo . ID , true ) ; err != nil {
return fmt . Errorf ( "watchRepo: %v" , err )
}
// Remove watch for organization.
if oldOwner . IsOrganization ( ) {
if err := watchRepo ( sess , oldOwner . ID , repo . ID , false ) ; err != nil {
return fmt . Errorf ( "watchRepo [false]: %v" , err )
}
}
2021-03-12 17:45:49 +00:00
// Delete labels that belong to the old organization and comments that added these labels
if oldOwner . IsOrganization ( ) {
if _ , err := sess . Exec ( ` DELETE FROM issue_label WHERE issue_label . id IN (
SELECT il_too . id FROM (
SELECT il_too_too . id
FROM issue_label AS il_too_too
2021-03-22 18:26:38 +00:00
INNER JOIN label ON il_too_too . label_id = label . id
2021-03-12 17:45:49 +00:00
INNER JOIN issue on issue . id = il_too_too . issue_id
WHERE
2021-03-23 23:10:19 +00:00
issue . repo_id = ? AND ( ( label . org_id = 0 AND issue . repo_id != label . repo_id ) OR ( label . repo_id = 0 AND label . org_id != ? ) )
2021-03-12 17:45:49 +00:00
) AS il_too ) ` , repo . ID , newOwner . ID ) ; err != nil {
return fmt . Errorf ( "Unable to remove old org labels: %v" , err )
}
if _ , err := sess . Exec ( ` DELETE FROM comment WHERE comment . id IN (
SELECT il_too . id FROM (
SELECT com . id
FROM comment AS com
INNER JOIN label ON com . label_id = label . id
2021-03-23 23:10:19 +00:00
INNER JOIN issue ON issue . id = com . issue_id
2021-03-12 17:45:49 +00:00
WHERE
2021-03-23 23:10:19 +00:00
com . type = ? AND issue . repo_id = ? AND ( ( label . org_id = 0 AND issue . repo_id != label . repo_id ) OR ( label . repo_id = 0 AND label . org_id != ? ) )
2021-03-12 17:45:49 +00:00
) AS il_too ) ` , CommentTypeLabel , repo . ID , newOwner . ID ) ; err != nil {
return fmt . Errorf ( "Unable to remove old org label comments: %v" , err )
}
}
2021-03-01 00:47:30 +00:00
// Rename remote repository to new path and delete local copy.
dir := UserPath ( newOwner . Name )
if err := os . MkdirAll ( dir , os . ModePerm ) ; err != nil {
return fmt . Errorf ( "Failed to create dir %s: %v" , dir , err )
}
2021-07-15 15:46:07 +00:00
if err := util . Rename ( RepoPath ( oldOwner . Name , repo . Name ) , RepoPath ( newOwner . Name , repo . Name ) ) ; err != nil {
2021-03-01 00:47:30 +00:00
return fmt . Errorf ( "rename repository directory: %v" , err )
}
2021-03-05 02:28:52 +00:00
repoRenamed = true
2021-03-01 00:47:30 +00:00
// Rename remote wiki repository to new path and delete local copy.
wikiPath := WikiPath ( oldOwner . Name , repo . Name )
if isExist , err := util . IsExist ( wikiPath ) ; err != nil {
log . Error ( "Unable to check if %s exists. Error: %v" , wikiPath , err )
return err
} else if isExist {
2021-07-15 15:46:07 +00:00
if err := util . Rename ( wikiPath , WikiPath ( newOwner . Name , repo . Name ) ) ; err != nil {
2021-03-01 00:47:30 +00:00
return fmt . Errorf ( "rename repository wiki: %v" , err )
}
2021-03-05 02:28:52 +00:00
wikiRenamed = true
2021-03-01 00:47:30 +00:00
}
if err := deleteRepositoryTransfer ( sess , repo . ID ) ; err != nil {
return fmt . Errorf ( "deleteRepositoryTransfer: %v" , err )
}
repo . Status = RepositoryReady
if err := updateRepositoryCols ( sess , repo , "status" ) ; err != nil {
return err
}
// If there was previously a redirect at this location, remove it.
if err := deleteRepoRedirect ( sess , newOwner . ID , repo . Name ) ; err != nil {
return fmt . Errorf ( "delete repo redirect: %v" , err )
}
if err := newRepoRedirect ( sess , oldOwner . ID , repo . ID , repo . Name , repo . Name ) ; err != nil {
return fmt . Errorf ( "newRepoRedirect: %v" , err )
}
return sess . Commit ( )
}