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 120 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
36 changes: 36 additions & 0 deletions models/fixtures/branch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,39 @@
is_deleted: false
deleted_by_id: 0
deleted_unix: 0

-
id: 5
repo_id: 10
name: 'master'
commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d'
commit_message: 'Initial commit'
commit_time: 1489927679
pusher_id: 12
is_deleted: false
deleted_by_id: 0
deleted_unix: 0

-
id: 6
repo_id: 10
name: 'outdated-new-branch'
commit_id: 'cb24c347e328d83c1e0c3c908a6b2c0a2fcb8a3d'
commit_message: 'add'
commit_time: 1489927679
pusher_id: 12
is_deleted: false
deleted_by_id: 0
deleted_unix: 0

-
id: 14
repo_id: 11
name: 'master'
commit_id: '65f1bf27bc3bf70f64657658635e66094edbcb4d'
commit_message: 'Initial commit'
commit_time: 1489927679
pusher_id: 13
is_deleted: false
deleted_by_id: 0
deleted_unix: 0
8 changes: 8 additions & 0 deletions models/fixtures/issue_index.yml
Original file line number Diff line number Diff line change
@@ -1,27 +1,35 @@
-
group_id: 1
max_index: 5

-
group_id: 2
max_index: 2

-
group_id: 3
max_index: 2

-
group_id: 10
max_index: 1

-
group_id: 32
max_index: 2

-
group_id: 48
max_index: 1

-
group_id: 42
max_index: 1

-
group_id: 50
max_index: 1

-
group_id: 51
max_index: 1
12 changes: 12 additions & 0 deletions models/fixtures/org_user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -117,3 +117,15 @@
uid: 40
org_id: 41
is_public: true

-
id: 21
uid: 12
org_id: 25
is_public: true

-
id: 22
uid: 2
org_id: 35
is_public: true
2 changes: 1 addition & 1 deletion models/fixtures/repository.yml
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@
is_archived: false
is_mirror: false
status: 0
is_fork: false
is_fork: true
fork_id: 10
is_template: false
template_id: 0
Expand Down
22 changes: 22 additions & 0 deletions models/fixtures/team.yml
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,25 @@
num_members: 2
includes_all_repositories: false
can_create_org_repo: false

-
id: 23
org_id: 25
lower_name: owners
name: Owners
authorize: 4 # owner
num_repos: 0
num_members: 1
includes_all_repositories: false
can_create_org_repo: true

-
id: 24
org_id: 35
lower_name: team24
name: team24
authorize: 2 # write
num_repos: 0
num_members: 1
includes_all_repositories: true
can_create_org_repo: false
18 changes: 18 additions & 0 deletions models/fixtures/team_unit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -322,3 +322,21 @@
team_id: 22
type: 3
access_mode: 1

-
id: 55
team_id: 18
type: 1 # code
access_mode: 4

-
id: 56
team_id: 23
type: 1 # code
access_mode: 4

-
id: 57
team_id: 24
type: 1 # code
access_mode: 2
12 changes: 12 additions & 0 deletions models/fixtures/team_user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -147,3 +147,15 @@
org_id: 41
team_id: 22
uid: 39

-
id: 26
org_id: 25
team_id: 23
uid: 12

-
id: 27
org_id: 35
team_id: 24
uid: 2
8 changes: 4 additions & 4 deletions models/fixtures/user.yml
Original file line number Diff line number Diff line change
Expand Up @@ -918,8 +918,8 @@
num_following: 0
num_stars: 0
num_repos: 0
num_teams: 1
num_members: 1
num_teams: 2
num_members: 2
visibility: 0
repo_admin_change_team_access: false
theme: ""
Expand Down Expand Up @@ -1289,8 +1289,8 @@
num_following: 0
num_stars: 0
num_repos: 0
num_teams: 1
num_members: 1
num_teams: 2
num_members: 2
visibility: 2
repo_admin_change_team_access: false
theme: ""
Expand Down
142 changes: 120 additions & 22 deletions models/git/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@ import (

"code.gitea.io/gitea/models/db"
repo_model "code.gitea.io/gitea/models/repo"
"code.gitea.io/gitea/models/unit"
user_model "code.gitea.io/gitea/models/user"
"code.gitea.io/gitea/modules/git"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/optional"
"code.gitea.io/gitea/modules/timeutil"
"code.gitea.io/gitea/modules/util"

Expand Down Expand Up @@ -102,8 +104,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 +142,14 @@ 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 {
return nil
}
b.Repo, err = repo_model.GetRepositoryByID(ctx, b.RepoID)
return err
}

func init() {
db.RegisterModel(new(Branch))
db.RegisterModel(new(RenamedBranch))
Expand Down Expand Up @@ -400,24 +411,111 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to str
return committer.Commit()
}

