]> source.dussan.org Git - gitblit.git/commitdiff
Fire an event on received ticket branch updates to trigger reindex
authorJames Moger <james.moger@gmail.com>
Thu, 6 Mar 2014 02:50:16 +0000 (21:50 -0500)
committerJames Moger <james.moger@gmail.com>
Thu, 6 Mar 2014 02:50:16 +0000 (21:50 -0500)
It would be useful to be able to push a complete repository with a
refs/gitblit/tickets branch and have Gitblit index all those tickets.
Additionally, it would be nice for the mirroring service to recognize
an incoming update to this ref and fire an event that can be processed
by the BranchTicketService, if so configured, to reindex the tickets.

src/main/java/com/gitblit/git/GitblitReceivePack.java
src/main/java/com/gitblit/git/PatchsetReceivePack.java
src/main/java/com/gitblit/service/MirrorService.java
src/main/java/com/gitblit/tickets/BranchTicketService.java

index 3a0eff2212ce408e7c59aa2c3f7fd1f9a00fca2a..605632cf964de55936808a9891e30b7b22062534 100644 (file)
@@ -51,6 +51,7 @@ import com.gitblit.manager.IGitblit;
 import com.gitblit.models.RepositoryModel;\r
 import com.gitblit.models.UserModel;\r
 import com.gitblit.tickets.BranchTicketService;\r
+import com.gitblit.tickets.BranchTicketService.TicketsBranchUpdated;\r
 import com.gitblit.utils.ArrayUtils;\r
 import com.gitblit.utils.ClientLogger;\r
 import com.gitblit.utils.CommitCache;\r
@@ -343,6 +344,14 @@ public class GitblitReceivePack extends ReceivePack implements PreReceiveHook, P
                } catch (Exception e) {\r
                        LOGGER.error(MessageFormat.format("Failed to update {0} pushlog", repository.name), e);\r
                }\r
+               \r
+               // check for updates pushed to the BranchTicketService branch\r
+               for (ReceiveCommand cmd : commands) {\r
+                       if (Result.OK.equals(cmd.getResult())\r
+                                       && BranchTicketService.BRANCH.equals(cmd.getRefName())) {\r
+                               rp.getRepository().fireEvent(new TicketsBranchUpdated(repository));\r
+                       }\r
+               }\r
 \r
                // run Groovy hook scripts\r
                Set<String> scripts = new LinkedHashSet<String>();\r
