Skip to content

Commit 06aae9a

Browse files
committed
[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 8149a82) (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)
1 parent fa07599 commit 06aae9a

File tree

2 files changed

+140
-1
lines changed

2 files changed

+140
-1
lines changed

models/pull/automerge.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ func GetScheduledMergeByPullID(ctx context.Context, pullID int64) (bool, *AutoMe
7474
return false, nil, err
7575
}
7676

77-
doer, err := user_model.GetUserByID(ctx, scheduledPRM.DoerID)
77+
doer, err := user_model.GetPossibleUserByID(ctx, scheduledPRM.DoerID)
7878
if err != nil {
7979
return false, nil, err
8080
}

tests/integration/forgejo_git_test.go

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
// Copyright Earl Warren <[email protected]>
2+
// SPDX-License-Identifier: MIT
3+
4+
package integration
5+
6+
import (
7+
"net/http"
8+
"net/url"
9+
"os"
10+
"path"
11+
"testing"
12+
"time"
13+
14+
actions_model "code.gitea.io/gitea/models/actions"
15+
auth_model "code.gitea.io/gitea/models/auth"
16+
"code.gitea.io/gitea/models/db"
17+
repo_model "code.gitea.io/gitea/models/repo"
18+
"code.gitea.io/gitea/models/unittest"
19+
user_model "code.gitea.io/gitea/models/user"
20+
"code.gitea.io/gitea/modules/git"
21+
api "code.gitea.io/gitea/modules/structs"
22+
"code.gitea.io/gitea/tests"
23+
24+
"github.com/stretchr/testify/assert"
25+
)
26+
27+
func TestActionsUserGit(t *testing.T) {
28+
onGiteaRun(t, testActionsUserGit)
29+
}
30+
31+
func NewActionsUserTestContext(t *testing.T, username, reponame string) APITestContext {
32+
t.Helper()
33+
34+
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: reponame})
35+
repoOwner := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: username})
36+
37+
task := unittest.AssertExistsAndLoadBean(t, &actions_model.ActionTask{ID: 47})
38+
task.RepoID = repo.ID
39+
task.OwnerID = repoOwner.ID
40+
task.GenerateToken()
41+
42+
actions_model.UpdateTask(db.DefaultContext, task)
43+
return APITestContext{
44+
Session: emptyTestSession(t),
45+
Token: task.Token,
46+
Username: username,
47+
Reponame: reponame,
48+
}
49+
}
50+
51+
func testActionsUserGit(t *testing.T, u *url.URL) {
52+
username := "user2"
53+
reponame := "repo1"
54+
httpContext := NewAPITestContext(t, username, reponame, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
55+
56+
for _, testCase := range []struct {
57+
name string
58+
head string
59+
ctx APITestContext
60+
}{
61+
{
62+
name: "UserTypeIndividual",
63+
head: "individualhead",
64+
ctx: httpContext,
65+
},
66+
{
67+
name: "ActionsUser",
68+
head: "actionsuserhead",
69+
ctx: NewActionsUserTestContext(t, username, reponame),
70+
},
71+
} {
72+
t.Run("CreatePR "+testCase.name, func(t *testing.T) {
73+
defer tests.PrintCurrentTest(t)()
74+
75+
dstPath := t.TempDir()
76+
u.Path = httpContext.GitPath()
77+
u.User = url.UserPassword(httpContext.Username, userPassword)
78+
t.Run("Clone", doGitClone(dstPath, u))
79+
t.Run("PopulateBranch", doActionsUserPopulateBranch(dstPath, &httpContext, "master", testCase.head))
80+
t.Run("CreatePR", doActionsUserPR(httpContext, testCase.ctx, "master", testCase.head))
81+
})
82+
}
83+
}
84+
85+
func doActionsUserPopulateBranch(dstPath string, ctx *APITestContext, baseBranch, headBranch string) func(t *testing.T) {
86+
return func(t *testing.T) {
87+
defer tests.PrintCurrentTest(t)()
88+
89+
t.Run("CreateHeadBranch", doGitCreateBranch(dstPath, headBranch))
90+
91+
t.Run("AddCommit", func(t *testing.T) {
92+
err := os.WriteFile(path.Join(dstPath, "test_file"), []byte("## test content"), 0o666)
93+
if !assert.NoError(t, err) {
94+
return
95+
}
96+
97+
err = git.AddChanges(dstPath, true)
98+
assert.NoError(t, err)
99+
100+
err = git.CommitChanges(dstPath, git.CommitChangesOptions{
101+
Committer: &git.Signature{
102+
103+
Name: "user2",
104+
When: time.Now(),
105+
},
106+
Author: &git.Signature{
107+
108+
Name: "user2",
109+
When: time.Now(),
110+
},
111+
Message: "Testing commit 1",
112+
})
113+
assert.NoError(t, err)
114+
})
115+
116+
t.Run("Push", func(t *testing.T) {
117+
err := git.NewCommand(git.DefaultContext, "push", "origin").AddDynamicArguments("HEAD:refs/heads/" + headBranch).Run(&git.RunOpts{Dir: dstPath})
118+
assert.NoError(t, err)
119+
})
120+
}
121+
}
122+
123+
func doActionsUserPR(ctx, doerCtx APITestContext, baseBranch, headBranch string) func(t *testing.T) {
124+
return func(t *testing.T) {
125+
defer tests.PrintCurrentTest(t)()
126+
var pr api.PullRequest
127+
var err error
128+
129+
// Create a test pullrequest
130+
t.Run("CreatePullRequest", func(t *testing.T) {
131+
pr, err = doAPICreatePullRequest(doerCtx, ctx.Username, ctx.Reponame, baseBranch, headBranch)(t)
132+
assert.NoError(t, err)
133+
})
134+
doerCtx.ExpectedCode = http.StatusCreated
135+
t.Run("AutoMergePR", doAPIAutoMergePullRequest(doerCtx, ctx.Username, ctx.Reponame, pr.Index))
136+
// Ensure the PR page works
137+
t.Run("EnsureCanSeePull", doEnsureCanSeePull(ctx, pr))
138+
}
139+
}

0 commit comments

Comments
 (0)