Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix wrong display of recently pushed notification #25812

Merged
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
122 commits
Select commit Hold shift + click to select a range
c8f707b
fix
yp05327 Jul 10, 2023
b3e3b73
support show notification in origin repo
yp05327 Jul 11, 2023
df97530
support show notification in forked repo
yp05327 Jul 11, 2023
266d07f
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
yp05327 Jul 11, 2023
71dd740
avoid attach already created pr branch
yp05327 Jul 11, 2023
2b0abf9
use branch.id instead of branch.name
yp05327 Jul 11, 2023
4c11682
allow forked repo from forked repo
yp05327 Jul 11, 2023
54f4fd5
improve TODO
yp05327 Jul 11, 2023
172ceeb
fix lint
yp05327 Jul 11, 2023
bc176bb
improve
yp05327 Jul 13, 2023
d3f72aa
remove PathEscapeSegments
yp05327 Jul 13, 2023
e74709e
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
yp05327 Jul 20, 2023
d93b2bc
use findbranch to search branch
yp05327 Jul 20, 2023
5c3f7aa
add test 1
yp05327 Jul 20, 2023
c704332
fix
yp05327 Jul 20, 2023
a7fc279
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
yp05327 Jul 20, 2023
69b7624
fix bug
yp05327 Jul 21, 2023
0bfec1a
add test
yp05327 Jul 21, 2023
f729517
use searchrepo
yp05327 Jul 21, 2023
1ecb6fb
fix org_user
yp05327 Jul 21, 2023
f06e091
fix test
yp05327 Jul 21, 2023
d848876
fix repo size
yp05327 Jul 21, 2023
5449bc1
fix TestSearchUsers
yp05327 Jul 21, 2023
63562c3
fix TestSearchRepository
yp05327 Jul 21, 2023
4d42a63
fix TestFindOrgs
yp05327 Jul 21, 2023
e263227
fix TestFixtureGeneration
yp05327 Jul 21, 2023
17876b4
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
yp05327 Jul 24, 2023
6517ad9
fix
yp05327 Jul 24, 2023
9a33437
improve
yp05327 Jul 24, 2023
4de1b05
revert RepoIDs
yp05327 Jul 24, 2023
94025ef
improve
yp05327 Jul 24, 2023
274e5ab
fix test
yp05327 Jul 24, 2023
f9b4085
fix test
yp05327 Jul 24, 2023
86892d4
fix tests
yp05327 Jul 25, 2023
66071ac
remove test code
yp05327 Jul 26, 2023
d60c758
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
yp05327 Jul 26, 2023
76f472d
move ListOptions
yp05327 Jul 26, 2023
b57883c
improve
yp05327 Jul 26, 2023
a583c64
fix test
yp05327 Jul 26, 2023
3b6ff9b
improve test
yp05327 Jul 27, 2023
5cf3d9e
fix TestIssue_DeleteIssue
yp05327 Jul 27, 2023
64ece1c
fix TestTeam_AddRepository
yp05327 Jul 27, 2023
fb3afac
fix TestSearchIssues
yp05327 Jul 27, 2023
9a9dd06
improve test
yp05327 Jul 27, 2023
dcc91fb
fix
yp05327 Jul 27, 2023
4df1529
fix conflicts
yp05327 Jul 31, 2023
7559311
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
yp05327 Jul 31, 2023
8e96323
fix test
yp05327 Jul 31, 2023
08b0159
fix test
yp05327 Jul 31, 2023
d7cb37c
improve
yp05327 Aug 1, 2023
aa2b8c4
fix conflicts
yp05327 Aug 15, 2023
5bfa11b
follow 26257
yp05327 Aug 15, 2023
a641d84
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
yp05327 Aug 15, 2023
2e93f87
follow 26257
yp05327 Aug 15, 2023
6248701
add permission check
yp05327 Aug 15, 2023
bb53d57
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
yp05327 Aug 21, 2023
44c80ae
remove unnecessary file
yp05327 Aug 22, 2023
60fd886
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
yp05327 Aug 28, 2023
38f4b66
remove test repo meta data
yp05327 Aug 28, 2023
330c3f8
rewrite test
yp05327 Aug 28, 2023
8f3d5c9
fix
yp05327 Aug 28, 2023
e79c8e0
fix ci
yp05327 Aug 28, 2023
34734c2
fix comment
yp05327 Aug 28, 2023
7fa0362
remove ignore no repo error
yp05327 Sep 6, 2023
a2425ce
Update models/git/branch.go
yp05327 Nov 10, 2023
2549c33
Update models/git/branch.go
yp05327 Nov 10, 2023
2b677fc
Update models/git/branch.go
yp05327 Nov 10, 2023
2d53287
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
yp05327 Nov 10, 2023
bff0db2
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
yp05327 Nov 16, 2023
3d98673
fix api user orgs test
yp05327 Nov 17, 2023
e44cec4
fix indexer test
yp05327 Nov 17, 2023
3922011
fix lint
yp05327 Nov 17, 2023
b3d6bed
fix lint
yp05327 Nov 17, 2023
4bd9cdb
Update models/git/branch.go
yp05327 Nov 19, 2023
6f5f65e
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
yp05327 Dec 7, 2023
2e860ed
fix fixture
yp05327 Jan 24, 2024
58dbb67
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
yp05327 Jan 24, 2024
ba4406f
use db.Find instead of FindBranch
yp05327 Jan 24, 2024
01088a9
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
yp05327 Feb 22, 2024
66f9676
fix conflict
yp05327 Apr 9, 2024
5af46f7
fix
yp05327 Apr 9, 2024
17ad1f6
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
yp05327 Apr 9, 2024
7b06c46
fix
yp05327 Apr 9, 2024
c4386f0
fix
yp05327 Apr 9, 2024
eed8b07
fix
yp05327 Apr 9, 2024
263a443
fix test
yp05327 Apr 9, 2024
a8a5de2
fix test
yp05327 Apr 9, 2024
58147c1
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
yp05327 Apr 12, 2024
ed19b8b
use container.FilterSlice
yp05327 Apr 12, 2024
c397f08
improve
yp05327 Apr 12, 2024
7031377
fix
yp05327 Apr 18, 2024
43495eb
improve
yp05327 Apr 18, 2024
f178450
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
yp05327 Apr 22, 2024
db0bfbb
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
yp05327 Apr 25, 2024
67ce9ca
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
lunny May 7, 2024
0596a13
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
yp05327 May 8, 2024
fb7abfe
fix misspelling
yp05327 May 8, 2024
3798d95
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
yp05327 May 9, 2024
3a96038
pre auto generate test data
yp05327 May 14, 2024
b65bad0
remove unused fixture
yp05327 May 14, 2024
b1a5d9e
fix tests
yp05327 May 14, 2024
1263116
improve test
yp05327 May 14, 2024
7323322
create pr should have write permission
yp05327 May 14, 2024
1f7b93c
fix lint
yp05327 May 14, 2024
41a2bb2
fix tests
yp05327 May 14, 2024
d99cf1a
revert permission change
yp05327 May 14, 2024
71556ed
fix test
yp05327 May 14, 2024
9fb523c
fix access
yp05327 May 14, 2024
c6edde1
when doer is nil, return empty branch
yp05327 May 15, 2024
4ed9e41
fix test
yp05327 May 15, 2024
b1346d5
use api to create collaborator
yp05327 May 21, 2024
0d589f4
fix id in access.yml
yp05327 May 21, 2024
d13e559
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
yp05327 May 21, 2024
4175d64
fix lint
yp05327 May 21, 2024
484b334
improve
yp05327 May 21, 2024
9fcb6b7
fix
yp05327 May 21, 2024
8cea058
improve
yp05327 May 21, 2024
8929a16
Update models/git/branch_list.go
wxiaoguang May 21, 2024
3152655
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
lunny May 21, 2024
cd0a5cd
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
GiteaBot May 21, 2024
f1b17d0
Fix test
lunny May 21, 2024
6e85adb
Merge branch 'main' into fix-incorrect-recently-pushed-new-branches-c…
GiteaBot May 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 44 additions & 17 deletions models/git/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
yp05327 marked this conversation as resolved.
Show resolved Hide resolved
err = nil
}
}
return err
}

