Commits

Sebastian Sdorra committed b6c89d5

print messages even if the push is aborted

Comments (0)

Files changed (2)

scm-plugins/scm-hg-plugin/src/main/java/sonia/scm/web/HgHookCallbackServlet.java

 
 //~--- non-JDK imports --------------------------------------------------------
 
+import com.google.common.base.Strings;
 import com.google.common.io.Closeables;
 import com.google.inject.Inject;
 import com.google.inject.Provider;
 import sonia.scm.repository.RepositoryHookType;
 import sonia.scm.repository.RepositoryNotFoundException;
 import sonia.scm.repository.RepositoryUtil;
+import sonia.scm.repository.api.HgHookMessage;
+import sonia.scm.repository.api.HgHookMessage.Severity;
+import sonia.scm.repository.api.HookMessageProvider;
 import sonia.scm.repository.spi.HgHookContextProvider;
 import sonia.scm.repository.spi.HookEventFacade;
-import sonia.scm.repository.api.HgHookMessage;
-import sonia.scm.repository.api.HgHookMessage.Severity;
 import sonia.scm.security.CipherUtil;
 import sonia.scm.security.Tokens;
 import sonia.scm.util.HttpUtil;
     String node, RepositoryHookType type)
     throws IOException
   {
+    HgHookContextProvider context = null;
+
     try
     {
       if (type == RepositoryHookType.PRE_RECEIVE)
         contextProvider.get().setPending(true);
       }
 
-      HgHookContextProvider context = new HgHookContextProvider(handler,
-                                        repositoryName, hookManager, node,
-                                        type);
+      context = new HgHookContextProvider(handler, repositoryName, hookManager,
+        node, type);
 
       hookEventFacade.handle(HgRepositoryHandler.TYPE_NAME,
         repositoryName).fireHookEvent(type, context);
     }
     catch (Exception ex)
     {
-      sendError(response, ex);
+      sendError(response, context, ex);
     }
   }
 
    * Method description
    *
    *
+   * @param writer
+   * @param msg
+   */
+  private void printMessage(PrintWriter writer, HgHookMessage msg)
+  {
+    writer.append('_');
+
+    if (msg.getSeverity() == Severity.ERROR)
+    {
+      writer.append("e[SCM] Error: ");
+    }
+    else
+    {
+      writer.append("n[SCM] ");
+    }
+
+    writer.println(msg.getMessage());
+  }
+
+  /**
+   * Method description
+   *
+   *
    * @param resonse
    * @param context
    *
       {
         writer = resonse.getWriter();
 
-        for (HgHookMessage msg : msgs)
-        {
-          writer.append('_');
-
-          if (msg.getSeverity() == Severity.ERROR)
-          {
-            writer.append("e[SCM] Error: ");
-          }
-          else
-          {
-            writer.append("n[SCM] ");
-          }
-
-          writer.println(msg.getMessage());
-        }
+        printMessages(writer, msgs);
       }
       finally
       {
    * Method description
    *
    *
+   * @param writer
+   * @param msgs
+   */
+  private void printMessages(PrintWriter writer, List<HgHookMessage> msgs)
+  {
+    for (HgHookMessage msg : msgs)
+    {
+      printMessage(writer, msg);
+    }
+  }
+
+  /**
+   * Method description
+   *
+   *
    * @param response
+   * @param context
    * @param ex
    *
    * @throws IOException
    */
-  private void sendError(HttpServletResponse response, Exception ex)
+  private void sendError(HttpServletResponse response,
+    HgHookContextProvider context, Exception ex)
     throws IOException
   {
     logger.warn("hook ended with exception", ex);
     response.setStatus(HttpServletResponse.SC_CONFLICT);
 
     String msg = ex.getMessage();
+    List<HgHookMessage> msgs = null;
 
-    if (msg != null)
+    if (context != null)
+    {
+      msgs = context.getHgMessageProvider().getMessages();
+    }
+
+    if (!Strings.isNullOrEmpty(msg) || Util.isNotEmpty(msgs))
     {
       PrintWriter writer = null;
 
       try
       {
         writer = response.getWriter();
-        writer.println(msg);
+
+        if (Util.isNotEmpty(msgs))
+        {
+          printMessages(writer, msgs);
+        }
+
+        if (!Strings.isNullOrEmpty(msg))
+        {
+          printMessage(writer, new HgHookMessage(Severity.ERROR, msg));
+        }
       }
       finally
       {

scm-plugins/scm-hg-plugin/src/main/resources/sonia/scm/python/scmhooks.py

 challenge = os.environ['SCM_CHALLENGE']
 credentials = os.environ['SCM_CREDENTIALS']
 
+def printMessages(ui, msgs):
+  for line in msgs:
+    if line.startswith("_e") or line.startswith("_n"):
+      ui.warn(line[2:]);
+
 def callHookUrl(ui, repo, hooktype, node):
   abort = True
   try:
     conn = opener.open(req)
     if conn.code >= 200 and conn.code < 300:
       ui.debug( "scm-hook " + hooktype + " success with status code " + str(conn.code) + "\n" )
-      for line in conn:
-        if line.startswith("_e") or line.startswith("_n"):
-          ui.warn(line[2:]);
+      printMessages(ui, conn)
       abort = False
     else:
       ui.warn( "ERROR: scm-hook failed with error code " + str(conn.code) + "\n" )
   except urllib2.URLError, e:
     msg = e.read();
     if len(msg) > 0:
-      ui.warn( "ERROR: " + msg)
+      printMessages(ui, msg.splitlines(True))
     else:
       ui.warn( "ERROR: scm-hook failed with error code " + str(e.getcode()) + "\n" )
   except ValueError: