Merge "Add stream event and hook for hashtag changes"
This commit is contained in:
		| @@ -14,6 +14,7 @@ | ||||
|  | ||||
| package com.google.gerrit.common; | ||||
|  | ||||
| import com.google.common.collect.Sets; | ||||
| import com.google.common.util.concurrent.ThreadFactoryBuilder; | ||||
| import com.google.gerrit.common.data.ContributorAgreement; | ||||
| import com.google.gerrit.common.data.LabelType; | ||||
| @@ -41,6 +42,7 @@ import com.google.gerrit.server.events.ChangeRestoredEvent; | ||||
| import com.google.gerrit.server.events.CommentAddedEvent; | ||||
| import com.google.gerrit.server.events.DraftPublishedEvent; | ||||
| import com.google.gerrit.server.events.EventFactory; | ||||
| import com.google.gerrit.server.events.HashtagsChangedEvent; | ||||
| import com.google.gerrit.server.events.MergeFailedEvent; | ||||
| import com.google.gerrit.server.events.PatchSetCreatedEvent; | ||||
| import com.google.gerrit.server.events.RefUpdatedEvent; | ||||
| @@ -72,6 +74,7 @@ import java.io.StringWriter; | ||||
| import java.util.ArrayList; | ||||
| import java.util.List; | ||||
| import java.util.Map; | ||||
| import java.util.Set; | ||||
| import java.util.Map.Entry; | ||||
| import java.util.concurrent.Callable; | ||||
| import java.util.concurrent.ConcurrentHashMap; | ||||
| @@ -197,6 +200,9 @@ public class ChangeHookRunner implements ChangeHooks, LifecycleListener { | ||||
|     /** Filename of the update hook. */ | ||||
|     private final File refUpdateHook; | ||||
|  | ||||
|     /** Filename of the hashtags changed hook */ | ||||
|     private final File hashtagsChangedHook; | ||||
|  | ||||
|     private final String anonymousCowardName; | ||||
|  | ||||
|     /** Repository Manager. */ | ||||
| @@ -262,6 +268,7 @@ public class ChangeHookRunner implements ChangeHooks, LifecycleListener { | ||||
|         topicChangedHook = sitePath.resolve(new File(hooksPath, getValue(config, "hooks", "topicChangedHook", "topic-changed")).getPath()); | ||||
|         claSignedHook = sitePath.resolve(new File(hooksPath, getValue(config, "hooks", "claSignedHook", "cla-signed")).getPath()); | ||||
|         refUpdateHook = sitePath.resolve(new File(hooksPath, getValue(config, "hooks", "refUpdateHook", "ref-update")).getPath()); | ||||
|         hashtagsChangedHook = sitePath.resolve(new File(hooksPath, getValue(config, "hooks", "hashtagsChangedHook", "hashtags-changed")).getPath()); | ||||
|         syncHookTimeout = config.getInt("hooks", "syncHookTimeout", 30); | ||||
|         syncHookThreadPool = Executors.newCachedThreadPool( | ||||
|             new ThreadFactoryBuilder() | ||||
| @@ -610,6 +617,52 @@ public class ChangeHookRunner implements ChangeHooks, LifecycleListener { | ||||
|       runHook(change.getProject(), topicChangedHook, args); | ||||
|     } | ||||
|  | ||||
|     String[] hashtagArray(Set<String> hashtags) { | ||||
|       if (hashtags != null && hashtags.size() > 0) { | ||||
|         return Sets.newHashSet(hashtags).toArray( | ||||
|             new String[hashtags.size()]); | ||||
|       } | ||||
|       return null; | ||||
|     } | ||||
|  | ||||
|     public void doHashtagsChangedHook(Change change, Account account, | ||||
|         Set<String> added, Set<String> removed, Set<String> hashtags, ReviewDb db) | ||||
|             throws OrmException { | ||||
|       HashtagsChangedEvent event = new HashtagsChangedEvent(); | ||||
|       AccountState owner = accountCache.get(change.getOwner()); | ||||
|  | ||||
|       event.change = eventFactory.asChangeAttribute(change); | ||||
|       event.editor = eventFactory.asAccountAttribute(account); | ||||
|       event.hashtags = hashtagArray(hashtags); | ||||
|       event.added = hashtagArray(added); | ||||
|       event.removed = hashtagArray(removed); | ||||
|  | ||||
|       fireEvent(change, event, db); | ||||
|  | ||||
|       final List<String> args = new ArrayList<>(); | ||||
|       addArg(args, "--change", event.change.id); | ||||
|       addArg(args, "--change-owner", getDisplayName(owner.getAccount())); | ||||
|       addArg(args, "--project", event.change.project); | ||||
|       addArg(args, "--branch", event.change.branch); | ||||
|       addArg(args, "--editor", getDisplayName(account)); | ||||
|       if (hashtags != null) { | ||||
|         for (String hashtag : hashtags) { | ||||
|           addArg(args, "--hashtag", hashtag); | ||||
|         } | ||||
|       } | ||||
|       if (added != null) { | ||||
|         for (String hashtag : added) { | ||||
|           addArg(args, "--added", hashtag); | ||||
|         } | ||||
|       } | ||||
|       if (removed != null) { | ||||
|         for (String hashtag : removed) { | ||||
|           addArg(args, "--removed", hashtag); | ||||
|         } | ||||
|       } | ||||
|       runHook(change.getProject(), hashtagsChangedHook, args); | ||||
|     } | ||||
|  | ||||
|     public void doClaSignupHook(Account account, ContributorAgreement cla) { | ||||
|       if (account != null) { | ||||
|         final List<String> args = new ArrayList<>(); | ||||
|   | ||||
| @@ -30,6 +30,7 @@ import org.eclipse.jgit.lib.ObjectId; | ||||
| import org.eclipse.jgit.lib.RefUpdate; | ||||
|  | ||||
| import java.util.Map; | ||||
| import java.util.Set; | ||||
|  | ||||
| /** Invokes hooks on server actions. */ | ||||
| public interface ChangeHooks { | ||||
| @@ -172,6 +173,20 @@ public interface ChangeHooks { | ||||
|   public HookResult doRefUpdateHook(Project project,  String refName, | ||||
|        Account uploader, ObjectId oldId, ObjectId newId); | ||||
|  | ||||
|   /** | ||||
|    * Fire the hashtags changed Hook. | ||||
|    * @param change The change | ||||
|    * @param account The gerrit user changing the hashtags | ||||
|    * @param added List of hashtags that were added to the change | ||||
|    * @param removed List of hashtags that were removed from the change | ||||
|    * @param hashtags List of hashtags on the change after adding or removing | ||||
|    * @param db The database | ||||
|    * @throws OrmException | ||||
|    */ | ||||
|   public void doHashtagsChangedHook(Change change, Account account, | ||||
|       Set<String>added, Set<String> removed, Set<String> hashtags, | ||||
|       ReviewDb db) throws OrmException; | ||||
|  | ||||
|   /** | ||||
|    * Post a stream event that is related to a change | ||||
|    * | ||||
|   | ||||
| @@ -29,6 +29,7 @@ import org.eclipse.jgit.lib.ObjectId; | ||||
| import org.eclipse.jgit.lib.RefUpdate; | ||||
|  | ||||
| import java.util.Map; | ||||
| import java.util.Set; | ||||
|  | ||||
| /** Does not invoke hooks. */ | ||||
| public final class DisabledChangeHooks implements ChangeHooks { | ||||
| @@ -96,6 +97,11 @@ public final class DisabledChangeHooks implements ChangeHooks { | ||||
|       ReviewDb db) { | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public void doHashtagsChangedHook(Change change, Account account, Set<String> added, | ||||
|       Set<String> removed, Set<String> hashtags, ReviewDb db) { | ||||
|   } | ||||
|  | ||||
|   @Override | ||||
|   public void removeChangeListener(ChangeListener listener) { | ||||
|   } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Edwin Kempin
					Edwin Kempin