mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-12 16:35:52 +01:00
(refs #947)Fix referenced link from pull request
This commit is contained in:
@@ -7,13 +7,31 @@ import gitbucket.core.util.Implicits.RichString
|
|||||||
trait LinkConverter { self: RequestCache =>
|
trait LinkConverter { self: RequestCache =>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Converts issue id, username and commit id to link.
|
* Creates a link to the issue or the pull request from the issue id.
|
||||||
*/
|
*/
|
||||||
protected def convertRefsLinks(value: String, repository: RepositoryService.RepositoryInfo,
|
protected def createIssueLink(repository: RepositoryService.RepositoryInfo, issueId: Int)(implicit context: Context): String = {
|
||||||
|
val userName = repository.repository.userName
|
||||||
|
val repositoryName = repository.repository.repositoryName
|
||||||
|
|
||||||
|
getIssue(userName, repositoryName, issueId.toString) match {
|
||||||
|
case Some(issue) if (issue.isPullRequest) =>
|
||||||
|
s"""<a href="${context.path}/${userName}/${repositoryName}/pull/${issueId}">Pull #${issueId}</a>"""
|
||||||
|
case Some(_) =>
|
||||||
|
s"""<a href="${context.path}/${userName}/${repositoryName}/issues/${issueId}">Issue #${issueId}</a>"""
|
||||||
|
case None =>
|
||||||
|
s"Unknown #${issueId}"
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts issue id, username and commit id to link in the given text.
|
||||||
|
*/
|
||||||
|
protected def convertRefsLinks(text: String, repository: RepositoryService.RepositoryInfo,
|
||||||
issueIdPrefix: String = "#", escapeHtml: Boolean = true)(implicit context: Context): String = {
|
issueIdPrefix: String = "#", escapeHtml: Boolean = true)(implicit context: Context): String = {
|
||||||
|
|
||||||
// escape HTML tags
|
// escape HTML tags
|
||||||
val escaped = if(escapeHtml) value.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """) else value
|
val escaped = if(escapeHtml) text.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """) else text
|
||||||
|
|
||||||
escaped
|
escaped
|
||||||
// convert username/project@SHA to link
|
// convert username/project@SHA to link
|
||||||
@@ -26,10 +44,12 @@ trait LinkConverter { self: RequestCache =>
|
|||||||
// convert username/project#Num to link
|
// convert username/project#Num to link
|
||||||
.replaceBy( ("(?<=(^|\\W))([a-zA-Z0-9\\-_]+)/([a-zA-Z0-9\\-_\\.]+)" + issueIdPrefix + "([0-9]+)(?=(\\W|$))").r){ m =>
|
.replaceBy( ("(?<=(^|\\W))([a-zA-Z0-9\\-_]+)/([a-zA-Z0-9\\-_\\.]+)" + issueIdPrefix + "([0-9]+)(?=(\\W|$))").r){ m =>
|
||||||
getIssue(m.group(2), m.group(3), m.group(4)) match {
|
getIssue(m.group(2), m.group(3), m.group(4)) match {
|
||||||
case Some(issue) if (issue.isPullRequest)
|
case Some(issue) if (issue.isPullRequest) =>
|
||||||
=> Some( s"""<a href="${context.path}/${m.group(2)}/${m.group(3)}/pull/${m.group(4)}">${m.group(2)}/${m.group(3)}#${m.group(4)}</a>""")
|
Some(s"""<a href="${context.path}/${m.group(2)}/${m.group(3)}/pull/${m.group(4)}">${m.group(2)}/${m.group(3)}#${m.group(4)}</a>""")
|
||||||
case Some(_) => Some( s"""<a href="${context.path}/${m.group(2)}/${m.group(3)}/issues/${m.group(4)}">${m.group(2)}/${m.group(3)}#${m.group(4)}</a>""")
|
case Some(_) =>
|
||||||
case None => Some( s"""${m.group(2)}/${m.group(3)}#${m.group(4)}""")
|
Some(s"""<a href="${context.path}/${m.group(2)}/${m.group(3)}/issues/${m.group(4)}">${m.group(2)}/${m.group(3)}#${m.group(4)}</a>""")
|
||||||
|
case None =>
|
||||||
|
Some(s"""${m.group(2)}/${m.group(3)}#${m.group(4)}""")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -43,10 +63,12 @@ trait LinkConverter { self: RequestCache =>
|
|||||||
// convert username#Num to link
|
// convert username#Num to link
|
||||||
.replaceBy( ("(?<=(^|\\W))([a-zA-Z0-9\\-_]+)" + issueIdPrefix + "([0-9]+)(?=(\\W|$))").r ) { m =>
|
.replaceBy( ("(?<=(^|\\W))([a-zA-Z0-9\\-_]+)" + issueIdPrefix + "([0-9]+)(?=(\\W|$))").r ) { m =>
|
||||||
getIssue(m.group(2), repository.name, m.group(3)) match {
|
getIssue(m.group(2), repository.name, m.group(3)) match {
|
||||||
case Some(issue) if(issue.isPullRequest)
|
case Some(issue) if(issue.isPullRequest) =>
|
||||||
=> Some(s"""<a href="${context.path}/${m.group(2)}/${repository.name}/pull/${m.group(3)}">${m.group(2)}#${m.group(3)}</a>""")
|
Some(s"""<a href="${context.path}/${m.group(2)}/${repository.name}/pull/${m.group(3)}">${m.group(2)}#${m.group(3)}</a>""")
|
||||||
case Some(_) => Some(s"""<a href="${context.path}/${m.group(2)}/${repository.name}/issues/${m.group(3)}">${m.group(2)}#${m.group(3)}</a>""")
|
case Some(_) =>
|
||||||
case None => Some(s"""${m.group(2)}#${m.group(3)}""")
|
Some(s"""<a href="${context.path}/${m.group(2)}/${repository.name}/issues/${m.group(3)}">${m.group(2)}#${m.group(3)}</a>""")
|
||||||
|
case None =>
|
||||||
|
Some(s"""${m.group(2)}#${m.group(3)}""")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,10 +76,12 @@ trait LinkConverter { self: RequestCache =>
|
|||||||
.replaceBy(("(?<=(^|\\W))(GH-|" + issueIdPrefix + ")([0-9]+)(?=(\\W|$))").r){ m =>
|
.replaceBy(("(?<=(^|\\W))(GH-|" + issueIdPrefix + ")([0-9]+)(?=(\\W|$))").r){ m =>
|
||||||
val prefix = if(m.group(2) == "issue:") "#" else m.group(2)
|
val prefix = if(m.group(2) == "issue:") "#" else m.group(2)
|
||||||
getIssue(repository.owner, repository.name, m.group(3)) match {
|
getIssue(repository.owner, repository.name, m.group(3)) match {
|
||||||
case Some(issue) if(issue.isPullRequest)
|
case Some(issue) if(issue.isPullRequest) =>
|
||||||
=> Some(s"""<a href="${context.path}/${repository.owner}/${repository.name}/pull/${m.group(3)}">${prefix}${m.group(3)}</a>""")
|
Some(s"""<a href="${context.path}/${repository.owner}/${repository.name}/pull/${m.group(3)}">${prefix}${m.group(3)}</a>""")
|
||||||
case Some(_) => Some(s"""<a href="${context.path}/${repository.owner}/${repository.name}/issues/${m.group(3)}">${prefix}${m.group(3)}</a>""")
|
case Some(_) =>
|
||||||
case None => Some(s"""${m.group(2)}${m.group(3)}""")
|
Some(s"""<a href="${context.path}/${repository.owner}/${repository.name}/issues/${m.group(3)}">${prefix}${m.group(3)}</a>""")
|
||||||
|
case None =>
|
||||||
|
Some(s"""${m.group(2)}${m.group(3)}""")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -93,6 +93,10 @@ object helpers extends AvatarImageProvider with LinkConverter with RequestCache
|
|||||||
pages: List[String] = Nil)(implicit context: Context): Html =
|
pages: List[String] = Nil)(implicit context: Context): Html =
|
||||||
Html(Markdown.toHtml(value, repository, enableWikiLink, enableRefsLink, true, enableTaskList, hasWritePermission, pages))
|
Html(Markdown.toHtml(value, repository, enableWikiLink, enableRefsLink, true, enableTaskList, hasWritePermission, pages))
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Render the given source (only markdown is supported in default) as HTML.
|
||||||
|
* You can test if a file is renderable in this method by [[isRenderable()]].
|
||||||
|
*/
|
||||||
def renderMarkup(filePath: List[String], fileContent: String, branch: String,
|
def renderMarkup(filePath: List[String], fileContent: String, branch: String,
|
||||||
repository: RepositoryService.RepositoryInfo,
|
repository: RepositoryService.RepositoryInfo,
|
||||||
enableWikiLink: Boolean, enableRefsLink: Boolean, enableAnchor: Boolean)(implicit context: Context): Html = {
|
enableWikiLink: Boolean, enableRefsLink: Boolean, enableAnchor: Boolean)(implicit context: Context): Html = {
|
||||||
@@ -103,10 +107,20 @@ object helpers extends AvatarImageProvider with LinkConverter with RequestCache
|
|||||||
renderer.render(RenderRequest(filePath, fileContent, branch, repository, enableWikiLink, enableRefsLink, enableAnchor, context))
|
renderer.render(RenderRequest(filePath, fileContent, branch, repository, enableWikiLink, enableRefsLink, enableAnchor, context))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Tests whether the given file is renderable. It's tested by the file extension.
|
||||||
|
*/
|
||||||
def isRenderable(fileName: String): Boolean = {
|
def isRenderable(fileName: String): Boolean = {
|
||||||
PluginRegistry().renderableExtensions.exists(extension => fileName.toLowerCase.endsWith("." + extension))
|
PluginRegistry().renderableExtensions.exists(extension => fileName.toLowerCase.endsWith("." + extension))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Creates a link to the issue or the pull request from the issue id.
|
||||||
|
*/
|
||||||
|
def issueLink(repository: RepositoryService.RepositoryInfo, issueId: Int)(implicit context: Context): Html = {
|
||||||
|
Html(createIssueLink(repository, issueId))
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns <img> which displays the avatar icon for the given user name.
|
* Returns <img> which displays the avatar icon for the given user name.
|
||||||
* This method looks up Gravatar if avatar icon has not been configured in user settings.
|
* This method looks up Gravatar if avatar icon has not been configured in user settings.
|
||||||
|
|||||||
@@ -55,7 +55,7 @@
|
|||||||
} else {
|
} else {
|
||||||
@if(comment.action == "refer"){
|
@if(comment.action == "refer"){
|
||||||
@defining(comment.content.split(":")){ case Array(issueId, rest @ _*) =>
|
@defining(comment.content.split(":")){ case Array(issueId, rest @ _*) =>
|
||||||
<strong><a href="@path/@repository.owner/@repository.name/issues/@issueId">Issue #@issueId</a>: @rest.mkString(":")</strong>
|
<strong>@issueLink(repository, issueId.toInt): @rest.mkString(":")</strong>
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
<div class="markdown-body">@markdown(comment.content, repository, false, true, true, hasWritePermission)</div>
|
<div class="markdown-body">@markdown(comment.content, repository, false, true, true, hasWritePermission)</div>
|
||||||
|
|||||||
Reference in New Issue
Block a user