James Moger
2012-03-31 2f5d1569cb08328c5358c919b53faa0cc8835775
Adjustments to ClientLogger
6 files modified
141 ■■■■ changed files
groovy/blockpush.groovy 14 ●●●● patch | view | raw | blame | history
groovy/jenkins.groovy 13 ●●●● patch | view | raw | blame | history
groovy/protect-refs.groovy 13 ●●●● patch | view | raw | blame | history
groovy/sendmail.groovy 13 ●●●● patch | view | raw | blame | history
src/com/gitblit/utils/ClientLogger.java 67 ●●●● patch | view | raw | blame | history
tests/com/gitblit/tests/GroovyScriptTest.java 21 ●●●● patch | view | raw | blame | history
groovy/blockpush.groovy
@@ -51,13 +51,13 @@
 * 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
 *  
 */
groovy/jenkins.groovy
@@ -46,12 +46,13 @@
 * 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
groovy/protect-refs.groovy
@@ -57,12 +57,13 @@
 * 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
 *  
 */
groovy/sendmail.groovy
@@ -52,12 +52,13 @@
 * 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
 *  
 */
src/com/gitblit/utils/ClientLogger.java
@@ -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 jcrygier
 *
 * @author John Crygier
 *
 */
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());
    }
}
tests/com/gitblit/tests/GroovyScriptTest.java
@@ -23,6 +23,8 @@
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 @@
        
        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 @@
    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 {