]> source.dussan.org Git - gitblit.git/commitdiff
Add TicketHook extension
authorJames Moger <james.moger@gitblit.com>
Sat, 12 Apr 2014 14:00:12 +0000 (10:00 -0400)
committerJames Moger <james.moger@gitblit.com>
Sat, 12 Apr 2014 15:40:10 +0000 (11:40 -0400)
12 files changed:
src/main/java/com/gitblit/GitBlit.java
src/main/java/com/gitblit/ReindexTickets.java
src/main/java/com/gitblit/extensions/TicketHook.java [new file with mode: 0644]
src/main/java/com/gitblit/tickets/BranchTicketService.java
src/main/java/com/gitblit/tickets/FileTicketService.java
src/main/java/com/gitblit/tickets/ITicketService.java
src/main/java/com/gitblit/tickets/NullTicketService.java
src/main/java/com/gitblit/tickets/RedisTicketService.java
src/site/setup_plugins.mkd
src/test/java/com/gitblit/tests/BranchTicketServiceTest.java
src/test/java/com/gitblit/tests/FileTicketServiceTest.java
src/test/java/com/gitblit/tests/RedisTicketServiceTest.java

index 0834252168111ea1b0b9626e8a6970a5752e2ee6..3db5f0871f0401f1329c7abefadbd76b7eaea728 100644 (file)
@@ -352,6 +352,7 @@ public class GitBlit extends GitblitManager {
 
                                        // core managers
                                        IRuntimeManager.class,
+                                       IPluginManager.class,
                                        INotificationManager.class,
                                        IUserManager.class,
                                        IAuthenticationManager.class,
@@ -379,6 +380,10 @@ public class GitBlit extends GitblitManager {
                        return runtimeManager;
                }
 
+               @Provides @Singleton IPluginManager providePluginManager() {
+                       return pluginManager;
+               }
+
                @Provides @Singleton INotificationManager provideNotificationManager() {
                        return notificationManager;
                }
@@ -410,6 +415,7 @@ public class GitBlit extends GitblitManager {
                @Provides @Singleton NullTicketService provideNullTicketService() {
                        return new NullTicketService(
                                        runtimeManager,
+                                       pluginManager,
                                        notificationManager,
                                        userManager,
                                        repositoryManager);
@@ -418,6 +424,7 @@ public class GitBlit extends GitblitManager {
                @Provides @Singleton FileTicketService provideFileTicketService() {
                        return new FileTicketService(
                                        runtimeManager,
+                                       pluginManager,
                                        notificationManager,
                                        userManager,
                                        repositoryManager);
@@ -426,6 +433,7 @@ public class GitBlit extends GitblitManager {
                @Provides @Singleton BranchTicketService provideBranchTicketService() {
                        return new BranchTicketService(
                                        runtimeManager,
+                                       pluginManager,
                                        notificationManager,
                                        userManager,
                                        repositoryManager);
@@ -434,6 +442,7 @@ public class GitBlit extends GitblitManager {
                @Provides @Singleton RedisTicketService provideRedisTicketService() {
                        return new RedisTicketService(
                                        runtimeManager,
+                                       pluginManager,
                                        notificationManager,
                                        userManager,
                                        repositoryManager);
index f3d6d266ecdcceb3968917296abe099a13e274ff..51ca165757f1f7f22ae338c6bbe302e80170a332 100644 (file)
@@ -139,13 +139,13 @@ public class ReindexTickets {
                        Class<?> serviceClass = Class.forName(serviceName);
                        if (RedisTicketService.class.isAssignableFrom(serviceClass)) {
                                // Redis ticket service
-                               ticketService = new RedisTicketService(runtimeManager, null, null, repositoryManager).start();
+                               ticketService = new RedisTicketService(runtimeManager, null, null, null, repositoryManager).start();
                        } else if (BranchTicketService.class.isAssignableFrom(serviceClass)) {
                                // Branch ticket service
-                               ticketService = new BranchTicketService(runtimeManager, null, null, repositoryManager).start();
+                               ticketService = new BranchTicketService(runtimeManager, null, null, null, repositoryManager).start();
                        } else if (FileTicketService.class.isAssignableFrom(serviceClass)) {
                                // File ticket service
-                               ticketService = new FileTicketService(runtimeManager, null, null, repositoryManager).start();
+                               ticketService = new FileTicketService(runtimeManager, null, null, null, repositoryManager).start();
                        } else {
                                System.err.println("Unknown ticket service " + serviceName);
                                System.exit(1);
diff --git a/src/main/java/com/gitblit/extensions/TicketHook.java b/src/main/java/com/gitblit/extensions/TicketHook.java
new file mode 100644 (file)
index 0000000..3acbef3
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2014 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.extensions;
+
+import ro.fortsoft.pf4j.ExtensionPoint;
+
+import com.gitblit.models.TicketModel;
+import com.gitblit.models.TicketModel.Change;
+
+/**
+ * Extension point for plugins to respond to Ticket changes.
+ *
+ * @author James Moger
+ *
+ */
+public abstract class TicketHook implements ExtensionPoint {
+
+       /**
+        * Called when a new ticket is created.
+        *
+        * @param ticket
+        */
+       public abstract void onNewTicket(TicketModel ticket);
+
+       /**
+        * Called when an existing ticket is updated.  Tickets can be updated for
+        * many, many reasons like state changes votes, watches, etc.
+        *
+        * @param ticket
+        * @param change
+        */
+       public abstract void onUpdateTicket(TicketModel ticket, Change change);
+}
index 56055a4a759479f64abad3a4fdc4f69c75bec151..3a634e0553dbf92a0cc30e399c1f2d8e0a99724d 100644 (file)
@@ -58,6 +58,7 @@ import org.eclipse.jgit.treewalk.TreeWalk;
 import com.gitblit.Constants;
 import com.gitblit.git.ReceiveCommandEvent;
 import com.gitblit.manager.INotificationManager;
+import com.gitblit.manager.IPluginManager;
 import com.gitblit.manager.IRepositoryManager;
 import com.gitblit.manager.IRuntimeManager;
 import com.gitblit.manager.IUserManager;
@@ -92,11 +93,13 @@ public class BranchTicketService extends ITicketService implements RefsChangedLi
 
        public BranchTicketService(
                        IRuntimeManager runtimeManager,
+                       IPluginManager pluginManager,
                        INotificationManager notificationManager,
                        IUserManager userManager,
                        IRepositoryManager repositoryManager) {
 
                super(runtimeManager,
+                               pluginManager,
                                notificationManager,
                                userManager,
                                repositoryManager);
index 2247a66c6e342cd86e6caab0be295fae639646a4..4386020fa751c511b746f7b332f6906a3b18bea0 100644 (file)
@@ -29,6 +29,7 @@ import org.eclipse.jgit.lib.Repository;
 
 import com.gitblit.Constants;
 import com.gitblit.manager.INotificationManager;
+import com.gitblit.manager.IPluginManager;
 import com.gitblit.manager.IRepositoryManager;
 import com.gitblit.manager.IRuntimeManager;
 import com.gitblit.manager.IUserManager;
@@ -58,11 +59,13 @@ public class FileTicketService extends ITicketService {
 
        public FileTicketService(
                        IRuntimeManager runtimeManager,
+                       IPluginManager pluginManager,
                        INotificationManager notificationManager,
                        IUserManager userManager,
                        IRepositoryManager repositoryManager) {
 
                super(runtimeManager,
+                               pluginManager,
                                notificationManager,
                                userManager,
                                repositoryManager);
index 90f9c6dd3342644508ba48965ed822f2395a5979..9522e420ec00241ea1160cc790774c74618fab3a 100644 (file)
@@ -35,7 +35,9 @@ import org.slf4j.LoggerFactory;
 
 import com.gitblit.IStoredSettings;
 import com.gitblit.Keys;
+import com.gitblit.extensions.TicketHook;
 import com.gitblit.manager.INotificationManager;
+import com.gitblit.manager.IPluginManager;
 import com.gitblit.manager.IRepositoryManager;
 import com.gitblit.manager.IRuntimeManager;
 import com.gitblit.manager.IUserManager;
@@ -94,6 +96,8 @@ public abstract class ITicketService {
 
        protected final IRepositoryManager repositoryManager;
 
+       protected final IPluginManager pluginManager;
+
        protected final TicketIndexer indexer;
 
        private final Cache<TicketKey, TicketModel> ticketsCache;
@@ -136,6 +140,7 @@ public abstract class ITicketService {
         */
        public ITicketService(
                        IRuntimeManager runtimeManager,
+                       IPluginManager pluginManager,
                        INotificationManager notificationManager,
                        IUserManager userManager,
                        IRepositoryManager repositoryManager) {
@@ -143,6 +148,7 @@ public abstract class ITicketService {
                this.log = LoggerFactory.getLogger(getClass());
                this.settings = runtimeManager.getSettings();
                this.runtimeManager = runtimeManager;
+               this.pluginManager = pluginManager;
                this.notificationManager = notificationManager;
                this.userManager = userManager;
                this.repositoryManager = repositoryManager;
@@ -832,6 +838,17 @@ public abstract class ITicketService {
                if (success) {
                        TicketModel ticket = getTicket(repository, ticketId);
                        indexer.index(ticket);
+
+                       // call the ticket hooks
+                       if (pluginManager != null) {
+                               for (TicketHook hook : pluginManager.getExtensions(TicketHook.class)) {
+                                       try {
+                                               hook.onNewTicket(ticket);
+                                       } catch (Exception e) {
+                                               log.error("Failed to execute extension", e);
+                                       }
+                               }
+                       }
                        return ticket;
                }
                return null;
@@ -862,6 +879,17 @@ public abstract class ITicketService {
                        TicketModel ticket = getTicket(repository, ticketId);
                        ticketsCache.put(key, ticket);
                        indexer.index(ticket);
+
+                       // call the ticket hooks
+                       if (pluginManager != null) {
+                               for (TicketHook hook : pluginManager.getExtensions(TicketHook.class)) {
+                                       try {
+                                               hook.onUpdateTicket(ticket, change);
+                                       } catch (Exception e) {
+                                               log.error("Failed to execute extension", e);
+                                       }
+                               }
+                       }
                        return ticket;
                }
                return null;
index 0ff33176c997fad3291bae52c82f63ba78f6bd97..749d80180792488467bc0eec797ac3bcd13cbea3 100644 (file)
@@ -19,6 +19,7 @@ import java.util.Collections;
 import java.util.List;
 
 import com.gitblit.manager.INotificationManager;
+import com.gitblit.manager.IPluginManager;
 import com.gitblit.manager.IRepositoryManager;
 import com.gitblit.manager.IRuntimeManager;
 import com.gitblit.manager.IUserManager;
@@ -37,11 +38,13 @@ public class NullTicketService extends ITicketService {
 
        public NullTicketService(
                        IRuntimeManager runtimeManager,
+                       IPluginManager pluginManager,
                        INotificationManager notificationManager,
                        IUserManager userManager,
                        IRepositoryManager repositoryManager) {
 
                super(runtimeManager,
+                               pluginManager,
                                notificationManager,
                                userManager,
                                repositoryManager);
index 58f7243cdfa92882cfd92b1f6ba2da6d5098a3c5..2c5b181fba225b0d7821204a2782451cf10c1cba 100644 (file)
@@ -32,6 +32,7 @@ import redis.clients.jedis.exceptions.JedisException;
 
 import com.gitblit.Keys;
 import com.gitblit.manager.INotificationManager;
+import com.gitblit.manager.IPluginManager;
 import com.gitblit.manager.IRepositoryManager;
 import com.gitblit.manager.IRuntimeManager;
 import com.gitblit.manager.IUserManager;
@@ -61,11 +62,13 @@ public class RedisTicketService extends ITicketService {
 
        public RedisTicketService(
                        IRuntimeManager runtimeManager,
+                       IPluginManager pluginManager,
                        INotificationManager notificationManager,
                        IUserManager userManager,
                        IRepositoryManager repositoryManager) {
 
                super(runtimeManager,
+                               pluginManager,
                                notificationManager,
                                userManager,
                                repositoryManager);
index 4c32e973129ad11ec4a58361e710fc9eccad64d4..6e4e92a7d2dc6cfe82dcab864fb439a56e18ae16 100644 (file)
@@ -111,6 +111,29 @@ public class MyPatchsetHook extends PatchsetHook {
 }
 ```
 
+### Extension Point: Ticket Hook
+
+You can provide your own custom ticket hook by extending the *TicketHook* class.
+
+```java
+import com.gitblit.extensions.TicketHook;
+import com.gitblit.models.TicketModel;
+import com.gitblit.models.TicketModel.Change;
+import ro.fortsoft.pf4j.Extension;
+
+@Extension
+public class MyTicketHook extends TicketHook {
+
+    @Override
+    public void onNewTicket(TicketModel ticket) {
+    }
+
+    @Override
+    public void onUpdateTicket(TicketModel ticket, Change change) {
+    }
+}
+```
+
 ### Mac OSX Fonts
 
 Gitblit's core SSH commands and those in the *powertools* plugin rely on use of ANSI border characters to provide a pretty presentation of data.  Unfortunately, the fonts provided by Apple - while very nice - don't work well with ANSI border characters.  The following public domain fixed-width, fixed-point, bitmapped fonts work very nicely.  I find the 6x12 font with a line spacing of ~0.8 to be quite acceptable.
index 4bd74f5197a9a0d0d5df19a792b7eba138f2b326..6119b8dbfd29bc2c305118c6967ca58a051254b6 100644 (file)
@@ -17,10 +17,12 @@ package com.gitblit.tests;
 
 import com.gitblit.IStoredSettings;
 import com.gitblit.manager.INotificationManager;
+import com.gitblit.manager.IPluginManager;
 import com.gitblit.manager.IRepositoryManager;
 import com.gitblit.manager.IRuntimeManager;
 import com.gitblit.manager.IUserManager;
 import com.gitblit.manager.NotificationManager;
+import com.gitblit.manager.PluginManager;
 import com.gitblit.manager.RepositoryManager;
 import com.gitblit.manager.RuntimeManager;
 import com.gitblit.manager.UserManager;
@@ -50,12 +52,14 @@ public class BranchTicketServiceTest extends TicketServiceTest {
                IStoredSettings settings = getSettings(deleteAll);
 
                IRuntimeManager runtimeManager = new RuntimeManager(settings).start();
+               IPluginManager pluginManager = new PluginManager(runtimeManager).start();
                INotificationManager notificationManager = new NotificationManager(settings).start();
                IUserManager userManager = new UserManager(runtimeManager).start();
                IRepositoryManager repositoryManager = new RepositoryManager(runtimeManager, userManager).start();
 
                BranchTicketService service = new BranchTicketService(
                                runtimeManager,
+                               pluginManager,
                                notificationManager,
                                userManager,
                                repositoryManager).start();
index 3cc25218a32d14aae90ea352420e95ef84e06245..20cde26b796e3f2c5e419d775e43bbcdab131ffc 100644 (file)
@@ -17,10 +17,12 @@ package com.gitblit.tests;
 
 import com.gitblit.IStoredSettings;
 import com.gitblit.manager.INotificationManager;
+import com.gitblit.manager.IPluginManager;
 import com.gitblit.manager.IRepositoryManager;
 import com.gitblit.manager.IRuntimeManager;
 import com.gitblit.manager.IUserManager;
 import com.gitblit.manager.NotificationManager;
+import com.gitblit.manager.PluginManager;
 import com.gitblit.manager.RepositoryManager;
 import com.gitblit.manager.RuntimeManager;
 import com.gitblit.manager.UserManager;
@@ -49,12 +51,14 @@ public class FileTicketServiceTest extends TicketServiceTest {
                IStoredSettings settings = getSettings(deleteAll);
 
                IRuntimeManager runtimeManager = new RuntimeManager(settings).start();
+               IPluginManager pluginManager = new PluginManager(runtimeManager).start();
                INotificationManager notificationManager = new NotificationManager(settings).start();
                IUserManager userManager = new UserManager(runtimeManager).start();
                IRepositoryManager repositoryManager = new RepositoryManager(runtimeManager, userManager).start();
 
                FileTicketService service = new FileTicketService(
                                runtimeManager,
+                               pluginManager,
                                notificationManager,
                                userManager,
                                repositoryManager).start();
index 5a4bda7395bddee6682926bab8600d850730b975..94391a1520242ccd3f0b1eb6b6437c1c47057674 100644 (file)
@@ -18,10 +18,12 @@ package com.gitblit.tests;
 import com.gitblit.IStoredSettings;
 import com.gitblit.Keys;
 import com.gitblit.manager.INotificationManager;
+import com.gitblit.manager.IPluginManager;
 import com.gitblit.manager.IRepositoryManager;
 import com.gitblit.manager.IRuntimeManager;
 import com.gitblit.manager.IUserManager;
 import com.gitblit.manager.NotificationManager;
+import com.gitblit.manager.PluginManager;
 import com.gitblit.manager.RepositoryManager;
 import com.gitblit.manager.RuntimeManager;
 import com.gitblit.manager.UserManager;
@@ -57,12 +59,14 @@ public class RedisTicketServiceTest extends TicketServiceTest {
                IStoredSettings settings = getSettings(deleteAll);
 
                IRuntimeManager runtimeManager = new RuntimeManager(settings).start();
+               IPluginManager pluginManager = new PluginManager(runtimeManager).start();
                INotificationManager notificationManager = new NotificationManager(settings).start();
                IUserManager userManager = new UserManager(runtimeManager).start();
                IRepositoryManager repositoryManager = new RepositoryManager(runtimeManager, userManager).start();
 
                RedisTicketService service = new RedisTicketService(
                                runtimeManager,
+                               pluginManager,
                                notificationManager,
                                userManager,
                                repositoryManager).start();