Fix branch api canPush and canMerge (#10776)
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
This commit is contained in:
parent
b3f4f812d8
commit
dcaa5643d7
3 changed files with 45 additions and 25 deletions
|
@ -28,6 +28,8 @@ func testAPIGetBranch(t *testing.T, branchName string, exists bool) {
|
||||||
var branch api.Branch
|
var branch api.Branch
|
||||||
DecodeJSON(t, resp, &branch)
|
DecodeJSON(t, resp, &branch)
|
||||||
assert.EqualValues(t, branchName, branch.Name)
|
assert.EqualValues(t, branchName, branch.Name)
|
||||||
|
assert.True(t, branch.UserCanPush)
|
||||||
|
assert.True(t, branch.UserCanMerge)
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAPIGetBranchProtection(t *testing.T, branchName string, expectedHTTPStatus int) {
|
func testAPIGetBranchProtection(t *testing.T, branchName string, expectedHTTPStatus int) {
|
||||||
|
|
|
@ -30,40 +30,48 @@ func ToEmail(email *models.EmailAddress) *api.Email {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToBranch convert a git.Commit and git.Branch to an api.Branch
|
// 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 {
|
if bp == nil {
|
||||||
return &api.Branch{
|
var hasPerm bool
|
||||||
Name: b.Name,
|
var err error
|
||||||
Commit: ToCommit(repo, c),
|
if user != nil {
|
||||||
Protected: false,
|
hasPerm, err = models.HasAccessUnit(user, repo, models.UnitTypeCode, models.AccessModeWrite)
|
||||||
RequiredApprovals: 0,
|
if err != nil {
|
||||||
EnableStatusCheck: false,
|
return nil, err
|
||||||
StatusCheckContexts: []string{},
|
}
|
||||||
UserCanPush: true,
|
|
||||||
UserCanMerge: true,
|
|
||||||
EffectiveBranchProtectionName: "",
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
branchProtectionName := ""
|
return &api.Branch{
|
||||||
if isRepoAdmin {
|
Name: b.Name,
|
||||||
branchProtectionName = bp.BranchName
|
Commit: ToCommit(repo, c),
|
||||||
|
Protected: false,
|
||||||
|
RequiredApprovals: 0,
|
||||||
|
EnableStatusCheck: false,
|
||||||
|
StatusCheckContexts: []string{},
|
||||||
|
UserCanPush: hasPerm,
|
||||||
|
UserCanMerge: hasPerm,
|
||||||
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
branch := &api.Branch{
|
branch := &api.Branch{
|
||||||
Name: b.Name,
|
Name: b.Name,
|
||||||
Commit: ToCommit(repo, c),
|
Commit: ToCommit(repo, c),
|
||||||
Protected: true,
|
Protected: true,
|
||||||
RequiredApprovals: bp.RequiredApprovals,
|
RequiredApprovals: bp.RequiredApprovals,
|
||||||
EnableStatusCheck: bp.EnableStatusCheck,
|
EnableStatusCheck: bp.EnableStatusCheck,
|
||||||
StatusCheckContexts: bp.StatusCheckContexts,
|
StatusCheckContexts: bp.StatusCheckContexts,
|
||||||
EffectiveBranchProtectionName: branchProtectionName,
|
}
|
||||||
|
|
||||||
|
if isRepoAdmin {
|
||||||
|
branch.EffectiveBranchProtectionName = bp.BranchName
|
||||||
}
|
}
|
||||||
|
|
||||||
if user != nil {
|
if user != nil {
|
||||||
branch.UserCanPush = bp.CanUserPush(user.ID)
|
branch.UserCanPush = bp.CanUserPush(user.ID)
|
||||||
branch.UserCanMerge = bp.IsUserMergeWhitelisted(user.ID)
|
branch.UserCanMerge = bp.IsUserMergeWhitelisted(user.ID)
|
||||||
}
|
}
|
||||||
return branch
|
|
||||||
|
return branch, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToBranchProtection convert a ProtectedBranch to api.BranchProtection
|
// ToBranchProtection convert a ProtectedBranch to api.BranchProtection
|
||||||
|
|
|
@ -72,7 +72,13 @@ func GetBranch(ctx *context.APIContext) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.JSON(http.StatusOK, convert.ToBranch(ctx.Repo.Repository, branch, c, branchProtection, ctx.User, ctx.Repo.IsAdmin()))
|
br, err := convert.ToBranch(ctx.Repo.Repository, branch, c, branchProtection, ctx.User, ctx.Repo.IsAdmin())
|
||||||
|
if err != nil {
|
||||||
|
ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx.JSON(http.StatusOK, br)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ListBranches list all the branches of a repository
|
// ListBranches list all the branches of a repository
|
||||||
|
@ -115,7 +121,11 @@ func ListBranches(ctx *context.APIContext) {
|
||||||
ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
|
ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
apiBranches[i] = convert.ToBranch(ctx.Repo.Repository, branches[i], c, branchProtection, ctx.User, ctx.Repo.IsAdmin())
|
apiBranches[i], err = convert.ToBranch(ctx.Repo.Repository, branches[i], c, branchProtection, ctx.User, ctx.Repo.IsAdmin())
|
||||||
|
if err != nil {
|
||||||
|
ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx.JSON(http.StatusOK, &apiBranches)
|
ctx.JSON(http.StatusOK, &apiBranches)
|
||||||
|
|
Loading…
Reference in a new issue