mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-06 21:45:50 +01:00
Use ControlUtil.
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
package app
|
package app
|
||||||
|
|
||||||
import util._
|
import util._
|
||||||
|
import ControlUtil._
|
||||||
import service._
|
import service._
|
||||||
import jp.sf.amateras.scalatra.forms._
|
import jp.sf.amateras.scalatra.forms._
|
||||||
|
|
||||||
@@ -26,25 +27,25 @@ trait SearchControllerBase extends ControllerBase { self: RepositoryService
|
|||||||
}
|
}
|
||||||
|
|
||||||
get("/:owner/:repository/search")(referrersOnly { repository =>
|
get("/:owner/:repository/search")(referrersOnly { repository =>
|
||||||
val query = params("q").trim
|
defining(params("q").trim, params.getOrElse("type", "code")){ case (query, target) =>
|
||||||
val target = params.getOrElse("type", "code")
|
val page = try {
|
||||||
val page = try {
|
val i = params.getOrElse("page", "1").toInt
|
||||||
val i = params.getOrElse("page", "1").toInt
|
if(i <= 0) 1 else i
|
||||||
if(i <= 0) 1 else i
|
} catch {
|
||||||
} catch {
|
case e: NumberFormatException => 1
|
||||||
case e: NumberFormatException => 1
|
}
|
||||||
}
|
|
||||||
|
|
||||||
target.toLowerCase match {
|
target.toLowerCase match {
|
||||||
case "issue" => search.html.issues(
|
case "issue" => search.html.issues(
|
||||||
searchIssues(repository.owner, repository.name, query),
|
searchIssues(repository.owner, repository.name, query),
|
||||||
countFiles(repository.owner, repository.name, query),
|
countFiles(repository.owner, repository.name, query),
|
||||||
query, page, repository)
|
query, page, repository)
|
||||||
|
|
||||||
case _ => search.html.code(
|
case _ => search.html.code(
|
||||||
searchFiles(repository.owner, repository.name, query),
|
searchFiles(repository.owner, repository.name, query),
|
||||||
countIssues(repository.owner, repository.name, query),
|
countIssues(repository.owner, repository.name, query),
|
||||||
query, page, repository)
|
query, page, repository)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package app
|
|||||||
import service._
|
import service._
|
||||||
import util.AdminAuthenticator
|
import util.AdminAuthenticator
|
||||||
import util.StringUtil._
|
import util.StringUtil._
|
||||||
|
import util.ControlUtil._
|
||||||
import jp.sf.amateras.scalatra.forms._
|
import jp.sf.amateras.scalatra.forms._
|
||||||
|
|
||||||
class UserManagementController extends UserManagementControllerBase
|
class UserManagementController extends UserManagementControllerBase
|
||||||
@@ -107,29 +108,29 @@ trait UserManagementControllerBase extends AccountManagementControllerBase {
|
|||||||
})
|
})
|
||||||
|
|
||||||
get("/admin/users/:groupName/_editgroup")(adminOnly {
|
get("/admin/users/:groupName/_editgroup")(adminOnly {
|
||||||
val groupName = params("groupName")
|
defining(params("groupName")){ groupName =>
|
||||||
admin.users.html.group(getAccountByUserName(groupName), getGroupMembers(groupName))
|
admin.users.html.group(getAccountByUserName(groupName), getGroupMembers(groupName))
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
post("/admin/users/:groupName/_editgroup", editGroupForm)(adminOnly { form =>
|
post("/admin/users/:groupName/_editgroup", editGroupForm)(adminOnly { form =>
|
||||||
val groupName = params("groupName")
|
defining(params("groupName"), form.memberNames.map(_.split(",").toList).getOrElse(Nil)){ case (groupName, memberNames) =>
|
||||||
getAccountByUserName(groupName).map { account =>
|
getAccountByUserName(groupName).map { account =>
|
||||||
updateGroup(groupName, form.url)
|
updateGroup(groupName, form.url)
|
||||||
|
updateGroupMembers(form.groupName, memberNames)
|
||||||
|
|
||||||
val memberNames = form.memberNames.map(_.split(",").toList).getOrElse(Nil)
|
getRepositoryNamesOfUser(form.groupName).foreach { repositoryName =>
|
||||||
updateGroupMembers(form.groupName, memberNames)
|
removeCollaborators(form.groupName, repositoryName)
|
||||||
|
memberNames.foreach { userName =>
|
||||||
getRepositoryNamesOfUser(form.groupName).foreach { repositoryName =>
|
addCollaborator(form.groupName, repositoryName, userName)
|
||||||
removeCollaborators(form.groupName, repositoryName)
|
}
|
||||||
memberNames.foreach { userName =>
|
|
||||||
addCollaborator(form.groupName, repositoryName, userName)
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
updateImage(form.groupName, form.fileId, form.clearImage)
|
updateImage(form.groupName, form.fileId, form.clearImage)
|
||||||
redirect("/admin/users")
|
redirect("/admin/users")
|
||||||
|
|
||||||
} getOrElse NotFound
|
} getOrElse NotFound
|
||||||
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
post("/admin/users/_usercheck")(adminOnly {
|
post("/admin/users/_usercheck")(adminOnly {
|
||||||
|
|||||||
@@ -132,8 +132,8 @@ trait WikiControllerBase extends ControllerBase {
|
|||||||
|
|
||||||
get("/:owner/:repository/wiki/_blob/*")(referrersOnly { repository =>
|
get("/:owner/:repository/wiki/_blob/*")(referrersOnly { repository =>
|
||||||
getFileContent(repository.owner, repository.name, multiParams("splat").head).map { content =>
|
getFileContent(repository.owner, repository.name, multiParams("splat").head).map { content =>
|
||||||
contentType = "application/octet-stream"
|
contentType = "application/octet-stream"
|
||||||
content
|
content
|
||||||
} getOrElse NotFound
|
} getOrElse NotFound
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
@@ -2,21 +2,21 @@ package service
|
|||||||
|
|
||||||
import scala.slick.driver.H2Driver.simple._
|
import scala.slick.driver.H2Driver.simple._
|
||||||
import Database.threadLocalSession
|
import Database.threadLocalSession
|
||||||
|
|
||||||
import model._
|
import model._
|
||||||
|
import util.ControlUtil._
|
||||||
|
|
||||||
trait PullRequestService { self: IssuesService =>
|
trait PullRequestService { self: IssuesService =>
|
||||||
import PullRequestService._
|
import PullRequestService._
|
||||||
|
|
||||||
def getPullRequest(owner: String, repository: String, issueId: Int): Option[(Issue, PullRequest)] = {
|
def getPullRequest(owner: String, repository: String, issueId: Int): Option[(Issue, PullRequest)] =
|
||||||
val issue = getIssue(owner, repository, issueId.toString)
|
defining(getIssue(owner, repository, issueId.toString)){ issue =>
|
||||||
if(issue.isDefined){
|
if(issue.isDefined){
|
||||||
Query(PullRequests).filter(_.byPrimaryKey(owner, repository, issueId)).firstOption match {
|
Query(PullRequests).filter(_.byPrimaryKey(owner, repository, issueId)).firstOption match {
|
||||||
case Some(pullreq) => Some((issue.get, pullreq))
|
case Some(pullreq) => Some((issue.get, pullreq))
|
||||||
case None => None
|
case None => None
|
||||||
}
|
}
|
||||||
} else None
|
} else None
|
||||||
}
|
}
|
||||||
|
|
||||||
def getPullRequestCountGroupByUser(closed: Boolean, owner: String, repository: Option[String]): List[PullRequestCount] =
|
def getPullRequestCountGroupByUser(closed: Boolean, owner: String, repository: Option[String]): List[PullRequestCount] =
|
||||||
Query(PullRequests)
|
Query(PullRequests)
|
||||||
|
|||||||
@@ -10,7 +10,8 @@ import scala.collection.mutable.ListBuffer
|
|||||||
import org.eclipse.jgit.lib.FileMode
|
import org.eclipse.jgit.lib.FileMode
|
||||||
import org.eclipse.jgit.api.Git
|
import org.eclipse.jgit.api.Git
|
||||||
|
|
||||||
trait RepositorySearchService { self: IssuesService =>
|
trait
|
||||||
|
RepositorySearchService { self: IssuesService =>
|
||||||
import RepositorySearchService._
|
import RepositorySearchService._
|
||||||
|
|
||||||
def countIssues(owner: String, repository: String, query: String): Int =
|
def countIssues(owner: String, repository: String, query: String): Int =
|
||||||
|
|||||||
@@ -1,73 +1,76 @@
|
|||||||
package service
|
package service
|
||||||
|
|
||||||
import util.Directory._
|
import util.Directory._
|
||||||
|
import util.ControlUtil._
|
||||||
import SystemSettingsService._
|
import SystemSettingsService._
|
||||||
|
|
||||||
trait SystemSettingsService {
|
trait SystemSettingsService {
|
||||||
|
|
||||||
def saveSystemSettings(settings: SystemSettings): Unit = {
|
def saveSystemSettings(settings: SystemSettings): Unit = {
|
||||||
val props = new java.util.Properties()
|
defining(new java.util.Properties()){ props =>
|
||||||
props.setProperty(AllowAccountRegistration, settings.allowAccountRegistration.toString)
|
props.setProperty(AllowAccountRegistration, settings.allowAccountRegistration.toString)
|
||||||
props.setProperty(Gravatar, settings.gravatar.toString)
|
props.setProperty(Gravatar, settings.gravatar.toString)
|
||||||
props.setProperty(Notification, settings.notification.toString)
|
props.setProperty(Notification, settings.notification.toString)
|
||||||
if(settings.notification) {
|
if(settings.notification) {
|
||||||
settings.smtp.foreach { smtp =>
|
settings.smtp.foreach { smtp =>
|
||||||
props.setProperty(SmtpHost, smtp.host)
|
props.setProperty(SmtpHost, smtp.host)
|
||||||
smtp.port.foreach(x => props.setProperty(SmtpPort, x.toString))
|
smtp.port.foreach(x => props.setProperty(SmtpPort, x.toString))
|
||||||
smtp.user.foreach(props.setProperty(SmtpUser, _))
|
smtp.user.foreach(props.setProperty(SmtpUser, _))
|
||||||
smtp.password.foreach(props.setProperty(SmtpPassword, _))
|
smtp.password.foreach(props.setProperty(SmtpPassword, _))
|
||||||
smtp.ssl.foreach(x => props.setProperty(SmtpSsl, x.toString))
|
smtp.ssl.foreach(x => props.setProperty(SmtpSsl, x.toString))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
props.setProperty(LdapAuthentication, settings.ldapAuthentication.toString)
|
||||||
props.setProperty(LdapAuthentication, settings.ldapAuthentication.toString)
|
if(settings.ldapAuthentication){
|
||||||
if(settings.ldapAuthentication){
|
settings.ldap.map { ldap =>
|
||||||
settings.ldap.map { ldap =>
|
props.setProperty(LdapHost, ldap.host)
|
||||||
props.setProperty(LdapHost, ldap.host)
|
ldap.port.foreach(x => props.setProperty(LdapPort, x.toString))
|
||||||
ldap.port.foreach(x => props.setProperty(LdapPort, x.toString))
|
ldap.bindDN.foreach(x => props.setProperty(LdapBindDN, x))
|
||||||
ldap.bindDN.foreach(x => props.setProperty(LdapBindDN, x))
|
ldap.bindPassword.foreach(x => props.setProperty(LdapBindPassword, x))
|
||||||
ldap.bindPassword.foreach(x => props.setProperty(LdapBindPassword, x))
|
props.setProperty(LdapBaseDN, ldap.baseDN)
|
||||||
props.setProperty(LdapBaseDN, ldap.baseDN)
|
props.setProperty(LdapUserNameAttribute, ldap.userNameAttribute)
|
||||||
props.setProperty(LdapUserNameAttribute, ldap.userNameAttribute)
|
props.setProperty(LdapMailAddressAttribute, ldap.mailAttribute)
|
||||||
props.setProperty(LdapMailAddressAttribute, ldap.mailAttribute)
|
}
|
||||||
}
|
}
|
||||||
|
props.store(new java.io.FileOutputStream(GitBucketConf), null)
|
||||||
}
|
}
|
||||||
props.store(new java.io.FileOutputStream(GitBucketConf), null)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
def loadSystemSettings(): SystemSettings = {
|
def loadSystemSettings(): SystemSettings = {
|
||||||
val props = new java.util.Properties()
|
defining(new java.util.Properties()){ props =>
|
||||||
if(GitBucketConf.exists){
|
if(GitBucketConf.exists){
|
||||||
props.load(new java.io.FileInputStream(GitBucketConf))
|
props.load(new java.io.FileInputStream(GitBucketConf))
|
||||||
}
|
|
||||||
SystemSettings(
|
|
||||||
getValue(props, AllowAccountRegistration, false),
|
|
||||||
getValue(props, Gravatar, true),
|
|
||||||
getValue(props, Notification, false),
|
|
||||||
if(getValue(props, Notification, false)){
|
|
||||||
Some(Smtp(
|
|
||||||
getValue(props, SmtpHost, ""),
|
|
||||||
getOptionValue(props, SmtpPort, Some(DefaultSmtpPort)),
|
|
||||||
getOptionValue(props, SmtpUser, None),
|
|
||||||
getOptionValue(props, SmtpPassword, None),
|
|
||||||
getOptionValue[Boolean](props, SmtpSsl, None)))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
},
|
|
||||||
getValue(props, LdapAuthentication, false),
|
|
||||||
if(getValue(props, LdapAuthentication, false)){
|
|
||||||
Some(Ldap(
|
|
||||||
getValue(props, LdapHost, ""),
|
|
||||||
getOptionValue(props, LdapPort, Some(DefaultLdapPort)),
|
|
||||||
getOptionValue(props, LdapBindDN, None),
|
|
||||||
getOptionValue(props, LdapBindPassword, None),
|
|
||||||
getValue(props, LdapBaseDN, ""),
|
|
||||||
getValue(props, LdapUserNameAttribute, ""),
|
|
||||||
getValue(props, LdapMailAddressAttribute, "")))
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
)
|
SystemSettings(
|
||||||
|
getValue(props, AllowAccountRegistration, false),
|
||||||
|
getValue(props, Gravatar, true),
|
||||||
|
getValue(props, Notification, false),
|
||||||
|
if(getValue(props, Notification, false)){
|
||||||
|
Some(Smtp(
|
||||||
|
getValue(props, SmtpHost, ""),
|
||||||
|
getOptionValue(props, SmtpPort, Some(DefaultSmtpPort)),
|
||||||
|
getOptionValue(props, SmtpUser, None),
|
||||||
|
getOptionValue(props, SmtpPassword, None),
|
||||||
|
getOptionValue[Boolean](props, SmtpSsl, None)))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
|
getValue(props, LdapAuthentication, false),
|
||||||
|
if(getValue(props, LdapAuthentication, false)){
|
||||||
|
Some(Ldap(
|
||||||
|
getValue(props, LdapHost, ""),
|
||||||
|
getOptionValue(props, LdapPort, Some(DefaultLdapPort)),
|
||||||
|
getOptionValue(props, LdapBindDN, None),
|
||||||
|
getOptionValue(props, LdapBindPassword, None),
|
||||||
|
getValue(props, LdapBaseDN, ""),
|
||||||
|
getValue(props, LdapUserNameAttribute, ""),
|
||||||
|
getValue(props, LdapMailAddressAttribute, "")))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
|
)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -119,23 +122,23 @@ object SystemSettingsService {
|
|||||||
private val LdapUserNameAttribute = "ldap.username_attribute"
|
private val LdapUserNameAttribute = "ldap.username_attribute"
|
||||||
private val LdapMailAddressAttribute = "ldap.mail_attribute"
|
private val LdapMailAddressAttribute = "ldap.mail_attribute"
|
||||||
|
|
||||||
private def getValue[A: ClassTag](props: java.util.Properties, key: String, default: A): A = {
|
private def getValue[A: ClassTag](props: java.util.Properties, key: String, default: A): A =
|
||||||
val value = props.getProperty(key)
|
defining(props.getProperty(key)){ value =>
|
||||||
if(value == null || value.isEmpty) default
|
if(value == null || value.isEmpty) default
|
||||||
else convertType(value).asInstanceOf[A]
|
else convertType(value).asInstanceOf[A]
|
||||||
}
|
}
|
||||||
|
|
||||||
private def getOptionValue[A: ClassTag](props: java.util.Properties, key: String, default: Option[A]): Option[A] = {
|
private def getOptionValue[A: ClassTag](props: java.util.Properties, key: String, default: Option[A]): Option[A] =
|
||||||
val value = props.getProperty(key)
|
defining(props.getProperty(key)){ value =>
|
||||||
if(value == null || value.isEmpty) default
|
if(value == null || value.isEmpty) default
|
||||||
else Some(convertType(value)).asInstanceOf[Option[A]]
|
else Some(convertType(value)).asInstanceOf[Option[A]]
|
||||||
}
|
}
|
||||||
|
|
||||||
private def convertType[A: ClassTag](value: String) = {
|
private def convertType[A: ClassTag](value: String) =
|
||||||
val c = implicitly[ClassTag[A]].runtimeClass
|
defining(implicitly[ClassTag[A]].runtimeClass){ c =>
|
||||||
if(c == classOf[Boolean]) value.toBoolean
|
if(c == classOf[Boolean]) value.toBoolean
|
||||||
else if(c == classOf[Int]) value.toInt
|
else if(c == classOf[Int]) value.toInt
|
||||||
else value
|
else value
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -34,40 +34,40 @@ object WikiService {
|
|||||||
trait WikiService {
|
trait WikiService {
|
||||||
import WikiService._
|
import WikiService._
|
||||||
|
|
||||||
def createWikiRepository(loginAccount: model.Account, owner: String, repository: String): Unit = {
|
def createWikiRepository(loginAccount: model.Account, owner: String, repository: String): Unit =
|
||||||
LockUtil.lock(s"${owner}/${repository}/wiki"){
|
LockUtil.lock(s"${owner}/${repository}/wiki"){
|
||||||
val dir = Directory.getWikiRepositoryDir(owner, repository)
|
defining(Directory.getWikiRepositoryDir(owner, repository)){ dir =>
|
||||||
if(!dir.exists){
|
if(!dir.exists){
|
||||||
try {
|
try {
|
||||||
JGitUtil.initRepository(dir)
|
JGitUtil.initRepository(dir)
|
||||||
saveWikiPage(owner, repository, "Home", "Home", s"Welcome to the ${repository} wiki!!", loginAccount, "Initial Commit")
|
saveWikiPage(owner, repository, "Home", "Home", s"Welcome to the ${repository} wiki!!", loginAccount, "Initial Commit")
|
||||||
} finally {
|
} finally {
|
||||||
// once delete cloned repository because initial cloned repository does not have 'branch.master.merge'
|
// once delete cloned repository because initial cloned repository does not have 'branch.master.merge'
|
||||||
FileUtils.deleteDirectory(Directory.getWikiWorkDir(owner, repository))
|
FileUtils.deleteDirectory(Directory.getWikiWorkDir(owner, repository))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the wiki page.
|
* Returns the wiki page.
|
||||||
*/
|
*/
|
||||||
def getWikiPage(owner: String, repository: String, pageName: String): Option[WikiPageInfo] = {
|
def getWikiPage(owner: String, repository: String, pageName: String): Option[WikiPageInfo] = {
|
||||||
using(Git.open(Directory.getWikiRepositoryDir(owner, repository))){ git =>
|
using(Git.open(Directory.getWikiRepositoryDir(owner, repository))){ git =>
|
||||||
if(!JGitUtil.isEmpty(git)){
|
optionIf(!JGitUtil.isEmpty(git)){
|
||||||
JGitUtil.getFileList(git, "master", ".").find(_.name == pageName + ".md").map { file =>
|
JGitUtil.getFileList(git, "master", ".").find(_.name == pageName + ".md").map { file =>
|
||||||
WikiPageInfo(file.name, new String(git.getRepository.open(file.id).getBytes, "UTF-8"), file.committer, file.time)
|
WikiPageInfo(file.name, new String(git.getRepository.open(file.id).getBytes, "UTF-8"), file.committer, file.time)
|
||||||
}
|
}
|
||||||
} else None
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the content of the specified file.
|
* Returns the content of the specified file.
|
||||||
*/
|
*/
|
||||||
def getFileContent(owner: String, repository: String, path: String): Option[Array[Byte]] = {
|
def getFileContent(owner: String, repository: String, path: String): Option[Array[Byte]] =
|
||||||
using(Git.open(Directory.getWikiRepositoryDir(owner, repository))){ git =>
|
using(Git.open(Directory.getWikiRepositoryDir(owner, repository))){ git =>
|
||||||
if(!JGitUtil.isEmpty(git)){
|
optionIf(!JGitUtil.isEmpty(git)){
|
||||||
val index = path.lastIndexOf('/')
|
val index = path.lastIndexOf('/')
|
||||||
val parentPath = if(index < 0) "." else path.substring(0, index)
|
val parentPath = if(index < 0) "." else path.substring(0, index)
|
||||||
val fileName = if(index < 0) path else path.substring(index + 1)
|
val fileName = if(index < 0) path else path.substring(index + 1)
|
||||||
@@ -75,9 +75,8 @@ trait WikiService {
|
|||||||
JGitUtil.getFileList(git, "master", parentPath).find(_.name == fileName).map { file =>
|
JGitUtil.getFileList(git, "master", parentPath).find(_.name == fileName).map { file =>
|
||||||
git.getRepository.open(file.id).getBytes
|
git.getRepository.open(file.id).getBytes
|
||||||
}
|
}
|
||||||
} else None
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the list of wiki page names.
|
* Returns the list of wiki page names.
|
||||||
@@ -98,36 +97,31 @@ trait WikiService {
|
|||||||
content: String, committer: model.Account, message: String): Option[String] = {
|
content: String, committer: model.Account, message: String): Option[String] = {
|
||||||
|
|
||||||
LockUtil.lock(s"${owner}/${repository}/wiki"){
|
LockUtil.lock(s"${owner}/${repository}/wiki"){
|
||||||
// clone working copy
|
defining(Directory.getWikiWorkDir(owner, repository)){ workDir =>
|
||||||
val workDir = Directory.getWikiWorkDir(owner, repository)
|
// clone working copy
|
||||||
cloneOrPullWorkingCopy(workDir, owner, repository)
|
cloneOrPullWorkingCopy(workDir, owner, repository)
|
||||||
|
|
||||||
// write as file
|
// write as file
|
||||||
using(Git.open(workDir)){ git =>
|
using(Git.open(workDir)){ git =>
|
||||||
val file = new File(workDir, newPageName + ".md")
|
defining(new File(workDir, newPageName + ".md")){ file =>
|
||||||
val added = if(!file.exists || FileUtils.readFileToString(file, "UTF-8") != content){
|
val added = executeIf(!file.exists || FileUtils.readFileToString(file, "UTF-8") != content){
|
||||||
FileUtils.writeStringToFile(file, content, "UTF-8")
|
FileUtils.writeStringToFile(file, content, "UTF-8")
|
||||||
git.add.addFilepattern(file.getName).call
|
git.add.addFilepattern(file.getName).call
|
||||||
true
|
}
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
// delete file
|
// delete file
|
||||||
val deleted = if(currentPageName != "" && currentPageName != newPageName){
|
val deleted = executeIf(currentPageName != "" && currentPageName != newPageName){
|
||||||
git.rm.addFilepattern(currentPageName + ".md").call
|
git.rm.addFilepattern(currentPageName + ".md").call
|
||||||
true
|
}
|
||||||
} else {
|
|
||||||
false
|
|
||||||
}
|
|
||||||
|
|
||||||
// commit and push
|
// commit and push
|
||||||
if(added || deleted){
|
optionIf(added || deleted){
|
||||||
val commit = git.commit.setCommitter(committer.userName, committer.mailAddress).setMessage(message).call
|
defining(git.commit.setCommitter(committer.userName, committer.mailAddress).setMessage(message).call){ commit =>
|
||||||
git.push.call
|
git.push.call
|
||||||
Some(commit.getName)
|
Some(commit.getName)
|
||||||
} else {
|
}
|
||||||
None
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -139,35 +133,34 @@ trait WikiService {
|
|||||||
def deleteWikiPage(owner: String, repository: String, pageName: String,
|
def deleteWikiPage(owner: String, repository: String, pageName: String,
|
||||||
committer: String, mailAddress: String, message: String): Unit = {
|
committer: String, mailAddress: String, message: String): Unit = {
|
||||||
LockUtil.lock(s"${owner}/${repository}/wiki"){
|
LockUtil.lock(s"${owner}/${repository}/wiki"){
|
||||||
// clone working copy
|
defining(Directory.getWikiWorkDir(owner, repository)){ workDir =>
|
||||||
val workDir = Directory.getWikiWorkDir(owner, repository)
|
// clone working copy
|
||||||
cloneOrPullWorkingCopy(workDir, owner, repository)
|
cloneOrPullWorkingCopy(workDir, owner, repository)
|
||||||
|
|
||||||
// delete file
|
// delete file
|
||||||
new File(workDir, pageName + ".md").delete
|
new File(workDir, pageName + ".md").delete
|
||||||
|
|
||||||
using(Git.open(workDir)){ git =>
|
using(Git.open(workDir)){ git =>
|
||||||
git.rm.addFilepattern(pageName + ".md").call
|
git.rm.addFilepattern(pageName + ".md").call
|
||||||
|
|
||||||
// commit and push
|
// commit and push
|
||||||
git.commit.setAuthor(committer, mailAddress).setMessage(message).call
|
git.commit.setAuthor(committer, mailAddress).setMessage(message).call
|
||||||
git.push.call
|
git.push.call
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private def cloneOrPullWorkingCopy(workDir: File, owner: String, repository: String): Unit = {
|
private def cloneOrPullWorkingCopy(workDir: File, owner: String, repository: String): Unit = {
|
||||||
if(!workDir.exists){
|
if(!workDir.exists){
|
||||||
val git =
|
Git.cloneRepository
|
||||||
Git.cloneRepository
|
.setURI(Directory.getWikiRepositoryDir(owner, repository).toURI.toString)
|
||||||
.setURI(Directory.getWikiRepositoryDir(owner, repository).toURI.toString)
|
.setDirectory(workDir)
|
||||||
.setDirectory(workDir)
|
.call
|
||||||
.call
|
.getRepository
|
||||||
git.getRepository.close // close .git resources.
|
.close
|
||||||
} else {
|
} else using(Git.open(workDir)){ git =>
|
||||||
using(Git.open(workDir)){ git =>
|
git.pull.call
|
||||||
git.pull.call
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,7 +6,9 @@ import org.apache.commons.io.FileUtils
|
|||||||
import javax.servlet.ServletContextEvent
|
import javax.servlet.ServletContextEvent
|
||||||
import org.apache.commons.io.IOUtils
|
import org.apache.commons.io.IOUtils
|
||||||
import org.slf4j.LoggerFactory
|
import org.slf4j.LoggerFactory
|
||||||
import util.Directory
|
import util.Directory._
|
||||||
|
import util.ControlUtil._
|
||||||
|
import org.eclipse.jgit.api.Git
|
||||||
|
|
||||||
object AutoUpdate {
|
object AutoUpdate {
|
||||||
|
|
||||||
@@ -26,15 +28,14 @@ object AutoUpdate {
|
|||||||
*/
|
*/
|
||||||
def update(conn: Connection): Unit = {
|
def update(conn: Connection): Unit = {
|
||||||
val sqlPath = s"update/${majorVersion}_${minorVersion}.sql"
|
val sqlPath = s"update/${majorVersion}_${minorVersion}.sql"
|
||||||
val in = Thread.currentThread.getContextClassLoader.getResourceAsStream(sqlPath)
|
|
||||||
if(in != null){
|
using(Thread.currentThread.getContextClassLoader.getResourceAsStream(sqlPath)){ in =>
|
||||||
val sql = IOUtils.toString(in, "UTF-8")
|
if(in != null){
|
||||||
val stmt = conn.createStatement()
|
val sql = IOUtils.toString(in, "UTF-8")
|
||||||
try {
|
using(conn.createStatement()){ stmt =>
|
||||||
logger.debug(sqlPath + "=" + sql)
|
logger.debug(sqlPath + "=" + sql)
|
||||||
stmt.executeUpdate(sql)
|
stmt.executeUpdate(sql)
|
||||||
} finally {
|
}
|
||||||
stmt.close()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -56,16 +57,17 @@ object AutoUpdate {
|
|||||||
override def update(conn: Connection): Unit = {
|
override def update(conn: Connection): Unit = {
|
||||||
super.update(conn)
|
super.update(conn)
|
||||||
// Fix wiki repository configuration
|
// Fix wiki repository configuration
|
||||||
val rs = conn.createStatement.executeQuery("SELECT USER_NAME, REPOSITORY_NAME FROM REPOSITORY")
|
using(conn.createStatement.executeQuery("SELECT USER_NAME, REPOSITORY_NAME FROM REPOSITORY")){ rs =>
|
||||||
while(rs.next){
|
while(rs.next){
|
||||||
val wikidir = Directory.getWikiRepositoryDir(rs.getString("USER_NAME"), rs.getString("REPOSITORY_NAME"))
|
using(Git.open(getWikiRepositoryDir(rs.getString("USER_NAME"), rs.getString("REPOSITORY_NAME")))){ git =>
|
||||||
val repository = org.eclipse.jgit.api.Git.open(wikidir).getRepository
|
defining(git.getRepository.getConfig){ config =>
|
||||||
val config = repository.getConfig
|
if(!config.getBoolean("http", "receivepack", false)){
|
||||||
if(!config.getBoolean("http", "receivepack", false)){
|
config.setBoolean("http", null, "receivepack", true)
|
||||||
config.setBoolean("http", null, "receivepack", true)
|
config.save
|
||||||
config.save
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
repository.close
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
@@ -82,7 +84,7 @@ object AutoUpdate {
|
|||||||
/**
|
/**
|
||||||
* The version file (GITBUCKET_HOME/version).
|
* The version file (GITBUCKET_HOME/version).
|
||||||
*/
|
*/
|
||||||
val versionFile = new File(Directory.GitBucketHome, "version")
|
val versionFile = new File(GitBucketHome, "version")
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the current version from the version file.
|
* Returns the current version from the version file.
|
||||||
@@ -113,27 +115,29 @@ class AutoUpdateListener extends org.h2.server.web.DbStarter {
|
|||||||
private val logger = LoggerFactory.getLogger(classOf[AutoUpdateListener])
|
private val logger = LoggerFactory.getLogger(classOf[AutoUpdateListener])
|
||||||
|
|
||||||
override def contextInitialized(event: ServletContextEvent): Unit = {
|
override def contextInitialized(event: ServletContextEvent): Unit = {
|
||||||
event.getServletContext.setInitParameter("db.url", s"jdbc:h2:${Directory.DatabaseHome}")
|
event.getServletContext.setInitParameter("db.url", s"jdbc:h2:${DatabaseHome}")
|
||||||
super.contextInitialized(event)
|
super.contextInitialized(event)
|
||||||
logger.debug("H2 started")
|
logger.debug("H2 started")
|
||||||
|
|
||||||
logger.debug("Start schema update")
|
logger.debug("Start schema update")
|
||||||
val conn = getConnection()
|
defining(getConnection()){ conn =>
|
||||||
try {
|
try {
|
||||||
val currentVersion = getCurrentVersion()
|
defining(getCurrentVersion()){ currentVersion =>
|
||||||
if(currentVersion == headVersion){
|
if(currentVersion == headVersion){
|
||||||
logger.debug("No update")
|
logger.debug("No update")
|
||||||
} else {
|
} else {
|
||||||
versions.takeWhile(_ != currentVersion).reverse.foreach(_.update(conn))
|
versions.takeWhile(_ != currentVersion).reverse.foreach(_.update(conn))
|
||||||
FileUtils.writeStringToFile(versionFile, headVersion.versionString, "UTF-8")
|
FileUtils.writeStringToFile(versionFile, headVersion.versionString, "UTF-8")
|
||||||
conn.commit()
|
conn.commit()
|
||||||
logger.debug("Updated from " + currentVersion.versionString + " to " + headVersion.versionString)
|
logger.debug("Updated from " + currentVersion.versionString + " to " + headVersion.versionString)
|
||||||
}
|
}
|
||||||
} catch {
|
}
|
||||||
case ex: Throwable => {
|
} catch {
|
||||||
logger.error("Failed to schema update", ex)
|
case ex: Throwable => {
|
||||||
ex.printStackTrace()
|
logger.error("Failed to schema update", ex)
|
||||||
conn.rollback()
|
ex.printStackTrace()
|
||||||
|
conn.rollback()
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
logger.debug("End schema update")
|
logger.debug("End schema update")
|
||||||
|
|||||||
@@ -33,5 +33,18 @@ object ControlUtil {
|
|||||||
git.getRepository.close
|
git.getRepository.close
|
||||||
}
|
}
|
||||||
|
|
||||||
|
def executeIf(condition: => Boolean)(action: => Unit): Boolean =
|
||||||
|
if(condition){
|
||||||
|
action
|
||||||
|
true
|
||||||
|
} else {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
|
||||||
|
def optionIf[T](condition: => Boolean)(action: => Option[T]): Option[T] =
|
||||||
|
if(condition){
|
||||||
|
action
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -69,29 +69,17 @@ object JGitUtil {
|
|||||||
rev.getFullMessage,
|
rev.getFullMessage,
|
||||||
rev.getParents().map(_.name).toList)
|
rev.getParents().map(_.name).toList)
|
||||||
|
|
||||||
val summary = {
|
val summary = defining(fullMessage.trim.indexOf("\n")){ i =>
|
||||||
val i = fullMessage.trim.indexOf("\n")
|
defining(if(i >= 0) fullMessage.trim.substring(0, i).trim else fullMessage){ firstLine =>
|
||||||
val firstLine = if(i >= 0){
|
if(firstLine.length > shortMessage.length) shortMessage else firstLine
|
||||||
fullMessage.trim.substring(0, i).trim
|
|
||||||
} else {
|
|
||||||
fullMessage
|
|
||||||
}
|
|
||||||
if(firstLine.length > shortMessage.length){
|
|
||||||
shortMessage
|
|
||||||
} else {
|
|
||||||
firstLine
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
val description = {
|
val description = defining(fullMessage.trim.indexOf("\n")){ i =>
|
||||||
val i = fullMessage.trim.indexOf("\n")
|
optionIf(i >= 0){
|
||||||
if(i >= 0){
|
|
||||||
Some(fullMessage.trim.substring(i).trim)
|
Some(fullMessage.trim.substring(i).trim)
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
case class DiffInfo(changeType: ChangeType, oldPath: String, newPath: String, oldContent: Option[String], newContent: Option[String])
|
case class DiffInfo(changeType: ChangeType, oldPath: String, newPath: String, oldContent: Option[String], newContent: Option[String])
|
||||||
@@ -124,7 +112,7 @@ object JGitUtil {
|
|||||||
val revWalk = new RevWalk(git.getRepository)
|
val revWalk = new RevWalk(git.getRepository)
|
||||||
val revCommit = revWalk.parseAny(objectId) match {
|
val revCommit = revWalk.parseAny(objectId) match {
|
||||||
case r: RevTag => revWalk.parseCommit(r.getObject)
|
case r: RevTag => revWalk.parseCommit(r.getObject)
|
||||||
case _ => revWalk.parseCommit(objectId)
|
case _ => revWalk.parseCommit(objectId)
|
||||||
}
|
}
|
||||||
revWalk.dispose
|
revWalk.dispose
|
||||||
revCommit
|
revCommit
|
||||||
@@ -462,29 +450,24 @@ object JGitUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
def initRepository(dir: java.io.File): Unit = {
|
def initRepository(dir: java.io.File): Unit =
|
||||||
using(new RepositoryBuilder().setGitDir(dir).setBare.build){ repository =>
|
using(new RepositoryBuilder().setGitDir(dir).setBare.build){ repository =>
|
||||||
repository.create
|
repository.create
|
||||||
setReceivePack(repository)
|
setReceivePack(repository)
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
def cloneRepository(from: java.io.File, to: java.io.File): Unit = {
|
def cloneRepository(from: java.io.File, to: java.io.File): Unit =
|
||||||
val git = Git.cloneRepository.setURI(from.toURI.toString).setDirectory(to).setBare(true).call
|
using(Git.cloneRepository.setURI(from.toURI.toString).setDirectory(to).setBare(true).call){ git =>
|
||||||
try {
|
|
||||||
setReceivePack(git.getRepository)
|
setReceivePack(git.getRepository)
|
||||||
} finally {
|
|
||||||
git.getRepository.close
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
def isEmpty(git: Git): Boolean = git.getRepository.resolve(Constants.HEAD) == null
|
def isEmpty(git: Git): Boolean = git.getRepository.resolve(Constants.HEAD) == null
|
||||||
|
|
||||||
private def setReceivePack(repository: org.eclipse.jgit.lib.Repository): Unit = {
|
private def setReceivePack(repository: org.eclipse.jgit.lib.Repository): Unit =
|
||||||
val config = repository.getConfig
|
defining(repository.getConfig){ config =>
|
||||||
config.setBoolean("http", null, "receivepack", true)
|
config.setBoolean("http", null, "receivepack", true)
|
||||||
config.save
|
config.save
|
||||||
}
|
}
|
||||||
|
|
||||||
def getDefaultBranch(git: Git, repository: RepositoryService.RepositoryInfo,
|
def getDefaultBranch(git: Git, repository: RepositoryService.RepositoryInfo,
|
||||||
revstr: String = ""): Option[(ObjectId, String)] = {
|
revstr: String = ""): Option[(ObjectId, String)] = {
|
||||||
|
|||||||
@@ -1,10 +1,9 @@
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import service.SystemSettingsService.Ldap
|
import util.ControlUtil._
|
||||||
import service.SystemSettingsService
|
import service.SystemSettingsService
|
||||||
import com.novell.ldap._
|
import com.novell.ldap._
|
||||||
import service.SystemSettingsService.Ldap
|
import service.SystemSettingsService.Ldap
|
||||||
import scala.Some
|
|
||||||
import scala.annotation.tailrec
|
import scala.annotation.tailrec
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -96,12 +95,10 @@ object LDAPUtil {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private def findMailAddress(conn: LDAPConnection, userDN: String, mailAttribute: String): Option[String] = {
|
private def findMailAddress(conn: LDAPConnection, userDN: String, mailAttribute: String): Option[String] =
|
||||||
val results = conn.search(userDN, LDAPConnection.SCOPE_BASE, null, Array[String](mailAttribute), false)
|
defining(conn.search(userDN, LDAPConnection.SCOPE_BASE, null, Array[String](mailAttribute), false)){ results =>
|
||||||
if (results.hasMore) {
|
optionIf (results.hasMore) {
|
||||||
Option(results.next.getAttribute(mailAttribute)).map(_.getStringValue)
|
Option(results.next.getAttribute(mailAttribute)).map(_.getStringValue)
|
||||||
} else {
|
}
|
||||||
None
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,6 +2,7 @@ package util
|
|||||||
|
|
||||||
import java.util.concurrent.ConcurrentHashMap
|
import java.util.concurrent.ConcurrentHashMap
|
||||||
import java.util.concurrent.locks.{ReentrantLock, Lock}
|
import java.util.concurrent.locks.{ReentrantLock, Lock}
|
||||||
|
import util.ControlUtil._
|
||||||
|
|
||||||
object LockUtil {
|
object LockUtil {
|
||||||
|
|
||||||
@@ -23,8 +24,7 @@ object LockUtil {
|
|||||||
/**
|
/**
|
||||||
* Synchronizes a given function which modifies the working copy of the wiki repository.
|
* Synchronizes a given function which modifies the working copy of the wiki repository.
|
||||||
*/
|
*/
|
||||||
def lock[T](key: String)(f: => T): T = {
|
def lock[T](key: String)(f: => T): T = defining(getLockObject(key)){ lock =>
|
||||||
val lock = getLockObject(key)
|
|
||||||
try {
|
try {
|
||||||
lock.lock()
|
lock.lock()
|
||||||
f
|
f
|
||||||
|
|||||||
@@ -2,14 +2,15 @@ package util
|
|||||||
|
|
||||||
import java.net.{URLDecoder, URLEncoder}
|
import java.net.{URLDecoder, URLEncoder}
|
||||||
import org.mozilla.universalchardet.UniversalDetector
|
import org.mozilla.universalchardet.UniversalDetector
|
||||||
|
import util.ControlUtil._
|
||||||
|
|
||||||
object StringUtil {
|
object StringUtil {
|
||||||
|
|
||||||
def sha1(value: String): String = {
|
def sha1(value: String): String =
|
||||||
val md = java.security.MessageDigest.getInstance("SHA-1")
|
defining(java.security.MessageDigest.getInstance("SHA-1")){ md =>
|
||||||
md.update(value.getBytes)
|
md.update(value.getBytes)
|
||||||
md.digest.map(b => "%02x".format(b)).mkString
|
md.digest.map(b => "%02x".format(b)).mkString
|
||||||
}
|
}
|
||||||
|
|
||||||
def md5(value: String): String = {
|
def md5(value: String): String = {
|
||||||
val md = java.security.MessageDigest.getInstance("MD5")
|
val md = java.security.MessageDigest.getInstance("MD5")
|
||||||
@@ -28,13 +29,13 @@ object StringUtil {
|
|||||||
|
|
||||||
def convertFromByteArray(content: Array[Byte]): String = new String(content, detectEncoding(content))
|
def convertFromByteArray(content: Array[Byte]): String = new String(content, detectEncoding(content))
|
||||||
|
|
||||||
def detectEncoding(content: Array[Byte]): String = {
|
def detectEncoding(content: Array[Byte]): String =
|
||||||
val detector = new UniversalDetector(null)
|
defining(new UniversalDetector(null)){ detector =>
|
||||||
detector.handleData(content, 0, content.length)
|
detector.handleData(content, 0, content.length)
|
||||||
detector.dataEnd()
|
detector.dataEnd()
|
||||||
detector.getDetectedCharset match {
|
detector.getDetectedCharset match {
|
||||||
case null => "UTF-8"
|
case null => "UTF-8"
|
||||||
case e => e
|
case e => e
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,10 +25,7 @@ trait Validations {
|
|||||||
*/
|
*/
|
||||||
def date(constraints: Constraint*): SingleValueType[java.util.Date] =
|
def date(constraints: Constraint*): SingleValueType[java.util.Date] =
|
||||||
new SingleValueType[java.util.Date]((pattern("\\d{4}-\\d{2}-\\d{2}") +: constraints): _*){
|
new SingleValueType[java.util.Date]((pattern("\\d{4}-\\d{2}-\\d{2}") +: constraints): _*){
|
||||||
def convert(value: String): java.util.Date = {
|
def convert(value: String): java.util.Date = new java.text.SimpleDateFormat("yyyy-MM-dd").parse(value)
|
||||||
val formatter = new java.text.SimpleDateFormat("yyyy-MM-dd")
|
|
||||||
formatter.parse(value)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user