@@ -30,40 +30,48 @@ func ToEmail(email *models.EmailAddress) *api.Email {
30
30
}
31
31
32
32
// 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 ) {
34
34
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
+ }
45
42
}
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
50
54
}
51
55
52
56
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
60
67
}
61
68
62
69
if user != nil {
63
70
branch .UserCanPush = bp .CanUserPush (user .ID )
64
71
branch .UserCanMerge = bp .IsUserMergeWhitelisted (user .ID )
65
72
}
66
- return branch
73
+
74
+ return branch , nil
67
75
}
68
76
69
77
// ToBranchProtection convert a ProtectedBranch to api.BranchProtection
0 commit comments