Skip to content

Commit 32152a0

Browse files
NaxdywxiaoguangGiteaBot
authored
Also display "recently pushed branch" alert on PR view (#35001)
This commit adds the "You recently pushed to branch X" alert also to PR overview, as opposed to only the repository's home page. GitHub also shows this alert on the PR list, as well as the home page. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com> Co-authored-by: Giteabot <teabot@gitea.io>
1 parent f35dcfd commit 32152a0

File tree

9 files changed

+100
-60
lines changed

9 files changed

+100
-60
lines changed

models/git/branch.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ type RecentlyPushedNewBranch struct {
472472
// if opts.CommitAfterUnix is 0, we will find the branches that were committed to in the last 2 hours
473473
// if opts.ListOptions is not set, we will only display top 2 latest branches.
474474
// Protected branches will be skipped since they are unlikely to be used to create new PRs.
475-
func FindRecentlyPushedNewBranches(ctx context.Context, doer *user_model.User, opts *FindRecentlyPushedNewBranchesOptions) ([]*RecentlyPushedNewBranch, error) {
475+
func FindRecentlyPushedNewBranches(ctx context.Context, doer *user_model.User, opts FindRecentlyPushedNewBranchesOptions) ([]*RecentlyPushedNewBranch, error) {
476476
if doer == nil {
477477
return []*RecentlyPushedNewBranch{}, nil
478478
}

models/repo/repo.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -652,7 +652,13 @@ func (repo *Repository) AllowsPulls(ctx context.Context) bool {
652652
}
653653

654654
// CanEnableEditor returns true if repository meets the requirements of web editor.
655+
// FIXME: most CanEnableEditor calls should be replaced with CanContentChange
656+
// And all other like CanCreateBranch / CanEnablePulls should also be updated
655657
func (repo *Repository) CanEnableEditor() bool {
658+
return repo.CanContentChange()
659+
}
660+
661+
func (repo *Repository) CanContentChange() bool {
656662
return !repo.IsMirror && !repo.IsArchived
657663
}
658664

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
// Copyright 2025 The Gitea Authors. All rights reserved.
2+
// SPDX-License-Identifier: MIT
3+
4+
package repo
5+
6+
import (
7+
git_model "code.gitea.io/gitea/models/git"
8+
access_model "code.gitea.io/gitea/models/perm/access"
9+
unit_model "code.gitea.io/gitea/models/unit"
10+
"code.gitea.io/gitea/modules/log"
11+
"code.gitea.io/gitea/services/context"
12+
repo_service "code.gitea.io/gitea/services/repository"
13+
)
14+
15+
type RecentBranchesPromptDataStruct struct {
16+
RecentlyPushedNewBranches []*git_model.RecentlyPushedNewBranch
17+
}
18+
19+
func prepareRecentlyPushedNewBranches(ctx *context.Context) {
20+
if ctx.Doer == nil {
21+
return
22+
}
23+
if err := ctx.Repo.Repository.GetBaseRepo(ctx); err != nil {
24+
log.Error("GetBaseRepo: %v", err)
25+
return
26+
}
27+
28+
opts := git_model.FindRecentlyPushedNewBranchesOptions{
29+
Repo: ctx.Repo.Repository,
30+
BaseRepo: ctx.Repo.Repository,
31+
}
32+
if ctx.Repo.Repository.IsFork {
33+
opts.BaseRepo = ctx.Repo.Repository.BaseRepo
34+
}
35+
36+
baseRepoPerm, err := access_model.GetUserRepoPermission(ctx, opts.BaseRepo, ctx.Doer)
37+
if err != nil {
38+
log.Error("GetUserRepoPermission: %v", err)
39+
return
40+
}
41+
if !opts.Repo.CanContentChange() || !opts.BaseRepo.CanContentChange() {
42+
return
43+
}
44+
if !opts.BaseRepo.UnitEnabled(ctx, unit_model.TypePullRequests) || !baseRepoPerm.CanRead(unit_model.TypePullRequests) {
45+
return
46+
}
47+
48+
var finalBranches []*git_model.RecentlyPushedNewBranch
49+
branches, err := git_model.FindRecentlyPushedNewBranches(ctx, ctx.Doer, opts)
50+
if err != nil {
51+
log.Error("FindRecentlyPushedNewBranches failed: %v", err)
52+
return
53+
}
54+
55+
for _, branch := range branches {
56+
divergingInfo, err := repo_service.GetBranchDivergingInfo(ctx,
57+
branch.BranchRepo, branch.BranchName, // "base" repo for diverging info
58+
opts.BaseRepo, opts.BaseRepo.DefaultBranch, // "head" repo for diverging info
59+
)
60+
if err != nil {
61+
log.Error("GetBranchDivergingInfo failed: %v", err)
62+
continue
63+
}
64+
branchRepoHasNewCommits := divergingInfo.BaseHasNewCommits
65+
baseRepoCommitsBehind := divergingInfo.HeadCommitsBehind
66+
if branchRepoHasNewCommits || baseRepoCommitsBehind > 0 {
67+
finalBranches = append(finalBranches, branch)
68+
}
69+
}
70+
if len(finalBranches) > 0 {
71+
ctx.Data["RecentBranchesPromptData"] = RecentBranchesPromptDataStruct{finalBranches}
72+
}
73+
}

routers/web/repo/issue_list.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -767,6 +767,10 @@ func Issues(ctx *context.Context) {
767767
}
768768
ctx.Data["Title"] = ctx.Tr("repo.pulls")
769769
ctx.Data["PageIsPullList"] = true
770+
prepareRecentlyPushedNewBranches(ctx)
771+
if ctx.Written() {
772+
return
773+
}
770774
} else {
771775
MustEnableIssues(ctx)
772776
if ctx.Written() {

routers/web/repo/view_home.go

Lines changed: 0 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ import (
1515

1616
"code.gitea.io/gitea/models/db"
1717
git_model "code.gitea.io/gitea/models/git"
18-
access_model "code.gitea.io/gitea/models/perm/access"
1918
repo_model "code.gitea.io/gitea/models/repo"
2019
unit_model "code.gitea.io/gitea/models/unit"
2120
user_model "code.gitea.io/gitea/models/user"
@@ -196,56 +195,6 @@ func prepareUpstreamDivergingInfo(ctx *context.Context) {
196195
ctx.Data["UpstreamDivergingInfo"] = upstreamDivergingInfo
197196
}
198197

199-
func prepareRecentlyPushedNewBranches(ctx *context.Context) {
200-
if ctx.Doer != nil {
201-
if err := ctx.Repo.Repository.GetBaseRepo(ctx); err != nil {
202-
ctx.ServerError("GetBaseRepo", err)
203-
return
204-
}
205-
206-
opts := &git_model.FindRecentlyPushedNewBranchesOptions{
207-
Repo: ctx.Repo.Repository,
208-
BaseRepo: ctx.Repo.Repository,
209-
}
210-
if ctx.Repo.Repository.IsFork {
211-
opts.BaseRepo = ctx.Repo.Repository.BaseRepo
212-
}
213-
214-
baseRepoPerm, err := access_model.GetUserRepoPermission(ctx, opts.BaseRepo, ctx.Doer)
215-
if err != nil {
216-
ctx.ServerError("GetUserRepoPermission", err)
217-
return
218-
}
219-
220-
if !opts.Repo.IsMirror && !opts.BaseRepo.IsMirror &&
221-
opts.BaseRepo.UnitEnabled(ctx, unit_model.TypePullRequests) &&
222-
baseRepoPerm.CanRead(unit_model.TypePullRequests) {
223-
var finalBranches []*git_model.RecentlyPushedNewBranch
224-
branches, err := git_model.FindRecentlyPushedNewBranches(ctx, ctx.Doer, opts)
225-
if err != nil {
226-
log.Error("FindRecentlyPushedNewBranches failed: %v", err)
227-
}
228-
229-
for _, branch := range branches {
230-
divergingInfo, err := repo_service.GetBranchDivergingInfo(ctx,
231-
branch.BranchRepo, branch.BranchName, // "base" repo for diverging info
232-
opts.BaseRepo, opts.BaseRepo.DefaultBranch, // "head" repo for diverging info
233-
)
234-
if err != nil {
235-
log.Error("GetBranchDivergingInfo failed: %v", err)
236-
continue
237-
}
238-
branchRepoHasNewCommits := divergingInfo.BaseHasNewCommits
239-
baseRepoCommitsBehind := divergingInfo.HeadCommitsBehind
240-
if branchRepoHasNewCommits || baseRepoCommitsBehind > 0 {
241-
finalBranches = append(finalBranches, branch)
242-
}
243-
}
244-
ctx.Data["RecentlyPushedNewBranches"] = finalBranches
245-
}
246-
}
247-
}
248-
249198
func updateContextRepoEmptyAndStatus(ctx *context.Context, empty bool, status repo_model.RepositoryStatus) {
250199
if ctx.Repo.Repository.IsEmpty == empty && ctx.Repo.Repository.Status == status {
251200
return
Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1-
{{range .RecentlyPushedNewBranches}}
2-
<div class="ui positive message tw-flex tw-items-center tw-gap-2">
3-
<div class="tw-flex-1 tw-break-anywhere">
4-
{{$timeSince := DateUtils.TimeSince .CommitTime}}
5-
{{$branchLink := HTMLFormat `<a href="%s">%s</a>` .BranchLink .BranchDisplayName}}
1+
{{/* Template Attributes:
2+
* RecentBranchesPromptData
3+
*/}}
4+
{{$data := .RecentBranchesPromptData}}
5+
{{if $data}}
6+
{{range $recentBranch := $data.RecentlyPushedNewBranches}}
7+
<div class="ui positive message flex-text-block">
8+
<div class="tw-flex-1">
9+
{{$timeSince := DateUtils.TimeSince $recentBranch.CommitTime}}
10+
{{$branchLink := HTMLFormat `<a href="%s">%s</a>` $recentBranch.BranchLink .BranchDisplayName}}
611
{{ctx.Locale.Tr "repo.pulls.recently_pushed_new_branches" $branchLink $timeSince}}
712
</div>
8-
<a role="button" class="ui compact green button tw-m-0" href="{{QueryBuild .BranchCompareURL "expand" 1}}">
13+
<a role="button" class="ui compact green button" href="{{QueryBuild $recentBranch.BranchCompareURL "expand" 1}}">
914
{{ctx.Locale.Tr "repo.pulls.compare_changes"}}
1015
</a>
1116
</div>
17+
{{end}}
1218
{{end}}

templates/repo/home.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</div>
1616
{{end}}
1717

18-
{{template "repo/code/recently_pushed_new_branches" .}}
18+
{{template "repo/code/recently_pushed_new_branches" dict "RecentBranchesPromptData" .RecentBranchesPromptData}}
1919

2020
<div class="{{Iif $showSidebar "repo-grid-filelist-sidebar" "repo-grid-filelist-only"}}">
2121
<div class="repo-home-filelist">

templates/repo/issue/list.tmpl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
<div class="ui container">
55
{{template "base/alert" .}}
66

7+
{{template "repo/code/recently_pushed_new_branches" dict "RecentBranchesPromptData" .RecentBranchesPromptData}}
8+
79
{{if .PinnedIssues}}
810
<div id="issue-pins" {{if .IsRepoAdmin}}data-is-repo-admin{{end}}>
911
{{range .PinnedIssues}}

templates/repo/view.tmpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
</div>
1515
{{end}}
1616

17-
{{template "repo/code/recently_pushed_new_branches" .}}
17+
{{template "repo/code/recently_pushed_new_branches" dict "RecentBranchesPromptData" .RecentBranchesPromptData}}
1818

1919
<div class="repo-view-container">
2020
<div class="tw-flex tw-flex-col repo-view-file-tree-container not-mobile {{if not .UserSettingCodeViewShowFileTree}}tw-hidden{{end}}" {{if .IsSigned}}data-user-is-signed-in{{end}}>

0 commit comments

Comments
 (0)