support show notification in origin repo

This commit is contained in:
yp05327 2023-07-11 01:10:12 +00:00
parent c8f707b890
commit b3e3b7311c
4 changed files with 59 additions and 8 deletions

View File

@ -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)

View File

@ -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

View File

@ -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

View File

@ -1,10 +1,17 @@
{{range .RecentlyPushedNewBranches}}
{{$pullRepo := $.Repository}}
{{$branchName := .Name}}
{{if .Repo}}
{{$pullRepo = .Repo}}
{{$branchName = (print $pullRepo.FullName ":" .Name)}}
{{end}}
<div class="ui positive message gt-df gt-ac">
<div class="gt-f1">
{{$timeSince := TimeSince .UpdatedUnix.AsTime $.locale}}
{{$.locale.Tr "repo.pulls.recently_pushed_new_branches" (PathEscapeSegments .Name) $timeSince | Safe}}
{{$.locale.Tr "repo.pulls.recently_pushed_new_branches" (PathEscapeSegments $branchName) $timeSince | Safe}}
</div>
<a aria-role="button" class="ui compact positive button gt-m-0" href="{{$.Repository.ComposeBranchCompareURL $.Repository.BaseRepo (PathEscapeSegments .Name)}}">
<a aria-role="button" class="ui compact positive button gt-m-0" href="{{$pullRepo.ComposeBranchCompareURL $.Repository (PathEscapeSegments .Name)}}">
{{$.locale.Tr "repo.pulls.compare_changes"}}
</a>
</div>