index ae429d2ef5b6463a8f41e1f9baef70a5222f7ba6..2044f60497e30090f817ed20d66560568932cc8a 100644 (file)
@@ -436,10 +436,12 @@ public class PatchsetReceivePack extends GitblitReceivePack {
                                case CREATE:\r
                                case UPDATE:\r
                                case UPDATE_NONFASTFORWARD:\r
-                                       Collection<TicketModel> tickets = processMergedTickets(cmd);\r
-                                       ticketsProcessed += tickets.size();\r
-                                       for (TicketModel ticket : tickets) {\r
-                                               ticketNotifier.queueMailing(ticket);\r
+                                       if (cmd.getRefName().startsWith(Constants.R_HEADS)) {\r
+                                               Collection<TicketModel> tickets = processMergedTickets(cmd);\r
+                                               ticketsProcessed += tickets.size();\r
+                                               for (TicketModel ticket : tickets) {\r
+                                                       ticketNotifier.queueMailing(ticket);\r
+                                               }\r
                                        }\r
                                        break;\r
                                default:\r
index 9833d939e0162eaf018a5d137c40113e35e531e4..1eb54202a9ce3617d617c8454051d62d74c7a115 100644 (file)
@@ -38,6 +38,8 @@ import com.gitblit.Keys;
 import com.gitblit.manager.IRepositoryManager;\r
 import com.gitblit.models.RepositoryModel;\r
 import com.gitblit.models.UserModel;\r
+import com.gitblit.tickets.BranchTicketService;\r
+import com.gitblit.tickets.BranchTicketService.TicketsBranchUpdated;\r
 import com.gitblit.utils.JGitUtils;\r
 \r
 /**\r
@@ -145,6 +147,7 @@ public class MirrorService implements Runnable {
                                FetchResult result = git.fetch().setRemote(mirror.getName()).setDryRun(testing).call();\r
                                Collection<TrackingRefUpdate> refUpdates = result.getTrackingRefUpdates();\r
                                if (refUpdates.size() > 0) {\r
+                                       boolean ticketBranchUpdated = false;\r
                                        for (TrackingRefUpdate ru : refUpdates) {\r
                                                StringBuilder sb = new StringBuilder();\r
                                                sb.append("updated mirror ");\r
@@ -161,6 +164,14 @@ public class MirrorService implements Runnable {
                                                sb.append("..");\r
                                                sb.append(ru.getNewObjectId() == null ? "" : ru.getNewObjectId().abbreviate(7).name());\r
                                                logger.info(sb.toString());\r
+                                               \r
+                                               if (BranchTicketService.BRANCH.equals(ru.getLocalName())) {\r
+                                                       ticketBranchUpdated = true;\r
+                                               }\r
+                                       }\r
+                                       \r
+                                       if (ticketBranchUpdated) {\r
+                                               repository.fireEvent(new TicketsBranchUpdated(model));\r
                                        }\r
                                }\r
                        } catch (Exception e) {\r
index 14ed80940523f3dc513dea62db5b281d4e222a2e..60dca276fe87ad44dacba390bb2c83aef5a98316 100644 (file)
@@ -36,6 +36,8 @@ import org.eclipse.jgit.api.errors.JGitInternalException;
 import org.eclipse.jgit.dircache.DirCache;
 import org.eclipse.jgit.dircache.DirCacheBuilder;
 import org.eclipse.jgit.dircache.DirCacheEntry;
+import org.eclipse.jgit.events.RefsChangedEvent;
+import org.eclipse.jgit.events.RefsChangedListener;
 import org.eclipse.jgit.internal.JGitText;
 import org.eclipse.jgit.lib.CommitBuilder;
 import org.eclipse.jgit.lib.FileMode;
@@ -74,8 +76,20 @@ import com.gitblit.utils.StringUtils;
  * @author James Moger
  *
  */
-public class BranchTicketService extends ITicketService {
+public class BranchTicketService extends ITicketService implements RefsChangedListener {
 
+       /**
+        *  The event fired by other classes to allow this service to index tickets.
+        */
+       public static class TicketsBranchUpdated extends RefsChangedEvent {
+               
+               public final RepositoryModel model;
+               
+               public TicketsBranchUpdated(RepositoryModel model) {
+                       this.model = model;
+               }
+       }
+       
        public static final String BRANCH = "refs/gitblit/tickets";
 
        private static final String JOURNAL = "journal.json";
@@ -97,6 +111,9 @@ public class BranchTicketService extends ITicketService {
                                repositoryManager);
 
                lastAssignedId = new ConcurrentHashMap<String, AtomicLong>();
+               
+               // register the branch ticket service for repository ref changes
+               Repository.getGlobalListenerList().addRefsChangedListener(this);
        }
 
        @Override
@@ -120,6 +137,22 @@ public class BranchTicketService extends ITicketService {
        protected void close() {
        }
 
+       /**
+        * Listen for refs changed events and reindex that repository.
+        */
+       @Override
+       public void onRefsChanged(RefsChangedEvent event) {
+               if (!(event instanceof TicketsBranchUpdated)) {
+                       return;
+               }
+               RepositoryModel repository = ((TicketsBranchUpdated) event).model;
+               try {
+                       reindex(repository);
+               } catch (Exception e) {
+                       log.error("failed to reindex " + repository.name, e);
+               }
+       }
+
        /**
         * Returns a RefModel for the refs/gitblit/tickets branch in the repository.
         * If the branch can not be found, null is returned.