@@ -51,13 +51,13 @@ import com.gitblit.utils.ClientLogger | |||
* chain, "return false" at the appropriate failure points. | |||
* | |||
* Bound Variables: | |||
* gitblit Gitblit Server com.gitblit.GitBlit | |||
* repository Gitblit Repository com.gitblit.models.RepositoryModel | |||
* user Gitblit User com.gitblit.models.UserModel | |||
* clientLogger Logs messages to client com.gitblit.utils.ClientLogger | |||
* commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand> | |||
* url Base url for Gitblit String | |||
* logger Logger instance org.slf4j.Logger | |||
* gitblit Gitblit Server com.gitblit.GitBlit | |||
* repository Gitblit Repository com.gitblit.models.RepositoryModel | |||
* user Gitblit User com.gitblit.models.UserModel | |||
* commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand> | |||
* url Base url for Gitblit String | |||
* logger Logs messages to Gitblit org.slf4j.Logger | |||
* clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger | |||
* | |||
*/ | |||
@@ -46,12 +46,13 @@ import org.slf4j.Logger | |||
* exception handler so it will not crash another script nor crash Gitblit. | |||
* | |||
* Bound Variables: | |||
* gitblit Gitblit Server com.gitblit.GitBlit | |||
* repository Gitblit Repository com.gitblit.models.RepositoryModel | |||
* user Gitblit User com.gitblit.models.UserModel | |||
* commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand> | |||
* url Base url for Gitblit String | |||
* logger Logger instance org.slf4j.Logger | |||
* gitblit Gitblit Server com.gitblit.GitBlit | |||
* repository Gitblit Repository com.gitblit.models.RepositoryModel | |||
* user Gitblit User com.gitblit.models.UserModel | |||
* commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand> | |||
* url Base url for Gitblit String | |||
* logger Logs messages to Gitblit org.slf4j.Logger | |||
* clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger | |||
* | |||
*/ | |||
// Indicate we have started the script |
@@ -57,12 +57,13 @@ import org.slf4j.Logger | |||
* Subsequent scripts, if any, will always be invoked. | |||
* | |||
* Bound Variables: | |||
* gitblit Gitblit Server com.gitblit.GitBlit | |||
* repository Gitblit Repository com.gitblit.models.RepositoryModel | |||
* user Gitblit User com.gitblit.models.UserModel | |||
* commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand> | |||
* url Base url for Gitblit String | |||
* logger Logger instance org.slf4j.Logger | |||
* gitblit Gitblit Server com.gitblit.GitBlit | |||
* repository Gitblit Repository com.gitblit.models.RepositoryModel | |||
* user Gitblit User com.gitblit.models.UserModel | |||
* commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand> | |||
* url Base url for Gitblit String | |||
* logger Logs messages to Gitblit org.slf4j.Logger | |||
* clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger | |||
* | |||
*/ | |||
@@ -52,12 +52,13 @@ import org.slf4j.Logger | |||
* chain, "return false" at the appropriate failure points. | |||
* | |||
* Bound Variables: | |||
* gitblit Gitblit Server com.gitblit.GitBlit | |||
* repository Gitblit Repository com.gitblit.models.RepositoryModel | |||
* user Gitblit User com.gitblit.models.UserModel | |||
* commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand> | |||
* url Base url for Gitblit String | |||
* logger Logger instance org.slf4j.Logger | |||
* gitblit Gitblit Server com.gitblit.GitBlit | |||
* repository Gitblit Repository com.gitblit.models.RepositoryModel | |||
* user Gitblit User com.gitblit.models.UserModel | |||
* commands JGit commands Collection<org.eclipse.jgit.transport.ReceiveCommand> | |||
* url Base url for Gitblit String | |||
* logger Logs messages to Gitblit org.slf4j.Logger | |||
* clientLogger Logs messages to Git client com.gitblit.utils.ClientLogger | |||
* | |||
*/ | |||
@@ -1,32 +1,77 @@ | |||
/* | |||
* Copyright 2012 John Crygier | |||
* Copyright 2012 gitblit.com | |||
* | |||
* Licensed under the Apache License, Version 2.0 (the "License"); | |||
* you may not use this file except in compliance with the License. | |||
* You may obtain a copy of the License at | |||
* | |||
* http://www.apache.org/licenses/LICENSE-2.0 | |||
* | |||
* Unless required by applicable law or agreed to in writing, software | |||
* distributed under the License is distributed on an "AS IS" BASIS, | |||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |||
* See the License for the specific language governing permissions and | |||
* limitations under the License. | |||
*/ | |||
package com.gitblit.utils; | |||
import java.io.PrintWriter; | |||
import java.io.StringWriter; | |||
import org.eclipse.jgit.transport.ReceivePack; | |||
import org.slf4j.Logger; | |||
import org.slf4j.LoggerFactory; | |||
/** | |||
* Class to log messages to the pushing client. Intended to be used by | |||
* the Groovy Hooks. | |||
* Class to log messages to the pushing Git client. Intended to be used by the | |||
* Groovy Hooks. | |||
* | |||
* @author John Crygier | |||
* | |||
* @author jcrygier | |||
* | |||
*/ | |||
public class ClientLogger { | |||
static final Logger logger = LoggerFactory.getLogger(ClientLogger.class); | |||
static final Logger logger = LoggerFactory.getLogger(ClientLogger.class); | |||
private ReceivePack rp; | |||
public ClientLogger(ReceivePack rp) { | |||
this.rp = rp; | |||
} | |||
/** | |||
* Sends a message to the git client. Useful for sending INFO / WARNING messages. | |||
* Sends an info/warning message to the git client. | |||
* | |||
* @param message | |||
*/ | |||
public void sendMessage(String message) { | |||
public void info(String message) { | |||
rp.sendMessage(message); | |||
} | |||
/** | |||
* Sends an error message to the git client. | |||
* | |||
* @param message | |||
*/ | |||
public void error(String message) { | |||
rp.sendError(message); | |||
} | |||
/** | |||
* Sends an error message to the git client with an exception. | |||
* | |||
* @param message | |||
* @param t | |||
* an exception | |||
*/ | |||
public void error(String message, Throwable t) { | |||
PrintWriter writer = new PrintWriter(new StringWriter()); | |||
if (!StringUtils.isEmpty(message)) { | |||
writer.append(message); | |||
writer.append('\n'); | |||
} | |||
t.printStackTrace(writer); | |||
rp.sendError(writer.toString()); | |||
} | |||
} |
@@ -23,6 +23,8 @@ import groovy.util.GroovyScriptEngine; | |||
import java.io.BufferedWriter; | |||
import java.io.File; | |||
import java.io.FileWriter; | |||
import java.io.PrintWriter; | |||
import java.io.StringWriter; | |||
import java.text.MessageFormat; | |||
import java.util.ArrayList; | |||
import java.util.Arrays; | |||
@@ -226,8 +228,7 @@ public class GroovyScriptTest { | |||
BufferedWriter writer = new BufferedWriter(new FileWriter(tempScript)); | |||
writer.write("import com.gitblit.utils.ClientLogger\n"); | |||
writer.write("clientLogger.sendMessage('this is a test message')\n"); | |||
writer.write("clientLogger.info('this is a test message')\n"); | |||
writer.flush(); | |||
writer.close(); | |||
@@ -308,9 +309,23 @@ public class GroovyScriptTest { | |||
class MockClientLogger { | |||
List<String> messages = new ArrayList<String>(); | |||
public void sendMessage(String message) { | |||
public void info(String message) { | |||
messages.add(message); | |||
} | |||
public void error(String message) { | |||
messages.add(message); | |||
} | |||
public void error(String message, Throwable t) { | |||
PrintWriter writer = new PrintWriter(new StringWriter()); | |||
if (!StringUtils.isEmpty(message)) { | |||
writer.append(message); | |||
writer.append('\n'); | |||
} | |||
t.printStackTrace(writer); | |||
messages.add(writer.toString()); | |||
} | |||
} | |||
class MockMail { |