From bef5b5f22e09def1239e6841826cdd6e1939b462 Mon Sep 17 00:00:00 2001 From: tomoki1207 Date: Thu, 12 Jan 2017 17:13:17 +0900 Subject: [PATCH] Issue and PR template per repository --- .../core/controller/IssuesController.scala | 1 + .../controller/PullRequestsController.scala | 1 + .../core/service/RepositoryService.scala | 37 +++++++++++++++++++ .../gitbucket/core/issues/create.scala.html | 3 +- .../gitbucket/core/pulls/compare.scala.html | 3 +- 5 files changed, 43 insertions(+), 2 deletions(-) diff --git a/src/main/scala/gitbucket/core/controller/IssuesController.scala b/src/main/scala/gitbucket/core/controller/IssuesController.scala index 5969cb443..f55db681f 100644 --- a/src/main/scala/gitbucket/core/controller/IssuesController.scala +++ b/src/main/scala/gitbucket/core/controller/IssuesController.scala @@ -107,6 +107,7 @@ trait IssuesControllerBase extends ControllerBase { getMilestones(owner, name), getLabels(owner, name), isIssueManageable(repository), + getContentTemplate(repository, "ISSUE_TEMPLATE"), repository) } } else Unauthorized() diff --git a/src/main/scala/gitbucket/core/controller/PullRequestsController.scala b/src/main/scala/gitbucket/core/controller/PullRequestsController.scala index e33fca170..860942196 100644 --- a/src/main/scala/gitbucket/core/controller/PullRequestsController.scala +++ b/src/main/scala/gitbucket/core/controller/PullRequestsController.scala @@ -368,6 +368,7 @@ trait PullRequestsControllerBase extends ControllerBase { forkedId, oldId.getName, newId.getName, + getContentTemplate(originRepository, "PULL_REQUEST_TEMPLATE"), forkedRepository, originRepository, forkedRepository, diff --git a/src/main/scala/gitbucket/core/service/RepositoryService.scala b/src/main/scala/gitbucket/core/service/RepositoryService.scala index 30b4d5cbe..1fee8f754 100644 --- a/src/main/scala/gitbucket/core/service/RepositoryService.scala +++ b/src/main/scala/gitbucket/core/service/RepositoryService.scala @@ -3,7 +3,14 @@ package gitbucket.core.service import gitbucket.core.controller.Context import gitbucket.core.model.{Collaborator, Repository, RepositoryOptions, Account, Role} import gitbucket.core.model.Profile._ +import gitbucket.core.plugin.PluginRegistry import gitbucket.core.util.JGitUtil +import gitbucket.core.util.JGitUtil.FileInfo +import gitbucket.core.util.ControlUtil._ +import gitbucket.core.util.Directory +import gitbucket.core.util.FileUtil +import gitbucket.core.util.StringUtil +import org.eclipse.jgit.api.Git import profile.simple._ trait RepositoryService { self: AccountService => @@ -416,6 +423,36 @@ trait RepositoryService { self: AccountService => } .sortBy(_.userName asc).map(t => t.userName -> t.repositoryName).list + private val templateExtensions = Seq("md", "markdown") + + /** + * Returns content of template set per repository. + * + * @param repository the repository information + * @param fileBaseName the file basename without extension of template + * @return The content of template if the repository has it, otherwise empty string. + */ + def getContentTemplate(repository: RepositoryInfo, fileBaseName: String)(implicit s: Session): String = { + val withExtFilenames = templateExtensions.map(extension => s"${fileBaseName.toLowerCase()}.${extension}") + + def choiceTemplate(files: List[FileInfo]): Option[FileInfo] = + files.find { f => + f.name.toLowerCase() == fileBaseName + }.orElse { + files.find(f => withExtFilenames.contains(f.name.toLowerCase())) + } + + // Get template file from project root. When didn't find, will lookup default folder. + using(Git.open(Directory.getRepositoryDir(repository.owner, repository.name))) { git => + choiceTemplate(JGitUtil.getFileList(git, repository.repository.defaultBranch, ".")).orElse { + choiceTemplate(JGitUtil.getFileList(git, repository.repository.defaultBranch, ".gitbucket")) + }.map { file => + JGitUtil.getContentFromId(git, file.id, true).collect { + case bytes if FileUtil.isText(bytes) => StringUtil.convertFromByteArray(bytes) + } + } getOrElse None + } getOrElse "" + } } object RepositoryService { diff --git a/src/main/twirl/gitbucket/core/issues/create.scala.html b/src/main/twirl/gitbucket/core/issues/create.scala.html index a418a2b1e..f72df6bfc 100644 --- a/src/main/twirl/gitbucket/core/issues/create.scala.html +++ b/src/main/twirl/gitbucket/core/issues/create.scala.html @@ -2,6 +2,7 @@ milestones: List[gitbucket.core.model.Milestone], labels: List[gitbucket.core.model.Label], isManageable: Boolean, + content: String, repository: gitbucket.core.service.RepositoryService.RepositoryInfo)(implicit context: gitbucket.core.controller.Context) @import gitbucket.core.view.helpers @gitbucket.core.html.main(s"New Issue - ${repository.owner}/${repository.name}", Some(repository)){ @@ -13,7 +14,7 @@ @gitbucket.core.helper.html.preview( repository = repository, - content = "", + content = content, enableWikiLink = false, enableRefsLink = true, enableLineBreaks = true, diff --git a/src/main/twirl/gitbucket/core/pulls/compare.scala.html b/src/main/twirl/gitbucket/core/pulls/compare.scala.html index 06d00ff47..d97b36f0e 100644 --- a/src/main/twirl/gitbucket/core/pulls/compare.scala.html +++ b/src/main/twirl/gitbucket/core/pulls/compare.scala.html @@ -7,6 +7,7 @@ forkedId: String, sourceId: String, commitId: String, + content: String, repository: gitbucket.core.service.RepositoryService.RepositoryInfo, originRepository: gitbucket.core.service.RepositoryService.RepositoryInfo, forkedRepository: gitbucket.core.service.RepositoryService.RepositoryInfo, @@ -59,7 +60,7 @@ @gitbucket.core.helper.html.preview( repository = repository, - content = "", + content = content, enableWikiLink = false, enableRefsLink = true, enableLineBreaks = true,