diff options
author | James Moger <james.moger@gmail.com> | 2014-03-05 21:50:16 -0500 |
---|---|---|
committer | James Moger <james.moger@gmail.com> | 2014-03-05 21:50:16 -0500 |
commit | e462bbf2974d33f1a5a0b2a808e150aa0e22d6f4 (patch) | |
tree | 9bf1f678197967c9b9fce399386cbb4cc10810fb | |
parent | f0950d6f8d663612574a436c99cf098322e825f0 (diff) | |
download | gitblit-e462bbf2974d33f1a5a0b2a808e150aa0e22d6f4.tar.gz gitblit-e462bbf2974d33f1a5a0b2a808e150aa0e22d6f4.zip |
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.
4 files changed, 60 insertions, 5 deletions
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<String> scripts = new LinkedHashSet<String>();
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<TicketModel> tickets = processMergedTickets(cmd);
- ticketsProcessed += tickets.size();
- for (TicketModel ticket : tickets) {
- ticketNotifier.queueMailing(ticket);
+ if (cmd.getRefName().startsWith(Constants.R_HEADS)) {
+ Collection<TicketModel> 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<TrackingRefUpdate> 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<String, AtomicLong>(); + + // register the branch ticket service for repository ref changes + Repository.getGlobalListenerList().addRefsChangedListener(this); } @Override @@ -121,6 +138,22 @@ public class BranchTicketService extends ITicketService { } /** + * 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. * |