aboutsummaryrefslogtreecommitdiffstats
path: root/modules
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2020-03-21 11:41:33 +0800
committerGitHub <noreply@github.com>2020-03-20 23:41:33 -0400
commitdcaa5643d70cd1be72e53e82a30d2bc6e5f8de92 (patch)
tree55d98b7f1f8a24bf03d2be24b4c95f5e42e3b94b /modules
parentb3f4f812d818a860203d24dbf3f666a9557e0b7b (diff)
downloadgitea-dcaa5643d70cd1be72e53e82a30d2bc6e5f8de92.tar.gz
gitea-dcaa5643d70cd1be72e53e82a30d2bc6e5f8de92.zip
Fix branch api canPush and canMerge (#10776)
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'modules')
-rw-r--r--modules/convert/convert.go54
1 files changed, 31 insertions, 23 deletions
diff --git a/modules/convert/convert.go b/modules/convert/convert.go
index 240db77d2c..d75a130535 100644
--- a/modules/convert/convert.go
+++ b/modules/convert/convert.go
@@ -30,40 +30,48 @@ func ToEmail(email *models.EmailAddress) *api.Email {
}
// ToBranch convert a git.Commit and git.Branch to an api.Branch
-func ToBranch(repo *models.Repository, b *git.Branch, c *git.Commit, bp *models.ProtectedBranch, user *models.User, isRepoAdmin bool) *api.Branch {
+func ToBranch(repo *models.Repository, b *git.Branch, c *git.Commit, bp *models.ProtectedBranch, user *models.User, isRepoAdmin bool) (*api.Branch, error) {
if bp == nil {
- return &api.Branch{
- Name: b.Name,
- Commit: ToCommit(repo, c),
- Protected: false,
- RequiredApprovals: 0,
- EnableStatusCheck: false,
- StatusCheckContexts: []string{},
- UserCanPush: true,
- UserCanMerge: true,
- EffectiveBranchProtectionName: "",
+ var hasPerm bool
+ var err error
+ if user != nil {
+ hasPerm, err = models.HasAccessUnit(user, repo, models.UnitTypeCode, models.AccessModeWrite)
+ if err != nil {
+ return nil, err
+ }
}
- }
- branchProtectionName := ""
- if isRepoAdmin {
- branchProtectionName = bp.BranchName
+
+ return &api.Branch{
+ Name: b.Name,
+ Commit: ToCommit(repo, c),
+ Protected: false,
+ RequiredApprovals: 0,
+ EnableStatusCheck: false,
+ StatusCheckContexts: []string{},
+ UserCanPush: hasPerm,
+ UserCanMerge: hasPerm,
+ }, nil
}
branch := &api.Branch{
- Name: b.Name,
- Commit: ToCommit(repo, c),
- Protected: true,
- RequiredApprovals: bp.RequiredApprovals,
- EnableStatusCheck: bp.EnableStatusCheck,
- StatusCheckContexts: bp.StatusCheckContexts,
- EffectiveBranchProtectionName: branchProtectionName,
+ Name: b.Name,
+ Commit: ToCommit(repo, c),
+ Protected: true,
+ RequiredApprovals: bp.RequiredApprovals,
+ EnableStatusCheck: bp.EnableStatusCheck,
+ StatusCheckContexts: bp.StatusCheckContexts,
+ }
+
+ if isRepoAdmin {
+ branch.EffectiveBranchProtectionName = bp.BranchName
}
if user != nil {
branch.UserCanPush = bp.CanUserPush(user.ID)
branch.UserCanMerge = bp.IsUserMergeWhitelisted(user.ID)
}
- return branch
+
+ return branch, nil
}
// ToBranchProtection convert a ProtectedBranch to api.BranchProtection