test normal integration test
Some checks failed
ci/woodpecker/pr/build Pipeline was successful
testing / backend-checks (pull_request) Successful in 6m38s
ci/woodpecker/push/build Pipeline was successful
testing / frontend-checks (pull_request) Successful in 1m41s
/ release-notes (pull_request_target) Has been skipped
testing / test-unit (pull_request) Successful in 7m40s
testing / test-e2e (pull_request) Successful in 2m55s
testing / test-mysql (pull_request) Successful in 31m40s
testing / test-remote-cacher (map[image:docker.io/bitnami/redis:7.2 port:6379]) (pull_request) Has been cancelled
testing / test-remote-cacher (map[image:docker.io/bitnami/valkey:7.2 port:6379]) (pull_request) Has been cancelled
testing / test-remote-cacher (map[image:ghcr.io/microsoft/garnet-alpine:1.0.14 port:6379]) (pull_request) Has been cancelled
testing / test-remote-cacher (map[image:registry.redict.io/redict:7.3.0-scratch port:6379]) (pull_request) Has been cancelled
testing / test-sqlite (pull_request) Has been cancelled
testing / security-check (pull_request) Has been cancelled
testing / test-pgsql (pull_request) Has been cancelled

This commit is contained in:
Tom Neuber 2024-10-27 23:51:19 +01:00
parent f62aae159f
commit 7b2c5c19cd
Signed by: tom
GPG key ID: F17EFE4272D89FF6

View file

