Skip to content

Commit 7c0f2b9

Browse files
saitholunny
authored andcommitted
Show Pull Request button or status of latest PR in branch list (#6990)
* Show Pull Request button or status of latest PR in branch list Signed-off-by: Mario Lubenka <[email protected]> * Do not show pull request button on deleted branches Signed-off-by: Mario Lubenka <[email protected]> * Do not show commit divergence on deleted branches Signed-off-by: Mario Lubenka <[email protected]> * Use XORMs Get instead of limit * Links pull request ID and use smaller labels for displaying the pull request status Signed-off-by: Mario Lubenka <[email protected]> * Handle error when getting latest pull request Signed-off-by: Mario Lubenka <[email protected]> * Indent template Signed-off-by: Mario Lubenka <[email protected]> * Check error when loading issue Signed-off-by: Mario Lubenka <[email protected]>
1 parent c37ec66 commit 7c0f2b9

File tree

3 files changed

+63
-14
lines changed

3 files changed

+63
-14
lines changed

models/pull.go

+14
Original file line numberDiff line numberDiff line change
@@ -776,6 +776,20 @@ func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequ
776776
Find(&prs)
777777
}
778778

779+
// GetLatestPullRequestByHeadInfo returns the latest pull request (regardless of its status)
780+
// by given head information (repo and branch).
781+
func GetLatestPullRequestByHeadInfo(repoID int64, branch string) (*PullRequest, error) {
782+
pr := new(PullRequest)
783+
has, err := x.
784+
Where("head_repo_id = ? AND head_branch = ?", repoID, branch).
785+
OrderBy("id DESC").
786+
Get(pr)
787+
if !has {
788+
return nil, err
789+
}
790+
return pr, err
791+
}
792+
779793
// GetUnmergedPullRequestsByBaseInfo returns all pull requests that are open and has not been merged
780794
// by given base information (repo and branch).
781795
func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequest, error) {

routers/repo/branch.go

+26-12
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,14 @@ const (
2424

2525
// Branch contains the branch information
2626
type Branch struct {
27-
Name string
28-
Commit *git.Commit
29-
IsProtected bool
30-
IsDeleted bool
31-
DeletedBranch *models.DeletedBranch
32-
CommitsAhead int
33-
CommitsBehind int
27+
Name string
28+
Commit *git.Commit
29+
IsProtected bool
30+
IsDeleted bool
31+
DeletedBranch *models.DeletedBranch
32+
CommitsAhead int
33+
CommitsBehind int
34+
LatestPullRequest *models.PullRequest
3435
}
3536

3637
// Branches render repository branch page
@@ -181,12 +182,25 @@ func loadBranches(ctx *context.Context) []*Branch {
181182
return nil
182183
}
183184

185+
pr, err := models.GetLatestPullRequestByHeadInfo(ctx.Repo.Repository.ID, branchName)
186+
if err != nil {
187+
ctx.ServerError("GetLatestPullRequestByHeadInfo", err)
188+
return nil
189+
}
190+
if pr != nil {
191+
if err := pr.LoadIssue(); err != nil {
192+
ctx.ServerError("pr.LoadIssue", err)
193+
return nil
194+
}
195+
}
196+
184197
branches[i] = &Branch{
185-
Name: branchName,
186-
Commit: commit,
187-
IsProtected: isProtected,
188-
CommitsAhead: divergence.Ahead,
189-
CommitsBehind: divergence.Behind,
198+
Name: branchName,
199+
Commit: commit,
200+
IsProtected: isProtected,
201+
CommitsAhead: divergence.Ahead,
202+
CommitsBehind: divergence.Behind,
203+
LatestPullRequest: pr,
190204
}
191205
}
192206

templates/repo/branch/list.tmpl

+23-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,8 @@
2626
<table class="ui very basic striped fixed table single line">
2727
<thead>
2828
<tr>
29-
<th class="seven wide">{{.i18n.Tr "repo.branch.name"}}</th>
29+
<th class="six wide">{{.i18n.Tr "repo.branch.name"}}</th>
30+
<th class="two wide"></th>
3031
<th class="two wide"></th>
3132
{{if and $.IsWriter (not $.IsMirror)}}
3233
<th class="one wide right aligned">{{.i18n.Tr "repo.branch.delete_head"}}</th>
@@ -44,9 +45,10 @@
4445
{{else}}
4546
<a href="{{$.RepoLink}}/src/branch/{{.Name | EscapePound}}">{{.Name}}</a>
4647
<p class="time">{{$.i18n.Tr "org.repo_updated"}} {{TimeSince .Commit.Committer.When $.i18n.Lang}}</p>
47-
</td>
4848
{{end}}
49+
</td>
4950
<td class="ui">
51+
{{if not .IsDeleted}}
5052
<div class="commit-divergence">
5153
<div class="bar-group">
5254
<div class="count count-behind">{{.CommitsBehind}}</div>
@@ -57,6 +59,25 @@
5759
<div class="bar bar-ahead" style="width: {{percentage .CommitsAhead .CommitsBehind .CommitsAhead}}%"></div>
5860
</div>
5961
</div>
62+
{{end}}
63+
</td>
64+
<td class="right aligned">
65+
{{if not .LatestPullRequest}}
66+
{{if not .IsDeleted}}
67+
<a href="{{$.RepoLink}}/compare/{{$.DefaultBranch | EscapePound}}...{{if ne $.Repository.Owner.Name $.Owner.Name}}{{$.Owner.Name}}:{{end}}{{.Name | EscapePound}}">
68+
<button id="new-pull-request" class="ui compact basic button">{{$.i18n.Tr "repo.pulls.compare_changes"}}</button>
69+
</a>
70+
{{end}}
71+
{{else}}
72+
<a href="{{$.RepoLink}}/pulls/{{.LatestPullRequest.Issue.Index}}">#{{.LatestPullRequest.Issue.Index}}</a>
73+
{{if .LatestPullRequest.HasMerged}}
74+
<a href="{{$.RepoLink}}/pulls/{{.LatestPullRequest.Issue.Index}}" class="ui purple small label"><i class="octicon octicon-git-pull-request"></i> {{$.i18n.Tr "repo.pulls.merged"}}</a>
75+
{{else if .LatestPullRequest.Issue.IsClosed}}
76+
<a href="{{$.RepoLink}}/pulls/{{.LatestPullRequest.Issue.Index}}" class="ui red small label"><i class="octicon octicon-issue-closed"></i> {{$.i18n.Tr "repo.issues.closed_title"}}</a>
77+
{{else}}
78+
<a href="{{$.RepoLink}}/pulls/{{.LatestPullRequest.Issue.Index}}" class="ui green small label"><i class="octicon octicon-issue-opened"></i> {{$.i18n.Tr "repo.issues.open_title"}}</a>
79+
{{end}}
80+
{{end}}
6081
</td>
6182
{{if and $.IsWriter (not $.IsMirror)}}
6283
<td class="right aligned">

0 commit comments

Comments
 (0)