mirror of https://github.com/go-gitea/gitea.git
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>pull/35037/head^2
parent
f35dcfd489
commit
32152a0ac0
@ -0,0 +1,73 @@
|
||||
// Copyright 2025 The Gitea Authors. All rights reserved.
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
package repo
|
||||
|
||||
import (
|
||||
git_model "code.gitea.io/gitea/models/git"
|
||||
access_model "code.gitea.io/gitea/models/perm/access"
|
||||
unit_model "code.gitea.io/gitea/models/unit"
|
||||
"code.gitea.io/gitea/modules/log"
|
||||
"code.gitea.io/gitea/services/context"
|
||||
repo_service "code.gitea.io/gitea/services/repository"
|
||||
)
|
||||
|
||||
type RecentBranchesPromptDataStruct struct {
|
||||
RecentlyPushedNewBranches []*git_model.RecentlyPushedNewBranch
|
||||
}
|
||||
|
||||
func prepareRecentlyPushedNewBranches(ctx *context.Context) {
|
||||
if ctx.Doer == nil {
|
||||
return
|
||||
}
|
||||
if err := ctx.Repo.Repository.GetBaseRepo(ctx); err != nil {
|
||||
log.Error("GetBaseRepo: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
opts := git_model.FindRecentlyPushedNewBranchesOptions{
|
||||
Repo: ctx.Repo.Repository,
|
||||
BaseRepo: ctx.Repo.Repository,
|
||||
}
|
||||
if ctx.Repo.Repository.IsFork {
|
||||
opts.BaseRepo = ctx.Repo.Repository.BaseRepo
|
||||
}
|
||||
|
||||
baseRepoPerm, err := access_model.GetUserRepoPermission(ctx, opts.BaseRepo, ctx.Doer)
|
||||
if err != nil {
|
||||
log.Error("GetUserRepoPermission: %v", err)
|
||||
return
|
||||
}
|
||||
if !opts.Repo.CanContentChange() || !opts.BaseRepo.CanContentChange() {
|
||||
return
|
||||
}
|
||||
if !opts.BaseRepo.UnitEnabled(ctx, unit_model.TypePullRequests) || !baseRepoPerm.CanRead(unit_model.TypePullRequests) {
|
||||
return
|
||||
}
|
||||
|
||||
var finalBranches []*git_model.RecentlyPushedNewBranch
|
||||
branches, err := git_model.FindRecentlyPushedNewBranches(ctx, ctx.Doer, opts)
|
||||
if err != nil {
|
||||
log.Error("FindRecentlyPushedNewBranches failed: %v", err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, branch := range branches {
|
||||
divergingInfo, err := repo_service.GetBranchDivergingInfo(ctx,
|
||||
branch.BranchRepo, branch.BranchName, // "base" repo for diverging info
|
||||
opts.BaseRepo, opts.BaseRepo.DefaultBranch, // "head" repo for diverging info
|
||||
)
|
||||
if err != nil {
|
||||
log.Error("GetBranchDivergingInfo failed: %v", err)
|
||||
continue
|
||||
}
|
||||
branchRepoHasNewCommits := divergingInfo.BaseHasNewCommits
|
||||
baseRepoCommitsBehind := divergingInfo.HeadCommitsBehind
|
||||
if branchRepoHasNewCommits || baseRepoCommitsBehind > 0 {
|
||||
finalBranches = append(finalBranches, branch)
|
||||
}
|
||||
}
|
||||
if len(finalBranches) > 0 {
|
||||
ctx.Data["RecentBranchesPromptData"] = RecentBranchesPromptDataStruct{finalBranches}
|
||||
}
|
||||
}
|
||||
@ -1,12 +1,18 @@
|
||||
{{range .RecentlyPushedNewBranches}}
|
||||
<div class="ui positive message tw-flex tw-items-center tw-gap-2">
|
||||
<div class="tw-flex-1 tw-break-anywhere">
|
||||
{{$timeSince := DateUtils.TimeSince .CommitTime}}
|
||||
{{$branchLink := HTMLFormat `<a href="%s">%s</a>` .BranchLink .BranchDisplayName}}
|
||||
{{/* Template Attributes:
|
||||
* RecentBranchesPromptData
|
||||
*/}}
|
||||
{{$data := .RecentBranchesPromptData}}
|
||||
{{if $data}}
|
||||
{{range $recentBranch := $data.RecentlyPushedNewBranches}}
|
||||
<div class="ui positive message flex-text-block">
|
||||
<div class="tw-flex-1">
|
||||
{{$timeSince := DateUtils.TimeSince $recentBranch.CommitTime}}
|
||||
{{$branchLink := HTMLFormat `<a href="%s">%s</a>` $recentBranch.BranchLink .BranchDisplayName}}
|
||||
{{ctx.Locale.Tr "repo.pulls.recently_pushed_new_branches" $branchLink $timeSince}}
|
||||
</div>
|
||||
<a role="button" class="ui compact green button tw-m-0" href="{{QueryBuild .BranchCompareURL "expand" 1}}">
|
||||
<a role="button" class="ui compact green button" href="{{QueryBuild $recentBranch.BranchCompareURL "expand" 1}}">
|
||||
{{ctx.Locale.Tr "repo.pulls.compare_changes"}}
|
||||
</a>
|
||||
</div>
|
||||
{{end}}
|
||||
{{end}}
|
||||
|
||||
Loading…
Reference in New Issue