mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-09 17:37:20 -06:00
If the stderr output is not handled, then it needs to use RunWithStderr to make sure the git error message is included in the returned error. Otherwise, when the git command fails, the error log won't show useful information. Ref: * #39795 : no useful information in log messages * https://github.com/go-gitea/gitea/pull/40160#pullrequestreview-5752720866
236 lines
8.4 KiB
Go
236 lines
8.4 KiB
Go
// Copyright 2024 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package integration
|
|
|
|
import (
|
|
"bytes"
|
|
"io"
|
|
"net/url"
|
|
"sync"
|
|
"testing"
|
|
|
|
auth_model "gitea.dev/models/auth"
|
|
issues_model "gitea.dev/models/issues"
|
|
repo_model "gitea.dev/models/repo"
|
|
"gitea.dev/models/unittest"
|
|
user_model "gitea.dev/models/user"
|
|
"gitea.dev/modules/git"
|
|
"gitea.dev/modules/git/gitcmd"
|
|
files_service "gitea.dev/services/repository/files"
|
|
"gitea.dev/tests"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestDataAsyncDoubleRead_Issue29101(t *testing.T) {
|
|
onGiteaRun(t, func(t *testing.T, u *url.URL) {
|
|
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
|
|
testContent := bytes.Repeat([]byte{'a'}, 10000)
|
|
resp, err := files_service.ChangeRepoFiles(t.Context(), repo, user, &files_service.ChangeRepoFilesOptions{
|
|
Files: []*files_service.ChangeRepoFile{
|
|
{
|
|
Operation: "create",
|
|
TreePath: "test.txt",
|
|
ContentReader: bytes.NewReader(testContent),
|
|
},
|
|
},
|
|
OldBranch: repo.DefaultBranch,
|
|
NewBranch: repo.DefaultBranch,
|
|
})
|
|
assert.NoError(t, err)
|
|
|
|
sha := resp.Commit.SHA
|
|
|
|
gitRepo, err := git.OpenRepository(t.Context(), repo)
|
|
assert.NoError(t, err)
|
|
|
|
commit, err := gitRepo.GetCommit(t.Context(), sha)
|
|
assert.NoError(t, err)
|
|
|
|
entry, err := commit.GetTreeEntryByPath(t.Context(), gitRepo, "test.txt")
|
|
assert.NoError(t, err)
|
|
|
|
b := entry.Blob(gitRepo)
|
|
r1, err := b.DataAsync(t.Context())
|
|
assert.NoError(t, err)
|
|
defer r1.Close()
|
|
r2, err := b.DataAsync(t.Context())
|
|
assert.NoError(t, err)
|
|
defer r2.Close()
|
|
|
|
var data1, data2 []byte
|
|
wg := sync.WaitGroup{}
|
|
wg.Go(func() {
|
|
data1, _ = io.ReadAll(r1)
|
|
assert.NoError(t, err)
|
|
})
|
|
wg.Go(func() {
|
|
data2, _ = io.ReadAll(r2)
|
|
assert.NoError(t, err)
|
|
})
|
|
wg.Wait()
|
|
assert.Equal(t, testContent, data1)
|
|
assert.Equal(t, testContent, data2)
|
|
})
|
|
}
|
|
|
|
func TestAgitPullPush(t *testing.T) {
|
|
onGiteaRun(t, func(t *testing.T, u *url.URL) {
|
|
for _, testCase := range []struct {
|
|
branch, contentPrefix string
|
|
pushArgs gitcmd.TrustedCmdArgs
|
|
}{
|
|
{
|
|
"test-agit-push", "branch-data-file-",
|
|
gitcmd.TrustedCmdArgs{"push", "origin", "-o", "title=test-title", "-o", "description=test-description", "HEAD:refs/for/master/test-agit-push"},
|
|
},
|
|
{
|
|
"test-agit-review", "initial-",
|
|
gitcmd.TrustedCmdArgs{"push", "origin", "-o", "title=Test agit Review Staleness", "-o", "description=Testing review staleness", "HEAD:refs/for/master/test-agit-review"},
|
|
},
|
|
} {
|
|
t.Run(testCase.branch, func(t *testing.T) {
|
|
defer tests.PrintCurrentTest(t)()
|
|
baseAPITestContext := NewAPITestContext(t, "user2", "repo1", auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
|
|
|
|
u.Path = baseAPITestContext.GitPath()
|
|
u.User = url.UserPassword("user2", userPassword)
|
|
|
|
dstPath := t.TempDir()
|
|
doGitClone(dstPath, u)(t)
|
|
|
|
gitRepo, err := git.OpenRepositoryLocal(t.Context(), dstPath)
|
|
assert.NoError(t, err)
|
|
defer gitRepo.Close()
|
|
|
|
doGitCreateBranch(dstPath, testCase.branch)
|
|
|
|
// Create initial commit
|
|
_, err = generateCommitWithNewData(t.Context(), testFileSizeSmall, dstPath, "user2@example.com", "User Two", testCase.contentPrefix)
|
|
assert.NoError(t, err)
|
|
|
|
// create PR via agit
|
|
err = gitcmd.NewCommand(testCase.pushArgs...).WithDir(dstPath).RunWithStderr(t.Context())
|
|
assert.NoError(t, err)
|
|
|
|
// check pull request exist
|
|
pr := unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{BaseRepoID: 1, Flow: issues_model.PullRequestFlowAGit, HeadBranch: "user2/" + testCase.branch})
|
|
assert.NoError(t, pr.LoadIssue(t.Context()))
|
|
if testCase.branch == "test-agit-push" {
|
|
assert.Equal(t, "test-title", pr.Issue.Title)
|
|
assert.Equal(t, "test-description", pr.Issue.Content)
|
|
assert.Equal(t, 0, pr.CommitsBehind)
|
|
assert.Equal(t, 1, pr.CommitsAhead)
|
|
|
|
// commit 2
|
|
_, err = generateCommitWithNewData(t.Context(), testFileSizeSmall, dstPath, "user2@example.com", "User Two", "branch-data-file-2-")
|
|
assert.NoError(t, err)
|
|
|
|
// push 2
|
|
err = gitcmd.NewCommand("push", "origin", "HEAD:refs/for/master/test-agit-push").
|
|
WithDir(dstPath).
|
|
RunWithStderr(t.Context())
|
|
assert.NoError(t, err)
|
|
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{BaseRepoID: 1, Flow: issues_model.PullRequestFlowAGit, HeadBranch: "user2/test-agit-push"})
|
|
assert.Equal(t, 0, pr.CommitsBehind)
|
|
assert.Equal(t, 2, pr.CommitsAhead)
|
|
|
|
// reset to first commit
|
|
err = gitcmd.NewCommand("reset", "--hard", "HEAD~1").WithDir(dstPath).RunWithStderr(t.Context())
|
|
assert.NoError(t, err)
|
|
|
|
// test force push without confirm
|
|
var stderr string
|
|
_, stderr, err = gitcmd.NewCommand("push", "origin", "HEAD:refs/for/master/test-agit-push").
|
|
WithDir(dstPath).
|
|
RunStdString(t.Context())
|
|
assert.Error(t, err)
|
|
assert.Contains(t, stderr, "[remote rejected] HEAD -> refs/for/master/test-agit-push (request `force-push` push option)")
|
|
|
|
// test force push with confirm
|
|
err = gitcmd.NewCommand("push", "origin", "HEAD:refs/for/master/test-agit-push", "-o", "force-push").
|
|
WithDir(dstPath).
|
|
RunWithStderr(t.Context())
|
|
assert.NoError(t, err)
|
|
|
|
_, stderr, _ = gitcmd.NewCommand("push", "origin", "HEAD:refs/for/no-such-branch/test-agit-push").
|
|
WithDir(dstPath).
|
|
RunStdString(t.Context())
|
|
assert.Contains(t, stderr, "Unexpected ref: refs/for/no-such-branch/test-agit-push")
|
|
} else {
|
|
// Get initial commit ID for the review
|
|
initialCommitID := pr.HeadCommitID
|
|
t.Logf("Initial commit ID: %s", initialCommitID)
|
|
|
|
// Create a review on the PR (as user1 reviewing user2's PR)
|
|
reviewer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
|
|
review, err := issues_model.CreateReview(t.Context(), issues_model.CreateReviewOptions{
|
|
Type: issues_model.ReviewTypeApprove,
|
|
Reviewer: reviewer,
|
|
Issue: pr.Issue,
|
|
CommitID: initialCommitID,
|
|
Content: "LGTM! Looks good to merge.",
|
|
Official: false,
|
|
})
|
|
assert.NoError(t, err)
|
|
assert.False(t, review.Stale, "New review should not be stale")
|
|
|
|
// Verify review exists and is not stale
|
|
reviews, err := issues_model.FindReviews(t.Context(), issues_model.FindReviewOptions{
|
|
IssueID: pr.IssueID,
|
|
})
|
|
assert.NoError(t, err)
|
|
assert.Len(t, reviews, 1)
|
|
assert.Equal(t, initialCommitID, reviews[0].CommitID)
|
|
assert.False(t, reviews[0].Stale, "Review should not be stale initially")
|
|
|
|
// Create a new commit and update the agit PR
|
|
_, err = generateCommitWithNewData(t.Context(), testFileSizeSmall, dstPath, "user2@example.com", "User Two", "updated-")
|
|
assert.NoError(t, err)
|
|
|
|
err = gitcmd.NewCommand("push", "origin", "HEAD:refs/for/master/test-agit-review").
|
|
WithDir(dstPath).
|
|
RunWithStderr(t.Context())
|
|
assert.NoError(t, err)
|
|
|
|
// Reload PR to get updated commit ID
|
|
pr = unittest.AssertExistsAndLoadBean(t, &issues_model.PullRequest{
|
|
BaseRepoID: 1,
|
|
Flow: issues_model.PullRequestFlowAGit,
|
|
HeadBranch: "user2/test-agit-review",
|
|
})
|
|
assert.NoError(t, pr.LoadIssue(t.Context()))
|
|
|
|
// For AGit PRs, HeadCommitID must be loaded from git references
|
|
baseRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
baseGitRepo, err := git.OpenRepository(t.Context(), baseRepo)
|
|
assert.NoError(t, err)
|
|
defer baseGitRepo.Close()
|
|
|
|
updatedCommitID, err := baseGitRepo.GetRefCommitID(t.Context(), pr.GetGitHeadRefName())
|
|
assert.NoError(t, err)
|
|
t.Logf("Updated commit ID: %s", updatedCommitID)
|
|
|
|
// Verify the PR was updated with new commit
|
|
assert.NotEqual(t, initialCommitID, updatedCommitID, "PR should have new commit ID after update")
|
|
|
|
// Check that the review is now marked as stale
|
|
reviews, err = issues_model.FindReviews(t.Context(), issues_model.FindReviewOptions{
|
|
IssueID: pr.IssueID,
|
|
})
|
|
assert.NoError(t, err)
|
|
assert.Len(t, reviews, 1)
|
|
|
|
assert.True(t, reviews[0].Stale, "Review should be marked as stale after AGit PR update")
|
|
|
|
// The review commit ID should remain the same (pointing to the original commit)
|
|
assert.Equal(t, initialCommitID, reviews[0].CommitID, "Review commit ID should remain unchanged and point to original commit")
|
|
}
|
|
})
|
|
}
|
|
})
|
|
}
|