From 4c82485424adb0fcf72cf9cde74596a7f0f54ada Mon Sep 17 00:00:00 2001 From: Jimmy Praet Date: Thu, 18 Feb 2021 03:45:49 +0100 Subject: [PATCH] #14699 Make branch names in PR description clickable (#14716) --- models/pull.go | 25 +++++++++++++++++++++++++ routers/repo/pull.go | 2 ++ templates/repo/issue/view_title.tmpl | 20 ++++++++++++++------ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/models/pull.go b/models/pull.go index 0d4691aac94b..0eba65db4fad 100644 --- a/models/pull.go +++ b/models/pull.go @@ -13,6 +13,7 @@ import ( "code.gitea.io/gitea/modules/log" "code.gitea.io/gitea/modules/setting" "code.gitea.io/gitea/modules/timeutil" + "code.gitea.io/gitea/modules/util" ) // PullRequestType defines pull request type @@ -638,3 +639,27 @@ func (pr *PullRequest) updateCommitDivergence(e Engine, ahead, behind int) error func (pr *PullRequest) IsSameRepo() bool { return pr.BaseRepoID == pr.HeadRepoID } + +// GetBaseBranchHTMLURL returns the HTML URL of the base branch +func (pr *PullRequest) GetBaseBranchHTMLURL() string { + if err := pr.LoadBaseRepo(); err != nil { + log.Error("LoadBaseRepo: %v", err) + return "" + } + if pr.BaseRepo == nil { + return "" + } + return pr.BaseRepo.HTMLURL() + "/src/branch/" + util.PathEscapeSegments(pr.BaseBranch) +} + +// GetHeadBranchHTMLURL returns the HTML URL of the head branch +func (pr *PullRequest) GetHeadBranchHTMLURL() string { + if err := pr.LoadHeadRepo(); err != nil { + log.Error("LoadHeadRepo: %v", err) + return "" + } + if pr.HeadRepo == nil { + return "" + } + return pr.HeadRepo.HTMLURL() + "/src/branch/" + util.PathEscapeSegments(pr.HeadBranch) +} diff --git a/routers/repo/pull.go b/routers/repo/pull.go index d3dd9e4fb544..15af2a2a3f33 100644 --- a/routers/repo/pull.go +++ b/routers/repo/pull.go @@ -301,6 +301,8 @@ func setMergeTarget(ctx *context.Context, pull *models.PullRequest) { ctx.Data["HeadTarget"] = pull.MustHeadUserName() + "/" + pull.HeadRepo.Name + ":" + pull.HeadBranch } ctx.Data["BaseTarget"] = pull.BaseBranch + ctx.Data["HeadBranchHTMLURL"] = pull.GetHeadBranchHTMLURL() + ctx.Data["BaseBranchHTMLURL"] = pull.GetBaseBranchHTMLURL() } // PrepareMergedViewPullInfo show meta information for a merged pull request view page diff --git a/templates/repo/issue/view_title.tmpl b/templates/repo/issue/view_title.tmpl index 8956bf5e65d1..2f2787d67ceb 100644 --- a/templates/repo/issue/view_title.tmpl +++ b/templates/repo/issue/view_title.tmpl @@ -30,23 +30,31 @@ {{end}} {{if .Issue.IsPull}} + {{$headHref := .HeadTarget|Escape}} + {{if .HeadBranchHTMLURL}} + {{$headHref = printf "%s" .HeadBranchHTMLURL $headHref}} + {{end}} + {{$baseHref := .BaseTarget|Escape}} + {{if .BaseBranchHTMLURL}} + {{$baseHref = printf "%s" .BaseBranchHTMLURL $baseHref}} + {{end}} {{if .Issue.PullRequest.HasMerged}} {{ $mergedStr:= TimeSinceUnix .Issue.PullRequest.MergedUnix $.Lang }} {{if .Issue.OriginalAuthor }} {{.Issue.OriginalAuthor}} - {{$.i18n.Tr "repo.pulls.merged_title_desc" .NumCommits (.HeadTarget|Escape) (.BaseTarget|Escape) $mergedStr | Str2html}} + {{$.i18n.Tr "repo.pulls.merged_title_desc" .NumCommits $headHref $baseHref $mergedStr | Str2html}} {{else}} {{.Issue.PullRequest.Merger.GetDisplayName}} - {{$.i18n.Tr "repo.pulls.merged_title_desc" .NumCommits (.HeadTarget|Escape) (.BaseTarget|Escape) $mergedStr | Str2html}} + {{$.i18n.Tr "repo.pulls.merged_title_desc" .NumCommits $headHref $baseHref $mergedStr | Str2html}} {{end}} {{else}} {{if .Issue.OriginalAuthor }} - {{.Issue.OriginalAuthor}} {{$.i18n.Tr "repo.pulls.title_desc" .NumCommits (.HeadTarget|Escape) (.BaseTarget|Escape) | Str2html}} + {{.Issue.OriginalAuthor}} {{$.i18n.Tr "repo.pulls.title_desc" .NumCommits $headHref $baseHref | Str2html}} {{else}} - {{.Issue.Poster.GetDisplayName}} - {{$.i18n.Tr "repo.pulls.title_desc" .NumCommits (.HeadTarget|Escape) (.BaseTarget|Escape) | Str2html}} - + {{.Issue.Poster.GetDisplayName}} + {{$.i18n.Tr "repo.pulls.title_desc" .NumCommits $headHref $baseHref | Str2html}} + {{end}}