@@ -50,7 +50,9 @@ type preReceiveContext struct {
50
50
// CanWriteCode returns true if pusher can write code
51
51
func (ctx * preReceiveContext ) CanWriteCode () bool {
52
52
if ! ctx .checkedCanWriteCode {
53
- ctx .loadPusherAndPermission ()
53
+ if ! ctx .loadPusherAndPermission () {
54
+ return false
55
+ }
54
56
ctx .canWriteCode = ctx .userPerm .CanWrite (unit .TypeCode ) || ctx .deployKeyAccessMode >= perm_model .AccessModeWrite
55
57
ctx .checkedCanWriteCode = true
56
58
}
@@ -74,7 +76,9 @@ func (ctx *preReceiveContext) AssertCanWriteCode() bool {
74
76
// CanCreatePullRequest returns true if pusher can create pull requests
75
77
func (ctx * preReceiveContext ) CanCreatePullRequest () bool {
76
78
if ! ctx .checkedCanCreatePullRequest {
77
- ctx .loadPusherAndPermission ()
79
+ if ! ctx .loadPusherAndPermission () {
80
+ return false
81
+ }
78
82
ctx .canCreatePullRequest = ctx .userPerm .CanRead (unit .TypePullRequests )
79
83
ctx .checkedCanCreatePullRequest = true
80
84
}
@@ -295,7 +299,11 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID, refFullN
295
299
return
296
300
}
297
301
298
- ctx .loadPusherAndPermission ()
302
+ // although we should have called `loadPusherAndPermission` before, here we call it explicitly again because we need to access ctx.user below
303
+ if ! ctx .loadPusherAndPermission () {
304
+ // if error occurs, loadPusherAndPermission had written the error response
305
+ return
306
+ }
299
307
300
308
// Now check if the user is allowed to merge PRs for this repository
301
309
// Note: we can use ctx.perm and ctx.user directly as they will have been loaded above
@@ -444,9 +452,10 @@ func generateGitEnv(opts *private.HookOptions) (env []string) {
444
452
return env
445
453
}
446
454
447
- func (ctx * preReceiveContext ) loadPusherAndPermission () {
455
+ // loadPusherAndPermission returns false if an error occurs, and it writes the error response
456
+ func (ctx * preReceiveContext ) loadPusherAndPermission () bool {
448
457
if ctx .loadedPusher {
449
- return
458
+ return true
450
459
}
451
460
452
461
user , err := user_model .GetUserByID (ctx .opts .UserID )
@@ -455,7 +464,7 @@ func (ctx *preReceiveContext) loadPusherAndPermission() {
455
464
ctx .JSON (http .StatusInternalServerError , private.Response {
456
465
Err : fmt .Sprintf ("Unable to get User id %d Error: %v" , ctx .opts .UserID , err ),
457
466
})
458
- return
467
+ return false
459
468
}
460
469
ctx .user = user
461
470
@@ -465,7 +474,7 @@ func (ctx *preReceiveContext) loadPusherAndPermission() {
465
474
ctx .JSON (http .StatusInternalServerError , private.Response {
466
475
Err : fmt .Sprintf ("Unable to get Repo permission of repo %s/%s of User %s: %v" , ctx .Repo .Repository .OwnerName , ctx .Repo .Repository .Name , user .Name , err ),
467
476
})
468
- return
477
+ return false
469
478
}
470
479
ctx .userPerm = userPerm
471
480
@@ -476,10 +485,11 @@ func (ctx *preReceiveContext) loadPusherAndPermission() {
476
485
ctx .JSON (http .StatusInternalServerError , private.Response {
477
486
Err : fmt .Sprintf ("Unable to get DeployKey id %d Error: %v" , ctx .opts .DeployKeyID , err ),
478
487
})
479
- return
488
+ return false
480
489
}
481
490
ctx .deployKeyAccessMode = deployKey .Mode
482
491
}
483
492
484
493
ctx .loadedPusher = true
494
+ return true
485
495
}
0 commit comments