diff options
Diffstat (limited to 'routers/private')
-rw-r--r-- | routers/private/hook_post_receive.go | 36 | ||||
-rw-r--r-- | routers/private/hook_post_receive_test.go | 2 | ||||
-rw-r--r-- | routers/private/hook_pre_receive.go | 14 | ||||
-rw-r--r-- | routers/private/hook_verification.go | 3 | ||||
-rw-r--r-- | routers/private/hook_verification_test.go | 4 | ||||
-rw-r--r-- | routers/private/manager.go | 2 | ||||
-rw-r--r-- | routers/private/serv.go | 10 |
7 files changed, 32 insertions, 39 deletions
diff --git a/routers/private/hook_post_receive.go b/routers/private/hook_post_receive.go index dba6aef9a3..e8bef7d6c1 100644 --- a/routers/private/hook_post_receive.go +++ b/routers/private/hook_post_receive.go @@ -14,6 +14,7 @@ import ( repo_model "code.gitea.io/gitea/models/repo" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/cache" + "code.gitea.io/gitea/modules/cachegroup" "code.gitea.io/gitea/modules/git" "code.gitea.io/gitea/modules/gitrepo" "code.gitea.io/gitea/modules/log" @@ -206,25 +207,19 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) { return } - cols := make([]string, 0, 2) - - if isPrivate.Has() { + // FIXME: these options are not quite right, for example: changing visibility should do more works than just setting the is_private flag + // These options should only be used for "push-to-create" + if isPrivate.Has() && repo.IsPrivate != isPrivate.Value() { + // TODO: it needs to do more work repo.IsPrivate = isPrivate.Value() - cols = append(cols, "is_private") + if err = repo_model.UpdateRepositoryColsNoAutoTime(ctx, repo, "is_private"); err != nil { + ctx.JSON(http.StatusInternalServerError, private.HookPostReceiveResult{Err: "Failed to change visibility"}) + } } - - if isTemplate.Has() { + if isTemplate.Has() && repo.IsTemplate != isTemplate.Value() { repo.IsTemplate = isTemplate.Value() - cols = append(cols, "is_template") - } - - if len(cols) > 0 { - if err := repo_model.UpdateRepositoryCols(ctx, repo, cols...); err != nil { - log.Error("Failed to Update: %s/%s Error: %v", ownerName, repoName, err) - ctx.JSON(http.StatusInternalServerError, private.HookPostReceiveResult{ - Err: fmt.Sprintf("Failed to Update: %s/%s Error: %v", ownerName, repoName, err), - }) - return + if err = repo_model.UpdateRepositoryColsNoAutoTime(ctx, repo, "is_template"); err != nil { + ctx.JSON(http.StatusInternalServerError, private.HookPostReceiveResult{Err: "Failed to change template status"}) } } } @@ -303,14 +298,11 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) { } if pr == nil { - if repo.IsFork { - branch = fmt.Sprintf("%s:%s", repo.OwnerName, branch) - } results = append(results, private.HookPostReceiveBranchResult{ Message: setting.Git.PullRequestPushMessage && baseRepo.AllowsPulls(ctx), Create: true, Branch: branch, - URL: fmt.Sprintf("%s/compare/%s...%s", baseRepo.HTMLURL(), util.PathEscapeSegments(baseRepo.DefaultBranch), util.PathEscapeSegments(branch)), + URL: fmt.Sprintf("%s/pulls/new/%s", repo.HTMLURL(), util.PathEscapeSegments(branch)), }) } else { results = append(results, private.HookPostReceiveBranchResult{ @@ -329,9 +321,7 @@ func HookPostReceive(ctx *gitea_context.PrivateContext) { } func loadContextCacheUser(ctx context.Context, id int64) (*user_model.User, error) { - return cache.GetWithContextCache(ctx, "hook_post_receive_user", id, func() (*user_model.User, error) { - return user_model.GetUserByID(ctx, id) - }) + return cache.GetWithContextCache(ctx, cachegroup.User, id, user_model.GetUserByID) } // handlePullRequestMerging handle pull request merging, a pull request action should push at least 1 commit diff --git a/routers/private/hook_post_receive_test.go b/routers/private/hook_post_receive_test.go index 34722f910d..ca721b16d1 100644 --- a/routers/private/hook_post_receive_test.go +++ b/routers/private/hook_post_receive_test.go @@ -43,7 +43,7 @@ func TestHandlePullRequestMerging(t *testing.T) { pr, err = issues_model.GetPullRequestByID(db.DefaultContext, pr.ID) assert.NoError(t, err) assert.True(t, pr.HasMerged) - assert.EqualValues(t, "01234567", pr.MergedCommitID) + assert.Equal(t, "01234567", pr.MergedCommitID) unittest.AssertNotExistsBean(t, &pull_model.AutoMerge{ID: autoMerge.ID}) } diff --git a/routers/private/hook_pre_receive.go b/routers/private/hook_pre_receive.go index ae23abc542..dd9d0bc15e 100644 --- a/routers/private/hook_pre_receive.go +++ b/routers/private/hook_pre_receive.go @@ -4,6 +4,7 @@ package private import ( + "errors" "fmt" "net/http" "os" @@ -311,13 +312,13 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r if isForcePush { log.Warn("Forbidden: User %d is not allowed to force-push to protected branch: %s in %-v", ctx.opts.UserID, branchName, repo) ctx.JSON(http.StatusForbidden, private.Response{ - UserMsg: fmt.Sprintf("Not allowed to force-push to protected branch %s", branchName), + UserMsg: "Not allowed to force-push to protected branch " + branchName, }) return } log.Warn("Forbidden: User %d is not allowed to push to protected branch: %s in %-v", ctx.opts.UserID, branchName, repo) ctx.JSON(http.StatusForbidden, private.Response{ - UserMsg: fmt.Sprintf("Not allowed to push to protected branch %s", branchName), + UserMsg: "Not allowed to push to protected branch " + branchName, }) return } @@ -353,7 +354,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r if !allowedMerge { log.Warn("Forbidden: User %d is not allowed to push to protected branch: %s in %-v and is not allowed to merge pr #%d", ctx.opts.UserID, branchName, repo, pr.Index) ctx.JSON(http.StatusForbidden, private.Response{ - UserMsg: fmt.Sprintf("Not allowed to push to protected branch %s", branchName), + UserMsg: "Not allowed to push to protected branch " + branchName, }) return } @@ -374,7 +375,7 @@ func preReceiveBranch(ctx *preReceiveContext, oldCommitID, newCommitID string, r // Check all status checks and reviews are ok if err := pull_service.CheckPullBranchProtections(ctx, pr, true); err != nil { - if pull_service.IsErrDisallowedToMerge(err) { + if errors.Is(err, pull_service.ErrNotReadyToMerge) { log.Warn("Forbidden: User %d is not allowed push to protected branch %s in %-v and pr #%d is not ready to be merged: %s", ctx.opts.UserID, branchName, repo, pr.Index, err.Error()) ctx.JSON(http.StatusForbidden, private.Response{ UserMsg: fmt.Sprintf("Not allowed to push to protected branch %s and pr #%d is not ready to be merged: %s", branchName, ctx.opts.PullRequestID, err.Error()), @@ -447,10 +448,7 @@ func preReceiveFor(ctx *preReceiveContext, refFullName git.RefName) { baseBranchName := refFullName.ForBranchName() - baseBranchExist := false - if gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, baseBranchName) { - baseBranchExist = true - } + baseBranchExist := gitrepo.IsBranchExist(ctx, ctx.Repo.Repository, baseBranchName) if !baseBranchExist { for p, v := range baseBranchName { diff --git a/routers/private/hook_verification.go b/routers/private/hook_verification.go index 7c06cf8557..57d0964ead 100644 --- a/routers/private/hook_verification.go +++ b/routers/private/hook_verification.go @@ -6,7 +6,6 @@ package private import ( "bufio" "context" - "fmt" "io" "os" @@ -113,7 +112,7 @@ type errUnverifiedCommit struct { } func (e *errUnverifiedCommit) Error() string { - return fmt.Sprintf("Unverified commit: %s", e.sha) + return "Unverified commit: " + e.sha } func isErrUnverifiedCommit(err error) bool { diff --git a/routers/private/hook_verification_test.go b/routers/private/hook_verification_test.go index f6c2e1087f..8653e34daa 100644 --- a/routers/private/hook_verification_test.go +++ b/routers/private/hook_verification_test.go @@ -18,7 +18,9 @@ func TestVerifyCommits(t *testing.T) { unittest.PrepareTestEnv(t) gitRepo, err := git.OpenRepository(t.Context(), testReposDir+"repo1_hook_verification") - defer gitRepo.Close() + if err != nil { + defer gitRepo.Close() + } assert.NoError(t, err) objectFormat, err := gitRepo.GetObjectFormat() diff --git a/routers/private/manager.go b/routers/private/manager.go index c712bbcf21..00e52d6511 100644 --- a/routers/private/manager.go +++ b/routers/private/manager.go @@ -180,7 +180,7 @@ func AddLogger(ctx *context.PrivateContext) { writerOption.Addr, _ = opts.Config["address"].(string) writerMode.WriterOption = writerOption default: - panic(fmt.Sprintf("invalid log writer mode: %s", writerType)) + panic("invalid log writer mode: " + writerType) } writer, err := log.NewEventWriter(opts.Writer, writerType, writerMode) if err != nil { diff --git a/routers/private/serv.go b/routers/private/serv.go index ecff3b7a53..b879be0dc2 100644 --- a/routers/private/serv.go +++ b/routers/private/serv.go @@ -81,6 +81,7 @@ func ServCommand(ctx *context.PrivateContext) { ownerName := ctx.PathParam("owner") repoName := ctx.PathParam("repo") mode := perm.AccessMode(ctx.FormInt("mode")) + verb := ctx.FormString("verb") // Set the basic parts of the results to return results := private.ServCommandResults{ @@ -286,7 +287,7 @@ func ServCommand(ctx *context.PrivateContext) { repo.IsPrivate || owner.Visibility.IsPrivate() || (user != nil && user.IsRestricted) || // user will be nil if the key is a deploykey - setting.Service.RequireSignInView) { + setting.Service.RequireSignInViewStrict) { if key.Type == asymkey_model.KeyTypeDeploy { if deployKey.Mode < mode { ctx.JSON(http.StatusUnauthorized, private.Response{ @@ -295,8 +296,11 @@ func ServCommand(ctx *context.PrivateContext) { return } } else { - // Because of the special ref "refs/for" we will need to delay write permission check - if git.DefaultFeatures().SupportProcReceive && unitType == unit.TypeCode { + // Because of the special ref "refs/for" (AGit) we will need to delay write permission check, + // AGit flow needs to write its own ref when the doer has "reader" permission (allowing to create PR). + // The real permission check is done in HookPreReceive (routers/private/hook_pre_receive.go). + // Here it should relax the permission check for "git push (git-receive-pack)", but not for others like LFS operations. + if git.DefaultFeatures().SupportProcReceive && unitType == unit.TypeCode && verb == git.CmdVerbReceivePack { mode = perm.AccessModeRead } |