From 5c3f7aa231228baed2fa44b882ef7e3f1c19433d Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Thu, 20 Jul 2023 08:53:31 +0000 Subject: [PATCH] add test 1 --- models/fixtures/branch.yml | 24 ++++++++++++++++ models/git/branch.go | 7 ++++- models/git/branch_list.go | 13 ++++----- models/git/branch_test.go | 26 +++++++++++++++++- .../2a/e4cd61145dbece4166c20a7f683e41d5ee5a8b | Bin 0 -> 55 bytes .../96/cac303b24be71685d77202689de5a751b972d3 | Bin 0 -> 54 bytes .../cb/24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d | Bin 0 -> 148 bytes .../refs/heads/new-branch-no-notification | 1 + .../refs/heads/new-branch-notification | 1 + 9 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 tests/gitea-repositories-meta/user2/repo1.git/objects/2a/e4cd61145dbece4166c20a7f683e41d5ee5a8b create mode 100644 tests/gitea-repositories-meta/user2/repo1.git/objects/96/cac303b24be71685d77202689de5a751b972d3 create mode 100644 tests/gitea-repositories-meta/user2/repo1.git/objects/cb/24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d create mode 100644 tests/gitea-repositories-meta/user2/repo1.git/refs/heads/new-branch-no-notification create mode 100644 tests/gitea-repositories-meta/user2/repo1.git/refs/heads/new-branch-notification diff --git a/models/fixtures/branch.yml b/models/fixtures/branch.yml index 93003049c6..8233698cc0 100644 --- a/models/fixtures/branch.yml +++ b/models/fixtures/branch.yml @@ -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 diff --git a/models/git/branch.go b/models/git/branch.go index c0f95e7a89..3b2f75954d 100644 --- a/models/git/branch.go +++ b/models/git/branch.go @@ -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 diff --git a/models/git/branch_list.go b/models/git/branch_list.go index b089405a3a..2914003790 100644 --- a/models/git/branch_list.go +++ b/models/git/branch_list.go @@ -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 } diff --git a/models/git/branch_test.go b/models/git/branch_test.go index ba69026927..7525186499 100644 --- a/models/git/branch_test.go +++ b/models/git/branch_test.go @@ -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 +} diff --git a/tests/gitea-repositories-meta/user2/repo1.git/objects/2a/e4cd61145dbece4166c20a7f683e41d5ee5a8b b/tests/gitea-repositories-meta/user2/repo1.git/objects/2a/e4cd61145dbece4166c20a7f683e41d5ee5a8b new file mode 100644 index 0000000000000000000000000000000000000000..f3271266444f56b0b43aa0ff66b009188f4c0421 GIT binary patch literal 55 zcmV-70LcG%0ZYosPf{?kU{F>lN-fAYZaql`QKEzwcVg K%wW2O|04hjX%!Rz literal 0 HcmV?d00001 diff --git a/tests/gitea-repositories-meta/user2/repo1.git/objects/cb/24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d b/tests/gitea-repositories-meta/user2/repo1.git/objects/cb/24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d new file mode 100644 index 0000000000000000000000000000000000000000..5f318261228bae266b08c7819b1fe880ca9603b5 GIT binary patch literal 148 zcmV;F0Biqv0hNwB4gxU@1*vn2^hn70*iNJs7omz1M^G&LQ7E`QxdQFejONvCU0=g` zh?jAy+Dx_706}~dWk(4cvj