func init() {
db.RegisterModel(new(Branch))
db.RegisterModel(new(RenamedBranch))
Expand Down Expand Up @@ -382,22 +393,38 @@ 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
yp05327 marked this conversation as resolved.
Show resolved Hide resolved
// except the indicate branch
func FindRecentlyPushedNewBranches(ctx context.Context, repoID, userID int64, excludeBranchName string) (BranchList, error) {
// doer should not be nil
// TODO use options to find the branches
func FindRecentlyPushedNewBranches(ctx context.Context, baseRepo *repo_model.Repository, doer *user_model.User) (BranchList, error) {
branches := make(BranchList, 0, 2)
subQuery := builder.Select("head_branch").From("pull_request").
baseBranch, err := GetBranch(ctx, baseRepo.ID, baseRepo.DefaultBranch)
if err != nil {
return nil, err
}
// search all related repos
repoCond := builder.Select("id").From("repository").
Where(builder.Or(
builder.Eq{"id": baseRepo.ID},
builder.Eq{"is_fork": true, "fork_id": baseRepo.ID},
))
// avoid check branches which have already created PRs
// TODO add head_branch_id in pull_request table then we can get the branch id from pull_request table directly
invalidBranchCond := builder.Select("branch.id").From("branch").
InnerJoin("pull_request", "branch.name = pull_request.head_branch AND branch.repo_id = pull_request.head_repo_id").
InnerJoin("issue", "issue.id = pull_request.issue_id").
Where(builder.Eq{
"pull_request.head_repo_id": repoID,
"issue.is_closed": false,
})
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).
Find(&branches)
Where(builder.Or(
builder.Eq{"pull_request.has_merged": true},
builder.In("pull_request.head_repo_id", repoCond),
builder.Eq{"branch.id": baseBranch.ID},
))
cond := builder.And(
builder.Neq{"commit_id": baseBranch.CommitID}, // newly created branch have no changes, so skip them
builder.Eq{"pusher_id": doer.ID},
builder.Eq{"is_deleted": false},
builder.Gte{"updated_unix": time.Now().Add(-time.Hour * 6).Unix()},
builder.In("repo_id", repoCond),
builder.NotIn("id", invalidBranchCond),
)
err = db.GetEngine(ctx).Where(cond).OrderBy("branch.updated_unix DESC").Limit(2).Find(&branches)
return branches, err
}
21 changes: 21 additions & 0 deletions models/git/branch_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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]{}
yp05327 marked this conversation as resolved.
Show resolved Hide resolved
for _, branch := range branches {
if branch.RepoID > 0 {
yp05327 marked this conversation as resolved.
Show resolved Hide resolved
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
Expand Down
14 changes: 12 additions & 2 deletions routers/web/repo/view.go
Original file line number Diff line number Diff line change
Expand Up @@ -982,11 +982,21 @@ 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.Repo.Repository.DefaultBranch)

baseRepo := ctx.Repo.Repository
if ctx.Repo.Repository.IsFork {
baseRepo = ctx.Repo.Repository.BaseRepo
}
branches, err := git_model.FindRecentlyPushedNewBranches(ctx, baseRepo, ctx.Doer)
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
Expand Down
16 changes: 14 additions & 2 deletions templates/repo/code/recently_pushed_new_branches.tmpl
Original file line number Diff line number Diff line change
@@ -1,10 +1,22 @@
{{range .RecentlyPushedNewBranches}}
{{$pullRepo := $.Repository}}
{{$baseRepo := $.Repository}}
{{$branchName := .Name}}
{{if .Repo}}
{{$pullRepo = .Repo}}
{{if and $.Repository.IsFork}}
{{$baseRepo = $.Repository.BaseRepo}}
{{end}}
{{if not (eq .Repo.ID $.Repository.ID)}}
{{$branchName = (print .Repo.FullName ":" .Name)}}
{{end}}
{{end}}
silverwind marked this conversation as resolved.
Show resolved Hide resolved
<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}}
Copy link
Member

