Skip to content

Commit 60ac321

Browse files
committed
improve error handling
1 parent b7f53da commit 60ac321

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

routers/private/hook_pre_receive.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,9 @@ type preReceiveContext struct {
5050
// CanWriteCode returns true if pusher can write code
5151
func (ctx *preReceiveContext) CanWriteCode() bool {
5252
if !ctx.checkedCanWriteCode {
53-
ctx.loadPusherAndPermission()
53+
if !ctx.loadPusherAndPermission() {
54+
return false
55+
}
5456
ctx.canWriteCode = ctx.userPerm.CanWrite(unit.TypeCode) || ctx.deployKeyAccessMode >= perm_model.AccessModeWrite
5557
ctx.checkedCanWriteCode = true
5658
}
@@ -74,7 +76,9 @@ func (ctx *preReceiveContext) AssertCanWriteCode() bool {
7476
// CanCreatePullRequest returns true if pusher can create pull requests
7577
func (ctx *preReceiveContext) CanCreatePullRequest() bool {
7678
if !ctx.checkedCanCreatePullRequest {
77-
ctx.loadPusherAndPermission()
79+
if !ctx.loadPusherAndPermission() {
80+
return false
81+
}
7882
ctx.canCreatePullRequest = ctx.userPerm.CanRead(unit.TypePullRequests)
7983
ctx.checkedCanCreatePullRequest = true
8084
}
@@ -295,7 +299,11 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID, refFullN
295299
return
296300
}
297301

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+
}
299307

300308
// Now check if the user is allowed to merge PRs for this repository
301309
// 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) {
444452
return env
445453
}
446454

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 {
448457
if ctx.loadedPusher {
449-
return
458+
return true
450459
}
451460

452461
user, err := user_model.GetUserByID(ctx.opts.UserID)
@@ -455,7 +464,7 @@ func (ctx *preReceiveContext) loadPusherAndPermission() {
455464
ctx.JSON(http.StatusInternalServerError, private.Response{
456465
Err: fmt.Sprintf("Unable to get User id %d Error: %v", ctx.opts.UserID, err),
457466
})
458-
return
467+
return false
459468
}
460469
ctx.user = user
461470

@@ -465,7 +474,7 @@ func (ctx *preReceiveContext) loadPusherAndPermission() {
465474
ctx.JSON(http.StatusInternalServerError, private.Response{
466475
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),
467476
})
468-
return
477+
return false
469478
}
470479
ctx.userPerm = userPerm
471480

@@ -476,10 +485,11 @@ func (ctx *preReceiveContext) loadPusherAndPermission() {
476485
ctx.JSON(http.StatusInternalServerError, private.Response{
477486
Err: fmt.Sprintf("Unable to get DeployKey id %d Error: %v", ctx.opts.DeployKeyID, err),
478487
})
479-
return
488+
return false
480489
}
481490
ctx.deployKeyAccessMode = deployKey.Mode
482491
}
483492

484493
ctx.loadedPusher = true
494+
return true
485495
}

0 commit comments

Comments
 (0)