From b4510c7066933f9dec7e17d3c6dba40860ae4131 Mon Sep 17 00:00:00 2001 From: Mario Lubenka Date: Sun, 19 May 2019 11:47:48 +0200 Subject: [PATCH 1/8] Show Pull Request button or status of latest PR in branch list Signed-off-by: Mario Lubenka --- models/pull.go | 15 +++++++++++++++ routers/repo/branch.go | 31 +++++++++++++++++++------------ templates/repo/branch/list.tmpl | 23 +++++++++++++++++++++-- 3 files changed, 55 insertions(+), 14 deletions(-) diff --git a/models/pull.go b/models/pull.go index fe18765fc0c78..bf00b899231a3 100644 --- a/models/pull.go +++ b/models/pull.go @@ -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) { diff --git a/routers/repo/branch.go b/routers/repo/branch.go index ae87aa5b3a09d..9b2ad86dcbfb0 100644 --- a/routers/repo/branch.go +++ b/routers/repo/branch.go @@ -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 @@ -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, } } diff --git a/templates/repo/branch/list.tmpl b/templates/repo/branch/list.tmpl index 58e77f2c1146f..3b779fe2bc968 100644 --- a/templates/repo/branch/list.tmpl +++ b/templates/repo/branch/list.tmpl @@ -26,7 +26,8 @@ - + + {{if and $.IsWriter (not $.IsMirror)}} @@ -44,8 +45,8 @@ {{else}} {{.Name}}

{{$.i18n.Tr "org.repo_updated"}} {{TimeSince .Commit.Committer.When $.i18n.Lang}}