// FindRecentlyPushedNewBranches return at most 2 new branches pushed by the user in 6 hours which has no opened PRs created
// except the indicate branch
func FindRecentlyPushedNewBranches(ctx context.Context, repoID, userID int64, excludeBranchName string) (BranchList, error) {
branches := make(BranchList, 0, 2)
subQuery := builder.Select("head_branch").From("pull_request").
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("repo_id = ?", repoID).
And("commit_time >= ?", time.Now().Add(-time.Hour*6).Unix()).
NotIn("name", subQuery).
OrderBy("branch.commit_time DESC").
Limit(2).
Find(&branches)
return branches, err
type FindRecentlyPushedNewBranchesOptions struct {
Repo *repo_model.Repository
BaseRepo *repo_model.Repository
CommitAfterUnix int64
MaxCount int
}

type RecentlyPushedNewBranch struct {
BranchDisplayName string
BranchLink string
BranchCompareURL string
CommitTime timeutil.TimeStamp
}

// FindRecentlyPushedNewBranches return at most 2 new branches pushed by the user in 2 hours which has no opened PRs created
// if opts.CommitAfterUnix is 0, we will find the branches that were committed to in the last 2 hours
// if opts.ListOptions is not set, we will only display top 2 latest branch
func FindRecentlyPushedNewBranches(ctx context.Context, doer *user_model.User, opts *FindRecentlyPushedNewBranchesOptions) ([]*RecentlyPushedNewBranch, error) {
if doer == nil {
return []*RecentlyPushedNewBranch{}, nil
}

// find all related repo ids
lunny marked this conversation as resolved.
Show resolved Hide resolved
repoOpts := repo_model.SearchRepoOptions{
Actor: doer,
Private: true,
AllPublic: false, // Include also all public repositories of users and public organisations
AllLimited: false, // Include also all public repositories of limited organisations
Fork: optional.Some(true),
ForkFrom: opts.BaseRepo.ID,
Archived: optional.Some(false),
}
repoCond := repo_model.SearchRepositoryCondition(&repoOpts).And(repo_model.AccessibleRepositoryCondition(doer, unit.TypeCode))
if opts.Repo.ID == opts.BaseRepo.ID {
// should also include the base repo's branches
repoCond = repoCond.Or(builder.Eq{"id": opts.BaseRepo.ID})
} else {
// in fork repo, we only detect the fork repo's branch
repoCond = repoCond.And(builder.Eq{"id": opts.Repo.ID})
}
repoIDs := builder.Select("id").From("repository").Where(repoCond)

if opts.CommitAfterUnix == 0 {
opts.CommitAfterUnix = time.Now().Add(-time.Hour * 2).Unix()
}

baseBranch, err := GetBranch(ctx, opts.BaseRepo.ID, opts.BaseRepo.DefaultBranch)
if err != nil {
return nil, err
}

// find all related branches, these branches may already created PRs, we will check later
var branches []*Branch
if err := db.GetEngine(ctx).
Where(builder.And(
builder.Eq{
"pusher_id": doer.ID,
"is_deleted": false,
},
builder.Gte{"commit_time": opts.CommitAfterUnix},
builder.In("repo_id", repoIDs),
// newly created branch have no changes, so skip them
builder.Neq{"commit_id": baseBranch.CommitID},
)).
OrderBy(db.SearchOrderByRecentUpdated.String()).
Find(&branches); err != nil {
return nil, err
}

newBranches := make([]*RecentlyPushedNewBranch, 0, len(branches))
if opts.MaxCount == 0 {
// by default we display 2 recently pushed new branch
opts.MaxCount = 2
}
for _, branch := range branches {
// whether branch have already created PR
count, err := db.GetEngine(ctx).Table("pull_request").
// we should not only use branch name here, because if there are branches with same name in other repos,
// we can not detect them correctly
Where(builder.Eq{"head_repo_id": branch.RepoID, "head_branch": branch.Name}).Count()
if err != nil {
return nil, err
}

// if no PR, we add to the result
if count == 0 {
if err := branch.LoadRepo(ctx); err != nil {
return nil, err
}

branchDisplayName := branch.Name
if branch.Repo.ID != opts.BaseRepo.ID && branch.Repo.ID != opts.Repo.ID {
branchDisplayName = fmt.Sprintf("%s:%s", branch.Repo.FullName(), branchDisplayName)
}
newBranches = append(newBranches, &RecentlyPushedNewBranch{
BranchDisplayName: branchDisplayName,
BranchLink: fmt.Sprintf("%s/src/branch/%s", branch.Repo.Link(), util.PathEscapeSegments(branch.Name)),
BranchCompareURL: branch.Repo.ComposeBranchCompareURL(opts.BaseRepo, branch.Name),
CommitTime: branch.CommitTime,
})
}
if len(newBranches) == opts.MaxCount {
break
}
}

return newBranches, nil
}
19 changes: 19 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/optional"
Expand Down Expand Up @@ -59,6 +60,24 @@ func (branches BranchList) LoadPusher(ctx context.Context) error {
return nil
}

func (branches BranchList) LoadRepo(ctx context.Context) error {
ids := container.FilterSlice(branches, func(branch *Branch) (int64, bool) {
return branch.RepoID, branch.RepoID > 0 && branch.Repo == nil
})

reposMap := make(map[int64]*repo_model.Repository, len(ids))
if err := db.GetEngine(ctx).In("id", ids).Find(&reposMap); err != nil {
return err
}
for _, branch := range branches {
if branch.RepoID <= 0 || branch.Repo != nil {
continue
}
branch.Repo = reposMap[branch.RepoID]
}
return nil
}

type FindBranchOptions struct {
db.ListOptions
RepoID int64
Expand Down
Loading
Loading