@ -845,110 +845,160 @@ func TestPullMergeBranchProtect(t *testing.T) {
}) })
} }
func testPullAutoMergeAfterCommitStatusSucceed(t *testing.T, forkName string, approval, deleteBranch bool) { func TestPullAutoMergeAfterCommitStatusSucceed(t *testing.T) {
// create a pull request onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
session := loginUser(t, "user1") // create a pull request
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) session := loginUser(t, "user1")
testRepoFork(t, session, "user2", "repo1", "user1", forkName) user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
defer func() { forkedName := "repo1-1"
testDeleteRepository(t, session, "user1", forkName) testRepoFork(t, session, "user2", "repo1", "user1", forkedName)
}() defer func() {
branchName := "master" testDeleteRepository(t, session, "user1", forkedName)
if deleteBranch { }()
branchName = "new_branch_1" testEditFile(t, session, "user1", forkedName, "master", "README.md", "Hello, World (Edited)\n")
testEditFileToNewBranch(t, session, "user1", forkName, "master", branchName, "README.md", "Hello, World (Edited)\n") testPullCreate(t, session, "user1", forkedName, false, "master", "master", "Indexer notifier test pull")
testPullCreate(t, session, "user1", forkName, false, "master", branchName, "Indexer notifier test pull")
} else {
testEditFile(t, session, "user1", forkName, "master", "README.md", "Hello, World (Edited)\n")
testPullCreate(t, session, "user1", forkName, false, "master", branchName, "Indexer notifier test pull")
}
baseRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "repo1"}) baseRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "repo1"})
forkedRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user1", Name: forkName}) forkedRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user1", Name: forkedName})
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{
BaseRepoID: baseRepo.ID, BaseRepoID: baseRepo.ID,
BaseBranch: "master", BaseBranch: "master",
HeadRepoID: forkedRepo.ID, HeadRepoID: forkedRepo.ID,
HeadBranch: branchName, HeadBranch: "master",
})
// add protected branch for commit status
csrf := GetCSRF(t, session, "/user2/repo1/settings/branches")
// Change master branch to protected
req := NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/edit", map[string]string{
"_csrf": csrf,
"rule_name": "master",
"enable_push": "true",
"enable_status_check": "true",
"status_check_contexts": "gitea/actions",
})
session.MakeRequest(t, req, http.StatusSeeOther)
// first time insert automerge record, return true
scheduled, err := automerge.ScheduleAutoMerge(db.DefaultContext, user1, pr, repo_model.MergeStyleMerge, "auto merge test", true)
require.NoError(t, err)
assert.True(t, scheduled)
// second time insert automerge record, return false because it does exist
scheduled, err = automerge.ScheduleAutoMerge(db.DefaultContext, user1, pr, repo_model.MergeStyleMerge, "auto merge test", true)
require.Error(t, err)
assert.False(t, scheduled)
// reload pr again
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pr.ID})
assert.False(t, pr.HasMerged)
assert.Empty(t, pr.MergedCommitID)
// update commit status to success, then it should be merged automatically
baseGitRepo, err := gitrepo.OpenRepository(db.DefaultContext, baseRepo)
require.NoError(t, err)
sha, err := baseGitRepo.GetRefCommitID(pr.GetGitRefName())
require.NoError(t, err)
masterCommitID, err := baseGitRepo.GetBranchCommitID("master")
require.NoError(t, err)
branches, _, err := baseGitRepo.GetBranchNames(0, 100)
require.NoError(t, err)
assert.ElementsMatch(t, []string{"sub-home-md-img-check", "home-md-img-check", "pr-to-update", "branch2", "DefaultBranch", "develop", "feature/1", "master"}, branches)
baseGitRepo.Close()
defer func() {
testResetRepo(t, baseRepo.RepoPath(), "master", masterCommitID)
}()
err = commitstatus_service.CreateCommitStatus(db.DefaultContext, baseRepo, user1, sha, &git_model.CommitStatus{
State: api.CommitStatusSuccess,
TargetURL: "https://gitea.com",
Context: "gitea/actions",
})
require.NoError(t, err)
time.Sleep(2 * time.Second)
// realod pr again
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pr.ID})
assert.True(t, pr.HasMerged)
assert.NotEmpty(t, pr.MergedCommitID)
unittest.AssertNotExistsBean(t, &pull_model.AutoMerge{PullID: pr.ID})
}) })
}
// add protected branch for commit status func TestPullAutoMergeAfterCommitStatusSucceedAndApproval(t *testing.T) {
csrf := GetCSRF(t, session, "/user2/repo1/settings/branches") onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
// create a pull request
session := loginUser(t, "user1")
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
forkedName := "repo1-2"
testRepoFork(t, session, "user2", "repo1", "user1", forkedName)
defer func() {
testDeleteRepository(t, session, "user1", forkedName)
}()
testEditFile(t, session, "user1", forkedName, "master", "README.md", "Hello, World (Edited)\n")
testPullCreate(t, session, "user1", forkedName, false, "master", "master", "Indexer notifier test pull")
// Change master branch to protected baseRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "repo1"})
var reqParams map[string]string forkedRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user1", Name: forkedName})
if approval { pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{
reqParams = map[string]string{ BaseRepoID: baseRepo.ID,
BaseBranch: "master",
HeadRepoID: forkedRepo.ID,
HeadBranch: "master",
})
// add protected branch for commit status
csrf := GetCSRF(t, session, "/user2/repo1/settings/branches")
// Change master branch to protected
req := NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/edit", map[string]string{
"_csrf": csrf, "_csrf": csrf,
"rule_name": "master", "rule_name": "master",
"enable_push": "true", "enable_push": "true",
"enable_status_check": "true", "enable_status_check": "true",
"status_check_contexts": "gitea/actions", "status_check_contexts": "gitea/actions",
"required_approvals": "1", "required_approvals": "1",
} })
} else { session.MakeRequest(t, req, http.StatusSeeOther)
reqParams = map[string]string{
"_csrf": csrf,
"rule_name": "master",
"enable_push": "true",
"enable_status_check": "true",
"status_check_contexts": "gitea/actions",
}
}
req := NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/edit", reqParams) // first time insert automerge record, return true
session.MakeRequest(t, req, http.StatusSeeOther) scheduled, err := automerge.ScheduleAutoMerge(db.DefaultContext, user1, pr, repo_model.MergeStyleMerge, "auto merge test", true)
if deleteBranch {
// check if new branch exists
forkedGitRepo, err := gitrepo.OpenRepository(db.DefaultContext, forkedRepo)
require.NoError(t, err) require.NoError(t, err)
newBranch, err := forkedGitRepo.GetBranch(branchName) assert.True(t, scheduled)
// second time insert automerge record, return false because it does exist
scheduled, err = automerge.ScheduleAutoMerge(db.DefaultContext, user1, pr, repo_model.MergeStyleMerge, "auto merge test", true)
require.Error(t, err)
assert.False(t, scheduled)
// reload pr again
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pr.ID})
assert.False(t, pr.HasMerged)
assert.Empty(t, pr.MergedCommitID)
// update commit status to success, then it should be merged automatically
baseGitRepo, err := gitrepo.OpenRepository(db.DefaultContext, baseRepo)
require.NoError(t, err) require.NoError(t, err)
assert.NotNil(t, newBranch) sha, err := baseGitRepo.GetRefCommitID(pr.GetGitRefName())
} require.NoError(t, err)
masterCommitID, err := baseGitRepo.GetBranchCommitID("master")
require.NoError(t, err)
baseGitRepo.Close()
defer func() {
testResetRepo(t, baseRepo.RepoPath(), "master", masterCommitID)
}()
// first time insert automerge record, return true err = commitstatus_service.CreateCommitStatus(db.DefaultContext, baseRepo, user1, sha, &git_model.CommitStatus{
scheduled, err := automerge.ScheduleAutoMerge(db.DefaultContext, user1, pr, repo_model.MergeStyleMerge, "auto merge test", deleteBranch) State: api.CommitStatusSuccess,
require.NoError(t, err) TargetURL: "https://gitea.com",
assert.True(t, scheduled) Context: "gitea/actions",
})
require.NoError(t, err)
// second time insert automerge record, return false because it does exist time.Sleep(2 * time.Second)
scheduled, err = automerge.ScheduleAutoMerge(db.DefaultContext, user1, pr, repo_model.MergeStyleMerge, "auto merge test", deleteBranch)
require.Error(t, err)
assert.False(t, scheduled)
// reload pr again
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pr.ID})
assert.False(t, pr.HasMerged)
assert.Empty(t, pr.MergedCommitID)
// update commit status to success, then it should be merged automatically
baseGitRepo, err := gitrepo.OpenRepository(db.DefaultContext, baseRepo)
require.NoError(t, err)
sha, err := baseGitRepo.GetRefCommitID(pr.GetGitRefName())
require.NoError(t, err)
masterCommitID, err := baseGitRepo.GetBranchCommitID("master")
require.NoError(t, err)
branches, _, err := baseGitRepo.GetBranchNames(0, 100)
require.NoError(t, err)
assert.ElementsMatch(t, []string{"sub-home-md-img-check", "home-md-img-check", "pr-to-update", "branch2", "DefaultBranch", "develop", "feature/1", "master"}, branches)
baseGitRepo.Close()
defer func() {
testResetRepo(t, baseRepo.RepoPath(), "master", masterCommitID)
}()
err = commitstatus_service.CreateCommitStatus(db.DefaultContext, baseRepo, user1, sha, &git_model.CommitStatus{
State: api.CommitStatusSuccess,
TargetURL: "https://gitea.com",
Context: "gitea/actions",
})
require.NoError(t, err)
time.Sleep(2 * time.Second)
if approval {
// reload pr again // reload pr again
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pr.ID}) pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pr.ID})
assert.False(t, pr.HasMerged) assert.False(t, pr.HasMerged)
@ -962,62 +1012,13 @@ func testPullAutoMergeAfterCommitStatusSucceed(t *testing.T, forkName string, ap
testSubmitReview(t, approveSession, htmlDoc.GetCSRF(), "user2", "repo1", strconv.Itoa(int(pr.Index)), sha, "approve", http.StatusOK) testSubmitReview(t, approveSession, htmlDoc.GetCSRF(), "user2", "repo1", strconv.Itoa(int(pr.Index)), sha, "approve", http.StatusOK)
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
}
// reload pr again // realod pr again
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pr.ID}) pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pr.ID})
assert.True(t, pr.HasMerged) assert.True(t, pr.HasMerged)
assert.NotEmpty(t, pr.MergedCommitID) assert.NotEmpty(t, pr.MergedCommitID)
unittest.AssertNotExistsBean(t, &pull_model.AutoMerge{PullID: pr.ID}) unittest.AssertNotExistsBean(t, &pull_model.AutoMerge{PullID: pr.ID})
if deleteBranch {
// check if new branch got removed
forkedGitRepo, err := gitrepo.OpenRepository(db.DefaultContext, forkedRepo)
require.NoError(t, err)
_, err = forkedGitRepo.GetBranch(branchName)
require.Error(t, err)
assert.True(t, git.IsErrBranchNotExist(err))
}
}
func TestPullAutoMergeAfterCommitStatusSucceed(t *testing.T) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
for _, testCase := range []struct {
name string
forkName string
approval bool
deleteBranch bool
}{
{
name: "TestPullAutoMergeAfterCommitStatusSucceed",
forkName: "repo1-1",
approval: false,
deleteBranch: false,
},
{
name: "TestPullAutoMergeAfterCommitStatusSucceedWithBranchDeletion",
forkName: "repo1-2",
approval: false,
deleteBranch: true,
},
{
name: "TestPullAutoMergeAfterCommitStatusSucceedAndApproval",
forkName: "repo2-1",
approval: true,
deleteBranch: false,
},
{
name: "TestPullAutoMergeAfterCommitStatusSucceedAndApprovalWithBranchDeletion",
forkName: "repo2-2",
approval: true,
deleteBranch: true,
},
} {
t.Run(testCase.name, func(t *testing.T) {
testPullAutoMergeAfterCommitStatusSucceed(t, testCase.forkName, testCase.approval, testCase.deleteBranch)
})
}
}) })
} }
@ -1090,142 +1091,6 @@ func TestPullAutoMergeAfterCommitStatusSucceedAndApprovalForAgitFlow(t *testing.
}) })
session.MakeRequest(t, req, http.StatusSeeOther) session.MakeRequest(t, req, http.StatusSeeOther)
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
// first time insert automerge record, return true
scheduled, err := automerge.ScheduleAutoMerge(db.DefaultContext, user1, pr, repo_model.MergeStyleMerge, "auto merge test", false)
require.NoError(t, err)
assert.True(t, scheduled)
// second time insert automerge record, return false because it does exist
scheduled, err = automerge.ScheduleAutoMerge(db.DefaultContext, user1, pr, repo_model.MergeStyleMerge, "auto merge test", false)
require.Error(t, err)
assert.False(t, scheduled)
// reload pr again
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pr.ID})
assert.False(t, pr.HasMerged)
assert.Empty(t, pr.MergedCommitID)
// update commit status to success, then it should be merged automatically
baseGitRepo, err := gitrepo.OpenRepository(db.DefaultContext, baseRepo)
require.NoError(t, err)
sha, err := baseGitRepo.GetRefCommitID(pr.GetGitRefName())
require.NoError(t, err)
masterCommitID, err := baseGitRepo.GetBranchCommitID("master")
require.NoError(t, err)
baseGitRepo.Close()
defer func() {
testResetRepo(t, baseRepo.RepoPath(), "master", masterCommitID)
}()
err = commitstatus_service.CreateCommitStatus(db.DefaultContext, baseRepo, user1, sha, &git_model.CommitStatus{
State: api.CommitStatusSuccess,
TargetURL: "https://gitea.com",
Context: "gitea/actions",
})
require.NoError(t, err)
time.Sleep(2 * time.Second)
// reload pr again
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pr.ID})
assert.False(t, pr.HasMerged)
assert.Empty(t, pr.MergedCommitID)
// approve the PR from non-author
approveSession := loginUser(t, "user1")
req = NewRequest(t, "GET", fmt.Sprintf("/user2/repo1/pulls/%d", pr.Index))
resp := approveSession.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body)
testSubmitReview(t, approveSession, htmlDoc.GetCSRF(), "user2", "repo1", strconv.Itoa(int(pr.Index)), sha, "approve", http.StatusOK)
time.Sleep(2 * time.Second)
// realod pr again
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pr.ID})
assert.True(t, pr.HasMerged)
assert.NotEmpty(t, pr.MergedCommitID)
unittest.AssertNotExistsBean(t, &pull_model.AutoMerge{PullID: pr.ID})
})
}
func TestPullAutoMergeWithBranchDeletionAfterCommitStatusSucceedAndApprovalForAgitFlow(t *testing.T) {
onGiteaRun(t, func(t *testing.T, u *url.URL) {
// create a pull request
baseAPITestContext := NewAPITestContext(t, "user2", "repo1", auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
dstPath := t.TempDir()
u.Path = baseAPITestContext.GitPath()
u.User = url.UserPassword("user2", userPassword)
t.Run("Clone", doGitClone(dstPath, u))
err := os.WriteFile(path.Join(dstPath, "test_file"), []byte("## test content"), 0o666)
require.NoError(t, err)
err = git.AddChanges(dstPath, true)
require.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",
})
require.NoError(t, err)
stderrBuf := &bytes.Buffer{}
err = git.NewCommand(git.DefaultContext, "push", "origin", "HEAD:refs/for/master", "-o").
AddDynamicArguments(`topic=test/head2`).
AddArguments("-o").
AddDynamicArguments(`title="create a test pull request with agit"`).
AddArguments("-o").
AddDynamicArguments(`description="This PR is a test pull request which created with agit"`).
Run(&git.RunOpts{Dir: dstPath, Stderr: stderrBuf})
require.NoError(t, err)
assert.Contains(t, stderrBuf.String(), setting.AppURL+"user2/repo1/pulls/6")
baseRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "repo1"})
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{
Flow: issues_model.PullRequestFlowAGit,
BaseRepoID: baseRepo.ID,
BaseBranch: "master",
HeadRepoID: baseRepo.ID,
HeadBranch: "user2/test/head2",
})
session := loginUser(t, "user1")
// add protected branch for commit status
csrf := GetCSRF(t, session, "/user2/repo1/settings/branches")
// Change master branch to protected
req := NewRequestWithValues(t, "POST", "/user2/repo1/settings/branches/edit", map[string]string{
"_csrf": csrf,
"rule_name": "master",
"enable_push": "true",
"enable_status_check": "true",
"status_check_contexts": "gitea/actions",
"required_approvals": "1",
})
session.MakeRequest(t, req, http.StatusSeeOther)
// check if new branch exists
baseGitRepo, err := gitrepo.OpenRepository(db.DefaultContext, baseRepo)
require.NoError(t, err)
newBranch, err := baseGitRepo.GetBranch("user2/test/head2")
require.NoError(t, err)
assert.NotNil(t, newBranch)
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
// first time insert automerge record, return true // first time insert automerge record, return true
scheduled, err := automerge.ScheduleAutoMerge(db.DefaultContext, user1, pr, repo_model.MergeStyleMerge, "auto merge test", true) scheduled, err := automerge.ScheduleAutoMerge(db.DefaultContext, user1, pr, repo_model.MergeStyleMerge, "auto merge test", true)
@ -1243,7 +1108,7 @@ func TestPullAutoMergeWithBranchDeletionAfterCommitStatusSucceedAndApprovalForAg
assert.Empty(t, pr.MergedCommitID) assert.Empty(t, pr.MergedCommitID)
// update commit status to success, then it should be merged automatically // update commit status to success, then it should be merged automatically
baseGitRepo, err = gitrepo.OpenRepository(db.DefaultContext, baseRepo) baseGitRepo, err := gitrepo.OpenRepository(db.DefaultContext, baseRepo)
require.NoError(t, err) require.NoError(t, err)
sha, err := baseGitRepo.GetRefCommitID(pr.GetGitRefName()) sha, err := baseGitRepo.GetRefCommitID(pr.GetGitRefName())
require.NoError(t, err) require.NoError(t, err)
@ -1283,12 +1148,5 @@ func TestPullAutoMergeWithBranchDeletionAfterCommitStatusSucceedAndApprovalForAg
assert.NotEmpty(t, pr.MergedCommitID) assert.NotEmpty(t, pr.MergedCommitID)
unittest.AssertNotExistsBean(t, &pull_model.AutoMerge{PullID: pr.ID}) unittest.AssertNotExistsBean(t, &pull_model.AutoMerge{PullID: pr.ID})
// check if new branch got removed
baseGitRepo, err = gitrepo.OpenRepository(db.DefaultContext, baseRepo)
require.NoError(t, err)
_, err = baseGitRepo.GetBranch("user2/test/head2")
require.Error(t, err)
assert.True(t, git.IsErrBranchNotExist(err))
}) })
} }