mirror of
https://github.com/gitbucket/gitbucket.git
synced 2025-11-04 20:45:58 +01:00
Initial experimental implementation of the validation framework.
This commit is contained in:
@@ -10,8 +10,8 @@ import org.apache.commons.io._
|
||||
/**
|
||||
* Creates new repository.
|
||||
*/
|
||||
class CreateRepositoryServlet extends ScalatraServlet with ServletBase {
|
||||
|
||||
class CreateRepositoryServlet extends ServletBase {
|
||||
|
||||
/**
|
||||
* Show the new repository form.
|
||||
*/
|
||||
@@ -23,42 +23,60 @@ class CreateRepositoryServlet extends ScalatraServlet with ServletBase {
|
||||
* Create new repository.
|
||||
*/
|
||||
post("/") {
|
||||
val repositoryName = params("name")
|
||||
val description = params("description")
|
||||
withValidation(validate, params){
|
||||
val repositoryName = params("name")
|
||||
val description = params("description")
|
||||
|
||||
val gitdir = getRepositoryDir(LoginUser, repositoryName)
|
||||
val repository = new RepositoryBuilder().setGitDir(gitdir).setBare.build
|
||||
val gitdir = getRepositoryDir(LoginUser, repositoryName)
|
||||
val repository = new RepositoryBuilder().setGitDir(gitdir).setBare.build
|
||||
|
||||
repository.create
|
||||
repository.create
|
||||
|
||||
val config = repository.getConfig
|
||||
config.setBoolean("http", null, "receivepack", true)
|
||||
config.save
|
||||
val config = repository.getConfig
|
||||
config.setBoolean("http", null, "receivepack", true)
|
||||
config.save
|
||||
|
||||
val tmpdir = getInitRepositoryDir(LoginUser, repositoryName)
|
||||
try {
|
||||
// Clone the repository
|
||||
Git.cloneRepository.setURI(gitdir.toURI.toString).setDirectory(tmpdir).call
|
||||
val tmpdir = getInitRepositoryDir(LoginUser, repositoryName)
|
||||
try {
|
||||
// Clone the repository
|
||||
Git.cloneRepository.setURI(gitdir.toURI.toString).setDirectory(tmpdir).call
|
||||
|
||||
// Create README.md
|
||||
val readme = new File(tmpdir, "README.md")
|
||||
// Create README.md
|
||||
FileUtils.writeStringToFile(new File(tmpdir, "README.md"), if(description.nonEmpty){
|
||||
repositoryName + "\n===============\n\n" + description
|
||||
} else {
|
||||
repositoryName + "\n===============\n"
|
||||
}, "UTF-8")
|
||||
|
||||
FileUtils.writeStringToFile(readme, if(description.nonEmpty){
|
||||
repositoryName + "\n===============\n\n" + description
|
||||
} else {
|
||||
repositoryName + "\n===============\n"
|
||||
}, "UTF-8")
|
||||
|
||||
val git = Git.open(tmpdir)
|
||||
git.add.addFilepattern("README.md").call
|
||||
git.commit.setMessage("Initial commit").call
|
||||
git.push.call
|
||||
val git = Git.open(tmpdir)
|
||||
git.add.addFilepattern("README.md").call
|
||||
git.commit.setMessage("Initial commit").call
|
||||
git.push.call
|
||||
|
||||
} finally {
|
||||
FileUtils.deleteDirectory(tmpdir)
|
||||
} finally {
|
||||
FileUtils.deleteDirectory(tmpdir)
|
||||
}
|
||||
|
||||
// redirect to the repository
|
||||
redirect("/%s/%s".format(LoginUser, repositoryName))
|
||||
}
|
||||
}
|
||||
|
||||
get("/validate") {
|
||||
contentType = "application/json"
|
||||
validate(params).toJSON
|
||||
}
|
||||
|
||||
def validate(params: Map[String, String]): ValidationResult = {
|
||||
val name = params("name")
|
||||
if(name.isEmpty){
|
||||
ValidationResult(false, Map("name" -> "Repository name is required."))
|
||||
} else if(!name.matches("^[a-z0-6\\-_]+$")){
|
||||
ValidationResult(false, Map("name" -> "Repository name contans invalid character."))
|
||||
} else if(getRepositories(LoginUser).contains(name)){
|
||||
ValidationResult(false, Map("name" -> "Repository already exists."))
|
||||
} else {
|
||||
ValidationResult(true, Map.empty)
|
||||
}
|
||||
|
||||
// redirect to the repository
|
||||
redirect("/%s/%s".format(LoginUser, repositoryName))
|
||||
}
|
||||
}
|
||||
@@ -18,7 +18,7 @@ case class CommitInfo(id: String, time: Date, committer: String, message: String
|
||||
/**
|
||||
* The repository viewer.
|
||||
*/
|
||||
class RepositoryViewerServlet extends ScalatraServlet with ServletBase {
|
||||
class RepositoryViewerServlet extends ServletBase {
|
||||
|
||||
/**
|
||||
* Shows user information.
|
||||
|
||||
@@ -1,11 +1,36 @@
|
||||
package app
|
||||
|
||||
import org.scalatra._
|
||||
import org.scalatra.json._
|
||||
import org.json4s._
|
||||
import org.json4s.jackson._
|
||||
|
||||
/**
|
||||
* Provides generic features for ScalatraServlet implementations.
|
||||
*/
|
||||
trait ServletBase {
|
||||
abstract class ServletBase extends ScalatraServlet with JacksonJsonSupport {
|
||||
|
||||
implicit val jsonFormats = DefaultFormats
|
||||
|
||||
// TODO get from session
|
||||
val LoginUser = System.getProperty("user.name")
|
||||
|
||||
protected def withValidation(validator: Map[String, String] => ValidationResult, params: Map[String, String])(action: => Any): Any = {
|
||||
validator(params).valid match {
|
||||
case true => action
|
||||
case false => throw new RuntimeException("Invalid Request") // TODO show error page?
|
||||
}
|
||||
}
|
||||
|
||||
case class ValidationResult(valid: Boolean, errors: Map[String, String]){
|
||||
def toJSON(): JObject = {
|
||||
JObject(
|
||||
"valid" -> JBool(valid),
|
||||
"errors" -> JObject(errors.map { case (key, value) =>
|
||||
JField(key, JString(value))
|
||||
}.toList)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -13,6 +13,9 @@
|
||||
<script src="/assets/bootstrap/js/html5shiv.js"></script>
|
||||
<![endif]-->
|
||||
<link href="/assets/common/css/gitbucket.css" rel="stylesheet">
|
||||
<script src="/assets/common/js/jquery-1.9.1.js"></script>
|
||||
<script src="/assets/common/js/validation.js"></script>
|
||||
<script src="/assets/bootstrap/js/bootstrap.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div class="navbar navbar-inverse">
|
||||
@@ -37,7 +40,5 @@
|
||||
<div class="container">
|
||||
@body
|
||||
</div>
|
||||
<script src="/assets/common/js/jquery-1.9.1.js"></script>
|
||||
<script src="/assets/bootstrap/js/bootstrap.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
@@ -1,9 +1,10 @@
|
||||
@()
|
||||
@main("Create a New Repository"){
|
||||
<form method="post" action="/new">
|
||||
<form id="form" method="post" action="/new" validate="true">
|
||||
<fieldset>
|
||||
<label for="name"><strong>Repository name</strong></label>
|
||||
<input type="text" name="name" id="name" />
|
||||
<span id="error-name" class="error-message"></span>
|
||||
</fieldset>
|
||||
<fieldset>
|
||||
<label for="description"><strong>Description</strong> (optional)</label>
|
||||
|
||||
@@ -11,6 +11,10 @@ div.container {
|
||||
width: 920px;
|
||||
}
|
||||
|
||||
span.error-message {
|
||||
color: red;
|
||||
}
|
||||
|
||||
.small {
|
||||
font-size: small;
|
||||
}
|
||||
|
||||
29
src/main/webapp/assets/common/js/validation.js
Normal file
29
src/main/webapp/assets/common/js/validation.js
Normal file
@@ -0,0 +1,29 @@
|
||||
$(function(){
|
||||
$.each($('form[validate=true]'), function(i, form){
|
||||
$(form).submit(validate);
|
||||
});
|
||||
});
|
||||
|
||||
function validate(e){
|
||||
var form = $(e.target);
|
||||
|
||||
if(form.data('validated') == true){
|
||||
return true;
|
||||
}
|
||||
|
||||
// TODO use $.post() instead of $.getJSON
|
||||
$.getJSON(form.attr('action') + '/validate', $(e.target).serialize(), function(data){
|
||||
// clear all error messages
|
||||
$('.error-message').text('');
|
||||
|
||||
if(data.valid){
|
||||
form.data('validated', true);
|
||||
form.submit();
|
||||
} else {
|
||||
$.each(data.errors, function(key, value){
|
||||
$('#error-' + key).text(value);
|
||||
});
|
||||
}
|
||||
});
|
||||
return false;
|
||||
}
|
||||
Reference in New Issue
Block a user