diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2023-09-09 05:09:23 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-09-08 21:09:23 +0000 |
commit | e3ed67859a434f2fef1db175e9c82251a7f9f6c9 (patch) | |
tree | e4c48b0c17d6d5a9507c3fc1479d7da2faba59dd /services/repository | |
parent | b8ad558c93052ee3e2f43f1e24e6f5c134da36c4 (diff) | |
download | gitea-e3ed67859a434f2fef1db175e9c82251a7f9f6c9.tar.gz gitea-e3ed67859a434f2fef1db175e9c82251a7f9f6c9.zip |
Move some functions to service layer (#26969)
Diffstat (limited to 'services/repository')
-rw-r--r-- | services/repository/collaboration.go | 47 | ||||
-rw-r--r-- | services/repository/collaboration_test.go | 28 |
2 files changed, 75 insertions, 0 deletions
diff --git a/services/repository/collaboration.go b/services/repository/collaboration.go new file mode 100644 index 0000000000..28824d83f5 --- /dev/null +++ b/services/repository/collaboration.go @@ -0,0 +1,47 @@ +// Copyright 2016 The Gogs Authors. All rights reserved. +// Copyright 2020 The Gitea Authors. +// SPDX-License-Identifier: MIT + +package repository + +import ( + "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/db" + access_model "code.gitea.io/gitea/models/perm/access" + repo_model "code.gitea.io/gitea/models/repo" +) + +// DeleteCollaboration removes collaboration relation between the user and repository. +func DeleteCollaboration(repo *repo_model.Repository, uid int64) (err error) { + collaboration := &repo_model.Collaboration{ + RepoID: repo.ID, + UserID: uid, + } + + ctx, committer, err := db.TxContext(db.DefaultContext) + if err != nil { + return err + } + defer committer.Close() + + if has, err := db.GetEngine(ctx).Delete(collaboration); err != nil || has == 0 { + return err + } else if err = access_model.RecalculateAccesses(ctx, repo); err != nil { + return err + } + + if err = repo_model.WatchRepo(ctx, uid, repo.ID, false); err != nil { + return err + } + + if err = models.ReconsiderWatches(ctx, repo, uid); err != nil { + return err + } + + // Unassign a user from any issue (s)he has been assigned to in the repository + if err := models.ReconsiderRepoIssuesAssignee(ctx, repo, uid); err != nil { + return err + } + + return committer.Commit() +} diff --git a/services/repository/collaboration_test.go b/services/repository/collaboration_test.go new file mode 100644 index 0000000000..08159af7bc --- /dev/null +++ b/services/repository/collaboration_test.go @@ -0,0 +1,28 @@ +// Copyright 2017 The Gitea Authors. All rights reserved. +// SPDX-License-Identifier: MIT + +package repository + +import ( + "testing" + + "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" + "code.gitea.io/gitea/models/unittest" + + "github.com/stretchr/testify/assert" +) + +func TestRepository_DeleteCollaboration(t *testing.T) { + assert.NoError(t, unittest.PrepareTestDatabase()) + + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4}) + assert.NoError(t, repo.LoadOwner(db.DefaultContext)) + assert.NoError(t, DeleteCollaboration(repo, 4)) + unittest.AssertNotExistsBean(t, &repo_model.Collaboration{RepoID: repo.ID, UserID: 4}) + + assert.NoError(t, DeleteCollaboration(repo, 4)) + unittest.AssertNotExistsBean(t, &repo_model.Collaboration{RepoID: repo.ID, UserID: 4}) + + unittest.CheckConsistencyFor(t, &repo_model.Repository{ID: repo.ID}) +} |