diff --git a/src/main/scala/service/ActivityService.scala b/src/main/scala/service/ActivityService.scala index 50d0b0d..b607488 100644 --- a/src/main/scala/service/ActivityService.scala +++ b/src/main/scala/service/ActivityService.scala @@ -141,7 +141,13 @@ 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 diff --git a/src/main/scala/servlet/GitRepositoryServlet.scala b/src/main/scala/servlet/GitRepositoryServlet.scala index f7b2172..5718e46 100644 --- a/src/main/scala/servlet/GitRepositoryServlet.scala +++ b/src/main/scala/servlet/GitRepositoryServlet.scala @@ -15,6 +15,7 @@ import service._ import WebHookService._ import org.eclipse.jgit.api.Git +import util.JGitUtil.CommitInfo /** * Provides Git repository via HTTP. @@ -87,19 +88,26 @@ val commits = JGitUtil.getCommitLog(git, command.getOldId.name, command.getNewId.name) val refName = command.getRefName.split("/") - // apply issue comment - val newCommits = commits.flatMap { commit => - if(!existsCommitId(owner, repository, commit.id)){ - insertCommitId(owner, repository, commit.id) - "(^|\\W)#(\\d+)(\\W|$)".r.findAllIn(commit.fullMessage).matchData.foreach { matchData => - val issueId = matchData.group(2) - if(getAccountByUserName(commit.committer).isDefined && getIssue(owner, repository, issueId).isDefined){ - createComment(owner, repository, commit.committer, issueId.toInt, commit.fullMessage, "commit") - } + // Extract new commit and apply issue comment + val newCommits = if(commits.size > 1000){ + val existIds = getAllCommitIds(owner, repository) + commits.flatMap { commit => + optionIf(!existIds.contains(commit.id)){ + createIssueComment(commit) + Some(commit) } - Some(commit) - } else None - }.toList + } + } else { + commits.flatMap { commit => + optionIf(!existsCommitId(owner, repository, commit.id)){ + createIssueComment(commit) + Some(commit) + } + } + } + + // batch insert all new commit id + insertAllCommitIds(owner, repository, newCommits.map(_.id)) // record activity if(refName(1) == "heads"){ @@ -132,4 +140,14 @@ // update repository last modified time. updateLastActivityDate(owner, repository) } + + private def createIssueComment(commit: CommitInfo) = { + "(^|\\W)#(\\d+)(\\W|$)".r.findAllIn(commit.fullMessage).matchData.foreach { matchData => + val issueId = matchData.group(2) + if(getAccountByUserName(commit.committer).isDefined && getIssue(owner, repository, issueId).isDefined){ + createComment(owner, repository, commit.committer, issueId.toInt, commit.fullMessage, "commit") + } + } + } + }