add first integration function test
Some checks failed
ci/woodpecker/pr/build Pipeline was successful
testing / backend-checks (pull_request) Successful in 6m45s
ci/woodpecker/push/build Pipeline was successful
testing / frontend-checks (pull_request) Successful in 1m39s
/ release-notes (pull_request_target) Has been skipped
testing / test-unit (pull_request) Successful in 7m19s
testing / test-e2e (pull_request) Successful in 2m39s
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-pgsql (pull_request) Has been cancelled
testing / test-sqlite (pull_request) Has been cancelled
testing / security-check (pull_request) Has been cancelled
testing / test-mysql (pull_request) Has been cancelled

This commit is contained in:
Tom Neuber 2024-10-25 21:14:14 +02:00
parent 376b37a1aa
commit c943dab099
Signed by: tom
GPG key ID: F17EFE4272D89FF6
2 changed files with 208 additions and 152 deletions

View file

@ -8,13 +8,17 @@ import (
"io" "io"
"net/http" "net/http"
"testing" "testing"
"time"
auth_model "code.gitea.io/gitea/models/auth" auth_model "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/db" "code.gitea.io/gitea/models/db"
issues_model "code.gitea.io/gitea/models/issues" issues_model "code.gitea.io/gitea/models/issues"
pull_model "code.gitea.io/gitea/models/pull"
repo_model "code.gitea.io/gitea/models/repo" repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unittest" "code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user" user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/gitrepo"
"code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/setting"
api "code.gitea.io/gitea/modules/structs" api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/services/forms" "code.gitea.io/gitea/services/forms"
@ -107,6 +111,74 @@ func TestAPIMergePullWIP(t *testing.T) {
MakeRequest(t, req, http.StatusMethodNotAllowed) MakeRequest(t, req, http.StatusMethodNotAllowed)
} }
// TestAPIAutoMergePull ensures that we can schedule an automerge
func TestAPIAutoMergePull(t *testing.T) {
defer tests.PrepareTestEnv(t)()
session := loginUser(t, "user1")
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{Name: "user1"})
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{Name: "repo1", Owner: user, DefaultBranch: "master"})
ctx := NewAPITestContext(t, user.Name, repo.Name, auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
doProtectBranch(ctx, "master", parameterProtectBranch{
"enable_push": "true",
"enable_status_check": "true",
"status_check_contexts": "gitea/actions",
})(t)
testEditFileToNewBranch(t, session, user.Name, repo.Name, "master", "test", "README.md", "Hello, World (Edited)\n")
testPullCreate(t, session, user.Name, repo.Name, true, "master", "test", "Indexer notifier test pull")
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{
BaseRepoID: repo.ID,
BaseBranch: "master",
HeadRepoID: repo.ID,
HeadBranch: "test",
})
token := getTokenForLoggedInUser(t, session, auth_model.AccessTokenScopeWriteRepository)
req := NewRequestWithJSON(t, http.MethodPost, fmt.Sprintf("/api/v1/repos/%s/%s/pulls/%d/merge", user.Name, repo.Name, pr.Index), &forms.MergePullRequestForm{
MergeMessageField: pr.Issue.Title,
Do: string(repo_model.MergeStyleMerge),
MergeWhenChecksSucceed: true,
DeleteBranchAfterMerge: true,
}).AddTokenAuth(token)
MakeRequest(t, req, http.StatusCreated)
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: pr.ID})
assert.False(t, pr.HasMerged)
assert.Empty(t, pr.MergedCommitID)
gitRepo, err := gitrepo.OpenRepository(db.DefaultContext, repo)
require.NoError(t, err)
sha, err := gitRepo.GetRefCommitID(pr.GetGitRefName())
require.NoError(t, err)
newBranch, err := gitRepo.GetBranch("test")
require.NoError(t, err)
assert.NotNil(t, newBranch)
gitRepo.Close()
doAPICreateCommitStatus(ctx, sha, api.CreateStatusOption{
State: api.CommitStatusSuccess,
TargetURL: "https://gitea.com",
Context: "gitea/actions",
})
time.Sleep(2 * time.Second)
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})
gitRepo, err = gitrepo.OpenRepository(db.DefaultContext, repo)
require.NoError(t, err)
_, err = gitRepo.GetBranch("test")
require.Error(t, err)
assert.True(t, git.IsErrBranchNotExist(err))
gitRepo.Close()
}
func TestAPICreatePullSuccess(t *testing.T) { func TestAPICreatePullSuccess(t *testing.T) {
defer tests.PrepareTestEnv(t)() defer tests.PrepareTestEnv(t)()
repo10 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10}) repo10 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 10})

