This commit is contained in:
yp05327 2023-08-01 01:28:15 +00:00
parent 08b0159874
commit d7cb37c8ba
5 changed files with 52 additions and 42 deletions

View File

@ -1698,8 +1698,8 @@
id: 59
owner_id: 2
owner_name: user2
lower_name: repo58
name: repo58
lower_name: repo59
name: repo59
default_branch: master
num_watches: 0
num_stars: 0
@ -1729,8 +1729,8 @@
id: 60
owner_id: 40
owner_name: user40
lower_name: user_fork_repo
name: user_fork_repo
lower_name: user_fork_repo60
name: user_fork_repo60
num_watches: 0
num_stars: 0
num_forks: 0
@ -1759,8 +1759,8 @@
id: 61
owner_id: 37
owner_name: org37
lower_name: org_fork_repo1
name: org_fork_repo1
lower_name: org_fork_repo61
name: org_fork_repo61
num_watches: 0
num_stars: 0
num_forks: 0
@ -1789,8 +1789,8 @@
id: 62
owner_id: 38
owner_name: private_org38
lower_name: private_org_fork_repo1
name: private_org_fork_repo1
lower_name: private_org_fork_repo62
name: private_org_fork_repo62
num_watches: 0
num_stars: 0
num_forks: 0

View File

