From c8f707b89071a5d75af2906cde92f849c5529649 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Mon, 10 Jul 2023 08:30:09 +0000 Subject: [PATCH 01/97] fix --- models/git/branch.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/git/branch.go b/models/git/branch.go index 97891f01ebb40..ca38130bdc4af 100644 --- a/models/git/branch.go +++ b/models/git/branch.go @@ -391,7 +391,7 @@ func FindRecentlyPushedNewBranches(ctx context.Context, repoID, userID int64) (B "issue.is_closed": false, }) err := db.GetEngine(ctx). - Where("pusher_id=? AND is_deleted=?", userID, false). + Where("repo_id=? AND pusher_id=? AND is_deleted=?", repoID, userID, false). And("updated_unix >= ?", time.Now().Add(-time.Hour*6).Unix()). NotIn("name", subQuery). OrderBy("branch.updated_unix DESC"). From b3e3b7311cef5921de84130476c5b3693631d283 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 11 Jul 2023 01:10:12 +0000 Subject: [PATCH 02/97] support show notification in origin repo --- models/git/branch.go | 26 ++++++++++++++++--- models/git/branch_list.go | 21 +++++++++++++++ routers/web/repo/view.go | 9 +++++-- .../code/recently_pushed_new_branches.tmpl | 11 ++++++-- 4 files changed, 59 insertions(+), 8 deletions(-) diff --git a/models/git/branch.go b/models/git/branch.go index ca38130bdc4af..87c54e026e4f6 100644 --- a/models/git/branch.go +++ b/models/git/branch.go @@ -102,8 +102,9 @@ func (err ErrBranchesEqual) Unwrap() error { // for pagination, keyword search and filtering type Branch struct { ID int64 - RepoID int64 `xorm:"UNIQUE(s)"` - Name string `xorm:"UNIQUE(s) NOT NULL"` // git's ref-name is case-sensitive internally, however, in some databases (mssql, mysql, by default), it's case-insensitive at the moment + RepoID int64 `xorm:"UNIQUE(s)"` + Repo *repo_model.Repository `xorm:"-"` + Name string `xorm:"UNIQUE(s) NOT NULL"` // git's ref-name is case-sensitive internally, however, in some databases (mssql, mysql, by default), it's case-insensitive at the moment CommitID string CommitMessage string `xorm:"TEXT"` // it only stores the message summary (the first line) PusherID int64 @@ -139,6 +140,16 @@ func (b *Branch) LoadPusher(ctx context.Context) (err error) { return err } +func (b *Branch) LoadRepo(ctx context.Context) (err error) { + if b.Repo == nil && b.RepoID > 0 { + b.Repo, err = repo_model.GetRepositoryByID(ctx, b.RepoID) + if repo_model.IsErrRepoNotExist(err) { + err = nil + } + } + return err +} + func init() { db.RegisterModel(new(Branch)) db.RegisterModel(new(RenamedBranch)) @@ -382,8 +393,14 @@ 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 -func FindRecentlyPushedNewBranches(ctx context.Context, repoID, userID int64) (BranchList, error) { +func FindRecentlyPushedNewBranches(ctx context.Context, repoID, userID int64, latestCommitID string) (BranchList, error) { branches := make(BranchList, 0, 2) + repoCond := builder.Select("id").From("repository"). + Where(builder.Or( + builder.Eq{"id": repoID, "is_fork": false}, + builder.Eq{"is_fork": true, "fork_id": repoID}, + )) + subQuery := builder.Select("head_branch").From("pull_request"). InnerJoin("issue", "issue.id = pull_request.issue_id"). Where(builder.Eq{ @@ -391,9 +408,10 @@ func FindRecentlyPushedNewBranches(ctx context.Context, repoID, userID int64) (B "issue.is_closed": false, }) err := db.GetEngine(ctx). - Where("repo_id=? AND pusher_id=? AND is_deleted=?", repoID, userID, false). + Where("commit_id != ? AND pusher_id = ? AND is_deleted = ?", latestCommitID, userID, false). And("updated_unix >= ?", time.Now().Add(-time.Hour*6).Unix()). NotIn("name", subQuery). + In("repo_id", repoCond). OrderBy("branch.updated_unix DESC"). Limit(2). Find(&branches) diff --git a/models/git/branch_list.go b/models/git/branch_list.go index 131a149782e27..a87e93c05aa07 100644 --- a/models/git/branch_list.go +++ b/models/git/branch_list.go @@ -7,6 +7,7 @@ import ( "context" "code.gitea.io/gitea/models/db" + 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/util" @@ -64,6 +65,26 @@ func (branches BranchList) LoadPusher(ctx context.Context) error { return nil } +func (branches BranchList) LoadRepo(ctx context.Context) error { + ids := container.Set[int64]{} + for _, branch := range branches { + if branch.RepoID > 0 { + ids.Add(branch.RepoID) + } + } + reposMap := make(map[int64]*repo_model.Repository, len(ids)) + if err := db.GetEngine(ctx).In("id", ids.Values()).Find(&reposMap); err != nil { + return err + } + for _, branch := range branches { + if branch.RepoID <= 0 { + continue + } + branch.Repo = reposMap[branch.RepoID] + } + return nil +} + type FindBranchOptions struct { db.ListOptions RepoID int64 diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index acea08d6297ec..852cf8cc76a68 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -982,11 +982,16 @@ func renderCode(ctx *context.Context) { ctx.ServerError("GetBaseRepo", err) return } - ctx.Data["RecentlyPushedNewBranches"], err = git_model.FindRecentlyPushedNewBranches(ctx, ctx.Repo.Repository.ID, ctx.Doer.ID) + branches, err := git_model.FindRecentlyPushedNewBranches(ctx, ctx.Repo.Repository.ID, ctx.Doer.ID, ctx.Repo.CommitID) if err != nil { - ctx.ServerError("GetRecentlyPushedBranches", err) + ctx.ServerError("FindRecentlyPushedNewBranches", err) return } + if err := branches.LoadRepo(ctx); err != nil { + ctx.ServerError("branches.LoadRepo", err) + return + } + ctx.Data["RecentlyPushedNewBranches"] = branches } var treeNames []string diff --git a/templates/repo/code/recently_pushed_new_branches.tmpl b/templates/repo/code/recently_pushed_new_branches.tmpl index e936fa4bb462b..0a3f3f8965db0 100644 --- a/templates/repo/code/recently_pushed_new_branches.tmpl +++ b/templates/repo/code/recently_pushed_new_branches.tmpl @@ -1,10 +1,17 @@ {{range .RecentlyPushedNewBranches}} + {{$pullRepo := $.Repository}} + {{$branchName := .Name}} + {{if .Repo}} + {{$pullRepo = .Repo}} + {{$branchName = (print $pullRepo.FullName ":" .Name)}} + {{end}}
From df97530659c8e1f789c02c105316eb5632582853 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 11 Jul 2023 01:39:58 +0000 Subject: [PATCH 03/97] support show notification in forked repo --- routers/web/repo/view.go | 18 +++++++++++++++++- .../code/recently_pushed_new_branches.tmpl | 10 ++++++++-- 2 files changed, 25 insertions(+), 3 deletions(-) diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 852cf8cc76a68..7c649ea369fb8 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -982,7 +982,23 @@ func renderCode(ctx *context.Context) { ctx.ServerError("GetBaseRepo", err) return } - branches, err := git_model.FindRecentlyPushedNewBranches(ctx, ctx.Repo.Repository.ID, ctx.Doer.ID, ctx.Repo.CommitID) + baseRepo := ctx.Repo.Repository + latestCommit := ctx.Repo.Commit + if ctx.Repo.Repository.IsFork { + baseRepo = ctx.Repo.Repository.BaseRepo + repoPath := repo_model.RepoPath(baseRepo.OwnerName, baseRepo.Name) + gitRepo, err := git.OpenRepository(ctx, repoPath) + if err != nil { + ctx.ServerError("OpenRepository", err) + return + } + latestCommit, err = gitRepo.GetBranchCommit(baseRepo.DefaultBranch) + if err != nil { + ctx.ServerError("GetBranchCommit", err) + return + } + } + branches, err := git_model.FindRecentlyPushedNewBranches(ctx, baseRepo.ID, ctx.Doer.ID, latestCommit.ID.String()) if err != nil { ctx.ServerError("FindRecentlyPushedNewBranches", err) return diff --git a/templates/repo/code/recently_pushed_new_branches.tmpl b/templates/repo/code/recently_pushed_new_branches.tmpl index 0a3f3f8965db0..953063c06d17e 100644 --- a/templates/repo/code/recently_pushed_new_branches.tmpl +++ b/templates/repo/code/recently_pushed_new_branches.tmpl @@ -1,9 +1,15 @@ {{range .RecentlyPushedNewBranches}} {{$pullRepo := $.Repository}} + {{$baseRepo := $.Repository}} {{$branchName := .Name}} {{if .Repo}} {{$pullRepo = .Repo}} - {{$branchName = (print $pullRepo.FullName ":" .Name)}} + {{if and $.Repository.IsFork}} + {{$baseRepo = $.Repository.BaseRepo}} + {{end}} + {{if not (eq .Repo.ID $.Repository.ID)}} + {{$branchName = (print .Repo.FullName ":" .Name)}} + {{end}} {{end}} From 71dd7404160ed117fec0eb48f3a14e40d4850742 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 11 Jul 2023 02:35:36 +0000 Subject: [PATCH 04/97] avoid attach already created pr branch --- models/git/branch.go | 16 +++++++++------- routers/web/repo/view.go | 16 +++++++++------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/models/git/branch.go b/models/git/branch.go index 87c54e026e4f6..d84c8cd5a7447 100644 --- a/models/git/branch.go +++ b/models/git/branch.go @@ -395,22 +395,24 @@ 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 func FindRecentlyPushedNewBranches(ctx context.Context, repoID, userID int64, latestCommitID string) (BranchList, error) { branches := make(BranchList, 0, 2) + // search all related repos repoCond := builder.Select("id").From("repository"). Where(builder.Or( builder.Eq{"id": repoID, "is_fork": false}, builder.Eq{"is_fork": true, "fork_id": repoID}, )) - - subQuery := builder.Select("head_branch").From("pull_request"). + // avoid check branches which have already created PRs + usedBranchIDs := builder.Select("branch.id").From("branch"). + InnerJoin("pull_request", "branch.name = pull_request.head_branch AND branch.repo_id = pull_request.head_repo_id"). InnerJoin("issue", "issue.id = pull_request.issue_id"). - Where(builder.Eq{ - "pull_request.head_repo_id": repoID, - "issue.is_closed": false, - }) + Where(builder.And( + builder.Eq{"issue.is_closed": false}, + builder.In("pull_request.head_repo_id", repoCond), + )) err := db.GetEngine(ctx). Where("commit_id != ? AND pusher_id = ? AND is_deleted = ?", latestCommitID, userID, false). And("updated_unix >= ?", time.Now().Add(-time.Hour*6).Unix()). - NotIn("name", subQuery). + NotIn("id", usedBranchIDs). In("repo_id", repoCond). OrderBy("branch.updated_unix DESC"). Limit(2). diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index 7c649ea369fb8..eb923a2a5b4ec 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -982,21 +982,23 @@ func renderCode(ctx *context.Context) { ctx.ServerError("GetBaseRepo", err) return } + + var latestCommit *git.Commit baseRepo := ctx.Repo.Repository - latestCommit := ctx.Repo.Commit + gitRepo := ctx.Repo.GitRepo if ctx.Repo.Repository.IsFork { baseRepo = ctx.Repo.Repository.BaseRepo repoPath := repo_model.RepoPath(baseRepo.OwnerName, baseRepo.Name) - gitRepo, err := git.OpenRepository(ctx, repoPath) + gitRepo, err = git.OpenRepository(ctx, repoPath) if err != nil { ctx.ServerError("OpenRepository", err) return } - latestCommit, err = gitRepo.GetBranchCommit(baseRepo.DefaultBranch) - if err != nil { - ctx.ServerError("GetBranchCommit", err) - return - } + } + latestCommit, err = gitRepo.GetBranchCommit(baseRepo.DefaultBranch) + if err != nil { + ctx.ServerError("GetBranchCommit", err) + return } branches, err := git_model.FindRecentlyPushedNewBranches(ctx, baseRepo.ID, ctx.Doer.ID, latestCommit.ID.String()) if err != nil { From 2b0abf933184effda634698d1a19bd6c2a849696 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 11 Jul 2023 04:18:36 +0000 Subject: [PATCH 05/97] use branch.id instead of branch.name --- models/git/branch.go | 19 ++++++++++++------- routers/web/repo/view.go | 15 +-------------- 2 files changed, 13 insertions(+), 21 deletions(-) diff --git a/models/git/branch.go b/models/git/branch.go index d84c8cd5a7447..56653f01eac32 100644 --- a/models/git/branch.go +++ b/models/git/branch.go @@ -393,26 +393,31 @@ 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 -func FindRecentlyPushedNewBranches(ctx context.Context, repoID, userID int64, latestCommitID string) (BranchList, error) { +func FindRecentlyPushedNewBranches(ctx context.Context, baseRepo *repo_model.Repository, userID int64) (BranchList, error) { branches := make(BranchList, 0, 2) + baseBranch, err := GetBranch(ctx, baseRepo.ID, baseRepo.DefaultBranch) + if err != nil { + return nil, err + } // search all related repos repoCond := builder.Select("id").From("repository"). Where(builder.Or( - builder.Eq{"id": repoID, "is_fork": false}, - builder.Eq{"is_fork": true, "fork_id": repoID}, + builder.Eq{"id": baseRepo.ID, "is_fork": false}, + builder.Eq{"is_fork": true, "fork_id": baseRepo.ID}, )) // avoid check branches which have already created PRs - usedBranchIDs := builder.Select("branch.id").From("branch"). + // TODO add head_branch_id in pull_request table + invalidBranchCond := builder.Select("branch.id").From("branch"). InnerJoin("pull_request", "branch.name = pull_request.head_branch AND branch.repo_id = pull_request.head_repo_id"). InnerJoin("issue", "issue.id = pull_request.issue_id"). Where(builder.And( builder.Eq{"issue.is_closed": false}, builder.In("pull_request.head_repo_id", repoCond), )) - err := db.GetEngine(ctx). - Where("commit_id != ? AND pusher_id = ? AND is_deleted = ?", latestCommitID, userID, false). + err = db.GetEngine(ctx). + Where("id != ? AND commit_id != ? AND pusher_id = ? AND is_deleted = ?", baseBranch.ID, baseBranch.CommitID, userID, false). And("updated_unix >= ?", time.Now().Add(-time.Hour*6).Unix()). - NotIn("id", usedBranchIDs). + NotIn("id", invalidBranchCond). In("repo_id", repoCond). OrderBy("branch.updated_unix DESC"). Limit(2). diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index eb923a2a5b4ec..a75f26795a05f 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -983,24 +983,11 @@ func renderCode(ctx *context.Context) { return } - var latestCommit *git.Commit baseRepo := ctx.Repo.Repository - gitRepo := ctx.Repo.GitRepo if ctx.Repo.Repository.IsFork { baseRepo = ctx.Repo.Repository.BaseRepo - repoPath := repo_model.RepoPath(baseRepo.OwnerName, baseRepo.Name) - gitRepo, err = git.OpenRepository(ctx, repoPath) - if err != nil { - ctx.ServerError("OpenRepository", err) - return - } - } - latestCommit, err = gitRepo.GetBranchCommit(baseRepo.DefaultBranch) - if err != nil { - ctx.ServerError("GetBranchCommit", err) - return } - branches, err := git_model.FindRecentlyPushedNewBranches(ctx, baseRepo.ID, ctx.Doer.ID, latestCommit.ID.String()) + branches, err := git_model.FindRecentlyPushedNewBranches(ctx, baseRepo, ctx.Doer.ID) if err != nil { ctx.ServerError("FindRecentlyPushedNewBranches", err) return From 4c116821fa373898c1da64a02391eaf46f1ad3ff Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 11 Jul 2023 04:25:41 +0000 Subject: [PATCH 06/97] allow forked repo from forked repo --- models/git/branch.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/models/git/branch.go b/models/git/branch.go index 56653f01eac32..89913ff5a8be4 100644 --- a/models/git/branch.go +++ b/models/git/branch.go @@ -402,7 +402,7 @@ func FindRecentlyPushedNewBranches(ctx context.Context, baseRepo *repo_model.Rep // search all related repos repoCond := builder.Select("id").From("repository"). Where(builder.Or( - builder.Eq{"id": baseRepo.ID, "is_fork": false}, + builder.Eq{"id": baseRepo.ID}, builder.Eq{"is_fork": true, "fork_id": baseRepo.ID}, )) // avoid check branches which have already created PRs From 54f4fd56cdb28ad14bd91b1b4a3d9f23a54bf2dc Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 11 Jul 2023 05:02:20 +0000 Subject: [PATCH 07/97] improve TODO --- models/git/branch.go | 8 +++++--- routers/web/repo/view.go | 2 +- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/models/git/branch.go b/models/git/branch.go index 89913ff5a8be4..711f5768e3b26 100644 --- a/models/git/branch.go +++ b/models/git/branch.go @@ -393,7 +393,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 -func FindRecentlyPushedNewBranches(ctx context.Context, baseRepo *repo_model.Repository, userID int64) (BranchList, error) { +// doer should not be nil +// TODO use options to find the branches +func FindRecentlyPushedNewBranches(ctx context.Context, baseRepo *repo_model.Repository, doer *user_model.User) (BranchList, error) { branches := make(BranchList, 0, 2) baseBranch, err := GetBranch(ctx, baseRepo.ID, baseRepo.DefaultBranch) if err != nil { @@ -406,7 +408,7 @@ func FindRecentlyPushedNewBranches(ctx context.Context, baseRepo *repo_model.Rep builder.Eq{"is_fork": true, "fork_id": baseRepo.ID}, )) // avoid check branches which have already created PRs - // TODO add head_branch_id in pull_request table + // TODO add head_branch_id in pull_request table then we can get the branch id from pull_request table directly invalidBranchCond := builder.Select("branch.id").From("branch"). InnerJoin("pull_request", "branch.name = pull_request.head_branch AND branch.repo_id = pull_request.head_repo_id"). InnerJoin("issue", "issue.id = pull_request.issue_id"). @@ -415,7 +417,7 @@ func FindRecentlyPushedNewBranches(ctx context.Context, baseRepo *repo_model.Rep builder.In("pull_request.head_repo_id", repoCond), )) err = db.GetEngine(ctx). - Where("id != ? AND commit_id != ? AND pusher_id = ? AND is_deleted = ?", baseBranch.ID, baseBranch.CommitID, userID, false). + Where("id != ? AND commit_id != ? AND pusher_id = ? AND is_deleted = ?", baseBranch.ID, baseBranch.CommitID, doer.ID, false). And("updated_unix >= ?", time.Now().Add(-time.Hour*6).Unix()). NotIn("id", invalidBranchCond). In("repo_id", repoCond). diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index a75f26795a05f..e844dcf82b76e 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -987,7 +987,7 @@ func renderCode(ctx *context.Context) { if ctx.Repo.Repository.IsFork { baseRepo = ctx.Repo.Repository.BaseRepo } - branches, err := git_model.FindRecentlyPushedNewBranches(ctx, baseRepo, ctx.Doer.ID) + branches, err := git_model.FindRecentlyPushedNewBranches(ctx, baseRepo, ctx.Doer) if err != nil { ctx.ServerError("FindRecentlyPushedNewBranches", err) return From 172ceebf14cbe4a9a3ef857ce8c503255fa05047 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Tue, 11 Jul 2023 05:04:24 +0000 Subject: [PATCH 08/97] fix lint --- templates/repo/code/recently_pushed_new_branches.tmpl | 1 - 1 file changed, 1 deletion(-) diff --git a/templates/repo/code/recently_pushed_new_branches.tmpl b/templates/repo/code/recently_pushed_new_branches.tmpl index 953063c06d17e..339c40fca382e 100644 --- a/templates/repo/code/recently_pushed_new_branches.tmpl +++ b/templates/repo/code/recently_pushed_new_branches.tmpl @@ -16,7 +16,6 @@ {{$timeSince := TimeSince .UpdatedUnix.AsTime $.locale}} {{$.locale.Tr "repo.pulls.recently_pushed_new_branches" (PathEscapeSegments $branchName) $timeSince | Safe}} - {{$.locale.Tr "repo.pulls.compare_changes"}} From bc176bb1bc3b61494ef1564c0c94475e74d296a5 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Thu, 13 Jul 2023 06:20:33 +0000 Subject: [PATCH 09/97] improve --- models/git/branch.go | 22 ++++++++++++---------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/models/git/branch.go b/models/git/branch.go index 711f5768e3b26..59c1f48b48e31 100644 --- a/models/git/branch.go +++ b/models/git/branch.go @@ -412,17 +412,19 @@ func FindRecentlyPushedNewBranches(ctx context.Context, baseRepo *repo_model.Rep invalidBranchCond := builder.Select("branch.id").From("branch"). InnerJoin("pull_request", "branch.name = pull_request.head_branch AND branch.repo_id = pull_request.head_repo_id"). InnerJoin("issue", "issue.id = pull_request.issue_id"). - Where(builder.And( - builder.Eq{"issue.is_closed": false}, + Where(builder.Or( + builder.Eq{"pull_request.has_merged": true}, builder.In("pull_request.head_repo_id", repoCond), + builder.Eq{"branch.id": baseBranch.ID}, )) - err = db.GetEngine(ctx). - Where("id != ? AND commit_id != ? AND pusher_id = ? AND is_deleted = ?", baseBranch.ID, baseBranch.CommitID, doer.ID, false). - And("updated_unix >= ?", time.Now().Add(-time.Hour*6).Unix()). - NotIn("id", invalidBranchCond). - In("repo_id", repoCond). - OrderBy("branch.updated_unix DESC"). - Limit(2). - Find(&branches) + cond := builder.And( + builder.Neq{"commit_id": baseBranch.CommitID}, // newly created branch have no changes, so skip them + builder.Eq{"pusher_id": doer.ID}, + builder.Eq{"is_deleted": false}, + builder.Gte{"updated_unix": time.Now().Add(-time.Hour * 6).Unix()}, + builder.In("repo_id", repoCond), + builder.NotIn("id", invalidBranchCond), + ) + err = db.GetEngine(ctx).Where(cond).OrderBy("branch.updated_unix DESC").Limit(2).Find(&branches) return branches, err } From d3f72aac2fdb9f9c7f871d395032319e32443fe2 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Thu, 13 Jul 2023 07:52:58 +0000 Subject: [PATCH 10/97] remove PathEscapeSegments --- templates/repo/code/recently_pushed_new_branches.tmpl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/templates/repo/code/recently_pushed_new_branches.tmpl b/templates/repo/code/recently_pushed_new_branches.tmpl index 339c40fca382e..177dc5ee8e141 100644 --- a/templates/repo/code/recently_pushed_new_branches.tmpl +++ b/templates/repo/code/recently_pushed_new_branches.tmpl @@ -14,9 +14,9 @@ From d93b2bcc80dfe191a6e70c29ffec903bbf7c2362 Mon Sep 17 00:00:00 2001 From: yp05327 <576951401@qq.com> Date: Thu, 20 Jul 2023 05:19:56 +0000 Subject: [PATCH 11/97] use findbranch to search branch --- models/git/branch.go | 23 ++++++++--------- models/git/branch_list.go | 38 ++++++++++++++++++++++++++--- models/git/protected_branch_list.go | 2 +- modules/context/repo.go | 2 +- modules/repository/branch.go | 2 +- routers/api/v1/repo/branch.go | 4 +-- routers/web/repo/compare.go | 4 +-- routers/web/repo/issue.go | 2 +- services/repository/adopt.go | 2 +- services/repository/branch.go | 2 +- 10 files changed, 57 insertions(+), 24 deletions(-) diff --git a/models/git/branch.go b/models/git/branch.go index 59c1f48b48e31..c0f95e7a89e06 100644 --- a/models/git/branch.go +++ b/models/git/branch.go @@ -396,7 +396,6 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to str // doer should not be nil // TODO use options to find the branches func FindRecentlyPushedNewBranches(ctx context.Context, baseRepo *repo_model.Repository, doer *user_model.User) (BranchList, error) { - branches := make(BranchList, 0, 2) baseBranch, err := GetBranch(ctx, baseRepo.ID, baseRepo.DefaultBranch) if err != nil { return nil, err @@ -417,14 +416,16 @@ func FindRecentlyPushedNewBranches(ctx context.Context, baseRepo *repo_model.Rep builder.In("pull_request.head_repo_id", repoCond), builder.Eq{"branch.id": baseBranch.ID}, )) - cond := builder.And( - builder.Neq{"commit_id": baseBranch.CommitID}, // newly created branch have no changes, so skip them - builder.Eq{"pusher_id": doer.ID}, - builder.Eq{"is_deleted": false}, - builder.Gte{"updated_unix": time.Now().Add(-time.Hour * 6).Unix()}, - builder.In("repo_id", repoCond), - builder.NotIn("id", invalidBranchCond), - ) - err = db.GetEngine(ctx).Where(cond).OrderBy("branch.updated_unix DESC").Limit(2).Find(&branches) - return branches, err + + 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, + OrderBy: "branch.updated_unix DESC", + } + opts.PageSize = 2 + opts.Page = 1 + return FindBranches(ctx, opts) } diff --git a/models/git/branch_list.go b/models/git/branch_list.go index a87e93c05aa07..b089405a3a54c 100644 --- a/models/git/branch_list.go +++ b/models/git/branch_list.go @@ -10,6 +10,7 @@ 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" @@ -87,24 +88,55 @@ func (branches BranchList) LoadRepo(ctx context.Context) error { type FindBranchOptions struct { db.ListOptions - RepoID int64 + IDCond builder.Cond + RepoIDs []int64 // overwrites RepoCond if the length is not 0 + RepoCond builder.Cond ExcludeBranchNames []string + CommitCond builder.Cond + PusherID int64 IsDeletedBranch util.OptionalBool + UpdatedAfterUnix timeutil.TimeStamp + UpdatedBeforeUnix timeutil.TimeStamp OrderBy string } func (opts *FindBranchOptions) Cond() builder.Cond { cond := builder.NewCond() - if opts.RepoID > 0 { - cond = cond.And(builder.Eq{"repo_id": opts.RepoID}) + if opts.IDCond != nil { + cond = cond.And(opts.IDCond) + } + + if len(opts.RepoIDs) == 1 { + opts.RepoCond = builder.Eq{"issue.repo_id": opts.RepoIDs[0]} + } else if len(opts.RepoIDs) > 1 { + opts.RepoCond = builder.In("issue.repo_id", opts.RepoIDs) + } + if opts.RepoCond != nil { + cond = cond.And(opts.RepoCond) } if len(opts.ExcludeBranchNames) > 0 { cond = cond.And(builder.NotIn("name", opts.ExcludeBranchNames)) } + + if opts.CommitCond != nil { + cond = cond.And(opts.CommitCond) + } + + if opts.PusherID > 0 { + cond = cond.And(builder.Eq{"pusher_id": opts.PusherID}) + } + if !opts.IsDeletedBranch.IsNone() { 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.UpdatedBeforeUnix != 0 { + cond = cond.And(builder.Lte{"updated_unix": opts.UpdatedBeforeUnix}) + } return cond } diff --git a/models/git/protected_branch_list.go b/models/git/protected_branch_list.go index eeb307e2454e7..f67bb14d173db 100644 --- a/models/git/protected_branch_list.go +++ b/models/git/protected_branch_list.go @@ -55,7 +55,7 @@ func FindAllMatchedBranches(ctx context.Context, repoID int64, ruleName string) PageSize: 100, Page: page, }, - RepoID: repoID, + RepoIDs: []int64{repoID}, IsDeletedBranch: util.OptionalBoolFalse, }) if err != nil { diff --git a/modules/context/repo.go b/modules/context/repo.go index eae71cfb7be67..6eb08bef58847 100644 --- a/modules/context/repo.go +++ b/modules/context/repo.go @@ -668,7 +668,7 @@ func RepoAssignment(ctx *Context) context.CancelFunc { ctx.Data["Tags"] = tags branchOpts := git_model.FindBranchOptions{ - RepoID: ctx.Repo.Repository.ID, + RepoIDs: []int64{ctx.Repo.Repository.ID}, IsDeletedBranch: util.OptionalBoolFalse, ListOptions: db.ListOptions{ ListAll: true, diff --git a/modules/repository/branch.go b/modules/repository/branch.go index bffadd62f4d92..2c45dbb7f2707 100644 --- a/modules/repository/branch.go +++ b/modules/repository/branch.go @@ -53,7 +53,7 @@ func SyncRepoBranchesWithRepo(ctx context.Context, repo *repo_model.Repository, ListOptions: db.ListOptions{ ListAll: true, }, - RepoID: repo.ID, + RepoIDs: []int64{repo.ID}, }) if err != nil { return 0, err diff --git a/routers/api/v1/repo/branch.go b/routers/api/v1/repo/branch.go index 5e2c9878f0884..62c776403850a 100644 --- a/routers/api/v1/repo/branch.go +++ b/routers/api/v1/repo/branch.go @@ -142,7 +142,7 @@ func DeleteBranch(ctx *context.APIContext) { // check whether branches of this repository has been synced totalNumOfBranches, err := git_model.CountBranches(ctx, git_model.FindBranchOptions{ - RepoID: ctx.Repo.Repository.ID, + RepoIDs: []int64{ctx.Repo.Repository.ID}, IsDeletedBranch: util.OptionalBoolFalse, }) if err != nil { @@ -349,7 +349,7 @@ func ListBranches(ctx *context.APIContext) { branchOpts := git_model.FindBranchOptions{ ListOptions: listOptions, - RepoID: ctx.Repo.Repository.ID, + RepoIDs: []int64{ctx.Repo.Repository.ID}, IsDeletedBranch: util.OptionalBoolFalse, } var err error diff --git a/routers/web/repo/compare.go b/routers/web/repo/compare.go index 7089c219ad25c..68ccfcb1a5aa2 100644 --- a/routers/web/repo/compare.go +++ b/routers/web/repo/compare.go @@ -685,7 +685,7 @@ func getBranchesAndTagsForRepo(ctx gocontext.Context, repo *repo_model.Repositor defer gitRepo.Close() branches, err = git_model.FindBranchNames(ctx, git_model.FindBranchOptions{ - RepoID: repo.ID, + RepoIDs: []int64{repo.ID}, ListOptions: db.ListOptions{ ListAll: true, }, @@ -742,7 +742,7 @@ func CompareDiff(ctx *context.Context) { } headBranches, err := git_model.FindBranchNames(ctx, git_model.FindBranchOptions{ - RepoID: ci.HeadRepo.ID, + RepoIDs: []int64{ci.HeadRepo.ID}, ListOptions: db.ListOptions{ ListAll: true, }, diff --git a/routers/web/repo/issue.go b/routers/web/repo/issue.go index bd8959846c0ed..603f5a3fad892 100644 --- a/routers/web/repo/issue.go +++ b/routers/web/repo/issue.go @@ -786,7 +786,7 @@ func RetrieveRepoMetas(ctx *context.Context, repo *repo_model.Repository, isPull } brs, err := git_model.FindBranchNames(ctx, git_model.FindBranchOptions{ - RepoID: ctx.Repo.Repository.ID, + RepoIDs: []int64{ctx.Repo.Repository.ID}, ListOptions: db.ListOptions{ ListAll: true, }, diff --git a/services/repository/adopt.go b/services/repository/adopt.go index f95fb5988f66b..450c0371fc13a 100644 --- a/services/repository/adopt.go +++ b/services/repository/adopt.go @@ -149,7 +149,7 @@ func adoptRepository(ctx context.Context, repoPath string, u *user_model.User, r } branches, _ := git_model.FindBranchNames(ctx, git_model.FindBranchOptions{ - RepoID: repo.ID, + RepoIDs: []int64{repo.ID}, ListOptions: db.ListOptions{ ListAll: true, }, diff --git a/services/repository/branch.go b/services/repository/branch.go index 11a8b20531578..3dd42361291b9 100644 --- a/services/repository/branch.go +++ b/services/repository/branch.go @@ -73,7 +73,7 @@ func LoadBranches(ctx context.Context, repo *repo_model.Repository, gitRepo *git } branchOpts := git_model.FindBranchOptions{ - RepoID: repo.ID, + RepoIDs: []int64{repo.ID}, IsDeletedBranch: isDeletedBranch, ListOptions: db.ListOptions{ Page: page, 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 12/97] 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 93003049c67fb..8233698cc0030 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 c0f95e7a89e06..3b2f75954d90f 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 b089405a3a54c..291400379067f 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 ba6902692792e..75251864994bb 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-fAY