preparation: move url generation from RepositoryInfo towards the gui

This commit is contained in:
Herr Ritschwumm
2016-01-29 16:02:09 +01:00
parent e7c3376303
commit 63369258bd
11 changed files with 66 additions and 52 deletions

View File

@@ -180,11 +180,14 @@ abstract class ControllerBase extends ScalatraFilter
* Context object for the current request.
*/
case class Context(settings: SystemSettingsService.SystemSettings, loginAccount: Option[Account], request: HttpServletRequest){
val path = settings.baseUrl.getOrElse(request.getContextPath)
val currentPath = request.getRequestURI.substring(request.getContextPath.length)
val baseUrl = settings.baseUrl(request)
val host = new java.net.URL(baseUrl).getHost
val repoBase = RepoBase(
baseUrl,
if (settings.ssh) Some(SshAddress(host, settings.sshPortOrDefault)) else None
)
val platform = request.getHeader("User-Agent") match {
case null => null
case agent if agent.contains("Mac") => "mac"

View File

@@ -2,7 +2,7 @@ package gitbucket.core.service
import gitbucket.core.model.{Collaborator, Repository, Account}
import gitbucket.core.model.Profile._
import gitbucket.core.util.JGitUtil
import gitbucket.core.util.{JGitUtil, RepoBase}
import profile.simple._
trait RepositoryService { self: AccountService =>
@@ -392,23 +392,11 @@ trait RepositoryService { self: AccountService =>
object RepositoryService {
object RepositoryInfo {
def httpUrl(baseUrl:String, owner:String, name:String):String = s"${baseUrl}/git/${owner}/${name}.git"
def sshUrl(baseUrl:String, owner:String, name:String)(port: Int, userName: String):String = {
val host = """^https?://(.+?)(:\d+)?/""".r.findFirstMatchIn(baseUrl).get.group(1)
s"ssh://${userName}@${host}:${port}/${owner}/${name}.git"
}
}
case class RepositoryInfo(owner: String, name: String, repository: Repository,
httpUrl: String, sshUrl:(Int, String)=>String,
issueCount: Int, pullCount: Int, commitCount: Int, forkedCount: Int,
branchList: Seq[String], tags: Seq[JGitUtil.TagInfo], managers: Seq[String]){
branchList: Seq[String], tags: Seq[JGitUtil.TagInfo], managers: Seq[String]) {
def sshOpenRepoUrl(platform: String, port: Int, userName: String) = openRepoUrl(platform, sshUrl(port, userName))
def httpOpenRepoUrl(platform: String) = openRepoUrl(platform, httpUrl)
def openRepoUrl(platform: String, openUrl: String) = s"github-${platform}://openRepo/${openUrl}"
def urls(repoBase:RepoBase):RepositoryUrls = new RepositoryUrls(repoBase, owner, name)
/**
* Creates instance with issue count and pull request count.
@@ -416,8 +404,6 @@ object RepositoryService {
def this(repo: JGitUtil.RepositoryInfo, model: Repository, baseUrl:String, issueCount: Int, pullCount: Int, forkedCount: Int, managers: Seq[String]) =
this(
repo.owner, repo.name, model,
RepositoryInfo.httpUrl(baseUrl, repo.owner, repo.name),
RepositoryInfo.sshUrl(baseUrl, repo.owner, repo.name),
issueCount, pullCount,
repo.commitCount, forkedCount, repo.branchList, repo.tags, managers)
@@ -427,11 +413,27 @@ object RepositoryService {
def this(repo: JGitUtil.RepositoryInfo, model: Repository, baseUrl:String, forkedCount: Int, managers: Seq[String]) =
this(
repo.owner, repo.name, model,
RepositoryInfo.httpUrl(baseUrl, repo.owner, repo.name),
RepositoryInfo.sshUrl(baseUrl, repo.owner, repo.name),
0, 0,
repo.commitCount, forkedCount, repo.branchList, repo.tags, managers)
}
final class RepositoryUrls(repoBase:RepoBase, owner:String, name:String) {
def httpUrl:String =
s"${repoBase.baseUrl}/git/${owner}/${name}.git"
// BETTER make this return an Option and use it in the gui
def sshUrl(userName: String):String =
repoBase.sshAddress.fold("")(adr => s"ssh://${userName}@${adr.host}:${adr.port}/${owner}/${name}.git")
def sshOpenRepoUrl(platform: String, userName: String) =
openRepoUrl(platform, sshUrl(userName))
def httpOpenRepoUrl(platform: String) =
openRepoUrl(platform, httpUrl)
private def openRepoUrl(platform: String, openUrl: String) =
s"github-${platform}://openRepo/${openUrl}"
}
case class RepositoryTreeNode(owner: String, name: String, children: List[RepositoryTreeNode])
}

View File

@@ -38,10 +38,11 @@ object WikiService {
*/
case class WikiPageHistoryInfo(name: String, committer: String, message: String, date: Date)
def httpUrl(repository: RepositoryInfo) = repository.httpUrl.replaceFirst("\\.git\\Z", ".wiki.git")
def httpUrl(repoBase:RepoBase, repository: RepositoryInfo) =
repository.urls(repoBase).httpUrl.replaceFirst("\\.git\\Z", ".wiki.git")
def sshUrl(repository: RepositoryInfo, settings: SystemSettingsService.SystemSettings, userName: String) =
repository.sshUrl(settings.sshPortOrDefault, userName).replaceFirst("\\.git\\Z", ".wiki.git")
def sshUrl(repoBase:RepoBase, repository: RepositoryInfo, userName: String) =
repository.urls(repoBase).sshUrl(userName).replaceFirst("\\.git\\Z", ".wiki.git")
}
trait WikiService {

View File

@@ -0,0 +1,3 @@
package gitbucket.core.util
case class RepoBase(baseUrl:String, sshAddress:Option[SshAddress])

View File

@@ -0,0 +1,3 @@
package gitbucket.core.util
case class SshAddress(host:String, port:Int)

View File

@@ -60,6 +60,8 @@ object Markdown {
pages: List[String])
(implicit val context: Context) extends Renderer(options) with LinkConverter with RequestCache {
private val repositoryUrls = repository.urls(context.repoBase)
override def heading(text: String, level: Int, raw: String): String = {
val id = generateAnchorName(text)
val out = new StringBuilder()
@@ -135,7 +137,7 @@ object Markdown {
(link, link)
}
val url = repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/wiki/" + StringUtil.urlEncode(page)
val url = repositoryUrls.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/wiki/" + StringUtil.urlEncode(page)
if(pages.contains(page)){
"<a href=\"" + url + "\">" + escape(label) + "</a>"
} else {
@@ -157,14 +159,14 @@ object Markdown {
} else if(context.currentPath.contains("/tree/")){
val paths = context.currentPath.split("/")
val branch = if(paths.length > 3) paths.drop(4).mkString("/") else repository.repository.defaultBranch
repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/blob/" + branch + "/" + url + (if(isImage) "?raw=true" else "")
repositoryUrls.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/blob/" + branch + "/" + url + (if(isImage) "?raw=true" else "")
} else {
val paths = context.currentPath.split("/")
val branch = if(paths.length > 3) paths.last else repository.repository.defaultBranch
repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/blob/" + branch + "/" + url + (if(isImage) "?raw=true" else "")
repositoryUrls.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/blob/" + branch + "/" + url + (if(isImage) "?raw=true" else "")
}
} else {
repository.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/wiki/_blob/" + url
repositoryUrls.httpUrl.replaceFirst("/git/", "/").stripSuffix(".git") + "/wiki/_blob/" + url
}
}

View File

@@ -37,7 +37,7 @@
<script src="@assets/vendors/jquery-hotkeys/jquery.hotkeys.js"></script>
@repository.map { repository =>
@if(!repository.repository.isPrivate){
<meta name="go-import" content="@context.baseUrl.replaceFirst("^https?://", "")/@repository.owner/@repository.name git @repository.httpUrl" />
<meta name="go-import" content="@context.baseUrl.replaceFirst("^https?://", "")/@repository.owner/@repository.name git @repository.urls(repoBase).httpUrl" />
}
}
</head>

View File

@@ -80,8 +80,8 @@
<div class="small">
<strong id="repository-url-proto">HTTP</strong> <span class="mute">clone URL</span>
</div>
@helper.html.copy("repository-url-copy", repository.httpUrl){
<input type="text" value="@repository.httpUrl" id="repository-url" class="form-control input-sm" readonly>
@helper.html.copy("repository-url-copy", repository.urls(repoBase).httpUrl){
<input type="text" value="@repository.urls(repoBase).httpUrl" id="repository-url" class="form-control input-sm" readonly>
}
@if(settings.ssh && loginAccount.isDefined){
<div class="small">
@@ -91,7 +91,7 @@
@id.map { id =>
@if(context.platform != "linux" && context.platform != null){
<div style="margin-top: 10px;">
<a href="@repository.httpOpenRepoUrl(context.platform)" id="repository-clone-url" class="btn btn-sm btn-default btn-block"><i class="octicon octicon-desktop-download"></i>&nbsp;&nbsp;Clone in Desktop</a>
<a href="@repository.urls(repoBase).httpOpenRepoUrl(context.platform)" id="repository-clone-url" class="btn btn-sm btn-default btn-block"><i class="octicon octicon-desktop-download"></i>&nbsp;&nbsp;Clone in Desktop</a>
</div>
}
<div style="margin-top: 10px;">
@@ -184,15 +184,15 @@ $(function(){
@if(settings.ssh && loginAccount.isDefined){
$('#repository-url-http').click(function(){
$('#repository-url-proto').text('HTTP');
$('#repository-url').val('@repository.httpUrl');
$('#repository-clone-url').attr('href', '@repository.httpOpenRepoUrl(context.platform)')
$('#repository-url').val('@repository.urls(repoBase).httpUrl');
$('#repository-clone-url').attr('href', '@repository.urls(repoBase).httpOpenRepoUrl(context.platform)')
$('#repository-url-copy').attr('data-clipboard-text', $('#repository-url').val());
});
$('#repository-url-ssh').click(function(){
$('#repository-url-proto').text('SSH');
$('#repository-url').val('@repository.sshUrl(settings.sshPortOrDefault, loginAccount.get.userName)');
$('#repository-clone-url').attr('href', '@repository.sshOpenRepoUrl(context.platform, settings.sshPortOrDefault, loginAccount.get.userName)');
$('#repository-url').val('@repository.urls(repoBase).sshUrl(loginAccount.get.userName)');
$('#repository-clone-url').attr('href', '@repository.urls(repoBase).sshOpenRepoUrl(context.platform, loginAccount.get.userName)');
$('#repository-url-copy').attr('data-clipboard-text', $('#repository-url').val());
});
}

View File

@@ -100,21 +100,21 @@
you can perform a manual merge on the command line.
</p>
}
@helper.html.copy("repository-url-copy", forkedRepository.httpUrl, true){
@helper.html.copy("repository-url-copy", forkedRepository.urls(repoBase).httpUrl, true){
<div class="btn-group" data-toggle="buttons-radio">
<button class="btn btn-small active" type="button" id="repository-url-http">HTTP</button>
@if(settings.ssh && loginAccount.isDefined){
<button class="btn btn-small" type="button" id="repository-url-ssh" style="border-radius: 0px;">SSH</button>
}
</div>
<input type="text" style="width: 500px;" value="@forkedRepository.httpUrl" id="repository-url" readonly />
<input type="text" style="width: 500px;" value="@forkedRepository.urls(repoBase).httpUrl" id="repository-url" readonly />
}
<div>
<p>
<span class="strong">Step 1:</span> From your project repository, check out a new branch and test the changes.
</p>
@defining(s"git checkout -b ${pullreq.requestUserName}-${pullreq.requestBranch} ${pullreq.branch}\n" +
s"git pull ${forkedRepository.httpUrl} ${pullreq.requestBranch}"){ command =>
s"git pull ${forkedRepository.urls(repoBase).httpUrl} ${pullreq.requestBranch}"){ command =>
@helper.html.copy("merge-command-copy-1", command){
<pre style="width: 600px; float: left; font-size: 12px; border-radius: 3px 0px 3px 3px;" id="merge-command">@Html(command)</pre>
}
@@ -174,24 +174,24 @@ $(function(){
@if(settings.ssh && loginAccount.isDefined){
$('#repository-url-http').click(function(){
// Update URL box
$('#repository-url').val('@forkedRepository.httpUrl');
$('#repository-url').val('@forkedRepository.urls(repoBase).httpUrl');
$('#repository-url-copy').attr('data-clipboard-text', $('#repository-url').val());
// Update command guidance
$('#merge-command').text($('#merge-command').text().replace(
'@forkedRepository.sshUrl(settings.sshPortOrDefault, loginAccount.get.userName)',
'@forkedRepository.httpUrl'
'@forkedRepository.urls(repoBase).sshUrl(loginAccount.get.userName)',
'@forkedRepository.urls(repoBase).httpUrl'
));
$('#merge-command-copy-1').attr('data-clipboard-text', $('#merge-command').text());
});
$('#repository-url-ssh').click(function(){
// Update URL box
$('#repository-url').val('@forkedRepository.sshUrl(settings.sshPortOrDefault, loginAccount.get.userName)');
$('#repository-url').val('@forkedRepository.urls(repoBase).sshUrl(loginAccount.get.userName)');
$('#repository-url-copy').attr('data-clipboard-text', $('#repository-url').val());
// Update command guidance
$('#merge-command').text($('#merge-command').text().replace(
'@forkedRepository.httpUrl',
'@forkedRepository.sshUrl(settings.sshPortOrDefault, loginAccount.get.userName)'
'@forkedRepository.urls(repoBase).httpUrl',
'@forkedRepository.urls(repoBase).sshUrl(loginAccount.get.userName)'
));
$('#merge-command-copy-1').attr('data-clipboard-text', $('#merge-command').text());
});

View File

@@ -10,10 +10,10 @@
} else {
<h3><strong>Quick setup</strong> — if you've done this kind of thing before</h3>
<div class="empty-repo-options">
via <a href="@repository.httpUrl" class="git-protocol-selector">HTTP</a>
via <a href="@repository.urls(repoBase).httpUrl" class="git-protocol-selector">HTTP</a>
@if(settings.ssh && loginAccount.isDefined){
or
<a href="@repository.sshUrl(settings.sshPortOrDefault, loginAccount.get.userName)" class="git-protocol-selector">SSH</a>
<a href="@repository.urls(repoBase).sshUrl(loginAccount.get.userName)" class="git-protocol-selector">SSH</a>
}
</div>
<h3 style="margin-top: 30px;">Create a new repository on the command line</h3>
@@ -22,12 +22,12 @@
git init
git add README.md
git commit -m "first commit"
git remote add origin <span class="live-clone-url">@repository.httpUrl</span>
git remote add origin <span class="live-clone-url">@repository.urls(repoBase).httpUrl</span>
git push -u origin master
}
<h3 style="margin-top: 30px;">Push an existing repository from the command line</h3>
@pre {
git remote add origin <span class="live-clone-url">@repository.httpUrl</span>
git remote add origin <span class="live-clone-url">@repository.urls(repoBase).httpUrl</span>
git push -u origin master
}
<script>

View File

@@ -67,8 +67,8 @@
<div class="small">
<strong>Clone this wiki locally</strong>
</div>
@helper.html.copy("repository-url-copy", httpUrl(repository)){
<input type="text" value="@httpUrl(repository)" id="repository-url" class="form-control input-sm" readonly>
@helper.html.copy("repository-url-copy", httpUrl(repoBase, repository)){
<input type="text" value="@httpUrl(repoBase, repository)" id="repository-url" class="form-control input-sm" readonly>
}
@if(settings.ssh && loginAccount.isDefined){
<div class="small">
@@ -133,11 +133,11 @@ $(function(){
@if(settings.ssh && loginAccount.isDefined){
$('#repository-url-http').click(function(){
$('#repository-url').val('@httpUrl(repository)');
$('#repository-url').val('@httpUrl(repoBase, repository)');
$('#repository-url-copy').attr('data-clipboard-text', $('#repository-url').val());
});
$('#repository-url-ssh').click(function(){
$('#repository-url').val('@sshUrl(repository, settings, loginAccount.get.userName)');
$('#repository-url').val('@sshUrl(repoBase, repository, loginAccount.get.userName)');
$('#repository-url-copy').attr('data-clipboard-text', $('#repository-url').val());
});
}