@ -401,11 +401,17 @@ type FindRecentlyPushedNewBranchesOptions struct {
CommitAfterUnix int64
}
type RecentlyPushedNewBranch struct {
BranchName string
BranchCompareURL string
UpdatedUnix timeutil.TimeStamp
}
// FindRecentlyPushedNewBranches return at most 2 new branches pushed by the user in 6 hours which has no opened PRs created
// opts.Actor should not be nil
// if opts.CommitAfterUnix is 0, we will find the branches committed in recently 6 hours
// if opts.ListOptions is not set, we will only display top 2 latest branch
func FindRecentlyPushedNewBranches(ctx context.Context, opts *FindRecentlyPushedNewBranchesOptions) (BranchList, error) {
func FindRecentlyPushedNewBranches(ctx context.Context, opts *FindRecentlyPushedNewBranchesOptions) ([]*RecentlyPushedNewBranch, error) {
// find all related repo ids
repoOpts := repo_model.SearchRepoOptions{
Actor: opts.Actor,
@ -418,7 +424,7 @@ func FindRecentlyPushedNewBranches(ctx context.Context, opts *FindRecentlyPushed
}
repoCond := repo_model.SearchRepositoryCondition(&repoOpts).And(repo_model.AccessibleRepositoryCondition(opts.Actor, unit.TypeCode))
if opts.Repo == opts.BaseRepo {
// should also include the brase repo's branches
// 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
@ -462,5 +468,25 @@ func FindRecentlyPushedNewBranches(ctx context.Context, opts *FindRecentlyPushed
ListOptions: opts.ListOptions,
}
return FindBranches(ctx, findBranchOpts)
branches, err := FindBranches(ctx, findBranchOpts)
if err != nil {
return nil, err
}
if err := branches.LoadRepo(ctx); err != nil {
return nil, err
}
newBranches := make([]*RecentlyPushedNewBranch, 0, len(branches))
for _, branch := range branches {
branchName := branch.Name
if branch.Repo.ID != opts.BaseRepo.ID && branch.Repo.ID != opts.Repo.ID {
branchName = fmt.Sprintf("%s:%s", branch.Repo.FullName(), branchName)
}
newBranches = append(newBranches, &RecentlyPushedNewBranch{
BranchName: branchName,
BranchCompareURL: branch.Repo.ComposeBranchCompareURL(opts.BaseRepo, branch.Name),
UpdatedUnix: branch.UpdatedUnix,
})
}
return newBranches, nil
}

View File

@ -198,7 +198,7 @@ func TestFindRecentlyPushedNewBranches(t *testing.T) {
name string
opts *git_model.FindRecentlyPushedNewBranchesOptions
count int
want []int64
want []string
}{
// user39 is the owner of the repo and the organization
// in repo58, user39 has opening/closed/merged pr and closed/merged pr with deleted branch
@ -213,11 +213,11 @@ func TestFindRecentlyPushedNewBranches(t *testing.T) {
},
},
count: 2,
want: []int64{6, 18}, // "new-commit", "org-fork-new-commit"
want: []string{"new-commit", "org37/org_fork_repo61:org-fork-new-commit"},
},
// we have 2 branches with the same name in repo58 and repo59
// and repo59's branch has a pr, but repo58's branch doesn't
// in this case, we should get repo58's branch but not repo59's branch
// we have 2 branches with the same name in repo59 and repo60
// and repo60's branch has a pr, but repo59's branch doesn't
// in this case, we should get repo59's branch but not repo60's branch
{
name: "new branch from user fork repo and same name branch",
opts: &git_model.FindRecentlyPushedNewBranchesOptions{
@ -229,7 +229,7 @@ func TestFindRecentlyPushedNewBranches(t *testing.T) {
},
},
count: 2,
want: []int64{15, 25}, // "user-fork-new-commit", "same-name-branch-in-pr"
want: []string{"user40/user_fork_repo60:user-fork-new-commit", "same-name-branch-in-pr"},
},
{
name: "new branch from private org with code permisstion repo",
@ -238,7 +238,7 @@ func TestFindRecentlyPushedNewBranches(t *testing.T) {
CommitAfterUnix: 1489927670,
},
count: 1,
want: []int64{21}, // "private-org-fork-new-commit"
want: []string{"private_org38/private_org_fork_repo62:private-org-fork-new-commit"},
},
{
name: "new branch from private org with no code permisstion repo",
@ -247,7 +247,7 @@ func TestFindRecentlyPushedNewBranches(t *testing.T) {
CommitAfterUnix: 1489927670,
},
count: 0,
want: []int64{},
want: []string{},
},
{
name: "test commitAfterUnix option",
@ -256,7 +256,7 @@ func TestFindRecentlyPushedNewBranches(t *testing.T) {
CommitAfterUnix: 1489927690,
},
count: 1,
want: []int64{18}, // "org-fork-new-commit"
want: []string{"org37/org_fork_repo61:org-fork-new-commit"},
},
}
@ -265,11 +265,12 @@ func TestFindRecentlyPushedNewBranches(t *testing.T) {
tt.opts.Repo = repo
tt.opts.BaseRepo = repo
branches, err := git_model.FindRecentlyPushedNewBranches(db.DefaultContext, tt.opts)
assert.NoError(t, err)
assert.Equal(t, tt.count, len(branches))
for i := 1; i < tt.count; i++ {
assert.Equal(t, tt.want[i], branches[i].ID)
for i := 0; i < tt.count; i++ {
assert.Equal(t, tt.want[i], branches[i].BranchName)
}
})
}

View File

@ -991,16 +991,11 @@ func renderCode(ctx *context.Context) {
if ctx.Repo.Repository.IsFork {
opts.BaseRepo = ctx.Repo.Repository.BaseRepo
}
branches, err := git_model.FindRecentlyPushedNewBranches(ctx, opts)
ctx.Data["RecentlyPushedNewBranches"], err = git_model.FindRecentlyPushedNewBranches(ctx, opts)
if err != nil {
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,22 +1,10 @@
{{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}}
<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" $branchName $timeSince | Safe}}
{{$.locale.Tr "repo.pulls.recently_pushed_new_branches" .BranchName $timeSince | Safe}}
</div>
<a aria-role="button" class="ui compact positive button gt-m-0" href="{{$pullRepo.ComposeBranchCompareURL $baseRepo .Name}}">
<a aria-role="button" class="ui compact positive button gt-m-0" href="{{.BranchCompareURL}}">
{{$.locale.Tr "repo.pulls.compare_changes"}}
</a>
</div>