diff --git a/src/main/scala/gitbucket/core/controller/IssuesController.scala b/src/main/scala/gitbucket/core/controller/IssuesController.scala index 5969cb4..f55db68 100644 --- a/src/main/scala/gitbucket/core/controller/IssuesController.scala +++ b/src/main/scala/gitbucket/core/controller/IssuesController.scala @@ -107,6 +107,7 @@ 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 e33fca1..8609421 100644 --- a/src/main/scala/gitbucket/core/controller/PullRequestsController.scala +++ b/src/main/scala/gitbucket/core/controller/PullRequestsController.scala @@ -368,6 +368,7 @@ 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 30b4d5c..1fee8f7 100644 --- a/src/main/scala/gitbucket/core/service/RepositoryService.scala +++ b/src/main/scala/gitbucket/core/service/RepositoryService.scala @@ -3,7 +3,14 @@ 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 @@ } .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 a418a2b..f72df6b 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 06d00ff..d97b36f 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,