Paul Martin
2016-04-27 c2188a840bc4153ae92112b04b2e06a90d3944aa
src/main/java/com/gitblit/tickets/ITicketService.java
@@ -36,6 +36,7 @@
import com.gitblit.IStoredSettings;
import com.gitblit.Keys;
import com.gitblit.extensions.TicketHook;
import com.gitblit.manager.IManager;
import com.gitblit.manager.INotificationManager;
import com.gitblit.manager.IPluginManager;
import com.gitblit.manager.IRepositoryManager;
@@ -47,9 +48,13 @@
import com.gitblit.models.TicketModel.Change;
import com.gitblit.models.TicketModel.Field;
import com.gitblit.models.TicketModel.Patchset;
import com.gitblit.models.TicketModel.PatchsetType;
import com.gitblit.models.TicketModel.Status;
import com.gitblit.models.TicketModel.TicketLink;
import com.gitblit.tickets.TicketIndexer.Lucene;
import com.gitblit.utils.DeepCopier;
import com.gitblit.utils.DiffUtils;
import com.gitblit.utils.JGitUtils;
import com.gitblit.utils.DiffUtils.DiffStat;
import com.gitblit.utils.StringUtils;
import com.google.common.cache.Cache;
@@ -62,7 +67,9 @@
 * @author James Moger
 *
 */
