mirror of
https://codeberg.org/forgejo/forgejo.git
synced 2024-11-15 02:45:22 +00:00
50822f361e
* Split TestPullRequest out of AddTestPullRequestTask
* A Created field is added to the Issue table
* The Created field is set to the time (with nano resolution) on creation
* Record the nano time repo_module.PushUpdateOptions is created by the hook
* The decision to update a pull request created before a commit was
pushed is based on the time (with nano resolution) the git hook
was run and the Created field
It ensures the following happens:
* commit C is pushed
* the git hook queues AddTestPullRequestTask for processing and returns with success
* TestPullRequest is not called yet
* a pull request P with commit C as the head is created
* TestPullRequest runs and ignores P because it was created after the commit was received
When the "created" column is NULL, no verification is done, pull
requests that were created before the column was created in the
database cannot be newer than the latest call to a git hook.
Fixes: https://codeberg.org/forgejo/forgejo/issues/2009
(cherry picked from commit 998a431747
)
Conflicts:
models/forgejo_migrations/migrate.go
see https://codeberg.org/forgejo/forgejo/pulls/3165#issuecomment-1755941
services/pull/pull.go
trivial conflicts
110 lines
3.3 KiB
Go
110 lines
3.3 KiB
Go
// Copyright 2024 The Forgejo Authors
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package integration
|
|
|
|
import (
|
|
"context"
|
|
"testing"
|
|
"time"
|
|
|
|
"code.gitea.io/gitea/models/db"
|
|
issues_model "code.gitea.io/gitea/models/issues"
|
|
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"
|
|
"code.gitea.io/gitea/modules/log"
|
|
repo_module "code.gitea.io/gitea/modules/repository"
|
|
"code.gitea.io/gitea/modules/test"
|
|
"code.gitea.io/gitea/modules/timeutil"
|
|
pull_service "code.gitea.io/gitea/services/pull"
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
|
"code.gitea.io/gitea/tests"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestPullRequestSynchronized(t *testing.T) {
|
|
defer tests.PrepareTestEnv(t)()
|
|
|
|
// unmerged pull request of user2/repo1 from branch2 to master
|
|
pull := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{ID: 2})
|
|
// tip of tests/gitea-repositories-meta/user2/repo1 branch2
|
|
pull.HeadCommitID = "985f0301dba5e7b34be866819cd15ad3d8f508ee"
|
|
pull.LoadIssue(db.DefaultContext)
|
|
pull.Issue.Created = timeutil.TimeStampNanoNow()
|
|
issues_model.UpdateIssueCols(db.DefaultContext, pull.Issue, "created")
|
|
|
|
require.Equal(t, pull.HeadRepoID, pull.BaseRepoID)
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: pull.HeadRepoID})
|
|
owner := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
|
|
|
for _, testCase := range []struct {
|
|
name string
|
|
timeNano int64
|
|
expected bool
|
|
}{
|
|
{
|
|
name: "AddTestPullRequestTask process PR",
|
|
timeNano: int64(pull.Issue.Created),
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "AddTestPullRequestTask skip PR",
|
|
timeNano: 0,
|
|
expected: false,
|
|
},
|
|
} {
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
logChecker, cleanup := test.NewLogChecker(log.DEFAULT, log.TRACE)
|
|
logChecker.Filter("Updating PR").StopMark("TestPullRequest ")
|
|
defer cleanup()
|
|
|
|
opt := &repo_module.PushUpdateOptions{
|
|
PusherID: owner.ID,
|
|
PusherName: owner.Name,
|
|
RepoUserName: owner.Name,
|
|
RepoName: repo.Name,
|
|
RefFullName: git.RefName("refs/heads/branch2"),
|
|
OldCommitID: pull.HeadCommitID,
|
|
NewCommitID: pull.HeadCommitID,
|
|
TimeNano: testCase.timeNano,
|
|
}
|
|
require.NoError(t, repo_service.PushUpdate(opt))
|
|
logFiltered, logStopped := logChecker.Check(5 * time.Second)
|
|
assert.True(t, logStopped)
|
|
assert.Equal(t, testCase.expected, logFiltered[0])
|
|
})
|
|
}
|
|
|
|
for _, testCase := range []struct {
|
|
name string
|
|
olderThan int64
|
|
expected bool
|
|
}{
|
|
{
|
|
name: "TestPullRequest process PR",
|
|
olderThan: int64(pull.Issue.Created),
|
|
expected: true,
|
|
},
|
|
{
|
|
name: "TestPullRequest skip PR",
|
|
olderThan: int64(pull.Issue.Created) - 1,
|
|
expected: false,
|
|
},
|
|
} {
|
|
t.Run(testCase.name, func(t *testing.T) {
|
|
logChecker, cleanup := test.NewLogChecker(log.DEFAULT, log.TRACE)
|
|
logChecker.Filter("Updating PR").StopMark("TestPullRequest ")
|
|
defer cleanup()
|
|
|
|
pull_service.TestPullRequest(context.Background(), owner, repo.ID, testCase.olderThan, "branch2", true, pull.HeadCommitID, pull.HeadCommitID)
|
|
logFiltered, logStopped := logChecker.Check(5 * time.Second)
|
|
assert.True(t, logStopped)
|
|
assert.Equal(t, testCase.expected, logFiltered[0])
|
|
})
|
|
}
|
|
}
|