add test 1

This commit is contained in:
yp05327 2023-07-20 08:53:31 +00:00
parent d93b2bcc80
commit 5c3f7aa231
9 changed files with 63 additions and 9 deletions

View File

@ -45,3 +45,27 @@
is_deleted: false
deleted_by_id: 0
deleted_unix: 0
-
id: 5
repo_id: 1
name: 'new-branch-notification'
commit_id: 'cb24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d'
commit_message: 'add'
commit_time: 1689838761
pusher_id: 1
is_deleted: false
deleted_by_id: 0
deleted_unix: 0
-
id: 6
repo_id: 1
name: 'new-branch-no-notification'
commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d'
commit_message: 'Initial commit'
commit_time: 1489927679
pusher_id: 1
is_deleted: false
deleted_by_id: 0
deleted_unix: 0

View File

@ -394,8 +394,9 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to str
// FindRecentlyPushedNewBranches return at most 2 new branches pushed by the user in 6 hours which has no opened PRs created
// doer should not be nil
// if commitAfterUnix is 0, will find the branches commited in recently 6 hours
// TODO use options to find the branches
func FindRecentlyPushedNewBranches(ctx context.Context, baseRepo *repo_model.Repository, doer *user_model.User) (BranchList, error) {
func FindRecentlyPushedNewBranches(ctx context.Context, baseRepo *repo_model.Repository, doer *user_model.User, commitAfterUnix int64) (BranchList, error) {
baseBranch, err := GetBranch(ctx, baseRepo.ID, baseRepo.DefaultBranch)
if err != nil {
return nil, err
@ -417,12 +418,16 @@ func FindRecentlyPushedNewBranches(ctx context.Context, baseRepo *repo_model.Rep
builder.Eq{"branch.id": baseBranch.ID},
))
if commitAfterUnix == 0 {
commitAfterUnix = time.Now().Add(-time.Hour * 6).Unix()
}
opts := FindBranchOptions{
IDCond: builder.NotIn("id", invalidBranchCond),
RepoCond: builder.In("repo_id", repoCond),
CommitCond: builder.Neq{"commit_id": baseBranch.CommitID}, // newly created branch have no changes, so skip them,
PusherID: doer.ID,
IsDeletedBranch: util.OptionalBoolFalse,
CommitAfterUnix: commitAfterUnix,
OrderBy: "branch.updated_unix DESC",
}
opts.PageSize = 2

View File

@ -10,7 +10,6 @@ import (
repo_model "code.gitea.io/gitea/models/repo"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/container"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"
"xorm.io/builder"
@ -95,8 +94,8 @@ type FindBranchOptions struct {
CommitCond builder.Cond
PusherID int64
IsDeletedBranch util.OptionalBool
UpdatedAfterUnix timeutil.TimeStamp
UpdatedBeforeUnix timeutil.TimeStamp
CommitAfterUnix int64
CommitBeforeUnix int64
OrderBy string
}
@ -131,11 +130,11 @@ func (opts *FindBranchOptions) Cond() builder.Cond {
cond = cond.And(builder.Eq{"is_deleted": opts.IsDeletedBranch.IsTrue()})
}
if opts.UpdatedAfterUnix != 0 {
cond = cond.And(builder.Gte{"updated_unix": opts.UpdatedAfterUnix})
if opts.CommitAfterUnix != 0 {
cond = cond.And(builder.Gte{"commit_time": opts.CommitAfterUnix})
}
if opts.UpdatedBeforeUnix != 0 {
cond = cond.And(builder.Lte{"updated_unix": opts.UpdatedBeforeUnix})
if opts.CommitBeforeUnix != 0 {
cond = cond.And(builder.Lte{"commit_time": opts.CommitBeforeUnix})
}
return cond
}

View File

@ -11,6 +11,7 @@ import (
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/util"
@ -49,7 +50,7 @@ func TestGetDeletedBranches(t *testing.T) {
ListOptions: db.ListOptions{
ListAll: true,
},
RepoID: repo.ID,
RepoIDs: []int64{repo.ID},
IsDeletedBranch: util.OptionalBoolTrue,
})
assert.NoError(t, err)
@ -183,3 +184,26 @@ func TestOnlyGetDeletedBranchOnCorrectRepo(t *testing.T) {
assert.NoError(t, err)
assert.NotNil(t, deletedBranch)
}
func TestFindRecentlyPushedNewBranches(t *testing.T) {
assert.NoError(t, unittest.PrepareTestDatabase())
repo := unittest.AssertExistsAndLoadBean(t, &repo_model.Repository{ID: 1})
user1 := unittest.AssertExistsAndLoadBean(t, &user_model.User{ID: 1})
// test new branch of the repo
branches, err := git_model.FindRecentlyPushedNewBranches(db.DefaultContext, repo, user1, 1689838760)
assert.NoError(t, err)
assert.Equal(t, 1, len(branches))
assert.Equal(t, "new-branch-notification", branches[0].Name)
// TODO: test new branch from user fork repo
// TODO: test new branch from org fork repo
// TODO: test new branch from private user repo
// TODO: test new branch from private org with code permisstion repo
// TODO: test new branch from private org with no code permisstion repo
}

View File

@ -0,0 +1 @@
65f1bf27bc3bf70f64657658635e66094edbcb4d

View File

@ -0,0 +1 @@
cb24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d