Skip to content

Commit

Permalink
change git client restriction strategy to ReceiveHook
Browse files Browse the repository at this point in the history
  • Loading branch information
onukura committed Dec 7, 2020
1 parent 4f35d2d commit 9cfb21e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 29 deletions.
3 changes: 2 additions & 1 deletion src/main/scala/gitbucket/core/plugin/PluginRegistry.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import com.github.zafarkhaja.semver.Version
import gitbucket.core.controller.{Context, ControllerBase}
import gitbucket.core.model.{Account, Issue}
import gitbucket.core.service.ProtectedBranchService.ProtectedBranchReceiveHook
import gitbucket.core.service.RepositoryService.RepositoryInfo
import gitbucket.core.service.RepositoryService.{RepositoryInfo, ArchivedRepositoryReceiveHook}
import gitbucket.core.service.SystemSettingsService
import gitbucket.core.service.SystemSettingsService.SystemSettings
import gitbucket.core.util.{ConfigUtil, DatabaseConfig}
Expand Down Expand Up @@ -40,6 +40,7 @@ class PluginRegistry {
private val repositoryRoutings = new ConcurrentLinkedQueue[GitRepositoryRouting]
private val accountHooks = new ConcurrentLinkedQueue[AccountHook]
private val receiveHooks = new ConcurrentLinkedQueue[ReceiveHook]
receiveHooks.add(new ArchivedRepositoryReceiveHook())
receiveHooks.add(new ProtectedBranchReceiveHook())
private val repositoryHooks = new ConcurrentLinkedQueue[RepositoryHook]
private val issueHooks = new ConcurrentLinkedQueue[IssueHook]
Expand Down
36 changes: 28 additions & 8 deletions src/main/scala/gitbucket/core/service/RepositoryService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@ import gitbucket.core.model.{CommitComments => _, Session => _, _}
import gitbucket.core.model.Profile._
import gitbucket.core.model.Profile.profile.blockingApi._
import gitbucket.core.model.Profile.dateColumnType
import gitbucket.core.plugin.PluginRegistry
import gitbucket.core.plugin.{PluginRegistry, ReceiveHook}
import gitbucket.core.util.Directory.{getRepositoryDir, getRepositoryFilesDir, getTemporaryDir, getWikiRepositoryDir}
import gitbucket.core.util.JGitUtil.FileInfo
import org.apache.commons.io.FileUtils
import org.eclipse.jgit.api.Git
import org.eclipse.jgit.lib.{Repository => _, _}
import org.eclipse.jgit.transport.{ReceiveCommand, ReceivePack}

import scala.util.Using

trait RepositoryService {
Expand Down Expand Up @@ -325,13 +327,15 @@ trait RepositoryService {
.update(false)
}

def isArchivedRepository(owner: String, repository: String)(implicit s: Session): Boolean =
getRepository(owner, repository).exists { repository =>
if (repository.repository.isArchived) {
true
} else {
false
}
def isArchivedRepository(owner: String, repository: String)(implicit s: Session): Option[Boolean] =
getRepository(owner, repository) match {
case Some(repository) =>
if (repository.repository.isArchived) {
Some(true)
} else {
Some(false)
}
case _ => None
}

/**
Expand Down Expand Up @@ -877,4 +881,20 @@ object RepositoryService {
PluginRegistry().renderableExtensions.map { extension =>
s"readme.${extension}"
} ++ Seq("readme.txt", "readme")

class ArchivedRepositoryReceiveHook extends ReceiveHook with RepositoryService with AccountService {
override def preReceive(
owner: String,
repository: String,
receivePack: ReceivePack,
command: ReceiveCommand,
pusher: String
)(implicit session: Session): Option[String] = {
isArchivedRepository(owner, repository) match {
case Some(true) => Some("This repository was archived so it is read-only.")
case Some(false) => None
case _ => None
}
}
}
}
31 changes: 11 additions & 20 deletions src/main/scala/gitbucket/core/servlet/GitRepositoryServlet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -261,26 +261,17 @@ class CommitLogHook(owner: String, repository: String, pusher: String, baseUrl:
def onPreReceive(receivePack: ReceivePack, commands: java.util.Collection[ReceiveCommand]): Unit = {
Database() withTransaction { implicit session =>
try {
if (isArchivedRepository(owner, repository)) {
commands.asScala.foreach(
_.setResult(
ReceiveCommand.Result.REJECTED_OTHER_REASON,
"This repository was archived so it is read-only."
)
)
} else {
commands.asScala.foreach { command =>
// call pre-commit hook
PluginRegistry().getReceiveHooks
.flatMap(_.preReceive(owner, repository, receivePack, command, pusher))
.headOption
.foreach { error =>
command.setResult(ReceiveCommand.Result.REJECTED_OTHER_REASON, error)
}
}
Using.resource(Git.open(Directory.getRepositoryDir(owner, repository))) { git =>
existIds = JGitUtil.getAllCommitIds(git)
}
commands.asScala.foreach { command =>
// call pre-commit hook
PluginRegistry().getReceiveHooks
.flatMap(_.preReceive(owner, repository, receivePack, command, pusher))
.headOption
.foreach { error =>
command.setResult(ReceiveCommand.Result.REJECTED_OTHER_REASON, error)
}
}
Using.resource(Git.open(Directory.getRepositoryDir(owner, repository))) { git =>
existIds = JGitUtil.getAllCommitIds(git)
}
} catch {
case ex: Exception => {
Expand Down

0 comments on commit 9cfb21e

Please sign in to comment.