summaryrefslogtreecommitdiffstats
path: root/models/repo.go
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2017-09-14 16:16:22 +0800
committerGitHub <noreply@github.com>2017-09-14 16:16:22 +0800
commit1739e84ac02c0384c04576a00abab9348293f9c7 (patch)
tree1015e68f36421f274d2e883ff3ddb0cb29b6af71 /models/repo.go
parentbe3319b3d545289b772d7a92b4b62205863954d9 (diff)
downloadgitea-1739e84ac02c0384c04576a00abab9348293f9c7.tar.gz
gitea-1739e84ac02c0384c04576a00abab9348293f9c7.zip
improve protected branch to add whitelist support (#2451)
* improve protected branch to add whitelist support * fix lint * fix style check * fix tests * fix description on UI and import * fix test * bug fixed * fix tests and languages * move isSliceInt64Eq to util pkg; improve function names & typo
Diffstat (limited to 'models/repo.go')
-rw-r--r--models/repo.go36
1 files changed, 36 insertions, 0 deletions
diff --git a/models/repo.go b/models/repo.go
index a2e63e2af7..8d5b3b87c7 100644
--- a/models/repo.go
+++ b/models/repo.go
@@ -656,6 +656,42 @@ func (repo *Repository) CanEnableEditor() bool {
return !repo.IsMirror
}
+// GetWriters returns all users that have write access to the repository.
+func (repo *Repository) GetWriters() (_ []*User, err error) {
+ return repo.getUsersWithAccessMode(x, AccessModeWrite)
+}
+
+// getUsersWithAccessMode returns users that have at least given access mode to the repository.
+func (repo *Repository) getUsersWithAccessMode(e Engine, mode AccessMode) (_ []*User, err error) {
+ if err = repo.getOwner(e); err != nil {
+ return nil, err
+ }
+
+ accesses := make([]*Access, 0, 10)
+ if err = e.Where("repo_id = ? AND mode >= ?", repo.ID, mode).Find(&accesses); err != nil {
+ return nil, err
+ }
+
+ // Leave a seat for owner itself to append later, but if owner is an organization
+ // and just waste 1 unit is cheaper than re-allocate memory once.
+ users := make([]*User, 0, len(accesses)+1)
+ if len(accesses) > 0 {
+ userIDs := make([]int64, len(accesses))
+ for i := 0; i < len(accesses); i++ {
+ userIDs[i] = accesses[i].UserID
+ }
+
+ if err = e.In("id", userIDs).Find(&users); err != nil {
+ return nil, err
+ }
+ }
+ if !repo.Owner.IsOrganization() {
+ users = append(users, repo.Owner)
+ }
+
+ return users, nil
+}
+
// NextIssueIndex returns the next issue index
// FIXME: should have a mutex to prevent producing same index for two issues that are created
// closely enough.