Skip to content

Commit dcaa564

Browse files
Fix branch api canPush and canMerge (go-gitea#10776)
Co-authored-by: techknowlogick <[email protected]>
1 parent b3f4f81 commit dcaa564

File tree

3 files changed

+45
-25
lines changed

3 files changed

+45
-25
lines changed

integrations/api_branch_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ func testAPIGetBranch(t *testing.T, branchName string, exists bool) {
2828
var branch api.Branch
2929
DecodeJSON(t, resp, &branch)
3030
assert.EqualValues(t, branchName, branch.Name)
31+
assert.True(t, branch.UserCanPush)
32+
assert.True(t, branch.UserCanMerge)
3133
}
3234

3335
func testAPIGetBranchProtection(t *testing.T, branchName string, expectedHTTPStatus int) {

modules/convert/convert.go

Lines changed: 31 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,40 +30,48 @@ func ToEmail(email *models.EmailAddress) *api.Email {
3030
}
3131

3232
// ToBranch convert a git.Commit and git.Branch to an api.Branch
33-
func ToBranch(repo *models.Repository, b *git.Branch, c *git.Commit, bp *models.ProtectedBranch, user *models.User, isRepoAdmin bool) *api.Branch {
33+
func ToBranch(repo *models.Repository, b *git.Branch, c *git.Commit, bp *models.ProtectedBranch, user *models.User, isRepoAdmin bool) (*api.Branch, error) {
3434
if bp == nil {
35-
return &api.Branch{
36-
Name: b.Name,
37-
Commit: ToCommit(repo, c),
38-
Protected: false,
39-
RequiredApprovals: 0,
40-
EnableStatusCheck: false,
41-
StatusCheckContexts: []string{},
42-
UserCanPush: true,
43-
UserCanMerge: true,
44-
EffectiveBranchProtectionName: "",
35+
var hasPerm bool
36+
var err error
37+
if user != nil {
38+
hasPerm, err = models.HasAccessUnit(user, repo, models.UnitTypeCode, models.AccessModeWrite)
39+
if err != nil {
40+
return nil, err
41+
}
4542
}
46-
}
47-
branchProtectionName := ""
48-
if isRepoAdmin {
49-
branchProtectionName = bp.BranchName
43+
44+
return &api.Branch{
45+
Name: b.Name,
46+
Commit: ToCommit(repo, c),
47+
Protected: false,
48+
RequiredApprovals: 0,
49+
EnableStatusCheck: false,
50+
StatusCheckContexts: []string{},
51+
UserCanPush: hasPerm,
52+
UserCanMerge: hasPerm,
53+
}, nil
5054
}
5155

5256
branch := &api.Branch{
53-
Name: b.Name,
54-
Commit: ToCommit(repo, c),
55-
Protected: true,
56-
RequiredApprovals: bp.RequiredApprovals,
57-
EnableStatusCheck: bp.EnableStatusCheck,
58-
StatusCheckContexts: bp.StatusCheckContexts,
59-
EffectiveBranchProtectionName: branchProtectionName,
57+
Name: b.Name,
58+
Commit: ToCommit(repo, c),
59+
Protected: true,
60+
RequiredApprovals: bp.RequiredApprovals,
61+
EnableStatusCheck: bp.EnableStatusCheck,
62+
StatusCheckContexts: bp.StatusCheckContexts,
63+
}
64+
65+
if isRepoAdmin {
66+
branch.EffectiveBranchProtectionName = bp.BranchName
6067
}
6168

6269
if user != nil {
6370
branch.UserCanPush = bp.CanUserPush(user.ID)
6471
branch.UserCanMerge = bp.IsUserMergeWhitelisted(user.ID)
6572
}
66-
return branch
73+
74+
return branch, nil
6775
}
6876

6977
// ToBranchProtection convert a ProtectedBranch to api.BranchProtection

routers/api/v1/repo/branch.go

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,13 @@ func GetBranch(ctx *context.APIContext) {
7272
return
7373
}
7474

75-
ctx.JSON(http.StatusOK, convert.ToBranch(ctx.Repo.Repository, branch, c, branchProtection, ctx.User, ctx.Repo.IsAdmin()))
75+
br, err := convert.ToBranch(ctx.Repo.Repository, branch, c, branchProtection, ctx.User, ctx.Repo.IsAdmin())
76+
if err != nil {
77+
ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
78+
return
79+
}
80+
81+
ctx.JSON(http.StatusOK, br)
7682
}
7783

7884
// ListBranches list all the branches of a repository
@@ -115,7 +121,11 @@ func ListBranches(ctx *context.APIContext) {
115121
ctx.Error(http.StatusInternalServerError, "GetBranchProtection", err)
116122
return
117123
}
118-
apiBranches[i] = convert.ToBranch(ctx.Repo.Repository, branches[i], c, branchProtection, ctx.User, ctx.Repo.IsAdmin())
124+
apiBranches[i], err = convert.ToBranch(ctx.Repo.Repository, branches[i], c, branchProtection, ctx.User, ctx.Repo.IsAdmin())
125+
if err != nil {
126+
ctx.Error(http.StatusInternalServerError, "convert.ToBranch", err)
127+
return
128+
}
119129
}
120130

121131
ctx.JSON(http.StatusOK, &apiBranches)

0 commit comments

Comments
 (0)