1
0
Fork 0
mirror of https://codeberg.org/forgejo/forgejo.git synced 2024-11-21 08:31:27 -05:00

[GITEA] GetScheduledMergeByPullID may involve a system user

Refs: https://codeberg.org/forgejo/forgejo/issues/1897
(cherry picked from commit ddc3c22558)
(cherry picked from commit a7fe969b93)
(cherry picked from commit 62bda95774)
(cherry picked from commit 8149a822c7)
(cherry picked from commit 9ed4e685eb)
(cherry picked from commit 4f072b4f80)
(cherry picked from commit ca5924037b)
(cherry picked from commit 88e2b47e29)
(cherry picked from commit 784f860cfa)
This commit is contained in:
Earl Warren 2023-12-09 14:52:30 +01:00
parent fa0759962b
commit 06aae9ae72
No known key found for this signature in database
GPG key ID: 0579CB2928A78A00
2 changed files with 140 additions and 1 deletions

View file

@ -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
}

View file

@ -0,0 +1,139 @@
// Copyright Earl Warren <contact@earl-warren.org>
// 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))
}
}