From 06aae9ae728c721891e8a5d7bd278546fe8dd6a0 Mon Sep 17 00:00:00 2001 From: Earl Warren Date: Sat, 9 Dec 2023 14:52:30 +0100 Subject: [PATCH] [GITEA] GetScheduledMergeByPullID may involve a system user Refs: https://codeberg.org/forgejo/forgejo/issues/1897 (cherry picked from commit ddc3c2255840d347afd13c272d2695c68196d6ef) (cherry picked from commit a7fe969b93ffe00aa66942d04e66ddb4221cb5ad) (cherry picked from commit 62bda95774ae0840652daf29f557b61650745ac1) (cherry picked from commit 8149a822c797eb6c9ea648757261c0aba8f8d548) (cherry picked from commit 9ed4e685ebd1a0c32d5550fd96f4f912fbd02c18) (cherry picked from commit 4f072b4f80d86e41004a107b56d74476e39d0536) (cherry picked from commit ca5924037b7d161e332709d01534d03e1c2bcf45) (cherry picked from commit 88e2b47e29285a761dc08a31288af3dd146ac63b) (cherry picked from commit 784f860cfa2d763e39551a84067a161db9ca3ddb) --- models/pull/automerge.go | 2 +- tests/integration/forgejo_git_test.go | 139 ++++++++++++++++++++++++++ 2 files changed, 140 insertions(+), 1 deletion(-) create mode 100644 tests/integration/forgejo_git_test.go diff --git a/models/pull/automerge.go b/models/pull/automerge.go index f69fcb60d..f31159a8d 100644 --- a/models/pull/automerge.go +++ b/models/pull/automerge.go @@ -74,7 +74,7 @@ func GetScheduledMergeByPullID(ctx context.Context, pullID int64) (bool, *AutoMe return false, nil, err } - doer, err := user_model.GetUserByID(ctx, scheduledPRM.DoerID) + doer, err := user_model.GetPossibleUserByID(ctx, scheduledPRM.DoerID) if err != nil { return false, nil, err } diff --git a/tests/integration/forgejo_git_test.go b/tests/integration/forgejo_git_test.go new file mode 100644 index 000000000..f81521d73 --- /dev/null +++ b/tests/integration/forgejo_git_test.go @@ -0,0 +1,139 @@ +// Copyright Earl Warren +// SPDX-License-Identifier: MIT + +package integration + +import ( + "net/http" + "net/url" + "os" + "path" + "testing" + "time" + + actions_model "code.gitea.io/gitea/models/actions" + auth_model "code.gitea.io/gitea/models/auth" + "code.gitea.io/gitea/models/db" + repo_model "code.gitea.io/gitea/models/repo" + "code.gitea.io/gitea/models/unittest" + user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/git" + api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/tests" + + "github.com/stretchr/testify/assert" +) + +func TestActionsUserGit(t *testing.T) { + onGiteaRun(t, testActionsUserGit) +} + +func NewActionsUserTestContext(t *testing.T, username, reponame string) APITestContext { + t.Helper() + + repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: reponame}) + repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: username}) + + task := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionTask{ID: 47}) + task.RepoID = repo.ID + task.OwnerID = repoOwner.ID + task.GenerateToken() + + actions_model.UpdateTask(db.DefaultContext, task) + return APITestContext{ + Session: emptyTestSession(t), + Token: task.Token, + Username: username, + Reponame: reponame, + } +} + +func testActionsUserGit(t *testing.T, u *url.URL) { + username := "user2" + reponame := "repo1" + httpContext := NewAPITestContext(t, username, reponame, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser) + + for _, testCase := range []struct { + name string + head string + ctx APITestContext + }{ + { + name: "UserTypeIndividual", + head: "individualhead", + ctx: httpContext, + }, + { + name: "ActionsUser", + head: "actionsuserhead", + ctx: NewActionsUserTestContext(t, username, reponame), + }, + } { + t.Run("CreatePR "+testCase.name, func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + + dstPath := t.TempDir() + u.Path = httpContext.GitPath() + u.User = url.UserPassword(httpContext.Username, userPassword) + t.Run("Clone", doGitClone(dstPath, u)) + t.Run("PopulateBranch", doActionsUserPopulateBranch(dstPath, &httpContext, "master", testCase.head)) + t.Run("CreatePR", doActionsUserPR(httpContext, testCase.ctx, "master", testCase.head)) + }) + } +} + +func doActionsUserPopulateBranch(dstPath string, ctx *APITestContext, baseBranch, headBranch string) func(t *testing.T) { + return func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + + t.Run("CreateHeadBranch", doGitCreateBranch(dstPath, headBranch)) + + t.Run("AddCommit", func(t *testing.T) { + err := os.WriteFile(path.Join(dstPath, "test_file"), []byte("## test content"), 0o666) + if !assert.NoError(t, err) { + return + } + + err = git.AddChanges(dstPath, true) + assert.NoError(t, err) + + err = git.CommitChanges(dstPath, git.CommitChangesOptions{ + Committer: &git.Signature{ + Email: "user2@example.com", + Name: "user2", + When: time.Now(), + }, + Author: &git.Signature{ + Email: "user2@example.com", + Name: "user2", + When: time.Now(), + }, + Message: "Testing commit 1", + }) + assert.NoError(t, err) + }) + + t.Run("Push", func(t *testing.T) { + err := git.NewCommand(git.DefaultContext, "push", "origin").AddDynamicArguments("HEAD:refs/heads/" + headBranch).Run(&git.RunOpts{Dir: dstPath}) + assert.NoError(t, err) + }) + } +} + +func doActionsUserPR(ctx, doerCtx APITestContext, baseBranch, headBranch string) func(t *testing.T) { + return func(t *testing.T) { + defer tests.PrintCurrentTest(t)() + var pr api.PullRequest + var err error + + // Create a test pullrequest + t.Run("CreatePullRequest", func(t *testing.T) { + pr, err = doAPICreatePullRequest(doerCtx, ctx.Username, ctx.Reponame, baseBranch, headBranch)(t) + assert.NoError(t, err) + }) + doerCtx.ExpectedCode = http.StatusCreated + t.Run("AutoMergePR", doAPIAutoMergePullRequest(doerCtx, ctx.Username, ctx.Reponame, pr.Index)) + // Ensure the PR page works + t.Run("EnsureCanSeePull", doEnsureCanSeePull(ctx, pr)) + } +}