Feedback error in repository creation to users

This commit is contained in:
Naoki Takezoe
2017-12-04 16:42:15 +09:00
parent efd5a64749
commit c4dc1d7334
3 changed files with 50 additions and 20 deletions

View File

@@ -25,6 +25,7 @@ import org.eclipse.jgit.dircache.{DirCache, DirCacheBuilder}
import org.eclipse.jgit.errors.MissingObjectException import org.eclipse.jgit.errors.MissingObjectException
import org.eclipse.jgit.lib._ import org.eclipse.jgit.lib._
import org.eclipse.jgit.transport.{ReceiveCommand, ReceivePack} import org.eclipse.jgit.transport.{ReceiveCommand, ReceivePack}
import org.json4s.jackson.Serialization
import org.scalatra._ import org.scalatra._
import org.scalatra.i18n.Messages import org.scalatra.i18n.Messages
@@ -166,7 +167,11 @@ trait RepositoryViewerControllerBase extends ControllerBase {
ajaxGet("/:owner/:repository/creating") { ajaxGet("/:owner/:repository/creating") {
val owner = params("owner") val owner = params("owner")
val repository = params("repository") val repository = params("repository")
RepositoryCreationService.isCreating(owner, repository) contentType = formats("json")
Serialization.write(Map(
"creating" -> RepositoryCreationService.isCreating(owner, repository),
"error" -> RepositoryCreationService.getCreationError(owner, repository)
))
} }
/** /**
@@ -416,7 +421,7 @@ trait RepositoryViewerControllerBase extends ControllerBase {
contentType = formats("json") contentType = formats("json")
using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git => using(Git.open(getRepositoryDir(repository.owner, repository.name))){ git =>
val last = git.log.add(git.getRepository.resolve(id)).addPath(path).setMaxCount(1).call.iterator.next.name val last = git.log.add(git.getRepository.resolve(id)).addPath(path).setMaxCount(1).call.iterator.next.name
Map( Serialization.write(Map(
"root" -> s"${context.baseUrl}/${repository.owner}/${repository.name}", "root" -> s"${context.baseUrl}/${repository.owner}/${repository.name}",
"id" -> id, "id" -> id,
"path" -> path, "path" -> path,
@@ -431,8 +436,9 @@ trait RepositoryViewerControllerBase extends ControllerBase {
"prevPath" -> blame.prevPath, "prevPath" -> blame.prevPath,
"commited" -> blame.commitTime.getTime, "commited" -> blame.commitTime.getTime,
"message" -> blame.message, "message" -> blame.message,
"lines" -> blame.lines) "lines" -> blame.lines
}) )
}))
} }
}) })

View File

@@ -19,18 +19,25 @@ import scala.concurrent.Future
object RepositoryCreationService { object RepositoryCreationService {
private val Creating = new ConcurrentHashMap[String, Boolean]() private val Creating = new ConcurrentHashMap[String, Option[String]]()
def isCreating(owner: String, repository: String): Boolean = { def isCreating(owner: String, repository: String): Boolean = {
Creating.containsKey(s"${owner}/${repository}") Option(Creating.get(s"${owner}/${repository}")).map(_.isEmpty).getOrElse(false)
} }
def startCreation(owner: String, repository: String): Unit = { def startCreation(owner: String, repository: String): Unit = {
Creating.put(s"${owner}/${repository}", true) Creating.put(s"${owner}/${repository}", None)
} }
def endCreation(owner: String, repository: String): Unit = { def endCreation(owner: String, repository: String, error: Option[String]): Unit = {
Creating.remove(s"${owner}/${repository}") error match {
case None => Creating.remove(s"${owner}/${repository}")
case Some(error) => Creating.put(s"${owner}/${repository}", Some(error))
}
}
def getCreationError(owner: String, repository: String): Option[String] = {
Option(Creating.get(s"${owner}/${repository}")).getOrElse(None)
} }
} }
@@ -51,6 +58,15 @@ trait RepositoryCreationService {
val ownerAccount = getAccountByUserName(owner).get val ownerAccount = getAccountByUserName(owner).get
val loginUserName = loginAccount.userName val loginUserName = loginAccount.userName
val copyRepositoryDir = if (initOption == "COPY") {
sourceUrl.flatMap { url =>
val dir = Files.createTempDirectory(s"gitbucket-${owner}-${name}").toFile
Git.cloneRepository().setBare(true).setURI(url).setDirectory(dir).setCloneAllBranches(true).call()
Some(dir)
}
} else None
// Insert to the database at first // Insert to the database at first
insertRepository(name, owner, description, isPrivate) insertRepository(name, owner, description, isPrivate)
@@ -95,16 +111,12 @@ trait RepositoryCreationService {
} }
} }
if (initOption == "COPY") { copyRepositoryDir.foreach { dir =>
sourceUrl.foreach { url => try {
// TODO How to feedback error in this block?
val dir = Files.createTempDirectory(s"gitbucket-${owner}-${name}").toFile
Git.cloneRepository().setBare(true).setURI(url).setDirectory(dir).setCloneAllBranches(true).call()
using(Git.open(dir)) { git => using(Git.open(dir)) { git =>
git.push().setRemote(gitdir.toURI.toString).setPushAll().setPushTags().call() git.push().setRemote(gitdir.toURI.toString).setPushAll().setPushTags().call()
} }
} finally {
FileUtils.deleteQuietly(dir) FileUtils.deleteQuietly(dir)
} }
} }
@@ -115,8 +127,14 @@ trait RepositoryCreationService {
// Record activity // Record activity
recordCreateRepositoryActivity(owner, name, loginUserName) recordCreateRepositoryActivity(owner, name, loginUserName)
} }
} finally {
RepositoryCreationService.endCreation(owner, name) RepositoryCreationService.endCreation(owner, name, None)
} catch {
case ex: Exception => {
ex.printStackTrace()
RepositoryCreationService.endCreation(owner, name, Some(ex.toString))
}
} }
} }

View File

@@ -16,11 +16,17 @@
function checkCreating() { function checkCreating() {
$.get('@context.path/@owner/@repository/creating', function (data) { $.get('@context.path/@owner/@repository/creating', function (data) {
if (data == 'true') { console.log(data);
if (data.creating == true) {
setTimeout(checkCreating, 2000); setTimeout(checkCreating, 2000);
} else {
if (data.error) {
alert(data.error);
location.href = '@context.path/new';
} else { } else {
location.href = '@context.path/@owner/@repository'; location.href = '@context.path/@owner/@repository';
} }
}
}); });
} }
</script> </script>