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

Remove unnecessary syncbranchToDB with tests (#28624) #28625

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 9 additions & 20 deletions services/repository/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -281,28 +281,17 @@ func CreateNewBranchFromCommit(ctx context.Context, doer *user_model.User, repo
return err
}

return db.WithTx(ctx, func(ctx context.Context) error {
commit, err := gitRepo.GetCommit(commitID)
if err != nil {
return err
}
// database operation should be done before git operation so that we can rollback if git operation failed
if err := syncBranchToDB(ctx, repo.ID, doer.ID, branchName, commit); err != nil {
if err := git.Push(ctx, repo.RepoPath(), git.PushOptions{
Remote: repo.RepoPath(),
Branch: fmt.Sprintf("%s:%s%s", commitID, git.BranchPrefix, branchName),
Env: repo_module.PushingEnvironment(doer, repo),
}); err != nil {
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
return err
}

if err := git.Push(ctx, repo.RepoPath(), git.PushOptions{
Remote: repo.RepoPath(),
Branch: fmt.Sprintf("%s:%s%s", commitID, git.BranchPrefix, branchName),
Env: repo_module.PushingEnvironment(doer, repo),
}); err != nil {
if git.IsErrPushOutOfDate(err) || git.IsErrPushRejected(err) {
return err
}
return fmt.Errorf("push: %w", err)
}
return nil
})
return fmt.Errorf("push: %w", err)
}
return nil
}

// RenameBranch rename a branch
Expand Down
36 changes: 36 additions & 0 deletions tests/integration/api_branch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
"testing"

auth_model "code.gitea.io/gitea/models/auth"
"code.gitea.io/gitea/models/db"
git_model "code.gitea.io/gitea/models/git"
api "code.gitea.io/gitea/modules/structs"
"code.gitea.io/gitea/tests"

Expand Down Expand Up @@ -212,3 +214,37 @@
testAPIDeleteBranch(t, "master", http.StatusForbidden)
testAPIDeleteBranch(t, "branch2", http.StatusNoContent)
}

func TestAPICreateBranchWithSyncBranches(t *testing.T) {
defer tests.PrepareTestEnv(t)()

branches, err := db.Find[git_model.Branch](db.DefaultContext, git_model.FindBranchOptions{

Check failure on line 221 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 221 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

not enough arguments in call to db.Find[git_model.Branch]

Check failure on line 221 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 221 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

not enough arguments in call to db.Find[git_model.Branch]

Check failure on line 221 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 221 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

not enough arguments in call to db.Find[git_model.Branch]

Check failure on line 221 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-mysql8

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 221 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-mysql8

not enough arguments in call to db.Find[git_model.Branch]

Check failure on line 221 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-sqlite

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 221 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-sqlite

not enough arguments in call to db.Find[git_model.Branch]

Check failure on line 221 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-mssql

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 221 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-mssql

not enough arguments in call to db.Find[git_model.Branch]

Check failure on line 221 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-pgsql

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 221 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-pgsql

not enough arguments in call to db.Find[git_model.Branch]

Check failure on line 221 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-mysql5

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 221 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-mysql5

not enough arguments in call to db.Find[git_model.Branch]
RepoID: 1,
})
assert.NoError(t, err)
assert.Len(t, branches, 4)

// make a broke repository with no branch on database
_, err = db.DeleteByBean(db.DefaultContext, git_model.Branch{RepoID: 1})
assert.NoError(t, err)

onGiteaRun(t, func(t *testing.T, giteaURL *url.URL) {
ctx := NewAPITestContext(t, "user2", "repo1", auth_model.AccessTokenScopeWriteRepository, auth_model.AccessTokenScopeWriteUser)
giteaURL.Path = ctx.GitPath()

testAPICreateBranch(t, ctx.Session, "user2", "repo1", "", "new_branch", http.StatusCreated)
})

branches, err = db.Find[git_model.Branch](db.DefaultContext, git_model.FindBranchOptions{

Check failure on line 238 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 238 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

not enough arguments in call to db.Find[git_model.Branch]

Check failure on line 238 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 238 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

not enough arguments in call to db.Find[git_model.Branch]

Check failure on line 238 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 238 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

not enough arguments in call to db.Find[git_model.Branch]

Check failure on line 238 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-mysql8

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 238 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-mysql8

not enough arguments in call to db.Find[git_model.Branch]

Check failure on line 238 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-sqlite

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 238 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-sqlite

not enough arguments in call to db.Find[git_model.Branch]

Check failure on line 238 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-mssql

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 238 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-mssql

not enough arguments in call to db.Find[git_model.Branch]

Check failure on line 238 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-pgsql

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 238 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-pgsql

not enough arguments in call to db.Find[git_model.Branch]

Check failure on line 238 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-mysql5

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 238 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-mysql5

not enough arguments in call to db.Find[git_model.Branch]
RepoID: 1,
})
assert.NoError(t, err)
assert.Len(t, branches, 5)

branches, err = db.Find[git_model.Branch](db.DefaultContext, git_model.FindBranchOptions{

Check failure on line 244 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 244 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / lint-backend

not enough arguments in call to db.Find[git_model.Branch]

Check failure on line 244 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 244 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / lint-go-windows

not enough arguments in call to db.Find[git_model.Branch]

Check failure on line 244 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 244 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / lint-go-gogit

not enough arguments in call to db.Find[git_model.Branch]

Check failure on line 244 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-mysql8

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 244 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-mysql8

not enough arguments in call to db.Find[git_model.Branch]

Check failure on line 244 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-sqlite

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 244 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-sqlite

not enough arguments in call to db.Find[git_model.Branch]

Check failure on line 244 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-mssql

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 244 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-mssql

not enough arguments in call to db.Find[git_model.Branch]

Check failure on line 244 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-pgsql

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 244 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-pgsql

not enough arguments in call to db.Find[git_model.Branch]

Check failure on line 244 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-mysql5

assignment mismatch: 2 variables but db.Find[git_model.Branch] returns 1 value

Check failure on line 244 in tests/integration/api_branch_test.go

View workflow job for this annotation

GitHub Actions / test-mysql5

not enough arguments in call to db.Find[git_model.Branch]
RepoID: 1,
Keyword: "new_branch",
})
assert.NoError(t, err)
assert.Len(t, branches, 1)
}
Loading