Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Sync branches to DB immediately when handle git hook calling #29493

Merged
merged 12 commits into from
Mar 6, 2024
Prev Previous commit
Next Next commit
test: TestGitPush
  • Loading branch information
wolfogre committed Mar 5, 2024
commit 7d7bdebda3f9ed743a734bf64d0622c1bf926e8a
131 changes: 131 additions & 0 deletions tests/integration/git_push_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
// Copyright 2024 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package integration

import (
"fmt"
"net/url"
"testing"

"code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
"code.gitea.io/gitea/models/unittest"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
repo_service "code.gitea.io/gitea/services/repository"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)

func TestGitPush(t *testing.T) {
onGiteaRun(t, testGitPush)
}

func testGitPush(t *testing.T, u *url.URL) {
t.Run("Push branches at once", func(t *testing.T) {
runTestGitPush(t, u, func(t *testing.T, gitPath string) (pushed, deleted []string) {
for i := 0; i < 100; i++ {
branchName := fmt.Sprintf("branch-%d", i)
pushed = append(pushed, branchName)
doGitCreateBranch(gitPath, branchName)(t)
}
pushed = append(pushed, "master")
doGitPushTestRepository(gitPath, "origin", "--all")(t)
return

Check failure on line 36 in tests/integration/git_push_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

naked return in func `testGitPush.<func():27>.<func():28>` with 9 lines of code (nakedret)

Check failure on line 36 in tests/integration/git_push_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

naked return in func `testGitPush.<func():27>.<func():28>` with 9 lines of code (nakedret)

Check failure on line 36 in tests/integration/git_push_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

naked return in func `testGitPush.<func():27>.<func():28>` with 9 lines of code (nakedret)
})
})

t.Run("Push branches one by one", func(t *testing.T) {
runTestGitPush(t, u, func(t *testing.T, gitPath string) (pushed, deleted []string) {
for i := 0; i < 100; i++ {
branchName := fmt.Sprintf("branch-%d", i)
doGitCreateBranch(gitPath, branchName)(t)
doGitPushTestRepository(gitPath, "origin", branchName)(t)
pushed = append(pushed, branchName)
}
return

Check failure on line 48 in tests/integration/git_push_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

naked return in func `testGitPush.<func():40>.<func():41>` with 8 lines of code (nakedret)

Check failure on line 48 in tests/integration/git_push_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

naked return in func `testGitPush.<func():40>.<func():41>` with 8 lines of code (nakedret)

Check failure on line 48 in tests/integration/git_push_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

naked return in func `testGitPush.<func():40>.<func():41>` with 8 lines of code (nakedret)
})
})

t.Run("Delete branches", func(t *testing.T) {
runTestGitPush(t, u, func(t *testing.T, gitPath string) (pushed, deleted []string) {
doGitPushTestRepository(gitPath, "origin", "master")(t) // make sure master is the default branch instead of a branch we are going to delete
pushed = append(pushed, "master")

for i := 0; i < 100; i++ {
branchName := fmt.Sprintf("branch-%d", i)
pushed = append(pushed, branchName)
doGitCreateBranch(gitPath, branchName)(t)
}
doGitPushTestRepository(gitPath, "origin", "--all")(t)

for i := 0; i < 10; i++ {
branchName := fmt.Sprintf("branch-%d", i)
doGitPushTestRepository(gitPath, "origin", "--delete", branchName)(t)
deleted = append(deleted, branchName)
}
return

Check failure on line 69 in tests/integration/git_push_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

naked return in func `testGitPush.<func():52>.<func():53>` with 17 lines of code (nakedret)

Check failure on line 69 in tests/integration/git_push_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

naked return in func `testGitPush.<func():52>.<func():53>` with 17 lines of code (nakedret)

Check failure on line 69 in tests/integration/git_push_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

naked return in func `testGitPush.<func():52>.<func():53>` with 17 lines of code (nakedret)
})
})
}

func runTestGitPush(t *testing.T, u *url.URL, gitOperation func(t *testing.T, gitPath string) (pushed, deleted []string)) {
user := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 2})
repo, err := repo_service.CreateRepository(db.DefaultContext, user, user, repo_service.CreateRepoOptions{
Name: "repo-to-push",
Description: "test git push",
AutoInit: false,
DefaultBranch: "main",
IsPrivate: false,
})
require.NoError(t, err)
require.NotEmpty(t, repo)

gitPath := t.TempDir()

doGitInitTestRepository(gitPath)(t)

oldPath := u.Path
oldUser := u.User
defer func() {
u.Path = oldPath
u.User = oldUser
}()
u.Path = repo.FullName() + ".git"
u.User = url.UserPassword(user.LowerName, userPassword)

doGitAddRemote(gitPath, "origin", u)(t)

gitRepo, err := git.OpenRepository(git.DefaultContext, gitPath)
require.NoError(t, err)
defer gitRepo.Close()

pushedBranches, deletedBranches := gitOperation(t, gitPath)

dbBranches := make([]*git_model.Branch, 0)
require.NoError(t, db.GetEngine(db.DefaultContext).Where("repo_id=?", repo.ID).Find(&dbBranches))
assert.Equalf(t, len(pushedBranches), len(dbBranches), "mismatched number of branches in db")
dbBranchesMap := make(map[string]*git_model.Branch, len(dbBranches))
for _, branch := range dbBranches {
dbBranchesMap[branch.Name] = branch
}

deletedBranchesMap := make(map[string]bool, len(deletedBranches))
for _, branchName := range deletedBranches {
deletedBranchesMap[branchName] = true
}

for _, branchName := range pushedBranches {
branch, ok := dbBranchesMap[branchName]
deleted := deletedBranchesMap[branchName]
assert.True(t, ok, "branch %s not found in database", branchName)
assert.Equal(t, deleted, branch.IsDeleted, "IsDeleted of %s is %v, but it's expected to be %v", branchName, branch.IsDeleted, deleted)
commitID, err := gitRepo.GetBranchCommitID(branchName)
require.NoError(t, err)
assert.Equal(t, commitID, branch.CommitID)
}

require.NoError(t, repo_service.DeleteRepositoryDirectly(db.DefaultContext, user, repo.ID))
}
Loading