From: James Moger Date: Thu, 6 Mar 2014 02:50:16 +0000 (-0500) Subject: Fire an event on received ticket branch updates to trigger reindex X-Git-Tag: v1.4.0~37^2~4 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=e462bbf2974d33f1a5a0b2a808e150aa0e22d6f4;p=gitblit.git Fire an event on received ticket branch updates to trigger reindex 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. --- diff --git a/src/main/java/com/gitblit/git/GitblitReceivePack.java b/src/main/java/com/gitblit/git/GitblitReceivePack.java index 3a0eff22..605632cf 100644 --- a/src/main/java/com/gitblit/git/GitblitReceivePack.java +++ b/src/main/java/com/gitblit/git/GitblitReceivePack.java @@ -51,6 +51,7 @@ import com.gitblit.manager.IGitblit; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; import com.gitblit.tickets.BranchTicketService; +import com.gitblit.tickets.BranchTicketService.TicketsBranchUpdated; import com.gitblit.utils.ArrayUtils; import com.gitblit.utils.ClientLogger; import com.gitblit.utils.CommitCache; @@ -343,6 +344,14 @@ public class GitblitReceivePack extends ReceivePack implements PreReceiveHook, P } catch (Exception e) { LOGGER.error(MessageFormat.format("Failed to update {0} pushlog", repository.name), e); } + + // check for updates pushed to the BranchTicketService branch + for (ReceiveCommand cmd : commands) { + if (Result.OK.equals(cmd.getResult()) + && BranchTicketService.BRANCH.equals(cmd.getRefName())) { + rp.getRepository().fireEvent(new TicketsBranchUpdated(repository)); + } + } // run Groovy hook scripts Set scripts = new LinkedHashSet(); diff --git a/src/main/java/com/gitblit/git/PatchsetReceivePack.java b/src/main/java/com/gitblit/git/PatchsetReceivePack.java index ae429d2e..2044f604 100644 --- a/src/main/java/com/gitblit/git/PatchsetReceivePack.java +++ b/src/main/java/com/gitblit/git/PatchsetReceivePack.java @@ -436,10 +436,12 @@ public class PatchsetReceivePack extends GitblitReceivePack { case CREATE: case UPDATE: case UPDATE_NONFASTFORWARD: - Collection tickets = processMergedTickets(cmd); - ticketsProcessed += tickets.size(); - for (TicketModel ticket : tickets) { - ticketNotifier.queueMailing(ticket); + if (cmd.getRefName().startsWith(Constants.R_HEADS)) { + Collection tickets = processMergedTickets(cmd); + ticketsProcessed += tickets.size(); + for (TicketModel ticket : tickets) { + ticketNotifier.queueMailing(ticket); + } } break; default: diff --git a/src/main/java/com/gitblit/service/MirrorService.java b/src/main/java/com/gitblit/service/MirrorService.java index 9833d939..1eb54202 100644 --- a/src/main/java/com/gitblit/service/MirrorService.java +++ b/src/main/java/com/gitblit/service/MirrorService.java @@ -38,6 +38,8 @@ import com.gitblit.Keys; import com.gitblit.manager.IRepositoryManager; import com.gitblit.models.RepositoryModel; import com.gitblit.models.UserModel; +import com.gitblit.tickets.BranchTicketService; +import com.gitblit.tickets.BranchTicketService.TicketsBranchUpdated; import com.gitblit.utils.JGitUtils; /** @@ -145,6 +147,7 @@ public class MirrorService implements Runnable { FetchResult result = git.fetch().setRemote(mirror.getName()).setDryRun(testing).call(); Collection refUpdates = result.getTrackingRefUpdates(); if (refUpdates.size() > 0) { + boolean ticketBranchUpdated = false; for (TrackingRefUpdate ru : refUpdates) { StringBuilder sb = new StringBuilder(); sb.append("updated mirror "); @@ -161,6 +164,14 @@ public class MirrorService implements Runnable { sb.append(".."); sb.append(ru.getNewObjectId() == null ? "" : ru.getNewObjectId().abbreviate(7).name()); logger.info(sb.toString()); + + if (BranchTicketService.BRANCH.equals(ru.getLocalName())) { + ticketBranchUpdated = true; + } + } + + if (ticketBranchUpdated) { + repository.fireEvent(new TicketsBranchUpdated(model)); } } } catch (Exception e) { diff --git a/src/main/java/com/gitblit/tickets/BranchTicketService.java b/src/main/java/com/gitblit/tickets/BranchTicketService.java index 14ed8094..60dca276 100644 --- a/src/main/java/com/gitblit/tickets/BranchTicketService.java +++ b/src/main/java/com/gitblit/tickets/BranchTicketService.java @@ -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(); + + // 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.