Skip to content

Commit

Permalink
Fix bug on branch API (go-gitea#10767)
Browse files Browse the repository at this point in the history
  • Loading branch information
lunny committed Mar 20, 2020
1 parent e2da9cd commit 5738928
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 12 deletions.
22 changes: 22 additions & 0 deletions models/branches.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,28 @@ func (protectBranch *ProtectedBranch) CanUserMerge(userID int64) bool {
return in
}

// IsUserMergeWhitelisted checks if some user is whitelisted to merge to this branch
func (protectBranch *ProtectedBranch) IsUserMergeWhitelisted(userID int64) bool {
if !protectBranch.EnableMergeWhitelist {
return true
}

if base.Int64sContains(protectBranch.MergeWhitelistUserIDs, userID) {
return true
}

if len(protectBranch.MergeWhitelistTeamIDs) == 0 {
return false
}

in, err := IsUserInTeams(userID, protectBranch.MergeWhitelistTeamIDs)
if err != nil {
log.Error("IsUserInTeams: %v", err)
return false
}
return in
}

// IsUserOfficialReviewer check if user is official reviewer for the branch (counts towards required approvals)
func (protectBranch *ProtectedBranch) IsUserOfficialReviewer(user *User) (bool, error) {
return protectBranch.isUserOfficialReviewer(x, user)
Expand Down
28 changes: 16 additions & 12 deletions modules/convert/convert.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,28 +31,32 @@ 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) *api.Branch {
var branch *api.Branch
if bp == nil {
return &api.Branch{
branch = &api.Branch{
Name: b.Name,
Commit: ToCommit(repo, c),
Protected: false,
RequiredApprovals: 0,
EnableStatusCheck: false,
StatusCheckContexts: []string{},
UserCanPush: true,
UserCanMerge: true,
}
} else {
branch = &api.Branch{
Name: b.Name,
Commit: ToCommit(repo, c),
Protected: true,
RequiredApprovals: bp.RequiredApprovals,
EnableStatusCheck: bp.EnableStatusCheck,
StatusCheckContexts: bp.StatusCheckContexts,
}
}
return &api.Branch{
Name: b.Name,
Commit: ToCommit(repo, c),
Protected: true,
RequiredApprovals: bp.RequiredApprovals,
EnableStatusCheck: bp.EnableStatusCheck,
StatusCheckContexts: bp.StatusCheckContexts,
UserCanPush: bp.CanUserPush(user.ID),
UserCanMerge: bp.CanUserMerge(user.ID),

if user != nil {
branch.UserCanPush = bp.CanUserPush(user.ID)
branch.UserCanMerge = bp.IsUserMergeWhitelisted(user.ID)
}
return branch
}

// ToTag convert a git.Tag to an api.Tag
Expand Down

0 comments on commit 5738928

Please sign in to comment.