mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-07 22:15:51 +01:00
Merge branch 'master' into #33_match-by-email
Conflicts: src/main/scala/view/helpers.scala
This commit is contained in:
@@ -3,9 +3,54 @@ package service
|
||||
import model._
|
||||
import scala.slick.driver.H2Driver.simple._
|
||||
import Database.threadLocalSession
|
||||
import service.SystemSettingsService.SystemSettings
|
||||
import util.StringUtil._
|
||||
import model.GroupMember
|
||||
import scala.Some
|
||||
import model.Account
|
||||
import util.LDAPUtil
|
||||
import org.slf4j.LoggerFactory
|
||||
|
||||
trait AccountService {
|
||||
|
||||
private val logger = LoggerFactory.getLogger(classOf[AccountService])
|
||||
|
||||
def authenticate(settings: SystemSettings, userName: String, password: String): Option[Account] =
|
||||
if(settings.ldapAuthentication){
|
||||
ldapAuthentication(settings, userName, password)
|
||||
} else {
|
||||
defaultAuthentication(userName, password)
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticate by internal database.
|
||||
*/
|
||||
private def defaultAuthentication(userName: String, password: String) = {
|
||||
getAccountByUserName(userName).collect {
|
||||
case account if(!account.isGroupAccount && account.password == sha1(password)) => Some(account)
|
||||
} getOrElse None
|
||||
}
|
||||
|
||||
/**
|
||||
* Authenticate by LDAP.
|
||||
*/
|
||||
private def ldapAuthentication(settings: SystemSettings, userName: String, password: String) = {
|
||||
LDAPUtil.authenticate(settings.ldap.get, userName, password) match {
|
||||
case Right(mailAddress) => {
|
||||
// Create or update account by LDAP information
|
||||
getAccountByUserName(userName) match {
|
||||
case Some(x) => updateAccount(x.copy(mailAddress = mailAddress))
|
||||
case None => createAccount(userName, "", userName, mailAddress, false, None)
|
||||
}
|
||||
getAccountByUserName(userName)
|
||||
}
|
||||
case Left(errorMessage) => {
|
||||
logger.info(s"LDAP Authentication Failed: ${errorMessage}")
|
||||
defaultAuthentication(userName, password)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
def getAccountByUserName(userName: String): Option[Account] =
|
||||
Query(Accounts) filter(_.userName is userName.bind) firstOption
|
||||
|
||||
@@ -14,24 +59,27 @@ trait AccountService {
|
||||
|
||||
def getAllUsers(): List[Account] = Query(Accounts) sortBy(_.userName) list
|
||||
|
||||
def createAccount(userName: String, password: String, mailAddress: String, isAdmin: Boolean, url: Option[String]): Unit =
|
||||
def createAccount(userName: String, password: String, fullName: String, mailAddress: String, isAdmin: Boolean, url: Option[String]): Unit =
|
||||
Accounts insert Account(
|
||||
userName = userName,
|
||||
password = password,
|
||||
fullName = fullName,
|
||||
mailAddress = mailAddress,
|
||||
isAdmin = isAdmin,
|
||||
url = url,
|
||||
registeredDate = currentDate,
|
||||
updatedDate = currentDate,
|
||||
lastLoginDate = None,
|
||||
image = None)
|
||||
image = None,
|
||||
isGroupAccount = false)
|
||||
|
||||
def updateAccount(account: Account): Unit =
|
||||
Accounts
|
||||
.filter { a => a.userName is account.userName.bind }
|
||||
.map { a => a.password ~ a.mailAddress ~ a.isAdmin ~ a.url.? ~ a.registeredDate ~ a.updatedDate ~ a.lastLoginDate.? }
|
||||
.map { a => a.password ~ a.fullName ~ a.mailAddress ~ a.isAdmin ~ a.url.? ~ a.registeredDate ~ a.updatedDate ~ a.lastLoginDate.? }
|
||||
.update (
|
||||
account.password,
|
||||
account.fullName,
|
||||
account.mailAddress,
|
||||
account.isAdmin,
|
||||
account.url,
|
||||
@@ -44,5 +92,43 @@ trait AccountService {
|
||||
|
||||
def updateLastLoginDate(userName: String): Unit =
|
||||
Accounts.filter(_.userName is userName.bind).map(_.lastLoginDate).update(currentDate)
|
||||
|
||||
|
||||
def createGroup(groupName: String, url: Option[String]): Unit =
|
||||
Accounts insert Account(
|
||||
userName = groupName,
|
||||
password = "",
|
||||
fullName = groupName,
|
||||
mailAddress = groupName + "@devnull",
|
||||
isAdmin = false,
|
||||
url = url,
|
||||
registeredDate = currentDate,
|
||||
updatedDate = currentDate,
|
||||
lastLoginDate = None,
|
||||
image = None,
|
||||
isGroupAccount = true)
|
||||
|
||||
def updateGroup(groupName: String, url: Option[String]): Unit =
|
||||
Accounts.filter(_.userName is groupName.bind).map(_.url.?).update(url)
|
||||
|
||||
def updateGroupMembers(groupName: String, members: List[String]): Unit = {
|
||||
Query(GroupMembers).filter(_.groupName is groupName.bind).delete
|
||||
members.foreach { userName =>
|
||||
GroupMembers insert GroupMember (groupName, userName)
|
||||
}
|
||||
}
|
||||
|
||||
def getGroupMembers(groupName: String): List[String] =
|
||||
Query(GroupMembers)
|
||||
.filter(_.groupName is groupName.bind)
|
||||
.sortBy(_.userName)
|
||||
.map(_.userName)
|
||||
.list
|
||||
|
||||
def getGroupsByUserName(userName: String): List[String] =
|
||||
Query(GroupMembers)
|
||||
.filter(_.userName is userName.bind)
|
||||
.sortBy(_.groupName)
|
||||
.map(_.groupName)
|
||||
.list
|
||||
|
||||
}
|
||||
|
||||
@@ -6,23 +6,23 @@ import Database.threadLocalSession
|
||||
|
||||
trait ActivityService {
|
||||
|
||||
def getActivitiesByUser(activityUserName: String, isPublic: Boolean): List[Activity] = {
|
||||
val q = Query(Activities)
|
||||
def getActivitiesByUser(activityUserName: String, isPublic: Boolean): List[Activity] =
|
||||
Activities
|
||||
.innerJoin(Repositories).on((t1, t2) => t1.byRepository(t2.userName, t2.repositoryName))
|
||||
.filter { case (t1, t2) =>
|
||||
if(isPublic){
|
||||
(t1.activityUserName is activityUserName.bind) && (t2.isPrivate is false.bind)
|
||||
} else {
|
||||
(t1.activityUserName is activityUserName.bind)
|
||||
}
|
||||
}
|
||||
.sortBy { case (t1, t2) => t1.activityId desc }
|
||||
.map { case (t1, t2) => t1 }
|
||||
.take(30)
|
||||
.list
|
||||
|
||||
(if(isPublic){
|
||||
q filter { case (t1, t2) => (t1.activityUserName is activityUserName.bind) && (t2.isPrivate is false.bind) }
|
||||
} else {
|
||||
q filter { case (t1, t2) => t1.activityUserName is activityUserName.bind }
|
||||
})
|
||||
.sortBy { case (t1, t2) => t1.activityId desc }
|
||||
.map { case (t1, t2) => t1 }
|
||||
.take(30)
|
||||
.list
|
||||
}
|
||||
|
||||
def getRecentActivities(): List[Activity] =
|
||||
Query(Activities)
|
||||
Activities
|
||||
.innerJoin(Repositories).on((t1, t2) => t1.byRepository(t2.userName, t2.repositoryName))
|
||||
.filter { case (t1, t2) => t2.isPrivate is false.bind }
|
||||
.sortBy { case (t1, t2) => t1.activityId desc }
|
||||
@@ -52,6 +52,13 @@ trait ActivityService {
|
||||
Some(title),
|
||||
currentDate)
|
||||
|
||||
def recordClosePullRequestActivity(userName: String, repositoryName: String, activityUserName: String, issueId: Int, title: String): Unit =
|
||||
Activities.autoInc insert(userName, repositoryName, activityUserName,
|
||||
"close_issue",
|
||||
s"[user:${activityUserName}] closed pull request [pullreq:${userName}/${repositoryName}#${issueId}]",
|
||||
Some(title),
|
||||
currentDate)
|
||||
|
||||
def recordReopenIssueActivity(userName: String, repositoryName: String, activityUserName: String, issueId: Int, title: String): Unit =
|
||||
Activities.autoInc insert(userName, repositoryName, activityUserName,
|
||||
"reopen_issue",
|
||||
@@ -65,7 +72,14 @@ trait ActivityService {
|
||||
s"[user:${activityUserName}] commented on issue [issue:${userName}/${repositoryName}#${issueId}]",
|
||||
Some(cut(comment, 200)),
|
||||
currentDate)
|
||||
|
||||
|
||||
def recordCommentPullRequestActivity(userName: String, repositoryName: String, activityUserName: String, issueId: Int, comment: String): Unit =
|
||||
Activities.autoInc insert(userName, repositoryName, activityUserName,
|
||||
"comment_issue",
|
||||
s"[user:${activityUserName}] commented on pull request [pullreq:${userName}/${repositoryName}#${issueId}]",
|
||||
Some(cut(comment, 200)),
|
||||
currentDate)
|
||||
|
||||
def recordCreateWikiPageActivity(userName: String, repositoryName: String, activityUserName: String, pageName: String) =
|
||||
Activities.autoInc insert(userName, repositoryName, activityUserName,
|
||||
"create_wiki",
|
||||
@@ -73,11 +87,11 @@ trait ActivityService {
|
||||
Some(pageName),
|
||||
currentDate)
|
||||
|
||||
def recordEditWikiPageActivity(userName: String, repositoryName: String, activityUserName: String, pageName: String) =
|
||||
def recordEditWikiPageActivity(userName: String, repositoryName: String, activityUserName: String, pageName: String, commitId: String) =
|
||||
Activities.autoInc insert(userName, repositoryName, activityUserName,
|
||||
"edit_wiki",
|
||||
s"[user:${activityUserName}] edited the [repo:${userName}/${repositoryName}] wiki",
|
||||
Some(pageName),
|
||||
Some(pageName + ":" + commitId),
|
||||
currentDate)
|
||||
|
||||
def recordPushActivity(userName: String, repositoryName: String, activityUserName: String,
|
||||
@@ -98,15 +112,42 @@ trait ActivityService {
|
||||
|
||||
def recordCreateBranchActivity(userName: String, repositoryName: String, activityUserName: String, branchName: String) =
|
||||
Activities.autoInc insert(userName, repositoryName, activityUserName,
|
||||
"create_tag",
|
||||
"create_branch",
|
||||
s"[user:${activityUserName}] created branch [tag:${userName}/${repositoryName}#${branchName}] at [repo:${userName}/${repositoryName}]",
|
||||
None,
|
||||
currentDate)
|
||||
|
||||
|
||||
def recordForkActivity(userName: String, repositoryName: String, activityUserName: String) =
|
||||
Activities.autoInc insert(userName, repositoryName, activityUserName,
|
||||
"fork",
|
||||
s"[user:${activityUserName}] forked [repo:${userName}/${repositoryName}] to [repo:${activityUserName}/${repositoryName}]",
|
||||
None,
|
||||
currentDate)
|
||||
|
||||
def recordPullRequestActivity(userName: String, repositoryName: String, activityUserName: String, issueId: Int, title: String): Unit =
|
||||
Activities.autoInc insert(userName, repositoryName, activityUserName,
|
||||
"open_pullreq",
|
||||
s"[user:${activityUserName}] opened pull request [pullreq:${userName}/${repositoryName}#${issueId}]",
|
||||
Some(title),
|
||||
currentDate)
|
||||
|
||||
def recordMergeActivity(userName: String, repositoryName: String, activityUserName: String, issueId: Int, message: String): Unit =
|
||||
Activities.autoInc insert(userName, repositoryName, activityUserName,
|
||||
"merge_pullreq",
|
||||
s"[user:${activityUserName}] merged pull request [pullreq:${userName}/${repositoryName}#${issueId}]",
|
||||
Some(message),
|
||||
currentDate)
|
||||
|
||||
def insertCommitId(userName: String, repositoryName: String, commitId: String) = {
|
||||
CommitLog insert (userName, repositoryName, commitId)
|
||||
}
|
||||
|
||||
|
||||
def insertAllCommitIds(userName: String, repositoryName: String, commitIds: List[String]) =
|
||||
CommitLog insertAll (commitIds.map(commitId => (userName, repositoryName, commitId)): _*)
|
||||
|
||||
def getAllCommitIds(userName: String, repositoryName: String): List[String] =
|
||||
Query(CommitLog).filter(_.byRepository(userName, repositoryName)).map(_.commitId).list
|
||||
|
||||
def existsCommitId(userName: String, repositoryName: String, commitId: String): Boolean =
|
||||
Query(CommitLog).filter(_.byPrimaryKey(userName, repositoryName, commitId)).firstOption.isDefined
|
||||
|
||||
|
||||
@@ -6,8 +6,8 @@ import scala.slick.jdbc.{StaticQuery => Q}
|
||||
import Q.interpolation
|
||||
|
||||
import model._
|
||||
import util.StringUtil._
|
||||
import util.Implicits._
|
||||
import util.StringUtil._
|
||||
|
||||
trait IssuesService {
|
||||
import IssuesService._
|
||||
@@ -42,33 +42,28 @@ trait IssuesService {
|
||||
/**
|
||||
* Returns the count of the search result against issues.
|
||||
*
|
||||
* @param owner the repository owner
|
||||
* @param repository the repository name
|
||||
* @param condition the search condition
|
||||
* @param filter the filter type ("all", "assigned" or "created_by")
|
||||
* @param userName the filter user name required for "assigned" and "created_by"
|
||||
* @param filterUser the filter user name (key is "all", "assigned" or "created_by", value is the user name)
|
||||
* @param onlyPullRequest if true then counts only pull request, false then counts both of issue and pull request.
|
||||
* @param repos Tuple of the repository owner and the repository name
|
||||
* @return the count of the search result
|
||||
*/
|
||||
def countIssue(owner: String, repository: String, condition: IssueSearchCondition, filter: String, userName: Option[String]): Int = {
|
||||
// TODO It must be _.length instead of map (_.issueId) list).length.
|
||||
// But it does not work on Slick 1.0.1 (worked on Slick 1.0.0).
|
||||
// https://github.com/slick/slick/issues/170
|
||||
(searchIssueQuery(owner, repository, condition, filter, userName) map (_.issueId) list).length
|
||||
}
|
||||
def countIssue(condition: IssueSearchCondition, filterUser: Map[String, String], onlyPullRequest: Boolean,
|
||||
repos: (String, String)*): Int =
|
||||
Query(searchIssueQuery(repos, condition, filterUser, onlyPullRequest).length).first
|
||||
/**
|
||||
* Returns the Map which contains issue count for each labels.
|
||||
*
|
||||
* @param owner the repository owner
|
||||
* @param repository the repository name
|
||||
* @param condition the search condition
|
||||
* @param filter the filter type ("all", "assigned" or "created_by")
|
||||
* @param userName the filter user name required for "assigned" and "created_by"
|
||||
* @return the Map which contains issue count for each labels (key is label name, value is issue count),
|
||||
* @param filterUser the filter user name (key is "all", "assigned" or "created_by", value is the user name)
|
||||
* @return the Map which contains issue count for each labels (key is label name, value is issue count)
|
||||
*/
|
||||
def countIssueGroupByLabels(owner: String, repository: String, condition: IssueSearchCondition,
|
||||
filter: String, userName: Option[String]): Map[String, Int] = {
|
||||
filterUser: Map[String, String]): Map[String, Int] = {
|
||||
|
||||
searchIssueQuery(owner, repository, condition.copy(labels = Set.empty), filter, userName)
|
||||
searchIssueQuery(Seq(owner -> repository), condition.copy(labels = Set.empty), filterUser, false)
|
||||
.innerJoin(IssueLabels).on { (t1, t2) =>
|
||||
t1.byIssue(t2.userName, t2.repositoryName, t2.issueId)
|
||||
}
|
||||
@@ -83,76 +78,100 @@ trait IssuesService {
|
||||
}
|
||||
.toMap
|
||||
}
|
||||
/**
|
||||
* Returns list which contains issue count for each repository.
|
||||
* If the issue does not exist, its repository is not included in the result.
|
||||
*
|
||||
* @param condition the search condition
|
||||
* @param filterUser the filter user name (key is "all", "assigned" or "created_by", value is the user name)
|
||||
* @param onlyPullRequest if true then returns only pull request, false then returns both of issue and pull request.
|
||||
* @param repos Tuple of the repository owner and the repository name
|
||||
* @return list which contains issue count for each repository
|
||||
*/
|
||||
def countIssueGroupByRepository(
|
||||
condition: IssueSearchCondition, filterUser: Map[String, String], onlyPullRequest: Boolean,
|
||||
repos: (String, String)*): List[(String, String, Int)] = {
|
||||
searchIssueQuery(repos, condition.copy(repo = None), filterUser, onlyPullRequest)
|
||||
.groupBy { t =>
|
||||
t.userName ~ t.repositoryName
|
||||
}
|
||||
.map { case (repo, t) =>
|
||||
repo ~ t.length
|
||||
}
|
||||
.sortBy(_._3 desc)
|
||||
.list
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the search result against issues.
|
||||
*
|
||||
* @param owner the repository owner
|
||||
* @param repository the repository name
|
||||
* @param condition the search condition
|
||||
* @param filter the filter type ("all", "assigned" or "created_by")
|
||||
* @param userName the filter user name required for "assigned" and "created_by"
|
||||
* @param filterUser the filter user name (key is "all", "assigned", "created_by" or "not_created_by", value is the user name)
|
||||
* @param onlyPullRequest if true then returns only pull request, false then returns both of issue and pull request.
|
||||
* @param offset the offset for pagination
|
||||
* @param limit the limit for pagination
|
||||
* @param repos Tuple of the repository owner and the repository name
|
||||
* @return the search result (list of tuples which contain issue, labels and comment count)
|
||||
*/
|
||||
def searchIssue(owner: String, repository: String, condition: IssueSearchCondition,
|
||||
filter: String, userName: Option[String], offset: Int, limit: Int): List[(Issue, List[Label], Int)] = {
|
||||
def searchIssue(condition: IssueSearchCondition, filterUser: Map[String, String], onlyPullRequest: Boolean,
|
||||
offset: Int, limit: Int, repos: (String, String)*): List[(Issue, List[Label], Int)] = {
|
||||
|
||||
// get issues and comment count
|
||||
val issues = searchIssueQuery(owner, repository, condition, filter, userName)
|
||||
.leftJoin(Query(IssueComments)
|
||||
.filter { t =>
|
||||
(t.byRepository(owner, repository)) &&
|
||||
(t.action inSetBind Seq("comment", "close_comment", "reopen_comment"))
|
||||
// get issues and comment count and labels
|
||||
searchIssueQuery(repos, condition, filterUser, onlyPullRequest)
|
||||
.innerJoin(IssueOutline).on { (t1, t2) => t1.byIssue(t2.userName, t2.repositoryName, t2.issueId) }
|
||||
.leftJoin (IssueLabels) .on { case ((t1, t2), t3) => t1.byIssue(t3.userName, t3.repositoryName, t3.issueId) }
|
||||
.leftJoin (Labels) .on { case (((t1, t2), t3), t4) => t3.byLabel(t4.userName, t4.repositoryName, t4.labelId) }
|
||||
.map { case (((t1, t2), t3), t4) =>
|
||||
(t1, t2.commentCount, t4.labelId.?, t4.labelName.?, t4.color.?)
|
||||
}
|
||||
.groupBy { _.issueId }
|
||||
.map { case (issueId, t) => issueId ~ t.length }).on((t1, t2) => t1.issueId is t2._1)
|
||||
.sortBy { case (t1, t2) =>
|
||||
(condition.sort match {
|
||||
case "created" => t1.registeredDate
|
||||
case "comments" => t2._2
|
||||
case "updated" => t1.updatedDate
|
||||
}) match {
|
||||
case sort => condition.direction match {
|
||||
case "asc" => sort asc
|
||||
case "desc" => sort desc
|
||||
.sortBy(_._4) // labelName
|
||||
.sortBy { case (t1, commentCount, _,_,_) =>
|
||||
(condition.sort match {
|
||||
case "created" => t1.registeredDate
|
||||
case "comments" => commentCount
|
||||
case "updated" => t1.updatedDate
|
||||
}) match {
|
||||
case sort => condition.direction match {
|
||||
case "asc" => sort asc
|
||||
case "desc" => sort desc
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.map { case (t1, t2) => (t1, t2._2.ifNull(0)) }
|
||||
.drop(offset).take(limit)
|
||||
.list
|
||||
|
||||
// get labels
|
||||
val labels = Query(IssueLabels)
|
||||
.innerJoin(Labels).on { (t1, t2) =>
|
||||
t1.byLabel(t2.userName, t2.repositoryName, t2.labelId)
|
||||
}
|
||||
.filter { case (t1, t2) =>
|
||||
(t1.byRepository(owner, repository)) &&
|
||||
(t1.issueId inSetBind (issues.map(_._1.issueId)))
|
||||
}
|
||||
.sortBy { case (t1, t2) => t1.issueId ~ t2.labelName }
|
||||
.map { case (t1, t2) => (t1.issueId, t2) }
|
||||
.list
|
||||
|
||||
issues.map { case (issue, commentCount) =>
|
||||
(issue, labels.collect { case (issueId, labels) if(issueId == issue.issueId) => labels }, commentCount)
|
||||
}
|
||||
.drop(offset).take(limit)
|
||||
.list
|
||||
.splitWith { (c1, c2) =>
|
||||
c1._1.userName == c2._1.userName &&
|
||||
c1._1.repositoryName == c2._1.repositoryName &&
|
||||
c1._1.issueId == c2._1.issueId
|
||||
}
|
||||
.map { issues => issues.head match {
|
||||
case (issue, commentCount, _,_,_) =>
|
||||
(issue,
|
||||
issues.flatMap { t => t._3.map (
|
||||
Label(issue.userName, issue.repositoryName, _, t._4.get, t._5.get)
|
||||
)} toList,
|
||||
commentCount)
|
||||
}} toList
|
||||
}
|
||||
|
||||
/**
|
||||
* Assembles query for conditional issue searching.
|
||||
*/
|
||||
private def searchIssueQuery(owner: String, repository: String, condition: IssueSearchCondition, filter: String, userName: Option[String]) =
|
||||
private def searchIssueQuery(repos: Seq[(String, String)], condition: IssueSearchCondition,
|
||||
filterUser: Map[String, String], onlyPullRequest: Boolean) =
|
||||
Query(Issues) filter { t1 =>
|
||||
(t1.byRepository(owner, repository)) &&
|
||||
condition.repo
|
||||
.map { _.split('/') match { case array => Seq(array(0) -> array(1)) } }
|
||||
.getOrElse (repos)
|
||||
.map { case (owner, repository) => t1.byRepository(owner, repository) }
|
||||
.foldLeft[Column[Boolean]](false) ( _ || _ ) &&
|
||||
(t1.closed is (condition.state == "closed").bind) &&
|
||||
(t1.milestoneId is condition.milestoneId.get.get.bind, condition.milestoneId.flatten.isDefined) &&
|
||||
(t1.milestoneId isNull, condition.milestoneId == Some(None)) &&
|
||||
(t1.assignedUserName is userName.get.bind, filter == "assigned") &&
|
||||
(t1.openedUserName is userName.get.bind, filter == "created_by") &&
|
||||
(t1.assignedUserName is filterUser("assigned").bind, filterUser.get("assigned").isDefined) &&
|
||||
(t1.openedUserName is filterUser("created_by").bind, filterUser.get("created_by").isDefined) &&
|
||||
(t1.openedUserName isNot filterUser("not_created_by").bind, filterUser.get("not_created_by").isDefined) &&
|
||||
(t1.pullRequest is true.bind, onlyPullRequest) &&
|
||||
(IssueLabels filter { t2 =>
|
||||
(t2.byIssue(t1.userName, t1.repositoryName, t1.issueId)) &&
|
||||
(t2.labelId in
|
||||
@@ -164,7 +183,7 @@ trait IssuesService {
|
||||
}
|
||||
|
||||
def createIssue(owner: String, repository: String, loginUser: String, title: String, content: Option[String],
|
||||
assignedUserName: Option[String], milestoneId: Option[Int]) =
|
||||
assignedUserName: Option[String], milestoneId: Option[Int], isPullRequest: Boolean = false) =
|
||||
// next id number
|
||||
sql"SELECT ISSUE_ID + 1 FROM ISSUE_ID WHERE USER_NAME = $owner AND REPOSITORY_NAME = $repository FOR UPDATE".as[Int]
|
||||
.firstOption.filter { id =>
|
||||
@@ -179,7 +198,8 @@ trait IssuesService {
|
||||
content,
|
||||
false,
|
||||
currentDate,
|
||||
currentDate)
|
||||
currentDate,
|
||||
isPullRequest)
|
||||
|
||||
// increment issue id
|
||||
IssueId
|
||||
@@ -237,6 +257,60 @@ trait IssuesService {
|
||||
}
|
||||
.update (closed, currentDate)
|
||||
|
||||
/**
|
||||
* Search issues by keyword.
|
||||
*
|
||||
* @param owner the repository owner
|
||||
* @param repository the repository name
|
||||
* @param query the keywords separated by whitespace.
|
||||
* @return issues with comment count and matched content of issue or comment
|
||||
*/
|
||||
def searchIssuesByKeyword(owner: String, repository: String, query: String): List[(Issue, Int, String)] = {
|
||||
import scala.slick.driver.H2Driver.likeEncode
|
||||
val keywords = splitWords(query.toLowerCase)
|
||||
|
||||
// Search Issue
|
||||
val issues = Issues
|
||||
.innerJoin(IssueOutline).on { case (t1, t2) =>
|
||||
t1.byIssue(t2.userName, t2.repositoryName, t2.issueId)
|
||||
}
|
||||
.filter { case (t1, t2) =>
|
||||
keywords.map { keyword =>
|
||||
(t1.title.toLowerCase like (s"%${likeEncode(keyword)}%", '^')) ||
|
||||
(t1.content.toLowerCase like (s"%${likeEncode(keyword)}%", '^'))
|
||||
} .reduceLeft(_ && _)
|
||||
}
|
||||
.map { case (t1, t2) =>
|
||||
(t1, 0, t1.content.?, t2.commentCount)
|
||||
}
|
||||
|
||||
// Search IssueComment
|
||||
val comments = IssueComments
|
||||
.innerJoin(Issues).on { case (t1, t2) =>
|
||||
t1.byIssue(t2.userName, t2.repositoryName, t2.issueId)
|
||||
}
|
||||
.innerJoin(IssueOutline).on { case ((t1, t2), t3) =>
|
||||
t2.byIssue(t3.userName, t3.repositoryName, t3.issueId)
|
||||
}
|
||||
.filter { case ((t1, t2), t3) =>
|
||||
keywords.map { query =>
|
||||
t1.content.toLowerCase like (s"%${likeEncode(query)}%", '^')
|
||||
}.reduceLeft(_ && _)
|
||||
}
|
||||
.map { case ((t1, t2), t3) =>
|
||||
(t2, t1.commentId, t1.content.?, t3.commentCount)
|
||||
}
|
||||
|
||||
issues.union(comments).sortBy { case (issue, commentId, _, _) =>
|
||||
issue.issueId ~ commentId
|
||||
}.list.splitWith { case ((issue1, _, _, _), (issue2, _, _, _)) =>
|
||||
issue1.issueId == issue2.issueId
|
||||
}.map { _.head match {
|
||||
case (issue, _, content, commentCount) => (issue, commentCount, content.getOrElse(""))
|
||||
}
|
||||
}.toList
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
object IssuesService {
|
||||
@@ -247,6 +321,7 @@ object IssuesService {
|
||||
case class IssueSearchCondition(
|
||||
labels: Set[String] = Set.empty,
|
||||
milestoneId: Option[Option[Int]] = None,
|
||||
repo: Option[String] = None,
|
||||
state: String = "open",
|
||||
sort: String = "created",
|
||||
direction: String = "desc"){
|
||||
@@ -258,6 +333,7 @@ object IssuesService {
|
||||
case Some(x) => x.toString
|
||||
case None => "none"
|
||||
})},
|
||||
repo.map("for=" + urlEncode(_)),
|
||||
Some("state=" + urlEncode(state)),
|
||||
Some("sort=" + urlEncode(sort)),
|
||||
Some("direction=" + urlEncode(direction))).flatten.mkString("&")
|
||||
@@ -274,12 +350,21 @@ object IssuesService {
|
||||
def apply(request: HttpServletRequest): IssueSearchCondition =
|
||||
IssueSearchCondition(
|
||||
param(request, "labels").map(_.split(" ").toSet).getOrElse(Set.empty),
|
||||
param(request, "milestone").map(_ match {
|
||||
param(request, "milestone").map{
|
||||
case "none" => None
|
||||
case x => Some(x.toInt)
|
||||
}),
|
||||
case x => x.toIntOpt
|
||||
},
|
||||
param(request, "for"),
|
||||
param(request, "state", Seq("open", "closed")).getOrElse("open"),
|
||||
param(request, "sort", Seq("created", "comments", "updated")).getOrElse("created"),
|
||||
param(request, "direction", Seq("asc", "desc")).getOrElse("desc"))
|
||||
|
||||
def page(request: HttpServletRequest) = try {
|
||||
val i = param(request, "page").getOrElse("1").toInt
|
||||
if(i <= 0) 1 else i
|
||||
} catch {
|
||||
case e: NumberFormatException => 1
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
54
src/main/scala/service/PullRequestService.scala
Normal file
54
src/main/scala/service/PullRequestService.scala
Normal file
@@ -0,0 +1,54 @@
|
||||
package service
|
||||
|
||||
import scala.slick.driver.H2Driver.simple._
|
||||
import Database.threadLocalSession
|
||||
import model._
|
||||
import util.ControlUtil._
|
||||
|
||||
trait PullRequestService { self: IssuesService =>
|
||||
import PullRequestService._
|
||||
|
||||
def getPullRequest(owner: String, repository: String, issueId: Int): Option[(Issue, PullRequest)] =
|
||||
getIssue(owner, repository, issueId.toString).flatMap{ issue =>
|
||||
Query(PullRequests).filter(_.byPrimaryKey(owner, repository, issueId)).firstOption.map{
|
||||
pullreq => (issue, pullreq)
|
||||
}
|
||||
}
|
||||
|
||||
def getPullRequestCountGroupByUser(closed: Boolean, owner: String, repository: Option[String]): List[PullRequestCount] =
|
||||
Query(PullRequests)
|
||||
.innerJoin(Issues).on { (t1, t2) => t1.byPrimaryKey(t2.userName, t2.repositoryName, t2.issueId) }
|
||||
.filter { case (t1, t2) =>
|
||||
(t2.closed is closed.bind) &&
|
||||
(t1.userName is owner.bind) &&
|
||||
(t1.repositoryName is repository.get.bind, repository.isDefined)
|
||||
}
|
||||
.groupBy { case (t1, t2) => t2.openedUserName }
|
||||
.map { case (userName, t) => userName ~ t.length }
|
||||
.sortBy(_._2 desc)
|
||||
.list
|
||||
.map { x => PullRequestCount(x._1, x._2) }
|
||||
|
||||
def createPullRequest(originUserName: String, originRepositoryName: String, issueId: Int,
|
||||
originBranch: String, requestUserName: String, requestRepositoryName: String, requestBranch: String,
|
||||
commitIdFrom: String, commitIdTo: String): Unit =
|
||||
PullRequests insert (PullRequest(
|
||||
originUserName,
|
||||
originRepositoryName,
|
||||
issueId,
|
||||
originBranch,
|
||||
requestUserName,
|
||||
requestRepositoryName,
|
||||
requestBranch,
|
||||
commitIdFrom,
|
||||
commitIdTo))
|
||||
|
||||
}
|
||||
|
||||
object PullRequestService {
|
||||
|
||||
val PullRequestLimit = 25
|
||||
|
||||
case class PullRequestCount(userName: String, count: Int)
|
||||
|
||||
}
|
||||
126
src/main/scala/service/RepositorySearchService.scala
Normal file
126
src/main/scala/service/RepositorySearchService.scala
Normal file
@@ -0,0 +1,126 @@
|
||||
package service
|
||||
|
||||
import util.{FileUtil, StringUtil, JGitUtil}
|
||||
import util.Directory._
|
||||
import util.ControlUtil._
|
||||
import model.Issue
|
||||
import org.eclipse.jgit.revwalk.RevWalk
|
||||
import org.eclipse.jgit.treewalk.TreeWalk
|
||||
import scala.collection.mutable.ListBuffer
|
||||
import org.eclipse.jgit.lib.FileMode
|
||||
import org.eclipse.jgit.api.Git
|
||||
|
||||
trait
|
||||
RepositorySearchService { self: IssuesService =>
|
||||
import RepositorySearchService._
|
||||
|
||||
def countIssues(owner: String, repository: String, query: String): Int =
|
||||
searchIssuesByKeyword(owner, repository, query).length
|
||||
|
||||
def searchIssues(owner: String, repository: String, query: String): List[IssueSearchResult] =
|
||||
searchIssuesByKeyword(owner, repository, query).map { case (issue, commentCount, content) =>
|
||||
IssueSearchResult(
|
||||
issue.issueId,
|
||||
issue.title,
|
||||
issue.openedUserName,
|
||||
issue.registeredDate,
|
||||
commentCount,
|
||||
getHighlightText(content, query)._1)
|
||||
}
|
||||
|
||||
def countFiles(owner: String, repository: String, query: String): Int =
|
||||
using(Git.open(getRepositoryDir(owner, repository))){ git =>
|
||||
if(JGitUtil.isEmpty(git)) 0 else searchRepositoryFiles(git, query).length
|
||||
}
|
||||
|
||||
def searchFiles(owner: String, repository: String, query: String): List[FileSearchResult] =
|
||||
using(Git.open(getRepositoryDir(owner, repository))){ git =>
|
||||
if(JGitUtil.isEmpty(git)){
|
||||
Nil
|
||||
} else {
|
||||
val files = searchRepositoryFiles(git, query)
|
||||
val commits = JGitUtil.getLatestCommitFromPaths(git, files.toList.map(_._1), "HEAD")
|
||||
files.map { case (path, text) =>
|
||||
val (highlightText, lineNumber) = getHighlightText(text, query)
|
||||
FileSearchResult(
|
||||
path,
|
||||
commits(path).getCommitterIdent.getWhen,
|
||||
highlightText,
|
||||
lineNumber)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private def searchRepositoryFiles(git: Git, query: String): List[(String, String)] = {
|
||||
val revWalk = new RevWalk(git.getRepository)
|
||||
val objectId = git.getRepository.resolve("HEAD")
|
||||
val revCommit = revWalk.parseCommit(objectId)
|
||||
val treeWalk = new TreeWalk(git.getRepository)
|
||||
treeWalk.setRecursive(true)
|
||||
treeWalk.addTree(revCommit.getTree)
|
||||
|
||||
val keywords = StringUtil.splitWords(query.toLowerCase)
|
||||
val list = new ListBuffer[(String, String)]
|
||||
|
||||
while (treeWalk.next()) {
|
||||
if(treeWalk.getFileMode(0) != FileMode.TREE){
|
||||
JGitUtil.getContent(git, treeWalk.getObjectId(0), false).foreach { bytes =>
|
||||
if(FileUtil.isText(bytes)){
|
||||
val text = StringUtil.convertFromByteArray(bytes)
|
||||
val lowerText = text.toLowerCase
|
||||
val indices = keywords.map(lowerText.indexOf _)
|
||||
if(!indices.exists(_ < 0)){
|
||||
list.append((treeWalk.getPathString, text))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
treeWalk.release
|
||||
revWalk.release
|
||||
|
||||
list.toList
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
object RepositorySearchService {
|
||||
|
||||
val CodeLimit = 10
|
||||
val IssueLimit = 10
|
||||
|
||||
def getHighlightText(content: String, query: String): (String, Int) = {
|
||||
val keywords = StringUtil.splitWords(query.toLowerCase)
|
||||
val lowerText = content.toLowerCase
|
||||
val indices = keywords.map(lowerText.indexOf _)
|
||||
|
||||
if(!indices.exists(_ < 0)){
|
||||
val lineNumber = content.substring(0, indices.min).split("\n").size - 1
|
||||
val highlightText = StringUtil.escapeHtml(content.split("\n").drop(lineNumber).take(5).mkString("\n"))
|
||||
.replaceAll("(?i)(" + keywords.map("\\Q" + _ + "\\E").mkString("|") + ")",
|
||||
"<span class=\"highlight\">$1</span>")
|
||||
(highlightText, lineNumber + 1)
|
||||
} else {
|
||||
(content.split("\n").take(5).mkString("\n"), 1)
|
||||
}
|
||||
}
|
||||
|
||||
case class SearchResult(
|
||||
files : List[(String, String)],
|
||||
issues: List[(Issue, Int, String)])
|
||||
|
||||
case class IssueSearchResult(
|
||||
issueId: Int,
|
||||
title: String,
|
||||
openedUserName: String,
|
||||
registeredDate: java.util.Date,
|
||||
commentCount: Int,
|
||||
highlightText: String)
|
||||
|
||||
case class FileSearchResult(
|
||||
path: String,
|
||||
lastModified: java.util.Date,
|
||||
highlightText: String,
|
||||
highlightLineNumber: Int)
|
||||
|
||||
}
|
||||
@@ -15,19 +15,27 @@ trait RepositoryService { self: AccountService =>
|
||||
* @param userName the user name of the repository owner
|
||||
* @param description the repository description
|
||||
* @param isPrivate the repository type (private is true, otherwise false)
|
||||
* @param originRepositoryName specify for the forked repository. (default is None)
|
||||
* @param originUserName specify for the forked repository. (default is None)
|
||||
*/
|
||||
def createRepository(repositoryName: String, userName: String, description: Option[String], isPrivate: Boolean): Unit = {
|
||||
def createRepository(repositoryName: String, userName: String, description: Option[String], isPrivate: Boolean,
|
||||
originRepositoryName: Option[String] = None, originUserName: Option[String] = None,
|
||||
parentRepositoryName: Option[String] = None, parentUserName: Option[String] = None): Unit = {
|
||||
Repositories insert
|
||||
Repository(
|
||||
userName = userName,
|
||||
repositoryName = repositoryName,
|
||||
isPrivate = isPrivate,
|
||||
description = description,
|
||||
defaultBranch = "master",
|
||||
registeredDate = currentDate,
|
||||
updatedDate = currentDate,
|
||||
lastActivityDate = currentDate)
|
||||
|
||||
userName = userName,
|
||||
repositoryName = repositoryName,
|
||||
isPrivate = isPrivate,
|
||||
description = description,
|
||||
defaultBranch = "master",
|
||||
registeredDate = currentDate,
|
||||
updatedDate = currentDate,
|
||||
lastActivityDate = currentDate,
|
||||
originUserName = originUserName,
|
||||
originRepositoryName = originRepositoryName,
|
||||
parentUserName = parentUserName,
|
||||
parentRepositoryName = parentRepositoryName)
|
||||
|
||||
IssueId insert (userName, repositoryName, 0)
|
||||
}
|
||||
|
||||
@@ -39,8 +47,10 @@ trait RepositoryService { self: AccountService =>
|
||||
Labels .filter(_.byRepository(userName, repositoryName)).delete
|
||||
IssueComments .filter(_.byRepository(userName, repositoryName)).delete
|
||||
Issues .filter(_.byRepository(userName, repositoryName)).delete
|
||||
PullRequests .filter(_.byRepository(userName, repositoryName)).delete
|
||||
IssueId .filter(_.byRepository(userName, repositoryName)).delete
|
||||
Milestones .filter(_.byRepository(userName, repositoryName)).delete
|
||||
WebHooks .filter(_.byRepository(userName, repositoryName)).delete
|
||||
Repositories .filter(_.byRepository(userName, repositoryName)).delete
|
||||
}
|
||||
|
||||
@@ -53,39 +63,6 @@ trait RepositoryService { self: AccountService =>
|
||||
def getRepositoryNamesOfUser(userName: String): List[String] =
|
||||
Query(Repositories) filter(_.userName is userName.bind) map (_.repositoryName) list
|
||||
|
||||
/**
|
||||
* Returns the list of specified user's repositories information.
|
||||
*
|
||||
* @param userName the user name
|
||||
* @param baseUrl the base url of this application
|
||||
* @param loginUserName the logged in user name
|
||||
* @return the list of repository information which is sorted in descending order of lastActivityDate.
|
||||
*/
|
||||
def getVisibleRepositories(userName: String, baseUrl: String, loginUserName: Option[String]): List[RepositoryInfo] = {
|
||||
val q1 = Repositories
|
||||
.filter { t => t.userName is userName.bind }
|
||||
.map { r => r }
|
||||
|
||||
val q2 = Collaborators
|
||||
.innerJoin(Repositories).on((t1, t2) => t1.byRepository(t2.userName, t2.repositoryName))
|
||||
.filter{ case (t1, t2) => t1.collaboratorName is userName.bind}
|
||||
.map { case (t1, t2) => t2 }
|
||||
|
||||
def visibleFor(t: Repositories.type, loginUserName: Option[String]) = {
|
||||
loginUserName match {
|
||||
case Some(x) => (t.isPrivate is false.bind) || (
|
||||
(t.isPrivate is true.bind) && ((t.userName is x.bind) || (Collaborators.filter { c =>
|
||||
c.byRepository(t.userName, t.repositoryName) && (c.collaboratorName is x.bind)
|
||||
}.exists)))
|
||||
case None => (t.isPrivate is false.bind)
|
||||
}
|
||||
}
|
||||
|
||||
q1.union(q2).filter(visibleFor(_, loginUserName)).sortBy(_.lastActivityDate desc).list map { repository =>
|
||||
new RepositoryInfo(JGitUtil.getRepositoryInfo(repository.userName, repository.repositoryName, baseUrl), repository)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the specified repository information.
|
||||
*
|
||||
@@ -96,34 +73,69 @@ trait RepositoryService { self: AccountService =>
|
||||
*/
|
||||
def getRepository(userName: String, repositoryName: String, baseUrl: String): Option[RepositoryInfo] = {
|
||||
(Query(Repositories) filter { t => t.byRepository(userName, repositoryName) } firstOption) map { repository =>
|
||||
new RepositoryInfo(JGitUtil.getRepositoryInfo(repository.userName, repository.repositoryName, baseUrl), repository)
|
||||
// for getting issue count and pull request count
|
||||
val issues = Query(Issues).filter { t =>
|
||||
t.byRepository(repository.userName, repository.repositoryName) && (t.closed is false.bind)
|
||||
}.map(_.pullRequest).list
|
||||
|
||||
new RepositoryInfo(
|
||||
JGitUtil.getRepositoryInfo(repository.userName, repository.repositoryName, baseUrl),
|
||||
repository,
|
||||
issues.size,
|
||||
issues.filter(_ == true).size,
|
||||
getForkedCount(
|
||||
repository.originUserName.getOrElse(repository.userName),
|
||||
repository.originRepositoryName.getOrElse(repository.repositoryName)
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
def getUserRepositories(userName: String, baseUrl: String): List[RepositoryInfo] = {
|
||||
Query(Repositories).filter { t1 =>
|
||||
(t1.userName is userName.bind) ||
|
||||
(Query(Collaborators).filter { t2 => t2.byRepository(t1.userName, t1.repositoryName) && (t2.collaboratorName is userName.bind)} exists)
|
||||
}.sortBy(_.lastActivityDate desc).list.map{ repository =>
|
||||
new RepositoryInfo(
|
||||
JGitUtil.getRepositoryInfo(repository.userName, repository.repositoryName, baseUrl),
|
||||
repository,
|
||||
getForkedCount(
|
||||
repository.originUserName.getOrElse(repository.userName),
|
||||
repository.originRepositoryName.getOrElse(repository.repositoryName)
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of accessible repositories information for the specified account user.
|
||||
*
|
||||
* @param account the account
|
||||
* Returns the list of visible repositories for the specified user.
|
||||
* If repositoryUserName is given then filters results by repository owner.
|
||||
*
|
||||
* @param loginAccount the logged in account
|
||||
* @param baseUrl the base url of this application
|
||||
* @return the repository informations which is sorted in descending order of lastActivityDate.
|
||||
* @param repositoryUserName the repository owner (if None then returns all repositories which are visible for logged in user)
|
||||
* @return the repository information which is sorted in descending order of lastActivityDate.
|
||||
*/
|
||||
def getAccessibleRepositories(account: Option[Account], baseUrl: String): List[RepositoryInfo] = {
|
||||
|
||||
def newRepositoryInfo(repository: Repository): RepositoryInfo = {
|
||||
new RepositoryInfo(JGitUtil.getRepositoryInfo(repository.userName, repository.repositoryName, baseUrl), repository)
|
||||
}
|
||||
|
||||
(account match {
|
||||
def getVisibleRepositories(loginAccount: Option[Account], baseUrl: String, repositoryUserName: Option[String] = None): List[RepositoryInfo] = {
|
||||
(loginAccount match {
|
||||
// for Administrators
|
||||
case Some(x) if(x.isAdmin) => Query(Repositories)
|
||||
// for Normal Users
|
||||
case Some(x) if(!x.isAdmin) =>
|
||||
Query(Repositories) filter { t => (t.isPrivate is false.bind) ||
|
||||
(Query(Collaborators).filter(t2 => t2.byRepository(t.userName, t.repositoryName) && (t2.collaboratorName is x.userName.bind)) exists)
|
||||
(Query(Collaborators).filter { t2 => t2.byRepository(t.userName, t.repositoryName) && (t2.collaboratorName is x.userName.bind)} exists)
|
||||
}
|
||||
// for Guests
|
||||
case None => Query(Repositories) filter(_.isPrivate is false.bind)
|
||||
}).sortBy(_.lastActivityDate desc).list.map(newRepositoryInfo _)
|
||||
}).filter { t =>
|
||||
repositoryUserName.map { userName => t.userName is userName.bind } getOrElse ConstColumn.TRUE
|
||||
}.sortBy(_.lastActivityDate desc).list.map{ repository =>
|
||||
new RepositoryInfo(
|
||||
JGitUtil.getRepositoryInfo(repository.userName, repository.repositoryName, baseUrl),
|
||||
repository,
|
||||
getForkedCount(
|
||||
repository.originUserName.getOrElse(repository.userName),
|
||||
repository.originRepositoryName.getOrElse(repository.repositoryName)
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -161,6 +173,15 @@ trait RepositoryService { self: AccountService =>
|
||||
def removeCollaborator(userName: String, repositoryName: String, collaboratorName: String): Unit =
|
||||
Collaborators.filter(_.byPrimaryKey(userName, repositoryName, collaboratorName)).delete
|
||||
|
||||
/**
|
||||
* Remove all collaborators from the repository.
|
||||
*
|
||||
* @param userName the user name of the repository owner
|
||||
* @param repositoryName the repository name
|
||||
*/
|
||||
def removeCollaborators(userName: String, repositoryName: String): Unit =
|
||||
Collaborators.filter(_.byRepository(userName, repositoryName)).delete
|
||||
|
||||
/**
|
||||
* Returns the list of collaborators name which is sorted with ascending order.
|
||||
*
|
||||
@@ -180,17 +201,39 @@ trait RepositoryService { self: AccountService =>
|
||||
}
|
||||
}
|
||||
|
||||
private def getForkedCount(userName: String, repositoryName: String): Int =
|
||||
Query(Repositories.filter { t =>
|
||||
(t.originUserName is userName.bind) && (t.originRepositoryName is repositoryName.bind)
|
||||
}.length).first
|
||||
|
||||
|
||||
def getForkedRepositories(userName: String, repositoryName: String): List[String] =
|
||||
Query(Repositories).filter { t =>
|
||||
(t.originUserName is userName.bind) && (t.originRepositoryName is repositoryName.bind)
|
||||
}
|
||||
.sortBy(_.userName asc).map(_.userName).list
|
||||
|
||||
}
|
||||
|
||||
object RepositoryService {
|
||||
|
||||
case class RepositoryInfo(owner: String, name: String, url: String, repository: Repository,
|
||||
commitCount: Int, branchList: List[String], tags: List[util.JGitUtil.TagInfo]){
|
||||
issueCount: Int, pullCount: Int, commitCount: Int, forkedCount: Int,
|
||||
branchList: List[String], tags: List[util.JGitUtil.TagInfo]){
|
||||
|
||||
def this(repo: JGitUtil.RepositoryInfo, model: Repository) = {
|
||||
this(repo.owner, repo.name, repo.url, model, repo.commitCount, repo.branchList, repo.tags)
|
||||
}
|
||||
/**
|
||||
* Creates instance with issue count and pull request count.
|
||||
*/
|
||||
def this(repo: JGitUtil.RepositoryInfo, model: Repository, issueCount: Int, pullCount: Int, forkedCount: Int) =
|
||||
this(repo.owner, repo.name, repo.url, model, issueCount, pullCount, repo.commitCount, forkedCount, repo.branchList, repo.tags)
|
||||
|
||||
/**
|
||||
* Creates instance without issue count and pull request count.
|
||||
*/
|
||||
def this(repo: JGitUtil.RepositoryInfo, model: Repository, forkedCount: Int) =
|
||||
this(repo.owner, repo.name, repo.url, model, 0, 0, repo.commitCount, forkedCount, repo.branchList, repo.tags)
|
||||
}
|
||||
|
||||
case class RepositoryTreeNode(owner: String, name: String, children: List[RepositoryTreeNode])
|
||||
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
package service
|
||||
|
||||
import model._
|
||||
import service.SystemSettingsService.SystemSettings
|
||||
|
||||
/**
|
||||
* This service is used for a view helper mainly.
|
||||
@@ -10,6 +11,11 @@ import model._
|
||||
*/
|
||||
trait RequestCache {
|
||||
|
||||
def getSystemSettings()(implicit context: app.Context): SystemSettings =
|
||||
context.cache("system_settings"){
|
||||
new SystemSettingsService {}.loadSystemSettings()
|
||||
}
|
||||
|
||||
def getIssue(userName: String, repositoryName: String, issueId: String)(implicit context: app.Context): Option[Issue] = {
|
||||
context.cache(s"issue.${userName}/${repositoryName}#${issueId}"){
|
||||
new IssuesService {}.getIssue(userName, repositoryName, issueId)
|
||||
|
||||
@@ -1,40 +1,152 @@
|
||||
package service
|
||||
|
||||
import util.Directory._
|
||||
import SystemSettingsService._
|
||||
|
||||
trait SystemSettingsService {
|
||||
|
||||
def saveSystemSettings(settings: SystemSettings): Unit = {
|
||||
val props = new java.util.Properties()
|
||||
props.setProperty(AllowAccountRegistration, settings.allowAccountRegistration.toString)
|
||||
props.store(new java.io.FileOutputStream(GitBucketConf), null)
|
||||
}
|
||||
|
||||
|
||||
def loadSystemSettings(): SystemSettings = {
|
||||
val props = new java.util.Properties()
|
||||
if(GitBucketConf.exists){
|
||||
props.load(new java.io.FileInputStream(GitBucketConf))
|
||||
}
|
||||
SystemSettings(getBoolean(props, "allow_account_registration"))
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
object SystemSettingsService {
|
||||
|
||||
case class SystemSettings(allowAccountRegistration: Boolean)
|
||||
|
||||
private val AllowAccountRegistration = "allow_account_registration"
|
||||
|
||||
private def getBoolean(props: java.util.Properties, key: String, default: Boolean = false): Boolean = {
|
||||
val value = props.getProperty(key)
|
||||
if(value == null || value.isEmpty){
|
||||
default
|
||||
} else {
|
||||
value.toBoolean
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
package service
|
||||
|
||||
import util.Directory._
|
||||
import util.ControlUtil._
|
||||
import SystemSettingsService._
|
||||
|
||||
trait SystemSettingsService {
|
||||
|
||||
def saveSystemSettings(settings: SystemSettings): Unit = {
|
||||
defining(new java.util.Properties()){ props =>
|
||||
props.setProperty(AllowAccountRegistration, settings.allowAccountRegistration.toString)
|
||||
props.setProperty(Gravatar, settings.gravatar.toString)
|
||||
props.setProperty(Notification, settings.notification.toString)
|
||||
if(settings.notification) {
|
||||
settings.smtp.foreach { smtp =>
|
||||
props.setProperty(SmtpHost, smtp.host)
|
||||
smtp.port.foreach(x => props.setProperty(SmtpPort, x.toString))
|
||||
smtp.user.foreach(props.setProperty(SmtpUser, _))
|
||||
smtp.password.foreach(props.setProperty(SmtpPassword, _))
|
||||
smtp.ssl.foreach(x => props.setProperty(SmtpSsl, x.toString))
|
||||
smtp.fromAddress.foreach(props.setProperty(SmtpFromAddress, _))
|
||||
smtp.fromName.foreach(props.setProperty(SmtpFromName, _))
|
||||
}
|
||||
}
|
||||
props.setProperty(LdapAuthentication, settings.ldapAuthentication.toString)
|
||||
if(settings.ldapAuthentication){
|
||||
settings.ldap.map { ldap =>
|
||||
props.setProperty(LdapHost, ldap.host)
|
||||
ldap.port.foreach(x => props.setProperty(LdapPort, x.toString))
|
||||
ldap.bindDN.foreach(x => props.setProperty(LdapBindDN, x))
|
||||
ldap.bindPassword.foreach(x => props.setProperty(LdapBindPassword, x))
|
||||
props.setProperty(LdapBaseDN, ldap.baseDN)
|
||||
props.setProperty(LdapUserNameAttribute, ldap.userNameAttribute)
|
||||
props.setProperty(LdapMailAddressAttribute, ldap.mailAttribute)
|
||||
}
|
||||
}
|
||||
props.store(new java.io.FileOutputStream(GitBucketConf), null)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
def loadSystemSettings(): SystemSettings = {
|
||||
defining(new java.util.Properties()){ props =>
|
||||
if(GitBucketConf.exists){
|
||||
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),
|
||||
getOptionValue(props, SmtpFromAddress, None),
|
||||
getOptionValue(props, SmtpFromName, 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
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
object SystemSettingsService {
|
||||
import scala.reflect.ClassTag
|
||||
|
||||
case class SystemSettings(
|
||||
allowAccountRegistration: Boolean,
|
||||
gravatar: Boolean,
|
||||
notification: Boolean,
|
||||
smtp: Option[Smtp],
|
||||
ldapAuthentication: Boolean,
|
||||
ldap: Option[Ldap])
|
||||
|
||||
case class Ldap(
|
||||
host: String,
|
||||
port: Option[Int],
|
||||
bindDN: Option[String],
|
||||
bindPassword: Option[String],
|
||||
baseDN: String,
|
||||
userNameAttribute: String,
|
||||
mailAttribute: String)
|
||||
|
||||
case class Smtp(
|
||||
host: String,
|
||||
port: Option[Int],
|
||||
user: Option[String],
|
||||
password: Option[String],
|
||||
ssl: Option[Boolean],
|
||||
fromAddress: Option[String],
|
||||
fromName: Option[String])
|
||||
|
||||
val DefaultSmtpPort = 25
|
||||
val DefaultLdapPort = 389
|
||||
|
||||
private val AllowAccountRegistration = "allow_account_registration"
|
||||
private val Gravatar = "gravatar"
|
||||
private val Notification = "notification"
|
||||
private val SmtpHost = "smtp.host"
|
||||
private val SmtpPort = "smtp.port"
|
||||
private val SmtpUser = "smtp.user"
|
||||
private val SmtpPassword = "smtp.password"
|
||||
private val SmtpSsl = "smtp.ssl"
|
||||
private val SmtpFromAddress = "smtp.from_address"
|
||||
private val SmtpFromName = "smtp.from_name"
|
||||
private val LdapAuthentication = "ldap_authentication"
|
||||
private val LdapHost = "ldap.host"
|
||||
private val LdapPort = "ldap.port"
|
||||
private val LdapBindDN = "ldap.bindDN"
|
||||
private val LdapBindPassword = "ldap.bind_password"
|
||||
private val LdapBaseDN = "ldap.baseDN"
|
||||
private val LdapUserNameAttribute = "ldap.username_attribute"
|
||||
private val LdapMailAddressAttribute = "ldap.mail_attribute"
|
||||
|
||||
private def getValue[A: ClassTag](props: java.util.Properties, key: String, default: A): A =
|
||||
defining(props.getProperty(key)){ value =>
|
||||
if(value == null || value.isEmpty) default
|
||||
else convertType(value).asInstanceOf[A]
|
||||
}
|
||||
|
||||
private def getOptionValue[A: ClassTag](props: java.util.Properties, key: String, default: Option[A]): Option[A] =
|
||||
defining(props.getProperty(key)){ value =>
|
||||
if(value == null || value.isEmpty) default
|
||||
else Some(convertType(value)).asInstanceOf[Option[A]]
|
||||
}
|
||||
|
||||
private def convertType[A: ClassTag](value: String) =
|
||||
defining(implicitly[ClassTag[A]].runtimeClass){ c =>
|
||||
if(c == classOf[Boolean]) value.toBoolean
|
||||
else if(c == classOf[Int]) value.toInt
|
||||
else value
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
143
src/main/scala/service/WebHookService.scala
Normal file
143
src/main/scala/service/WebHookService.scala
Normal file
@@ -0,0 +1,143 @@
|
||||
package service
|
||||
|
||||
import scala.slick.driver.H2Driver.simple._
|
||||
import Database.threadLocalSession
|
||||
|
||||
import model._
|
||||
import org.slf4j.LoggerFactory
|
||||
import service.RepositoryService.RepositoryInfo
|
||||
import util.JGitUtil
|
||||
import org.eclipse.jgit.diff.DiffEntry
|
||||
import util.JGitUtil.CommitInfo
|
||||
import org.eclipse.jgit.api.Git
|
||||
import org.apache.http.message.BasicNameValuePair
|
||||
import org.apache.http.client.entity.UrlEncodedFormEntity
|
||||
import org.apache.http.protocol.HTTP
|
||||
import org.apache.http.NameValuePair
|
||||
|
||||
trait WebHookService {
|
||||
import WebHookService._
|
||||
|
||||
private val logger = LoggerFactory.getLogger(classOf[WebHookService])
|
||||
|
||||
def getWebHookURLs(owner: String, repository: String): List[WebHook] =
|
||||
Query(WebHooks).filter(_.byRepository(owner, repository)).sortBy(_.url).list
|
||||
|
||||
def addWebHookURL(owner: String, repository: String, url :String): Unit =
|
||||
WebHooks.insert(WebHook(owner, repository, url))
|
||||
|
||||
def deleteWebHookURL(owner: String, repository: String, url :String): Unit =
|
||||
Query(WebHooks).filter(_.byPrimaryKey(owner, repository, url)).delete
|
||||
|
||||
def callWebHook(owner: String, repository: String, webHookURLs: List[WebHook], payload: WebHookPayload): Unit = {
|
||||
import org.json4s._
|
||||
import org.json4s.jackson.Serialization
|
||||
import org.json4s.jackson.Serialization.{read, write}
|
||||
import org.apache.http.client.methods.HttpPost
|
||||
import org.apache.http.impl.client.HttpClientBuilder
|
||||
import scala.concurrent._
|
||||
import ExecutionContext.Implicits.global
|
||||
|
||||
logger.debug("start callWebHook")
|
||||
implicit val formats = Serialization.formats(NoTypeHints)
|
||||
|
||||
if(webHookURLs.nonEmpty){
|
||||
val json = write(payload)
|
||||
val httpClient = HttpClientBuilder.create.build
|
||||
|
||||
webHookURLs.foreach { webHookUrl =>
|
||||
val f = future {
|
||||
logger.debug(s"start web hook invocation for ${webHookUrl}")
|
||||
val httpPost = new HttpPost(webHookUrl.url)
|
||||
|
||||
val params: java.util.List[NameValuePair] = new java.util.ArrayList()
|
||||
params.add(new BasicNameValuePair("payload", json))
|
||||
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF-8"))
|
||||
|
||||
httpClient.execute(httpPost)
|
||||
httpPost.releaseConnection()
|
||||
logger.debug(s"end web hook invocation for ${webHookUrl}")
|
||||
}
|
||||
f.onSuccess {
|
||||
case s => logger.debug(s"Success: web hook request to ${webHookUrl.url}")
|
||||
}
|
||||
f.onFailure {
|
||||
case t => logger.error(s"Failed: web hook request to ${webHookUrl.url}", t)
|
||||
}
|
||||
}
|
||||
}
|
||||
logger.debug("end callWebHook")
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
object WebHookService {
|
||||
|
||||
case class WebHookPayload(
|
||||
ref: String,
|
||||
commits: List[WebHookCommit],
|
||||
repository: WebHookRepository)
|
||||
|
||||
object WebHookPayload {
|
||||
def apply(git: Git, refName: String, repositoryInfo: RepositoryInfo,
|
||||
commits: List[CommitInfo], repositoryOwner: Account): WebHookPayload =
|
||||
WebHookPayload(
|
||||
refName,
|
||||
commits.map { commit =>
|
||||
val diffs = JGitUtil.getDiffs(git, commit.id, false)
|
||||
val commitUrl = repositoryInfo.url.replaceFirst("/git/", "/").replaceFirst("\\.git$", "") + "/commit/" + commit.id
|
||||
|
||||
WebHookCommit(
|
||||
id = commit.id,
|
||||
message = commit.fullMessage,
|
||||
timestamp = commit.time.toString,
|
||||
url = commitUrl,
|
||||
added = diffs._1.collect { case x if(x.changeType == DiffEntry.ChangeType.ADD) => x.newPath },
|
||||
removed = diffs._1.collect { case x if(x.changeType == DiffEntry.ChangeType.DELETE) => x.oldPath },
|
||||
modified = diffs._1.collect { case x if(x.changeType != DiffEntry.ChangeType.ADD &&
|
||||
x.changeType != DiffEntry.ChangeType.DELETE) => x.newPath },
|
||||
author = WebHookUser(
|
||||
name = commit.committer,
|
||||
email = commit.mailAddress
|
||||
)
|
||||
)
|
||||
}.toList,
|
||||
WebHookRepository(
|
||||
name = repositoryInfo.name,
|
||||
url = repositoryInfo.url,
|
||||
description = repositoryInfo.repository.description.getOrElse(""),
|
||||
watchers = 0,
|
||||
forks = repositoryInfo.forkedCount,
|
||||
`private` = repositoryInfo.repository.isPrivate,
|
||||
owner = WebHookUser(
|
||||
name = repositoryOwner.userName,
|
||||
email = repositoryOwner.mailAddress
|
||||
)
|
||||
)
|
||||
)
|
||||
}
|
||||
|
||||
case class WebHookCommit(
|
||||
id: String,
|
||||
message: String,
|
||||
timestamp: String,
|
||||
url: String,
|
||||
added: List[String],
|
||||
removed: List[String],
|
||||
modified: List[String],
|
||||
author: WebHookUser)
|
||||
|
||||
case class WebHookRepository(
|
||||
name: String,
|
||||
url: String,
|
||||
description: String,
|
||||
watchers: Int,
|
||||
forks: Int,
|
||||
`private`: Boolean,
|
||||
owner: WebHookUser)
|
||||
|
||||
case class WebHookUser(
|
||||
name: String,
|
||||
email: String)
|
||||
|
||||
}
|
||||
@@ -4,11 +4,11 @@ import java.io.File
|
||||
import java.util.Date
|
||||
import org.eclipse.jgit.api.Git
|
||||
import org.apache.commons.io.FileUtils
|
||||
import util.JGitUtil.DiffInfo
|
||||
import util.{Directory, JGitUtil}
|
||||
import org.eclipse.jgit.lib.RepositoryBuilder
|
||||
import util.{StringUtil, Directory, JGitUtil, LockUtil}
|
||||
import util.ControlUtil._
|
||||
import org.eclipse.jgit.treewalk.CanonicalTreeParser
|
||||
import java.util.concurrent.ConcurrentHashMap
|
||||
import org.eclipse.jgit.diff.DiffFormatter
|
||||
import org.eclipse.jgit.api.errors.PatchApplyException
|
||||
|
||||
object WikiService {
|
||||
|
||||
@@ -19,8 +19,9 @@ object WikiService {
|
||||
* @param content the page content
|
||||
* @param committer the last committer
|
||||
* @param time the last modified time
|
||||
* @param id the latest commit id
|
||||
*/
|
||||
case class WikiPageInfo(name: String, content: String, committer: String, time: Date)
|
||||
case class WikiPageInfo(name: String, content: String, committer: String, time: Date, id: String)
|
||||
|
||||
/**
|
||||
* The model for wiki page history.
|
||||
@@ -32,65 +33,35 @@ object WikiService {
|
||||
*/
|
||||
case class WikiPageHistoryInfo(name: String, committer: String, message: String, date: Date)
|
||||
|
||||
/**
|
||||
* lock objects
|
||||
*/
|
||||
private val locks = new ConcurrentHashMap[String, AnyRef]()
|
||||
|
||||
/**
|
||||
* Returns the lock object for the specified repository.
|
||||
*/
|
||||
private def getLockObject(owner: String, repository: String): AnyRef = synchronized {
|
||||
val key = owner + "/" + repository
|
||||
if(!locks.containsKey(key)){
|
||||
locks.put(key, new AnyRef())
|
||||
}
|
||||
locks.get(key)
|
||||
}
|
||||
|
||||
/**
|
||||
* Synchronizes a given function which modifies the working copy of the wiki repository.
|
||||
*
|
||||
* @param owner the repository owner
|
||||
* @param repository the repository name
|
||||
* @param f the function which modifies the working copy of the wiki repository
|
||||
* @tparam T the return type of the given function
|
||||
* @return the result of the given function
|
||||
*/
|
||||
def lock[T](owner: String, repository: String)(f: => T): T = getLockObject(owner, repository).synchronized(f)
|
||||
|
||||
}
|
||||
|
||||
trait WikiService {
|
||||
import WikiService._
|
||||
|
||||
def createWikiRepository(owner: model.Account, repository: String): Unit = {
|
||||
lock(owner.userName, repository){
|
||||
val dir = Directory.getWikiRepositoryDir(owner.userName, repository)
|
||||
if(!dir.exists){
|
||||
try {
|
||||
JGitUtil.initRepository(dir)
|
||||
saveWikiPage(owner.userName, repository, "Home", "Home", s"Welcome to the ${repository} wiki!!", owner, "Initial Commit")
|
||||
} finally {
|
||||
// once delete cloned repository because initial cloned repository does not have 'branch.master.merge'
|
||||
FileUtils.deleteDirectory(Directory.getWikiWorkDir(owner.userName, repository))
|
||||
def createWikiRepository(loginAccount: model.Account, owner: String, repository: String): Unit =
|
||||
LockUtil.lock(s"${owner}/${repository}/wiki"){
|
||||
defining(Directory.getWikiRepositoryDir(owner, repository)){ dir =>
|
||||
if(!dir.exists){
|
||||
try {
|
||||
JGitUtil.initRepository(dir)
|
||||
saveWikiPage(owner, repository, "Home", "Home", s"Welcome to the ${repository} wiki!!", loginAccount, "Initial Commit", None)
|
||||
} finally {
|
||||
// once delete cloned repository because initial cloned repository does not have 'branch.master.merge'
|
||||
FileUtils.deleteDirectory(Directory.getWikiWorkDir(owner, repository))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the wiki page.
|
||||
*/
|
||||
def getWikiPage(owner: String, repository: String, pageName: String): Option[WikiPageInfo] = {
|
||||
JGitUtil.withGit(Directory.getWikiRepositoryDir(owner, repository)){ git =>
|
||||
try {
|
||||
using(Git.open(Directory.getWikiRepositoryDir(owner, repository))){ git =>
|
||||
optionIf(!JGitUtil.isEmpty(git)){
|
||||
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, file.commitId)
|
||||
}
|
||||
} catch {
|
||||
// TODO no commit, but it should not judge by exception.
|
||||
case e: NullPointerException => None
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -98,9 +69,9 @@ trait WikiService {
|
||||
/**
|
||||
* Returns the content of the specified file.
|
||||
*/
|
||||
def getFileContent(owner: String, repository: String, path: String): Option[Array[Byte]] = {
|
||||
JGitUtil.withGit(Directory.getWikiRepositoryDir(owner, repository)){ git =>
|
||||
try {
|
||||
def getFileContent(owner: String, repository: String, path: String): Option[Array[Byte]] =
|
||||
using(Git.open(Directory.getWikiRepositoryDir(owner, repository))){ git =>
|
||||
optionIf(!JGitUtil.isEmpty(git)){
|
||||
val index = path.lastIndexOf('/')
|
||||
val parentPath = if(index < 0) "." else path.substring(0, index)
|
||||
val fileName = if(index < 0) path else path.substring(index + 1)
|
||||
@@ -108,57 +79,119 @@ trait WikiService {
|
||||
JGitUtil.getFileList(git, "master", parentPath).find(_.name == fileName).map { file =>
|
||||
git.getRepository.open(file.id).getBytes
|
||||
}
|
||||
} catch {
|
||||
// TODO no commit, but it should not judge by exception.
|
||||
case e: NullPointerException => None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of wiki page names.
|
||||
*/
|
||||
def getWikiPageList(owner: String, repository: String): List[String] = {
|
||||
JGitUtil.getFileList(Git.open(Directory.getWikiRepositoryDir(owner, repository)), "master", ".")
|
||||
.filter(_.name.endsWith(".md"))
|
||||
.map(_.name.replaceFirst("\\.md$", ""))
|
||||
.sortBy(x => x)
|
||||
using(Git.open(Directory.getWikiRepositoryDir(owner, repository))){ git =>
|
||||
JGitUtil.getFileList(git, "master", ".")
|
||||
.filter(_.name.endsWith(".md"))
|
||||
.map(_.name.replaceFirst("\\.md$", ""))
|
||||
.sortBy(x => x)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Reverts specified changes.
|
||||
*/
|
||||
def revertWikiPage(owner: String, repository: String, from: String, to: String,
|
||||
committer: model.Account, pageName: Option[String]): Boolean = {
|
||||
LockUtil.lock(s"${owner}/${repository}/wiki"){
|
||||
defining(Directory.getWikiWorkDir(owner, repository)){ workDir =>
|
||||
// clone working copy
|
||||
cloneOrPullWorkingCopy(workDir, owner, repository)
|
||||
|
||||
using(Git.open(workDir)){ git =>
|
||||
val reader = git.getRepository.newObjectReader
|
||||
val oldTreeIter = new CanonicalTreeParser
|
||||
oldTreeIter.reset(reader, git.getRepository.resolve(from + "^{tree}"))
|
||||
|
||||
val newTreeIter = new CanonicalTreeParser
|
||||
newTreeIter.reset(reader, git.getRepository.resolve(to + "^{tree}"))
|
||||
|
||||
import scala.collection.JavaConverters._
|
||||
val diffs = git.diff.setNewTree(oldTreeIter).setOldTree(newTreeIter).call.asScala.filter { diff =>
|
||||
pageName match {
|
||||
case Some(x) => diff.getNewPath == x + ".md"
|
||||
case None => true
|
||||
}
|
||||
}
|
||||
|
||||
val patch = using(new java.io.ByteArrayOutputStream()){ out =>
|
||||
val formatter = new DiffFormatter(out)
|
||||
formatter.setRepository(git.getRepository)
|
||||
formatter.format(diffs.asJava)
|
||||
new String(out.toByteArray, "UTF-8")
|
||||
}
|
||||
|
||||
try {
|
||||
git.apply.setPatch(new java.io.ByteArrayInputStream(patch.getBytes("UTF-8"))).call
|
||||
git.add.addFilepattern(".").call
|
||||
git.commit.setCommitter(committer.fullName, committer.mailAddress).setMessage(pageName match {
|
||||
case Some(x) => s"Revert ${from} ... ${to} on ${x}"
|
||||
case None => s"Revert ${from} ... ${to}"
|
||||
}).call
|
||||
git.push.call
|
||||
true
|
||||
} catch {
|
||||
case ex: PatchApplyException => false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Save the wiki page.
|
||||
*/
|
||||
def saveWikiPage(owner: String, repository: String, currentPageName: String, newPageName: String,
|
||||
content: String, committer: model.Account, message: String): Unit = {
|
||||
content: String, committer: model.Account, message: String, currentId: Option[String]): Option[String] = {
|
||||
|
||||
lock(owner, repository){
|
||||
// clone working copy
|
||||
val workDir = Directory.getWikiWorkDir(owner, repository)
|
||||
cloneOrPullWorkingCopy(workDir, owner, repository)
|
||||
LockUtil.lock(s"${owner}/${repository}/wiki"){
|
||||
defining(Directory.getWikiWorkDir(owner, repository)){ workDir =>
|
||||
// clone working copy
|
||||
cloneOrPullWorkingCopy(workDir, owner, repository)
|
||||
|
||||
// write as file
|
||||
JGitUtil.withGit(workDir){ git =>
|
||||
val file = new File(workDir, newPageName + ".md")
|
||||
val added = if(!file.exists || FileUtils.readFileToString(file, "UTF-8") != content){
|
||||
FileUtils.writeStringToFile(file, content, "UTF-8")
|
||||
git.add.addFilepattern(file.getName).call
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
// write as file
|
||||
using(Git.open(workDir)){ git =>
|
||||
defining(new File(workDir, newPageName + ".md")){ file =>
|
||||
// new page
|
||||
val created = !file.exists
|
||||
|
||||
// delete file
|
||||
val deleted = if(currentPageName != "" && currentPageName != newPageName){
|
||||
git.rm.addFilepattern(currentPageName + ".md").call
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
// created or updated
|
||||
val added = executeIf(!file.exists || FileUtils.readFileToString(file, "UTF-8") != content){
|
||||
FileUtils.writeStringToFile(file, content, "UTF-8")
|
||||
git.add.addFilepattern(file.getName).call
|
||||
}
|
||||
|
||||
// commit and push
|
||||
if(added || deleted){
|
||||
git.commit.setCommitter(committer.userName, committer.mailAddress).setMessage(message).call
|
||||
git.push.call
|
||||
// delete file
|
||||
val deleted = executeIf(currentPageName != "" && currentPageName != newPageName){
|
||||
git.rm.addFilepattern(currentPageName + ".md").call
|
||||
}
|
||||
|
||||
// commit and push
|
||||
optionIf(added || deleted){
|
||||
defining(git.commit.setCommitter(committer.fullName, committer.mailAddress)
|
||||
.setMessage(if(message.trim.length == 0){
|
||||
if(deleted){
|
||||
s"Rename ${currentPageName} to ${newPageName}"
|
||||
} else if(created){
|
||||
s"Created ${newPageName}"
|
||||
} else {
|
||||
s"Updated ${newPageName}"
|
||||
}
|
||||
} else {
|
||||
message
|
||||
}).call){ commit =>
|
||||
git.push.call
|
||||
Some(commit.getName)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -167,56 +200,38 @@ trait WikiService {
|
||||
/**
|
||||
* Delete the wiki page.
|
||||
*/
|
||||
def deleteWikiPage(owner: String, repository: String, pageName: String, committer: String, message: String): Unit = {
|
||||
lock(owner, repository){
|
||||
// clone working copy
|
||||
val workDir = Directory.getWikiWorkDir(owner, repository)
|
||||
cloneOrPullWorkingCopy(workDir, owner, repository)
|
||||
def deleteWikiPage(owner: String, repository: String, pageName: String,
|
||||
committer: String, mailAddress: String, message: String): Unit = {
|
||||
LockUtil.lock(s"${owner}/${repository}/wiki"){
|
||||
defining(Directory.getWikiWorkDir(owner, repository)){ workDir =>
|
||||
// clone working copy
|
||||
cloneOrPullWorkingCopy(workDir, owner, repository)
|
||||
|
||||
// delete file
|
||||
new File(workDir, pageName + ".md").delete
|
||||
|
||||
JGitUtil.withGit(workDir){ git =>
|
||||
git.rm.addFilepattern(pageName + ".md").call
|
||||
|
||||
// commit and push
|
||||
// TODO committer's mail address
|
||||
git.commit.setAuthor(committer, committer + "@devnull").setMessage(message).call
|
||||
git.push.call
|
||||
// delete file
|
||||
new File(workDir, pageName + ".md").delete
|
||||
|
||||
using(Git.open(workDir)){ git =>
|
||||
git.rm.addFilepattern(pageName + ".md").call
|
||||
|
||||
// commit and push
|
||||
git.commit.setCommitter(committer, mailAddress).setMessage(message).call
|
||||
git.push.call
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns differences between specified commits.
|
||||
*/
|
||||
def getWikiDiffs(git: Git, commitId1: String, commitId2: String): List[DiffInfo] = {
|
||||
// get diff between specified commit and its previous commit
|
||||
val reader = git.getRepository.newObjectReader
|
||||
|
||||
val oldTreeIter = new CanonicalTreeParser
|
||||
oldTreeIter.reset(reader, git.getRepository.resolve(commitId1 + "^{tree}"))
|
||||
|
||||
val newTreeIter = new CanonicalTreeParser
|
||||
newTreeIter.reset(reader, git.getRepository.resolve(commitId2 + "^{tree}"))
|
||||
|
||||
import scala.collection.JavaConverters._
|
||||
git.diff.setNewTree(newTreeIter).setOldTree(oldTreeIter).call.asScala.map { diff =>
|
||||
DiffInfo(diff.getChangeType, diff.getOldPath, diff.getNewPath,
|
||||
JGitUtil.getContent(git, diff.getOldId.toObjectId, false).map(new String(_, "UTF-8")),
|
||||
JGitUtil.getContent(git, diff.getNewId.toObjectId, false).map(new String(_, "UTF-8")))
|
||||
}.toList
|
||||
}
|
||||
|
||||
private def cloneOrPullWorkingCopy(workDir: File, owner: String, repository: String): Unit = {
|
||||
if(!workDir.exists){
|
||||
Git.cloneRepository
|
||||
.setURI(Directory.getWikiRepositoryDir(owner, repository).toURI.toString)
|
||||
.setDirectory(workDir)
|
||||
.call
|
||||
} else {
|
||||
Git.open(workDir).pull.call
|
||||
.getRepository
|
||||
.close
|
||||
} else using(Git.open(workDir)){ git =>
|
||||
git.pull.call
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user