summaryrefslogtreecommitdiffstats
path: root/models/repo.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2019-10-26 14:54:11 +0800
committerGitHub <noreply@github.com>2019-10-26 14:54:11 +0800
commit9e85358777ea4bd345f832d08b19915a72cc128b (patch)
tree56116e7a6acbfd91a29cade7f753802f9ea561ea /models/repo.go
parentd2d5910894cfe513f8aa331b30c991db6fbe8e6d (diff)
downloadgitea-9e85358777ea4bd345f832d08b19915a72cc128b.tar.gz
gitea-9e85358777ea4bd345f832d08b19915a72cc128b.zip
Move some repositories' operations to a standalone service package (#8557)
* Move some repositories' operations to a standalone service package * improve code * remove unused codes * add rollback when fork failed * add repo when return
Diffstat (limited to 'models/repo.go')
-rw-r--r--models/repo.go91
1 files changed, 35 insertions, 56 deletions
diff --git a/models/repo.go b/models/repo.go
index 3b5395ce16..ac3443f895 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -742,6 +742,28 @@ func (repo *Repository) CanUserFork(user *User) (bool, error) {
return false, nil
}
+// CanUserDelete returns true if user could delete the repository
+func (repo *Repository) CanUserDelete(user *User) (bool, error) {
+ if user.IsAdmin || user.ID == repo.OwnerID {
+ return true, nil
+ }
+
+ if err := repo.GetOwner(); err != nil {
+ return false, err
+ }
+
+ if repo.Owner.IsOrganization() {
+ isOwner, err := repo.Owner.IsOwnedBy(user.ID)
+ if err != nil {
+ return false, err
+ } else if isOwner {
+ return true, nil
+ }
+ }
+
+ return false, nil
+}
+
// CanEnablePulls returns true if repository meets the requirements of accepting pulls.
func (repo *Repository) CanEnablePulls() bool {
return !repo.IsMirror && !repo.IsEmpty
@@ -1430,15 +1452,9 @@ func createRepository(e *xorm.Session, doer, u *User, repo *Repository) (err err
t, err := u.getOwnerTeam(e)
if err != nil {
return fmt.Errorf("getOwnerTeam: %v", err)
- } else if err = t.addRepository(e, repo); err != nil {
+ }
+ if err = t.addRepository(e, repo); err != nil {
return fmt.Errorf("addRepository: %v", err)
- } else if err = prepareWebhooks(e, repo, HookEventRepository, &api.RepositoryPayload{
- Action: api.HookRepoCreated,
- Repository: repo.innerAPIFormat(e, AccessModeOwner, false),
- Organization: u.APIFormat(),
- Sender: doer.APIFormat(),
- }); err != nil {
- return fmt.Errorf("prepareWebhooks: %v", err)
}
} else if err = repo.recalculateAccesses(e); err != nil {
// Organization automatically called this in addRepository method.
@@ -1522,11 +1538,6 @@ func CreateRepository(doer, u *User, opts CreateRepoOptions) (_ *Repository, err
return nil, err
}
- // Add to hook queue for created repo after session commit.
- if u.IsOrganization() {
- go HookQueue.Add(repo.ID)
- }
-
return repo, err
}
@@ -2044,18 +2055,6 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
return fmt.Errorf("Commit: %v", err)
}
- if org.IsOrganization() {
- if err = PrepareWebhooks(repo, HookEventRepository, &api.RepositoryPayload{
- Action: api.HookRepoDeleted,
- Repository: repo.APIFormat(AccessModeOwner),
- Organization: org.APIFormat(),
- Sender: doer.APIFormat(),
- }); err != nil {
- return err
- }
- go HookQueue.Add(repo.ID)
- }
-
if len(repo.Avatar) > 0 {
avatarPath := repo.CustomAvatarPath()
if com.IsExist(avatarPath) {
@@ -2065,7 +2064,6 @@ func DeleteRepository(doer *User, uid, repoID int64) error {
}
}
- DeleteRepoFromIndexer(repo)
return nil
}
@@ -2521,22 +2519,22 @@ func HasForkedRepo(ownerID, repoID int64) (*Repository, bool) {
}
// ForkRepository forks a repository
-func ForkRepository(doer, u *User, oldRepo *Repository, name, desc string) (_ *Repository, err error) {
- forkedRepo, err := oldRepo.GetUserFork(u.ID)
+func ForkRepository(doer, owner *User, oldRepo *Repository, name, desc string) (_ *Repository, err error) {
+ forkedRepo, err := oldRepo.GetUserFork(owner.ID)
if err != nil {
return nil, err
}
if forkedRepo != nil {
return nil, ErrForkAlreadyExist{
- Uname: u.Name,
+ Uname: owner.Name,
RepoName: oldRepo.FullName(),
ForkName: forkedRepo.FullName(),
}
}
repo := &Repository{
- OwnerID: u.ID,
- Owner: u,
+ OwnerID: owner.ID,
+ Owner: owner,
Name: name,
LowerName: strings.ToLower(name),
Description: desc,
@@ -2553,7 +2551,7 @@ func ForkRepository(doer, u *User, oldRepo *Repository, name, desc string) (_ *R
return nil, err
}
- if err = createRepository(sess, doer, u, repo); err != nil {
+ if err = createRepository(sess, doer, owner, repo); err != nil {
return nil, err
}
@@ -2561,9 +2559,9 @@ func ForkRepository(doer, u *User, oldRepo *Repository, name, desc string) (_ *R
return nil, err
}
- repoPath := RepoPath(u.Name, repo.Name)
+ repoPath := RepoPath(owner.Name, repo.Name)
_, stderr, err := process.GetManager().ExecTimeout(10*time.Minute,
- fmt.Sprintf("ForkRepository(git clone): %s/%s", u.Name, repo.Name),
+ fmt.Sprintf("ForkRepository(git clone): %s/%s", owner.Name, repo.Name),
git.GitExecutable, "clone", "--bare", oldRepo.repoPath(sess), repoPath)
if err != nil {
return nil, fmt.Errorf("git clone: %v", stderr)
@@ -2586,24 +2584,6 @@ func ForkRepository(doer, u *User, oldRepo *Repository, name, desc string) (_ *R
return nil, err
}
- oldMode, _ := AccessLevel(doer, oldRepo)
- mode, _ := AccessLevel(doer, repo)
-
- if err = PrepareWebhooks(oldRepo, HookEventFork, &api.ForkPayload{
- Forkee: oldRepo.APIFormat(oldMode),
- Repo: repo.APIFormat(mode),
- Sender: doer.APIFormat(),
- }); err != nil {
- log.Error("PrepareWebhooks [repo_id: %d]: %v", oldRepo.ID, err)
- } else {
- go HookQueue.Add(oldRepo.ID)
- }
-
- // Add to hook queue for created repo after session commit.
- if u.IsOrganization() {
- go HookQueue.Add(repo.ID)
- }
-
if err = repo.UpdateSize(); err != nil {
log.Error("Failed to update size for repository: %v", err)
}
@@ -2612,20 +2592,19 @@ func ForkRepository(doer, u *User, oldRepo *Repository, name, desc string) (_ *R
sess2 := x.NewSession()
defer sess2.Close()
if err = sess2.Begin(); err != nil {
- return nil, err
+ return repo, err
}
var lfsObjects []*LFSMetaObject
-
if err = sess2.Where("repository_id=?", oldRepo.ID).Find(&lfsObjects); err != nil {
- return nil, err
+ return repo, err
}
for _, v := range lfsObjects {
v.ID = 0
v.RepositoryID = repo.ID
if _, err = sess2.Insert(v); err != nil {
- return nil, err
+ return repo, err
}
}