public abstract class ITicketService {
public abstract class ITicketService implements IManager {
   public static final String SETTING_UPDATE_DIFFSTATS = "migration.updateDiffstats";
   private static final String LABEL = "label";
@@ -105,6 +112,8 @@
   private final Map<String, List<TicketLabel>> labelsCache;
   private final Map<String, List<TicketMilestone>> milestonesCache;
   private final boolean updateDiffstats;
   private static class TicketKey {
      final String repository;
@@ -163,18 +172,22 @@
      this.labelsCache = new ConcurrentHashMap<String, List<TicketLabel>>();
      this.milestonesCache = new ConcurrentHashMap<String, List<TicketMilestone>>();
      this.updateDiffstats = settings.getBoolean(SETTING_UPDATE_DIFFSTATS, true);
   }
   /**
    * Start the service.
    * @since 1.4.0
    */
   @Override
   public abstract ITicketService start();
   /**
    * Stop the service.
    * @since 1.4.0
    */
   @Override
   public final ITicketService stop() {
      indexer.close();
      ticketsCache.invalidateAll();
@@ -244,6 +257,7 @@
    */
   public boolean isAcceptingTicketUpdates(RepositoryModel repository) {
      return isReady()
            && repository.hasCommits
            && repository.isBare
            && !repository.isFrozen
            && !repository.isMirror;
@@ -556,9 +570,10 @@
   public TicketMilestone getMilestone(RepositoryModel repository, String milestone) {
      for (TicketMilestone ms : getMilestones(repository)) {
         if (ms.name.equalsIgnoreCase(milestone)) {
            TicketMilestone tm = DeepCopier.copy(ms);
            String q = QueryBuilder.q(Lucene.rid.matches(repository.getRID())).and(Lucene.milestone.matches(milestone)).build();
            ms.tickets = indexer.queryFor(q, 1, 0, Lucene.number.name(), true);
            return ms;
            tm.tickets = indexer.queryFor(q, 1, 0, Lucene.number.name(), true);
            return tm;
         }
      }
      return null;
@@ -639,33 +654,56 @@
    * @since 1.4.0
    */
   public synchronized boolean renameMilestone(RepositoryModel repository, String oldName, String newName, String createdBy) {
      return renameMilestone(repository, oldName, newName, createdBy, true);
   }
   /**
    * Renames a milestone.
    *
    * @param repository
    * @param oldName
    * @param newName
    * @param createdBy
    * @param notifyOpenTickets
    * @return true if successful
    * @since 1.6.0
    */
   public synchronized boolean renameMilestone(RepositoryModel repository, String oldName,
         String newName, String createdBy, boolean notifyOpenTickets) {
      if (StringUtils.isEmpty(newName)) {
         throw new IllegalArgumentException("new milestone can not be empty!");
      }
      Repository db = null;
      try {
         db = repositoryManager.getRepository(repository.name);
         TicketMilestone milestone = getMilestone(repository, oldName);
         TicketMilestone tm = getMilestone(repository, oldName);
         if (tm == null) {
            return false;
         }
         StoredConfig config = db.getConfig();
         config.unsetSection(MILESTONE, oldName);
         config.setString(MILESTONE, newName, STATUS, milestone.status.name());
         config.setString(MILESTONE, newName, COLOR, milestone.color);
         if (milestone.due != null) {
            config.setString(MILESTONE, milestone.name, DUE,
                  new SimpleDateFormat(DUE_DATE_PATTERN).format(milestone.due));
         config.setString(MILESTONE, newName, STATUS, tm.status.name());
         config.setString(MILESTONE, newName, COLOR, tm.color);
         if (tm.due != null) {
            config.setString(MILESTONE, newName, DUE,
                  new SimpleDateFormat(DUE_DATE_PATTERN).format(tm.due));
         }
         config.save();
         milestonesCache.remove(repository.name);
         TicketNotifier notifier = createNotifier();
         for (QueryResult qr : milestone.tickets) {
         for (QueryResult qr : tm.tickets) {
            Change change = new Change(createdBy);
            change.setField(Field.milestone, newName);
            TicketModel ticket = updateTicket(repository, qr.number, change);
            notifier.queueMailing(ticket);
            if (notifyOpenTickets && ticket.isOpen()) {
               notifier.queueMailing(ticket);
            }
         }
         notifier.sendAll();
         if (notifyOpenTickets) {
            notifier.sendAll();
         }
         return true;
      } catch (IOException e) {
@@ -688,11 +726,30 @@
    * @since 1.4.0
    */
   public synchronized boolean deleteMilestone(RepositoryModel repository, String milestone, String createdBy) {
      return deleteMilestone(repository, milestone, createdBy, true);
   }
   /**
    * Deletes a milestone.
    *
    * @param repository
    * @param milestone
    * @param createdBy
    * @param notifyOpenTickets
    * @return true if successful
    * @since 1.6.0
    */
   public synchronized boolean deleteMilestone(RepositoryModel repository, String milestone,
         String createdBy, boolean notifyOpenTickets) {
      if (StringUtils.isEmpty(milestone)) {
         throw new IllegalArgumentException("milestone can not be empty!");
      }
      Repository db = null;
      try {
         TicketMilestone tm = getMilestone(repository, milestone);
         if (tm == null) {
            return false;
         }
         db = repositoryManager.getRepository(repository.name);
         StoredConfig config = db.getConfig();
         config.unsetSection(MILESTONE, milestone);
@@ -700,6 +757,18 @@
         milestonesCache.remove(repository.name);
         TicketNotifier notifier = createNotifier();
         for (QueryResult qr : tm.tickets) {
            Change change = new Change(createdBy);
            change.setField(Field.milestone, "");
            TicketModel ticket = updateTicket(repository, qr.number, change);
            if (notifyOpenTickets && ticket.isOpen()) {
               notifier.queueMailing(ticket);
            }
         }
         if (notifyOpenTickets) {
            notifier.sendAll();
         }
         return true;
      } catch (IOException e) {
         log.error("failed to delete milestone " + milestone + " in " + repository, e);
@@ -710,6 +779,15 @@
      }
      return false;
   }
   /**
    * Returns the set of assigned ticket ids in the repository.
    *
    * @param repository
    * @return a set of assigned ticket ids in the repository
    * @since 1.6.0
    */
   public abstract Set<Long> getIds(RepositoryModel repository);
   /**
    * Assigns a new ticket id.
@@ -773,7 +851,7 @@
         ticket = getTicketImpl(repository, ticketId);
         // if ticket exists
         if (ticket != null) {
            if (ticket.hasPatchsets()) {
            if (ticket.hasPatchsets() && updateDiffstats) {
               Repository r = repositoryManager.getRepository(repository.name);
               try {
                  Patchset patchset = ticket.getCurrentPatchset();
@@ -805,6 +883,33 @@
    * @since 1.4.0
    */
   protected abstract TicketModel getTicketImpl(RepositoryModel repository, long ticketId);
   /**
    * Returns the journal used to build a ticket.
    *
    * @param repository
    * @param ticketId
    * @return the journal for the ticket, if it exists, otherwise null
    * @since 1.6.0
    */
   public final List<Change> getJournal(RepositoryModel repository, long ticketId) {
      if (hasTicket(repository, ticketId)) {
         List<Change> journal = getJournalImpl(repository, ticketId);
         return journal;
      }
      return null;
   }
   /**
    * Retrieves the ticket journal.
    *
    * @param repository
    * @param ticketId
    * @return a ticket, if it exists, otherwise null
    * @since 1.6.0
    */
   protected abstract List<Change> getJournalImpl(RepositoryModel repository, long ticketId);
   /**
    * Get the ticket url
@@ -918,12 +1023,12 @@
   }
   /**
    * Updates a ticket.
    * Updates a ticket and promotes pending links into references.
    *
    * @param repository
    * @param ticketId
    * @param ticketId, or 0 to action pending links in general
    * @param change
    * @return the ticket model if successful
    * @return the ticket model if successful, null if failure or using 0 ticketId
    * @since 1.4.0
    */
   public final TicketModel updateTicket(RepositoryModel repository, long ticketId, Change change) {
@@ -935,28 +1040,78 @@
         throw new RuntimeException("must specify a change author!");
      }
      TicketKey key = new TicketKey(repository, ticketId);
      ticketsCache.invalidate(key);
      boolean success = commitChangeImpl(repository, ticketId, change);
      if (success) {
         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);
      boolean success = true;
      TicketModel ticket = null;
      if (ticketId > 0) {
         TicketKey key = new TicketKey(repository, ticketId);
         ticketsCache.invalidate(key);
         success = commitChangeImpl(repository, ticketId, change);
         if (success) {
            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;
      if (success) {
         //Now that the ticket has been successfully persisted add references to this ticket from linked tickets
         if (change.hasPendingLinks()) {
            for (TicketLink link : change.pendingLinks) {
               TicketModel linkedTicket = getTicket(repository, link.targetTicketId);
               Change dstChange = null;
               //Ignore if not available or self reference
               if (linkedTicket != null && link.targetTicketId != ticketId) {
                  dstChange = new Change(change.author, change.date);
                  switch (link.action) {
                     case Comment: {
                        if (ticketId == 0) {
                           throw new RuntimeException("must specify a ticket when linking a comment!");
                        }
                        dstChange.referenceTicket(ticketId, change.comment.id);
                     } break;
                     case Commit: {
                        dstChange.referenceCommit(link.hash);
                     } break;
                     default: {
                        throw new RuntimeException(
                              String.format("must add persist logic for link of type %s", link.action));
                     }
                  }
               }
               if (dstChange != null) {
                  //If not deleted then remain null in journal
                  if (link.isDelete) {
                     dstChange.reference.deleted = true;
                  }
                  if (updateTicket(repository, link.targetTicketId, dstChange) != null) {
                     link.success = true;
                  }
               }
            }
         }
      }
      return ticket;
   }
   /**
@@ -1111,6 +1266,39 @@
      TicketModel revisedTicket = updateTicket(repository, ticket.number, deletion);
      return revisedTicket;
   }
   /**
    * Deletes a patchset from a ticket.
    *
    * @param ticket
    * @param patchset
    *            the patchset to delete (should be the highest revision)
    * @param userName
    *          the user deleting the commit
    * @return the revised ticket if the deletion was successful
    * @since 1.8.0
    */
   public final TicketModel deletePatchset(TicketModel ticket, Patchset patchset, String userName) {
      Change deletion = new Change(userName);
      deletion.patchset = new Patchset();
      deletion.patchset.number = patchset.number;
      deletion.patchset.rev = patchset.rev;
      deletion.patchset.type = PatchsetType.Delete;
      //Find and delete references to tickets by the removed commits
      List<TicketLink> patchsetTicketLinks = JGitUtils.identifyTicketsBetweenCommits(
            repositoryManager.getRepository(ticket.repository),
            settings, patchset.base, patchset.tip);
      for (TicketLink link : patchsetTicketLinks) {
         link.isDelete = true;
      }
      deletion.pendingLinks = patchsetTicketLinks;
      RepositoryModel repositoryModel = repositoryManager.getRepositoryModel(ticket.repository);
      TicketModel revisedTicket = updateTicket(repositoryModel, ticket.number, deletion);
      return revisedTicket;
   }
   /**
    * Commit a ticket change to the repository.