- {{end}} + + {{if and $.IsWriter (not $.IsMirror)}} {{if and $.IsWriter (not $.IsMirror)}}
{{.i18n.Tr "repo.branch.name"}}{{.i18n.Tr "repo.branch.name"}} {{.i18n.Tr "repo.branch.delete_head"}}
@@ -57,6 +58,24 @@
+
+ + {{if not .LatestPullRequest}} + + + + {{else}} + #{{.LatestPullRequest.Issue.Index}} + {{if .LatestPullRequest.HasMerged}} + {{$.i18n.Tr "repo.pulls.merged"}} + {{else if .LatestPullRequest.Issue.IsClosed}} + {{$.i18n.Tr "repo.issues.closed_title"}} + {{else}} + {{$.i18n.Tr "repo.issues.open_title"}} + {{end}} + {{end}} + From 213ee5791e130cc4029baf50af22495d21fb5ddc Mon Sep 17 00:00:00 2001 From: Mario Lubenka Date: Sun, 19 May 2019 12:26:15 +0200 Subject: [PATCH 2/8] Do not show pull request button on deleted branches Signed-off-by: Mario Lubenka --- templates/repo/branch/list.tmpl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/templates/repo/branch/list.tmpl b/templates/repo/branch/list.tmpl index 3b779fe2bc968..ed706874daf4d 100644 --- a/templates/repo/branch/list.tmpl +++ b/templates/repo/branch/list.tmpl @@ -62,9 +62,11 @@ {{if not .LatestPullRequest}} + {{if not .IsDeleted}} + {{end}} {{else}} #{{.LatestPullRequest.Issue.Index}} {{if .LatestPullRequest.HasMerged}} From c95e075a433fd84aa9bfcec448c49551c8e957c0 Mon Sep 17 00:00:00 2001 From: Mario Lubenka Date: Sun, 19 May 2019 12:27:17 +0200 Subject: [PATCH 3/8] Do not show commit divergence on deleted branches Signed-off-by: Mario Lubenka --- templates/repo/branch/list.tmpl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/templates/repo/branch/list.tmpl b/templates/repo/branch/list.tmpl index ed706874daf4d..3a232833b6fff 100644 --- a/templates/repo/branch/list.tmpl +++ b/templates/repo/branch/list.tmpl @@ -48,6 +48,7 @@ {{end}} + {{if not .IsDeleted}}
{{.CommitsBehind}}
@@ -58,6 +59,7 @@
+ {{end}}
From 0a782996158ab8ab27ce51fe762d827aa18c3a8c Mon Sep 17 00:00:00 2001 From: Mario Lubenka Date: Mon, 20 May 2019 11:52:57 +0200 Subject: [PATCH 4/8] Use XORMs Get instead of limit --- models/pull.go | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/models/pull.go b/models/pull.go index bf00b899231a3..9bfeb4d306bdc 100644 --- a/models/pull.go +++ b/models/pull.go @@ -1040,16 +1040,15 @@ func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequ // 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. + pr := new(PullRequest) + has, 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 + Get(pr) + if !has { + return nil, err } - return prs[0], err + return pr, err } // GetUnmergedPullRequestsByBaseInfo returns all pull requests that are open and has not been merged From 0b86c389003d16b768a663a424eb3b286f8de664 Mon Sep 17 00:00:00 2001 From: Mario Lubenka Date: Thu, 23 May 2019 20:07:46 +0200 Subject: [PATCH 5/8] Links pull request ID and use smaller labels for displaying the pull request status Signed-off-by: Mario Lubenka --- models/pull.go | 2 +- templates/repo/branch/list.tmpl | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/models/pull.go b/models/pull.go index 9bfeb4d306bdc..7d8e139a6d54e 100644 --- a/models/pull.go +++ b/models/pull.go @@ -1046,7 +1046,7 @@ func GetLatestPullRequestsByHeadInfo(repoID int64, branch string) (*PullRequest, OrderBy("id DESC"). Get(pr) if !has { - return nil, err + return nil, err } return pr, err } diff --git a/templates/repo/branch/list.tmpl b/templates/repo/branch/list.tmpl index 3a232833b6fff..ea711e6843cbd 100644 --- a/templates/repo/branch/list.tmpl +++ b/templates/repo/branch/list.tmpl @@ -70,13 +70,13 @@ {{end}} {{else}} - #{{.LatestPullRequest.Issue.Index}} + #{{.LatestPullRequest.Issue.Index}} {{if .LatestPullRequest.HasMerged}} - {{$.i18n.Tr "repo.pulls.merged"}} + {{$.i18n.Tr "repo.pulls.merged"}} {{else if .LatestPullRequest.Issue.IsClosed}} - {{$.i18n.Tr "repo.issues.closed_title"}} + {{$.i18n.Tr "repo.issues.closed_title"}} {{else}} - {{$.i18n.Tr "repo.issues.open_title"}} + {{$.i18n.Tr "repo.issues.open_title"}} {{end}} {{end}} From b7ae9c94e035a565e03172bf9ef60ea453147f87 Mon Sep 17 00:00:00 2001 From: Mario Lubenka Date: Sat, 8 Jun 2019 17:36:27 +0200 Subject: [PATCH 6/8] Handle error when getting latest pull request Signed-off-by: Mario Lubenka --- models/pull.go | 4 ++-- routers/repo/branch.go | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/models/pull.go b/models/pull.go index 7d8e139a6d54e..1e243e74fa887 100644 --- a/models/pull.go +++ b/models/pull.go @@ -1037,9 +1037,9 @@ func GetUnmergedPullRequestsByHeadInfo(repoID int64, branch string) ([]*PullRequ Find(&prs) } -// GetLatestPullRequestsByHeadInfo returns the latest pull request (regardless of its status) +// GetLatestPullRequestByHeadInfo returns the latest pull request (regardless of its status) // by given head information (repo and branch). -func GetLatestPullRequestsByHeadInfo(repoID int64, branch string) (*PullRequest, error) { +func GetLatestPullRequestByHeadInfo(repoID int64, branch string) (*PullRequest, error) { pr := new(PullRequest) has, err := x. Where("head_repo_id = ? AND head_branch = ?", repoID, branch). diff --git a/routers/repo/branch.go b/routers/repo/branch.go index 9b2ad86dcbfb0..7ebb95fc86419 100644 --- a/routers/repo/branch.go +++ b/routers/repo/branch.go @@ -179,7 +179,11 @@ func loadBranches(ctx *context.Context) []*Branch { return nil } - pr, err := models.GetLatestPullRequestsByHeadInfo(ctx.Repo.Repository.ID, branchName) + pr, err := models.GetLatestPullRequestByHeadInfo(ctx.Repo.Repository.ID, branchName) + if err != nil { + ctx.ServerError("GetLatestPullRequestByHeadInfo", err) + return nil + } if pr != nil { pr.LoadIssue() } From 0f9f338714c89ec1762a797e6bc29bf673bbf930 Mon Sep 17 00:00:00 2001 From: Mario Lubenka Date: Sat, 8 Jun 2019 17:37:06 +0200 Subject: [PATCH 7/8] Indent template Signed-off-by: Mario Lubenka --- templates/repo/branch/list.tmpl | 32 +++++++++++++++----------------- 1 file changed, 15 insertions(+), 17 deletions(-) diff --git a/templates/repo/branch/list.tmpl b/templates/repo/branch/list.tmpl index ea711e6843cbd..a2fb5c20697f3 100644 --- a/templates/repo/branch/list.tmpl +++ b/templates/repo/branch/list.tmpl @@ -62,24 +62,22 @@ {{end}} - - {{if not .LatestPullRequest}} - {{if not .IsDeleted}} - - - + {{if not .LatestPullRequest}} + {{if not .IsDeleted}} + + + + {{end}} + {{else}} + #{{.LatestPullRequest.Issue.Index}} + {{if .LatestPullRequest.HasMerged}} + {{$.i18n.Tr "repo.pulls.merged"}} + {{else if .LatestPullRequest.Issue.IsClosed}} + {{$.i18n.Tr "repo.issues.closed_title"}} + {{else}} + {{$.i18n.Tr "repo.issues.open_title"}} + {{end}} {{end}} - {{else}} - #{{.LatestPullRequest.Issue.Index}} - {{if .LatestPullRequest.HasMerged}} - {{$.i18n.Tr "repo.pulls.merged"}} - {{else if .LatestPullRequest.Issue.IsClosed}} - {{$.i18n.Tr "repo.issues.closed_title"}} - {{else}} - {{$.i18n.Tr "repo.issues.open_title"}} - {{end}} - {{end}} - From fd2f7a133a1ad4061168ee5068f8270c45f57fd3 Mon Sep 17 00:00:00 2001 From: Mario Lubenka Date: Mon, 17 Jun 2019 21:03:13 +0200 Subject: [PATCH 8/8] Check error when loading issue Signed-off-by: Mario Lubenka --- routers/repo/branch.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/routers/repo/branch.go b/routers/repo/branch.go index 6a44adad8a311..708b33be09bd6 100644 --- a/routers/repo/branch.go +++ b/routers/repo/branch.go @@ -188,7 +188,10 @@ func loadBranches(ctx *context.Context) []*Branch { return nil } if pr != nil { - pr.LoadIssue() + if err := pr.LoadIssue(); err != nil { + ctx.ServerError("pr.LoadIssue", err) + return nil + } } branches[i] = &Branch{