Skip to content

Show Pull Request button or status of latest PR in branch list #6990

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

Merged
merged 13 commits into from
Jun 27, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
15 changes: 15 additions & 0 deletions models/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -1037,6 +1037,21 @@ func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequ
Find(&prs)
}

// GetLatestPullRequestsByHeadInfo returns the latest pull request (regardless of its status)
// by given head information (repo and branch).
func GetLatestPullRequestsByHeadInfo(repoID int64, branch string) (*PullRequest, error) {
prs := make([]*PullRequest, 0, 2)
err := x.
Where("head_repo_id = ? AND head_branch = ?", repoID, branch).
OrderBy("id DESC").
Limit(1).
Find(&prs)
if len(prs) == 0 {
return nil, err
}
return prs[0], err
}

// GetUnmergedPullRequestsByBaseInfo returns all pull requests that are open and has not been merged
// by given base information (repo and branch).
func GetUnmergedPullRequestsByBaseInfo(repoID int64, branch string) ([]*PullRequest, error) {
Expand Down
31 changes: 19 additions & 12 deletions routers/repo/branch.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,14 @@ const (

// Branch contains the branch information
type Branch struct {
Name string
Commit *git.Commit
IsProtected bool
IsDeleted bool
DeletedBranch *models.DeletedBranch
CommitsAhead int
CommitsBehind int
Name string
Commit *git.Commit
IsProtected bool
IsDeleted bool
DeletedBranch *models.DeletedBranch
CommitsAhead int
CommitsBehind int
LatestPullRequest *models.PullRequest
}

// Branches render repository branch page
Expand Down Expand Up @@ -178,12 +179,18 @@ func loadBranches(ctx *context.Context) []*Branch {
return nil
}

pr, err := models.GetLatestPullRequestsByHeadInfo(ctx.Repo.Repository.ID, branchName)
if pr != nil {
pr.LoadIssue()
}

branches[i] = &Branch{
Name: branchName,
Commit: commit,
IsProtected: isProtected,
CommitsAhead: divergence.Ahead,
CommitsBehind: divergence.Behind,
Name: branchName,
Commit: commit,
IsProtected: isProtected,
CommitsAhead: divergence.Ahead,
CommitsBehind: divergence.Behind,
LatestPullRequest: pr,
}
}

Expand Down
27 changes: 25 additions & 2 deletions templates/repo/branch/list.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
<table class="ui very basic striped fixed table single line">
<thead>
<tr>
<th class="seven wide">{{.i18n.Tr "repo.branch.name"}}</th>
<th class="six wide">{{.i18n.Tr "repo.branch.name"}}</th>
<th class="two wide"></th>
<th class="two wide"></th>
{{if and $.IsWriter (not $.IsMirror)}}
<th class="one wide right aligned">{{.i18n.Tr "repo.branch.delete_head"}}</th>
Expand All @@ -44,9 +45,10 @@
{{else}}
<a href="{{$.RepoLink}}/src/branch/{{.Name | EscapePound}}">{{.Name}}</a>
<p class="time">{{$.i18n.Tr "org.repo_updated"}} {{TimeSince .Commit.Committer.When $.i18n.Lang}}</p>
</td>
{{end}}
</td>
<td class="ui">
{{if not .IsDeleted}}
<div class="commit-divergence">
<div class="bar-group">
<div class="count count-behind">{{.CommitsBehind}}</div>
Expand All @@ -57,6 +59,27 @@
<div class="bar bar-ahead" style="width: {{percentage .CommitsAhead .CommitsBehind .CommitsAhead}}%"></div>
</div>
</div>
{{end}}
</td>
<td class="right aligned">

{{if not .LatestPullRequest}}
{{if not .IsDeleted}}
<a href="{{$.RepoLink}}/compare/{{$.DefaultBranch | EscapePound}}...{{if ne $.Repository.Owner.Name $.Owner.Name}}{{$.Owner.Name}}:{{end}}{{.Name | EscapePound}}">
<button id="new-pull-request" class="ui compact basic button">{{$.i18n.Tr "repo.pulls.compare_changes"}}</button>
</a>
{{end}}
{{else}}
#{{.LatestPullRequest.Issue.Index}}
{{if .LatestPullRequest.HasMerged}}
<a href="{{$.RepoLink}}/pulls/{{.LatestPullRequest.Issue.Index}}" class="ui purple large label"><i class="octicon octicon-git-pull-request"></i> {{$.i18n.Tr "repo.pulls.merged"}}</a>
{{else if .LatestPullRequest.Issue.IsClosed}}
<a href="{{$.RepoLink}}/pulls/{{.LatestPullRequest.Issue.Index}}" class="ui red large label"><i class="octicon octicon-issue-closed"></i> {{$.i18n.Tr "repo.issues.closed_title"}}</a>
{{else}}
<a href="{{$.RepoLink}}/pulls/{{.LatestPullRequest.Issue.Index}}" class="ui green large label"><i class="octicon octicon-issue-opened"></i> {{$.i18n.Tr "repo.issues.open_title"}}</a>
{{end}}
{{end}}

</td>
{{if and $.IsWriter (not $.IsMirror)}}
<td class="right aligned">
Expand Down