Commits

Sebastian Sdorra  committed e37c47f

implement execute method

  • Participants
  • Parent commits fcd19f7

Comments (0)

Files changed (2)

File src/main/java/sonia/scm/webhook/RepositoryWebHook.java

 
 //~--- non-JDK imports --------------------------------------------------------
 
+import com.google.inject.Inject;
+import com.google.inject.Provider;
+
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import sonia.scm.net.HttpClient;
 import sonia.scm.plugin.ext.Extension;
 import sonia.scm.repository.Changeset;
 import sonia.scm.repository.PostReceiveRepositoryHook;
   private static final Logger logger =
     LoggerFactory.getLogger(RepositoryWebHook.class);
 
+  //~--- constructors ---------------------------------------------------------
+
+  /**
+   * Constructs ...
+   *
+   *
+   * @param httpClientProvider
+   */
+  @Inject
+  public RepositoryWebHook(Provider<HttpClient> httpClientProvider)
+  {
+    this.httpClientProvider = httpClientProvider;
+  }
+
   //~--- methods --------------------------------------------------------------
 
   /**
     {
 
       // async ??
-      new WebHookExecutor(webHook, repository, changesets).run();
+      new WebHookExecutor(httpClientProvider.get(), webHook, repository,
+                          changesets).run();
     }
   }
+
+  //~--- fields ---------------------------------------------------------------
+
+  /** Field description */
+  private Provider<HttpClient> httpClientProvider;
 }

File src/main/java/sonia/scm/webhook/WebHookExecutor.java

 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import sonia.scm.net.HttpClient;
+import sonia.scm.net.HttpResponse;
 import sonia.scm.repository.Changeset;
 import sonia.scm.repository.Repository;
 
    * Constructs ...
    *
    *
+   *
+   * @param httpClient
    * @param webHook
    * @param repository
    * @param changesets
    */
-  public WebHookExecutor(WebHook webHook, Repository repository,
+  public WebHookExecutor(HttpClient httpClient, WebHook webHook,
+                         Repository repository,
                          Collection<Changeset> changesets)
   {
+    this.httpClient = httpClient;
     this.webHook = webHook;
     this.repository = repository;
     this.changesets = changesets;
       logger.info("execute webhook for url {}", url);
     }
 
-    // TODO implement
+    // TODO implement data
+    try
+    {
+      HttpResponse response = httpClient.get(url);
+      int statusCode = response.getStatusCode();
+
+      if ((statusCode >= 200) && (statusCode < 300))
+      {
+        if (logger.isInfoEnabled())
+        {
+          logger.info("webhook {} ended successfully with status code {}", url,
+                      statusCode);
+        }
+      }
+      else if (logger.isWarnEnabled())
+      {
+        logger.warn("webhook {} failed with statusCode {}", url, statusCode);
+      }
+    }
+    catch (Exception ex)
+    {
+      logger.error("error during webhook execution for {}", url);
+    }
   }
 
   //~--- fields ---------------------------------------------------------------
   private Collection<Changeset> changesets;
 
   /** Field description */
+  private HttpClient httpClient;
+
+  /** Field description */
   private Repository repository;
 
   /** Field description */