mirror of
https://github.com/go-gitea/gitea.git
synced 2026-10-09 04:30:16 +09:00
d6945fb0b5
Backport #39611 by @Kshot3000 * Fixes #39606 * Fixes #39329 `GetLatestCommitStatusForRepoCommitIDs` built one `OR` condition per commit SHA. Rendering a commit list of more than ~1000 commits (for example a large pull request, via `processGitCommits`) therefore failed on SQLite with `SQL logic error: Expression tree is too large (maximum depth 1000)`. The max-index lookup now queries SHAs in chunks of 500 with `IN`, and the follow-up status fetch runs in chunks of 100 `(index, sha)` pairs, so the expression depth stays bounded regardless of the commit count. Results are unchanged. Verified with a new regression test that calls the function with 2001 SHAs: it fails with the reported error on `main` and passes with this change. `go test ./models/git/` passes. — Fix offered freely; tips welcome: PayPal kyleblake0659@gmail.com · BTC 3GnR7TWBXAB3pPztBWpNF4LMNEX5yX8vZK Co-authored-by: KShot <kshot9000@gmail.com> Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
289 lines
7.9 KiB
Go
289 lines
7.9 KiB
Go
// Copyright 2017 Gitea. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package git_test
|
|
|
|
import (
|
|
"fmt"
|
|
"testing"
|
|
"time"
|
|
|
|
"gitea.dev/models/db"
|
|
git_model "gitea.dev/models/git"
|
|
repo_model "gitea.dev/models/repo"
|
|
"gitea.dev/models/unittest"
|
|
user_model "gitea.dev/models/user"
|
|
"gitea.dev/modules/commitstatus"
|
|
"gitea.dev/modules/git"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestGetCommitStatuses(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
repo1 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
|
|
sha1 := "1234123412341234123412341234123412341234" // the mocked commit ID in test fixtures
|
|
|
|
t.Run("CommitStatusOptions", func(t *testing.T) {
|
|
statuses, maxResults, err := db.FindAndCount[git_model.CommitStatus](t.Context(), &git_model.CommitStatusOptions{
|
|
ListOptions: db.ListOptions{Page: 1, PageSize: 50},
|
|
RepoID: repo1.ID,
|
|
SHA: sha1,
|
|
})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 5, int(maxResults))
|
|
var indexes []int64
|
|
for _, status := range statuses {
|
|
indexes = append(indexes, status.Index)
|
|
}
|
|
assert.Equal(t, []int64{5, 4, 3, 2, 1}, indexes)
|
|
assert.Equal(t, "deploy/awesomeness", statuses[0].Context)
|
|
assert.Equal(t, commitstatus.CommitStatusError, statuses[0].State)
|
|
assert.Equal(t, "https://try.gitea.io/api/v1/repos/user2/repo1/statuses/1234123412341234123412341234123412341234", statuses[0].APIURL(t.Context()))
|
|
|
|
statuses, maxResults, err = db.FindAndCount[git_model.CommitStatus](t.Context(), &git_model.CommitStatusOptions{
|
|
ListOptions: db.ListOptions{Page: 2, PageSize: 50},
|
|
RepoID: repo1.ID,
|
|
SHA: sha1,
|
|
})
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, 5, int(maxResults))
|
|
assert.Empty(t, statuses)
|
|
})
|
|
|
|
t.Run("GetCountLatestCommitStatus", func(t *testing.T) {
|
|
commitStatuses, err := git_model.GetLatestCommitStatus(t.Context(), repo1.ID, sha1, db.ListOptions{
|
|
Page: 1,
|
|
PageSize: 2,
|
|
})
|
|
assert.NoError(t, err)
|
|
assert.Len(t, commitStatuses, 2)
|
|
assert.Equal(t, commitstatus.CommitStatusFailure, commitStatuses[0].State)
|
|
assert.Equal(t, "ci/awesomeness", commitStatuses[0].Context)
|
|
assert.Equal(t, commitstatus.CommitStatusError, commitStatuses[1].State)
|
|
assert.Equal(t, "deploy/awesomeness", commitStatuses[1].Context)
|
|
|
|
count, err := git_model.CountLatestCommitStatus(t.Context(), repo1.ID, sha1)
|
|
assert.NoError(t, err)
|
|
assert.EqualValues(t, 3, count)
|
|
})
|
|
|
|
t.Run("GetLatestCommitStatusForRepoCommitIDs", func(t *testing.T) {
|
|
commitIDs := []string{sha1}
|
|
// SQLite has a limit of 1000 for WHERE expression variables per query, use a larger slice to test
|
|
for i := range 2000 {
|
|
commitIDs = append(commitIDs, fmt.Sprintf("%040x", i+1))
|
|
}
|
|
statuses, err := git_model.GetLatestCommitStatusForRepoCommitIDs(t.Context(), repo1.ID, commitIDs)
|
|
assert.NoError(t, err)
|
|
assert.Len(t, statuses[sha1], 3)
|
|
latestIndexes := make(map[string]int64)
|
|
for _, status := range statuses[sha1] {
|
|
latestIndexes[status.Context] = status.Index
|
|
}
|
|
assert.Equal(t, map[string]int64{
|
|
"ci/awesomeness": 4,
|
|
"cov/awesomeness": 3,
|
|
"deploy/awesomeness": 5,
|
|
}, latestIndexes)
|
|
})
|
|
}
|
|
|
|
func Test_CalcCommitStatus(t *testing.T) {
|
|
kases := []struct {
|
|
statuses []*git_model.CommitStatus
|
|
expected *git_model.CommitStatus
|
|
}{
|
|
{
|
|
statuses: []*git_model.CommitStatus{
|
|
{
|
|
State: commitstatus.CommitStatusPending,
|
|
},
|
|
},
|
|
expected: &git_model.CommitStatus{
|
|
State: commitstatus.CommitStatusPending,
|
|
},
|
|
},
|
|
{
|
|
statuses: []*git_model.CommitStatus{
|
|
{
|
|
State: commitstatus.CommitStatusSuccess,
|
|
},
|
|
{
|
|
State: commitstatus.CommitStatusPending,
|
|
},
|
|
},
|
|
expected: &git_model.CommitStatus{
|
|
State: commitstatus.CommitStatusPending,
|
|
},
|
|
},
|
|
{
|
|
statuses: []*git_model.CommitStatus{
|
|
{
|
|
State: commitstatus.CommitStatusSuccess,
|
|
},
|
|
{
|
|
State: commitstatus.CommitStatusPending,
|
|
},
|
|
{
|
|
State: commitstatus.CommitStatusSuccess,
|
|
},
|
|
},
|
|
expected: &git_model.CommitStatus{
|
|
State: commitstatus.CommitStatusPending,
|
|
},
|
|
},
|
|
{
|
|
statuses: []*git_model.CommitStatus{
|
|
{
|
|
State: commitstatus.CommitStatusError,
|
|
},
|
|
{
|
|
State: commitstatus.CommitStatusPending,
|
|
},
|
|
{
|
|
State: commitstatus.CommitStatusSuccess,
|
|
},
|
|
},
|
|
expected: &git_model.CommitStatus{
|
|
State: commitstatus.CommitStatusFailure,
|
|
},
|
|
},
|
|
{
|
|
statuses: []*git_model.CommitStatus{
|
|
{
|
|
State: commitstatus.CommitStatusWarning,
|
|
},
|
|
{
|
|
State: commitstatus.CommitStatusPending,
|
|
},
|
|
{
|
|
State: commitstatus.CommitStatusSuccess,
|
|
},
|
|
},
|
|
expected: &git_model.CommitStatus{
|
|
State: commitstatus.CommitStatusFailure,
|
|
},
|
|
},
|
|
{
|
|
statuses: []*git_model.CommitStatus{
|
|
{
|
|
State: commitstatus.CommitStatusSuccess,
|
|
},
|
|
{
|
|
State: commitstatus.CommitStatusSuccess,
|
|
},
|
|
{
|
|
State: commitstatus.CommitStatusSuccess,
|
|
},
|
|
},
|
|
expected: &git_model.CommitStatus{
|
|
State: commitstatus.CommitStatusSuccess,
|
|
},
|
|
},
|
|
{
|
|
statuses: []*git_model.CommitStatus{
|
|
{
|
|
State: commitstatus.CommitStatusFailure,
|
|
},
|
|
{
|
|
State: commitstatus.CommitStatusError,
|
|
},
|
|
{
|
|
State: commitstatus.CommitStatusWarning,
|
|
},
|
|
},
|
|
expected: &git_model.CommitStatus{
|
|
State: commitstatus.CommitStatusFailure,
|
|
},
|
|
},
|
|
}
|
|
|
|
for _, kase := range kases {
|
|
assert.Equal(t, kase.expected, git_model.CalcCommitStatus(kase.statuses), "statuses: %v", kase.statuses)
|
|
}
|
|
}
|
|
|
|
func TestFindRepoRecentCommitStatusContexts(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
repo2 := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
|
|
user2 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
|
|
gitRepo, err := git.OpenRepository(t.Context(), repo2)
|
|
assert.NoError(t, err)
|
|
defer gitRepo.Close()
|
|
|
|
commit, err := gitRepo.GetBranchCommit(t.Context(), repo2.DefaultBranch)
|
|
assert.NoError(t, err)
|
|
|
|
defer func() {
|
|
_, err := db.DeleteByBean(t.Context(), &git_model.CommitStatus{
|
|
RepoID: repo2.ID,
|
|
CreatorID: user2.ID,
|
|
SHA: commit.ID.String(),
|
|
})
|
|
assert.NoError(t, err)
|
|
}()
|
|
|
|
err = git_model.NewCommitStatus(t.Context(), git_model.NewCommitStatusOptions{
|
|
Repo: repo2,
|
|
Creator: user2,
|
|
SHA: commit.ID,
|
|
CommitStatus: &git_model.CommitStatus{
|
|
State: commitstatus.CommitStatusFailure,
|
|
TargetURL: "https://example.com/tests/",
|
|
Context: "compliance/lint-backend",
|
|
},
|
|
})
|
|
assert.NoError(t, err)
|
|
|
|
err = git_model.NewCommitStatus(t.Context(), git_model.NewCommitStatusOptions{
|
|
Repo: repo2,
|
|
Creator: user2,
|
|
SHA: commit.ID,
|
|
CommitStatus: &git_model.CommitStatus{
|
|
State: commitstatus.CommitStatusSuccess,
|
|
TargetURL: "https://example.com/tests/",
|
|
Context: "compliance/lint-backend",
|
|
},
|
|
})
|
|
assert.NoError(t, err)
|
|
|
|
contexts, err := git_model.FindRepoRecentCommitStatusContexts(t.Context(), repo2.ID, time.Hour)
|
|
assert.NoError(t, err)
|
|
if assert.Len(t, contexts, 1) {
|
|
assert.Equal(t, "compliance/lint-backend", contexts[0])
|
|
}
|
|
}
|
|
|
|
func TestCommitStatusesApplyDoerPermission(t *testing.T) {
|
|
assert.NoError(t, unittest.PrepareTestDatabase())
|
|
|
|
// repo4 is public and has the actions unit, repo2 is private and owned by someone else
|
|
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 4})
|
|
otherRepo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 2})
|
|
doer := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: repo.OwnerID})
|
|
|
|
visibleURL := repo.Link() + "/actions/runs/1/jobs/1"
|
|
statuses := []*git_model.CommitStatus{
|
|
{
|
|
RepoID: repo.ID,
|
|
TargetURL: visibleURL,
|
|
},
|
|
{
|
|
RepoID: otherRepo.ID,
|
|
TargetURL: otherRepo.Link() + "/actions/runs/1/jobs/1",
|
|
},
|
|
{
|
|
RepoID: repo.ID,
|
|
TargetURL: "https://mycicd.org/1",
|
|
},
|
|
}
|
|
|
|
git_model.CommitStatusesApplyDoerPermission(t.Context(), doer, statuses)
|
|
assert.Equal(t, visibleURL, statuses[0].TargetURL)
|
|
assert.Empty(t, statuses[1].TargetURL)
|
|
assert.Equal(t, "https://mycicd.org/1", statuses[2].TargetURL)
|
|
}
|