Pullreqs in forks now use upstream master as origin

This implements #668.
This commit is contained in:
Boris Bera
2015-07-03 20:56:06 -04:00
parent 3d5e4a4225
commit fd4fe0dc0d
3 changed files with 52 additions and 3 deletions

View File

@@ -4,7 +4,7 @@ import java.text.SimpleDateFormat
import java.util.{Date, Locale, TimeZone} import java.util.{Date, Locale, TimeZone}
import gitbucket.core.controller.Context import gitbucket.core.controller.Context
import gitbucket.core.model.CommitState import gitbucket.core.model.{CommitState, Repository}
import gitbucket.core.plugin.{RenderRequest, PluginRegistry, Renderer} import gitbucket.core.plugin.{RenderRequest, PluginRegistry, Renderer}
import gitbucket.core.service.{RepositoryService, RequestCache} import gitbucket.core.service.{RepositoryService, RequestCache}
import gitbucket.core.util.{FileUtil, JGitUtil, StringUtil} import gitbucket.core.util.{FileUtil, JGitUtil, StringUtil}
@@ -164,6 +164,18 @@ object helpers extends AvatarImageProvider with LinkConverter with RequestCache
def urlEncode(value: Option[String]): String = value.map(urlEncode).getOrElse("") def urlEncode(value: Option[String]): String = value.map(urlEncode).getOrElse("")
/**
* The default origin (branch or remote:branch pair) branches are compared to.
*
* There are two cases: when the repo is a fork and when the repo is not a
* fork.
*
* For a fork, the default ref is parentUserName:defaultBranch.
* For a non fork, the default ref is defaultBranch.
*/
def repositoryDefaultCompareOrigin(repo: Repository): String =
repo.parentUserName.map(n => s"$n:${repo.defaultBranch}").getOrElse(repo.defaultBranch)
/** /**
* Generates the url to the repository. * Generates the url to the repository.
*/ */

View File

@@ -30,9 +30,9 @@
} }
}.getOrElse{ }.getOrElse{
@if(context.loginAccount.isDefined){ @if(context.loginAccount.isDefined){
<a href="@url(repository)/compare/@{encodeRefName(repository.repository.defaultBranch)}...@{encodeRefName(branch.name)}?expand=1" class="btn btn-small">New Pull Request</a> <a href="@url(repository)/compare/@{encodeRefName(repositoryDefaultCompareOrigin(repository.repository))}...@{encodeRefName(branch.name)}?expand=1" class="btn btn-small">New Pull Request</a>
}else{ }else{
<a href="@url(repository)/compare/@{encodeRefName(repository.repository.defaultBranch)}...@{encodeRefName(branch.name)}" class="btn btn-small">Compare</a> <a href="@url(repository)/compare/@{encodeRefName(repositoryDefaultCompareOrigin(repository.repository))}...@{encodeRefName(branch.name)}" class="btn btn-small">Compare</a>
} }
} }
@if(hasWritePermission){ @if(hasWritePermission){

View File

@@ -0,0 +1,37 @@
package gitbucket.core.view
import org.specs2.mutable._
import gitbucket.core.model.Repository
import java.util.Date
class HelpersSpec extends Specification {
def repository(defaultBranch: String, parentUserName: Option[String]) =
Repository(
userName = "some-user",
repositoryName = "some-repo",
isPrivate = false,
description = None,
defaultBranch = defaultBranch,
parentUserName = parentUserName,
parentRepositoryName = Some("some-repo"),
registeredDate = new Date(),
updatedDate = new Date(),
lastActivityDate = new Date(),
originUserName = Some("some-other-user"),
originRepositoryName = Some("some-repo")
)
"repositoryDefaultCompareOrigin" should {
"return default branch when not fork" in {
val repo = repository("master", None)
helpers.repositoryDefaultCompareOrigin(repo) mustEqual "master"
}
"return [upstream]:[branch] when a fork" in {
val repo = repository("some-branch", Some("parent-user"))
helpers.repositoryDefaultCompareOrigin(repo) mustEqual "parent-user:some-branch"
}
}
}