Skip to content

Commit 9e31b22

Browse files
authored
Exclude protected branches from recently pushed (#31748)
Resolve #31566. Updates to protected branches often come from PR merges, and they are unlikely to be used to create new PRs. <img width="1346" alt="image" src="https://github.com/user-attachments/assets/9ed72bd6-0303-435d-856c-184784104c6a"> <img width="1347" alt="image" src="https://github.com/user-attachments/assets/c1a1df4b-1c16-4116-aea3-d452242119e0"> <img width="1336" alt="image" src="https://github.com/user-attachments/assets/706034ad-d3c3-4853-a6b8-cbaf87c70ba0">
1 parent 7674eaf commit 9e31b22

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

models/git/branch.go

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,8 @@ type RecentlyPushedNewBranch struct {
427427

428428
// FindRecentlyPushedNewBranches return at most 2 new branches pushed by the user in 2 hours which has no opened PRs created
429429
// if opts.CommitAfterUnix is 0, we will find the branches that were committed to in the last 2 hours
430-
// if opts.ListOptions is not set, we will only display top 2 latest branch
430+
// if opts.ListOptions is not set, we will only display top 2 latest branches.
431+
// Protected branches will be skipped since they are unlikely to be used to create new PRs.
431432
func FindRecentlyPushedNewBranches(ctx context.Context, doer *user_model.User, opts *FindRecentlyPushedNewBranchesOptions) ([]*RecentlyPushedNewBranch, error) {
432433
if doer == nil {
433434
return []*RecentlyPushedNewBranch{}, nil
@@ -486,6 +487,18 @@ func FindRecentlyPushedNewBranches(ctx context.Context, doer *user_model.User, o
486487
opts.MaxCount = 2
487488
}
488489
for _, branch := range branches {
490+
// whether the branch is protected
491+
protected, err := IsBranchProtected(ctx, branch.RepoID, branch.Name)
492+
if err != nil {
493+
return nil, fmt.Errorf("IsBranchProtected: %v", err)
494+
}
495+
if protected {
496+
// Skip protected branches,
497+
// since updates to protected branches often come from PR merges,
498+
// and they are unlikely to be used to create new PRs.
499+
continue
500+
}
501+
489502
// whether branch have already created PR
490503
count, err := db.GetEngine(ctx).Table("pull_request").
491504
// we should not only use branch name here, because if there are branches with same name in other repos,

0 commit comments

Comments
 (0)