View file

@ -845,160 +845,83 @@ func TestPullMergeBranchProtect(t *testing.T) {
}) })
} }
func TestPullAutoMergeAfterCommitStatusSucceed(t *testing.T) { func testPullAutoMergeAfterCommitStatusSucceed(t *testing.T, forkName string, approval, deleteBranch bool) {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { // create a pull request
// create a pull request session := loginUser(t, "user1")
session := loginUser(t, "user1") user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1}) testRepoFork(t, session, "user2", "repo1", "user1", forkName)
forkedName := "repo1-1" defer func() {
testRepoFork(t, session, "user2", "repo1", "user1", forkedName) testDeleteRepository(t, session, "user1", forkName)
defer func() { }()
testDeleteRepository(t, session, "user1", forkedName) branchName := "master"
}() if deleteBranch {
testEditFile(t, session, "user1", forkedName, "master", "README.md", "Hello, World (Edited)\n") branchName = "new_branch_1"
testPullCreate(t, session, "user1", forkedName, false, "master", "master", "Indexer notifier test pull") testEditFileToNewBranch(t, session, "user1", forkName, "master", branchName, "README.md", "Hello, World (Edited)\n")
} 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: forkedName}) forkedRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user1", Name: forkName})
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: "master", HeadBranch: branchName,
})
// 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", 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)
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})
}) })
}
func TestPullAutoMergeAfterCommitStatusSucceedAndApproval(t *testing.T) { if deleteBranch {
onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) { // check if new branch exists
// create a pull request forkedGitRepo, err := gitrepo.OpenRepository(db.DefaultContext, forkedRepo)
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")
baseRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user2", Name: "repo1"})
forkedRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{OwnerName: "user1", Name: forkedName})
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{
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,
"rule_name": "master",
"enable_push": "true",
"enable_status_check": "true",
"status_check_contexts": "gitea/actions",
"required_approvals": "1",
})
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", false)
require.NoError(t, err) require.NoError(t, err)
assert.True(t, scheduled) newBranch, err := forkedGitRepo.GetBranch(branchName)
// 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) require.NoError(t, err)
sha, err := baseGitRepo.GetRefCommitID(pr.GetGitRefName()) assert.NotNil(t, newBranch)
require.NoError(t, err) forkedGitRepo.Close()
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{ // first time insert automerge record, return true
State: api.CommitStatusSuccess, scheduled, err := automerge.ScheduleAutoMerge(db.DefaultContext, user1, pr, repo_model.MergeStyleMerge, "auto merge test", deleteBranch)
TargetURL: "https://gitea.com", require.NoError(t, err)
Context: "gitea/actions", assert.True(t, scheduled)
})
require.NoError(t, err)
time.Sleep(2 * time.Second) // 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", 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)
@ -1006,19 +929,80 @@ func TestPullAutoMergeAfterCommitStatusSucceedAndApproval(t *testing.T) {
// approve the PR from non-author // approve the PR from non-author
approveSession := loginUser(t, "user2") approveSession := loginUser(t, "user2")
req = NewRequest(t, "GET", fmt.Sprintf("/user2/repo1/pulls/%d", pr.Index)) req := NewRequest(t, "GET", fmt.Sprintf("/user2/repo1/pulls/%d", pr.Index))
resp := approveSession.MakeRequest(t, req, http.StatusOK) resp := approveSession.MakeRequest(t, req, http.StatusOK)
htmlDoc := NewHTMLParser(t, resp.Body) htmlDoc := NewHTMLParser(t, resp.Body)
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)
}
// realod 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.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))
forkedGitRepo.Close()
}
}
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: "repo1-3",
approval: true,
deleteBranch: false,
},
{
name: "TestPullAutoMergeAfterCommitStatusSucceedAndApprovalWithBranchDeletion",
forkName: "repo1-4",
approval: true,
deleteBranch: true,
},
} {
t.Run(testCase.name, func(t *testing.T) {
protectedBranch := parameterProtectBranch{
"enable_push": "true",
"enable_status_check": "true",
"status_check_contexts": "gitea/actions",
}
if testCase.approval {
protectedBranch["required_approvals"] = "1"
}
ctx := NewAPITestContext(t, "user2", "repo1", auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
doProtectBranch(ctx, "master", protectedBranch)(t)
testPullAutoMergeAfterCommitStatusSucceed(t, testCase.forkName, testCase.approval, testCase.deleteBranch)
})
}
}) })
} }