Add stream event and hook for hashtag changes

The stream event is fired and the hook is executed when hashtags
are added or removed.

Change-Id: Ifa66db4d5ea3d5aecd9bf56830cd50688cc6af29
This commit is contained in:
David Pursehouse 2014-09-12 15:09:12 +02:00
parent 6b15344e38
commit cb2e4853c9
8 changed files with 150 additions and 1 deletions

View File

@ -145,6 +145,19 @@ changer:: link:json.html#account[account attribute]
oldTopic:: Topic name before it was changed.
==== Hashtags Changed
type:: "hashtags-changed"
change:: link:json.html#change[change attribute]
editor:: link:json.html#account[account attribute]
added:: List of hashtags added to the change
removed:: List of hashtags removed from the change
hashtags:: List of hashtags on the change after tags were added or removed
== SEE ALSO
* link:json.html[JSON Data Formats]

View File

@ -1719,6 +1719,11 @@ Optional filename for the CLA signed hook, if not specified then
Optional filename for the ref update hook, if not specified then
`ref-update` will be used.
[[hooks.hashtagsChangedHook]]hooks.hashtagsChangedHook::
+
Optional filename for the hashtags changed hook, if not specified then
`hashtags-changed` will be used.
[[hooks.syncHookTimeout]]hooks.syncHookTimeout::
+
Optional timeout value in seconds for synchronous hooks, if not specified

View File

@ -125,6 +125,25 @@ Called whenever a change's topic is changed from the Web UI or via the REST API.
topic-changed --change <change id> --change-owner <change owner> --project <project name> --branch <branch> --changer <changer> --old-topic <old topic> --new-topic <new topic>
====
=== hashtags-changed
Called whenever hashtags are added to or removed from a change from the Web UI
or via the REST API.
====
hashtags-edited --change <change id> --change-owner <change owner> --project <project name> --branch <branch> --editor <editor> --added <hashtag> --removed <hashtag> --hashtag <hashtag>
====
The `--added` parameter may be passed multiple times, once for each
hashtag that was added to the change.
The `--removed` parameter may be passed multiple times, once for each
hashtag that was removed from the change.
The `--hashtag` parameter may be passed multiple times, once for each
hashtag remaining on the change after the add or remove operation has
been performed.
=== cla-signed
Called whenever a user signs a contributor license agreement.

View File

@ -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<>();

View File

@ -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
*

View File

@ -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) {
}

View File

@ -15,6 +15,7 @@
package com.google.gerrit.server.change;
import com.google.common.collect.ImmutableSet;
import com.google.gerrit.common.ChangeHooks;
import com.google.gerrit.extensions.restapi.AuthException;
import com.google.gerrit.extensions.restapi.BadRequestException;
import com.google.gerrit.extensions.restapi.DefaultInput;
@ -22,6 +23,7 @@ import com.google.gerrit.extensions.restapi.Response;
import com.google.gerrit.extensions.restapi.RestModifyView;
import com.google.gerrit.reviewdb.server.ReviewDb;
import com.google.gerrit.server.change.PostHashtags.Input;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.index.ChangeIndexer;
import com.google.gerrit.server.notedb.ChangeNotes;
import com.google.gerrit.server.notedb.ChangeUpdate;
@ -41,6 +43,7 @@ public class PostHashtags implements RestModifyView<ChangeResource, Input> {
private final ChangeUpdate.Factory updateFactory;
private final Provider<ReviewDb> dbProvider;
private final ChangeIndexer indexer;
private final ChangeHooks hooks;
public static class Input {
@DefaultInput
@ -50,10 +53,12 @@ public class PostHashtags implements RestModifyView<ChangeResource, Input> {
@Inject
PostHashtags(ChangeUpdate.Factory updateFactory,
Provider<ReviewDb> dbProvider, ChangeIndexer indexer) {
Provider<ReviewDb> dbProvider, ChangeIndexer indexer,
ChangeHooks hooks) {
this.updateFactory = updateFactory;
this.dbProvider = dbProvider;
this.indexer = indexer;
this.hooks = hooks;
}
private Set<String> extractTags(Set<String> input)
@ -107,6 +112,12 @@ public class PostHashtags implements RestModifyView<ChangeResource, Input> {
update.commit();
indexer.index(dbProvider.get(), update.getChange());
IdentifiedUser currentUser = ((IdentifiedUser) control.getCurrentUser());
hooks.doHashtagsChangedHook(
req.getChange(), currentUser.getAccount(),
toAdd, toRemove, updatedHashtags,
dbProvider.get());
}
return Response.ok(new TreeSet<String>(updatedHashtags));

View File

@ -0,0 +1,27 @@
// Copyright (C) 2014 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gerrit.server.events;
import com.google.gerrit.server.data.AccountAttribute;
import com.google.gerrit.server.data.ChangeAttribute;
public class HashtagsChangedEvent extends ChangeEvent {
public final String type = "hashtags-edited";
public ChangeAttribute change;
public AccountAttribute editor;
public String[] added;
public String[] removed;
public String[] hashtags;
}