From 7bdeccd02e006223da7cca660fc6f1fbb3f617eb Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 10 Jul 2023 08:45:32 +0800 Subject: [PATCH 1/2] Exclude default branch from pushed branch hint --- models/git/branch.go | 12 ++++++++---- routers/web/repo/view.go | 2 +- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/models/git/branch.go b/models/git/branch.go index 97891f01ebb40..95bd880ea1dd7 100644 --- a/models/git/branch.go +++ b/models/git/branch.go @@ -382,7 +382,8 @@ 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) { +// except the indicate branch +func FindRecentlyPushedNewBranches(ctx context.Context, repoID, userID int64, excludeBranchName string) (BranchList, error) { branches := make(BranchList, 0, 2) subQuery := builder.Select("head_branch").From("pull_request"). InnerJoin("issue", "issue.id = pull_request.issue_id"). @@ -390,9 +391,12 @@ func FindRecentlyPushedNewBranches(ctx context.Context, repoID, userID int64) (B "pull_request.head_repo_id": repoID, "issue.is_closed": false, }) - err := db.GetEngine(ctx). - Where("pusher_id=? AND is_deleted=?", userID, false). - And("updated_unix >= ?", time.Now().Add(-time.Hour*6).Unix()). + sess := db.GetEngine(ctx). + Where("pusher_id=? AND is_deleted=?", userID, false) + if excludeBranchName != "" { + sess.And("name <> ?", excludeBranchName) + } + err := sess.And("updated_unix >= ?", time.Now().Add(-time.Hour*6).Unix()). NotIn("name", subQuery). OrderBy("branch.updated_unix DESC"). Limit(2). diff --git a/routers/web/repo/view.go b/routers/web/repo/view.go index acea08d6297ec..ece2ec5416a5c 100644 --- a/routers/web/repo/view.go +++ b/routers/web/repo/view.go @@ -982,7 +982,7 @@ 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) + ctx.Data["RecentlyPushedNewBranches"], err = git_model.FindRecentlyPushedNewBranches(ctx, ctx.Repo.Repository.ID, ctx.Doer.ID, ctx.Repo.Repository.DefaultBranch) if err != nil { ctx.ServerError("GetRecentlyPushedBranches", err) return From 3e3207df05361fdecf39184288557e2c5fcbc1f7 Mon Sep 17 00:00:00 2001 From: Lunny Xiao Date: Mon, 10 Jul 2023 16:01:43 +0800 Subject: [PATCH 2/2] fix check --- models/git/branch.go | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/models/git/branch.go b/models/git/branch.go index 95bd880ea1dd7..d57b72719ce80 100644 --- a/models/git/branch.go +++ b/models/git/branch.go @@ -391,12 +391,10 @@ func FindRecentlyPushedNewBranches(ctx context.Context, repoID, userID int64, ex "pull_request.head_repo_id": repoID, "issue.is_closed": false, }) - sess := db.GetEngine(ctx). - Where("pusher_id=? AND is_deleted=?", userID, false) - if excludeBranchName != "" { - sess.And("name <> ?", excludeBranchName) - } - err := sess.And("updated_unix >= ?", time.Now().Add(-time.Hour*6).Unix()). + err := db.GetEngine(ctx). + Where("pusher_id=? AND is_deleted=?", userID, false). + And("name <> ?", excludeBranchName). + And("updated_unix >= ?", time.Now().Add(-time.Hour*6).Unix()). NotIn("name", subQuery). OrderBy("branch.updated_unix DESC"). Limit(2).