Skip to content

Commit 6375419

Browse files
authored
Newly pushed branches hints on repository home page (#25715)
This PR will display a pull request creation hint on the repository home page when there are newly created branches with no pull request. Only the recent 6 hours and 2 updated branches will be displayed. Inspired by #14003 Replace #14003 Resolves #311 Resolves #13196 Resolves #23743 co-authored by @kolaente
1 parent e0a780d commit 6375419

File tree

7 files changed

+61
-2
lines changed

7 files changed

+61
-2
lines changed

models/git/branch.go

+21
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ import (
1515
"code.gitea.io/gitea/modules/log"
1616
"code.gitea.io/gitea/modules/timeutil"
1717
"code.gitea.io/gitea/modules/util"
18+
19+
"xorm.io/builder"
1820
)
1921

2022
// ErrBranchNotExist represents an error that branch with such name does not exist.
@@ -378,3 +380,22 @@ func RenameBranch(ctx context.Context, repo *repo_model.Repository, from, to str
378380

379381
return committer.Commit()
380382
}
383+
384+
// FindRecentlyPushedNewBranches return at most 2 new branches pushed by the user in 6 hours which has no opened PRs created
385+
func FindRecentlyPushedNewBranches(ctx context.Context, repoID, userID int64) (BranchList, error) {
386+
branches := make(BranchList, 0, 2)
387+
subQuery := builder.Select("head_branch").From("pull_request").
388+
InnerJoin("issue", "issue.id = pull_request.issue_id").
389+
Where(builder.Eq{
390+
"pull_request.head_repo_id": repoID,
391+
"issue.is_closed": false,
392+
})
393+
err := db.GetEngine(ctx).
394+
Where("pusher_id=? AND is_deleted=?", userID, false).
395+
And("updated_unix >= ?", time.Now().Add(-time.Hour*6).Unix()).
396+
NotIn("name", subQuery).
397+
OrderBy("branch.updated_unix DESC").
398+
Limit(2).
399+
Find(&branches)
400+
return branches, err
401+
}

models/repo/repo.go

+12
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,18 @@ func (repo *Repository) ComposeCompareURL(oldCommitID, newCommitID string) strin
528528
return fmt.Sprintf("%s/%s/compare/%s...%s", url.PathEscape(repo.OwnerName), url.PathEscape(repo.Name), util.PathEscapeSegments(oldCommitID), util.PathEscapeSegments(newCommitID))
529529
}
530530

531+
func (repo *Repository) ComposeBranchCompareURL(baseRepo *Repository, branchName string) string {
532+
if baseRepo == nil {
533+
baseRepo = repo
534+
}
535+
var cmpBranchEscaped string
536+
if repo.ID != baseRepo.ID {
537+
cmpBranchEscaped = fmt.Sprintf("%s/%s:", url.PathEscape(repo.OwnerName), url.PathEscape(repo.Name))
538+
}
539+
cmpBranchEscaped = fmt.Sprintf("%s%s", cmpBranchEscaped, util.PathEscapeSegments(branchName))
540+
return fmt.Sprintf("%s/compare/%s...%s", baseRepo.Link(), util.PathEscapeSegments(baseRepo.DefaultBranch), cmpBranchEscaped)
541+
}
542+
531543
// IsOwnedBy returns true when user owns this repository
532544
func (repo *Repository) IsOwnedBy(userID int64) bool {
533545
return repo.OwnerID == userID

options/locale/locale_en-US.ini

+2
Original file line numberDiff line numberDiff line change
@@ -1767,6 +1767,8 @@ pulls.auto_merge_canceled_schedule_comment = `canceled auto merging this pull re
17671767
pulls.delete.title = Delete this pull request?
17681768
pulls.delete.text = Do you really want to delete this pull request? (This will permanently remove all content. Consider closing it instead, if you intend to keep it archived)
17691769

1770+
pulls.recently_pushed_new_branches = You pushed on branch <strong>%[1]s</strong> %[2]s
1771+
17701772
milestones.new = New Milestone
17711773
milestones.closed = Closed %s
17721774
milestones.update_ago = Updated %s

routers/web/repo/view.go

+12
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,18 @@ func renderCode(ctx *context.Context) {
977977
return
978978
}
979979

980+
if ctx.Doer != nil {
981+
if err := ctx.Repo.Repository.GetBaseRepo(ctx); err != nil {
982+
ctx.ServerError("GetBaseRepo", err)
983+
return
984+
}
985+
ctx.Data["RecentlyPushedNewBranches"], err = git_model.FindRecentlyPushedNewBranches(ctx, ctx.Repo.Repository.ID, ctx.Doer.ID)
986+
if err != nil {
987+
ctx.ServerError("GetRecentlyPushedBranches", err)
988+
return
989+
}
990+
}
991+
980992
var treeNames []string
981993
paths := make([]string, 0, 5)
982994
if len(ctx.Repo.TreePath) > 0 {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{{range .RecentlyPushedNewBranches}}
2+
<div class="ui positive message gt-df gt-ac">
3+
<div class="gt-f1">
4+
{{$timeSince := TimeSince .UpdatedUnix.AsTime $.locale}}
5+
{{$.locale.Tr "repo.pulls.recently_pushed_new_branches" (PathEscapeSegments .Name) $timeSince | Safe}}
6+
</div>
7+
<a aria-role="button" class="ui compact positive button gt-m-0" href="{{$.Repository.ComposeBranchCompareURL $.Repository.BaseRepo (PathEscapeSegments .Name)}}">
8+
{{$.locale.Tr "repo.pulls.compare_changes"}}
9+
</a>
10+
</div>
11+
{{end}}

templates/repo/home.tmpl

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
{{template "repo/header" .}}
44
<div class="ui container {{if .IsBlame}}fluid padded{{end}}">
55
{{template "base/alert" .}}
6+
{{template "repo/code/recently_pushed_new_branches" .}}
67
{{if and (not .HideRepoInfo) (not .IsBlame)}}
78
<div class="ui repo-description">
89
<div id="repo-desc">

tests/integration/repo_branch_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ func testCreateBranches(t *testing.T, giteaURL *url.URL) {
120120
req := NewRequest(t, "GET", redirectURL)
121121
resp := session.MakeRequest(t, req, http.StatusOK)
122122
htmlDoc := NewHTMLParser(t, resp.Body)
123-
assert.Equal(t,
124-
test.FlashMessage,
123+
assert.Contains(t,
125124
strings.TrimSpace(htmlDoc.doc.Find(".ui.message").Text()),
125+
test.FlashMessage,
126126
)
127127
}
128128
}

0 commit comments

Comments
 (0)