@puni9869 puni9869 Jul 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{{$.locale.Tr "repo.pulls.recently_pushed_new_branches" (PathEscapeSegments $branchName) $timeSince | Safe}}

PathEscapeSegments $branchname This will create the problem when branchname contains # . like branchname#123. Is is due to PathEscapeSegments.

Double encoding of html Escape is going on.

There was an issue raised
Screenshot 1:
https://user-images.githubusercontent.com/23124682/252675332-4f8da1bf-40f6-4442-bf39-a261bd7289fa.png

Screenshot 2:
https://user-images.githubusercontent.com/23124682/252675084-e3bc020c-1cb8-4801-87fe-dd79de9bcd02.png

Issue link: -

#25830

@yp05327 FYI.

Copy link
Contributor Author

@yp05327 yp05327 Jul 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not only here, for example: #25864
Maybe we should check all of them?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When I try to test this, I notice that we have more problems:
https://try.gitea.io/yp05327/testrepo/src/branch/main
If you select branch test#11, the url will be
https://try.gitea.io/yp05327/testrepo/src/branch/test#11
but in the list it is test
image
Maybe we need a summary. 🤔

Copy link
Contributor Author

@yp05327 yp05327 Jul 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@puni9869
I created a new issue to discuss branch name problems: #25865
It seems that it is a global problem, so there's no plan to fix it in this PR now.
Avoid forgetting fix this, I will fix it in this PR.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I appreciate your feedback and Opening a new issue on specific to branch name.

</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 $baseRepo (PathEscapeSegments .Name)}}">
{{$.locale.Tr "repo.pulls.compare_changes"}}
</a>
</div>
Expand Down