Add stream-events command

The stream-events command outputs patchset added, comment added,
change merged, and change abandoned events in JSON format over SSH.
Streaming ends when the client disconnects or aborts the command.

Change-Id: Iba3ff56c23ab91cf3b855d5dde6c40d9735b643f
This commit is contained in:
Kenny Root
2010-02-24 00:29:20 -08:00
committed by Shawn O. Pearce
parent f458bf6337
commit 15ac1b83f5
10 changed files with 577 additions and 17 deletions

View File

@@ -19,10 +19,14 @@ import com.google.gerrit.reviewdb.ApprovalCategory;
import com.google.gerrit.reviewdb.ApprovalCategoryValue;
import com.google.gerrit.reviewdb.Change;
import com.google.gerrit.reviewdb.PatchSet;
import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.config.GerritServerConfig;
import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.git.WorkQueue;
import com.google.gerrit.server.project.ProjectCache;
import com.google.gerrit.server.project.ProjectControl;
import com.google.gerrit.server.project.ProjectState;
import com.google.inject.Inject;
import com.google.inject.Singleton;
@@ -38,6 +42,7 @@ import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
/**
* This class implements hooks for certain gerrit events.
@@ -47,6 +52,72 @@ public class ChangeHookRunner {
/** A logger for this class. */
private static final Logger log = LoggerFactory.getLogger(ChangeHookRunner.class);
public static abstract class ChangeEvent {
}
public static class ApprovalAttribute {
public String type;
public String value;
}
public static class AuthorAttribute {
public String name;
public String email;
}
public static class CommentAddedEvent extends ChangeEvent {
public final String type = "comment-added";
public String project;
public String branch;
public String change;
public String revision;
public AuthorAttribute author;
public ApprovalAttribute[] approvals;
public String comment;
}
public static class ChangeMergedEvent extends ChangeEvent {
public final String type = "change-merged";
public String project;
public String branch;
public String change;
public String patchSet;
public AuthorAttribute submitter;
public String description;
}
public static class ChangeAbandonedEvent extends ChangeEvent {
public final String type = "change-abandoned";
public String project;
public String branch;
public String change;
public AuthorAttribute author;
public String reason;
}
public static class PatchSetCreatedEvent extends ChangeEvent {
public final String type = "patchset-created";
public String project;
public String branch;
public String change;
public String commit;
public String patchSet;
}
private static class ChangeListenerHolder {
final ChangeListener listener;
final IdentifiedUser user;
ChangeListenerHolder(ChangeListener l, IdentifiedUser u) {
listener = l;
user = u;
}
}
/** Listeners to receive changes as they happen. */
private final Map<ChangeListener, ChangeListenerHolder> listeners =
new ConcurrentHashMap<ChangeListener, ChangeListenerHolder>();
/** Filename of the new patchset hook. */
private final File patchsetCreatedHook;
@@ -65,6 +136,8 @@ public class ChangeHookRunner {
/** Queue of hooks that need to run. */
private final WorkQueue.Executor hookQueue;
private final ProjectCache projectCache;
/**
* Create a new ChangeHookRunner.
*
@@ -72,11 +145,16 @@ public class ChangeHookRunner {
* @param repoManager The repository manager.
* @param config Config file to use.
* @param sitePath The sitepath of this gerrit install.
* @param projectCache the project cache instance for the server.
*/
@Inject
public ChangeHookRunner(final WorkQueue queue, final GitRepositoryManager repoManager, @GerritServerConfig final Config config, final SitePaths sitePath) {
public ChangeHookRunner(final WorkQueue queue,
final GitRepositoryManager repoManager,
@GerritServerConfig final Config config, final SitePaths sitePath,
final ProjectCache projectCache) {
this.repoManager = repoManager;
this.hookQueue = queue.createQueue(1, "hook");
this.projectCache = projectCache;
final File hooksPath = sitePath.resolve(getValue(config, "hooks", "path", sitePath.hooks_dir.getAbsolutePath()));
@@ -86,6 +164,14 @@ public class ChangeHookRunner {
changeAbandonedHook = sitePath.resolve(new File(hooksPath, getValue(config, "hooks", "changeAbandonedHook", "change-abandoned")).getPath());
}
public void addChangeListener(ChangeListener listener, IdentifiedUser user) {
listeners.put(listener, new ChangeListenerHolder(listener, user));
}
public void removeChangeListener(ChangeListener listener) {
listeners.remove(listener);
}
/**
* Helper Method for getting values from the config.
*
@@ -121,19 +207,28 @@ public class ChangeHookRunner {
* @param patchSet The Patchset that was created.
*/
public void doPatchsetCreatedHook(final Change change, final PatchSet patchSet) {
final PatchSetCreatedEvent event = new PatchSetCreatedEvent();
event.project = change.getProject().get();
event.branch = change.getDest().getShortName();
event.change = change.getKey().get();
event.commit = patchSet.getRevision().get();
event.patchSet = Integer.toString(patchSet.getPatchSetId());
fireEvent(change, event);
final List<String> args = new ArrayList<String>();
args.add(patchsetCreatedHook.getAbsolutePath());
args.add("--change");
args.add(change.getKey().get());
args.add(event.change);
args.add("--project");
args.add(change.getProject().get());
args.add(event.project);
args.add("--branch");
args.add(change.getDest().getShortName());
args.add(event.branch);
args.add("--commit");
args.add(patchSet.getRevision().get());
args.add(event.commit);
args.add("--patchset");
args.add(Integer.toString(patchSet.getPatchSetId()));
args.add(event.patchSet);
runHook(getRepo(change), args);
}
@@ -148,19 +243,41 @@ public class ChangeHookRunner {
* @param approvals Map of Approval Categories and Scores
*/
public void doCommentAddedHook(final Change change, final Account account, final PatchSet patchSet, final String comment, final Map<ApprovalCategory.Id, ApprovalCategoryValue.Id> approvals) {
final CommentAddedEvent event = new CommentAddedEvent();
event.project = change.getProject().get();
event.branch = change.getDest().getShortName();
event.change = change.getKey().get();
event.author = getAuthorAttribute(account);
event.revision = patchSet.getRevision().get();
event.comment = comment;
if (approvals.size() > 0) {
event.approvals = new ApprovalAttribute[approvals.size()];
int i = 0;
for (Map.Entry<ApprovalCategory.Id, ApprovalCategoryValue.Id> approval : approvals.entrySet()) {
ApprovalAttribute a = new ApprovalAttribute();
a.type = approval.getKey().get();
a.value = Short.toString(approval.getValue().get());
event.approvals[i++] = a;
}
}
fireEvent(change, event);
final List<String> args = new ArrayList<String>();
args.add(commentAddedHook.getAbsolutePath());
args.add("--change");
args.add(change.getKey().get());
args.add(event.change);
args.add("--project");
args.add(change.getProject().get());
args.add(event.project);
args.add("--branch");
args.add(change.getDest().getShortName());
args.add(event.branch);
args.add("--author");
args.add(getDisplayName(account));
args.add("--commit");
args.add(patchSet.getRevision().get());
args.add(event.revision);
args.add("--comment");
args.add(comment == null ? "" : comment);
for (Map.Entry<ApprovalCategory.Id, ApprovalCategoryValue.Id> approval : approvals.entrySet()) {
@@ -179,19 +296,29 @@ public class ChangeHookRunner {
* @param patchSet The patchset that was merged.
*/
public void doChangeMergedHook(final Change change, final Account account, final PatchSet patchSet) {
final ChangeMergedEvent event = new ChangeMergedEvent();
event.project = change.getProject().get();
event.branch = change.getDest().getShortName();
event.change = change.getKey().get();
event.submitter = getAuthorAttribute(account);
event.patchSet = patchSet.getRevision().get();
event.description = change.getSubject();
fireEvent(change, event);
final List<String> args = new ArrayList<String>();
args.add(changeMergedHook.getAbsolutePath());
args.add("--change");
args.add(change.getKey().get());
args.add(event.change);
args.add("--project");
args.add(change.getProject().get());
args.add(event.project);
args.add("--branch");
args.add(change.getDest().getShortName());
args.add(event.branch);
args.add("--submitter");
args.add(getDisplayName(account));
args.add("--commit");
args.add(patchSet.getRevision().get());
args.add(event.patchSet);
runHook(getRepo(change), args);
}
@@ -204,15 +331,24 @@ public class ChangeHookRunner {
* @param reason Reason for abandoning the change.
*/
public void doChangeAbandonedHook(final Change change, final Account account, final String reason) {
final ChangeAbandonedEvent event = new ChangeAbandonedEvent();
event.project = change.getProject().get();
event.branch = change.getDest().getShortName();
event.change = change.getKey().get();
event.author = getAuthorAttribute(account);
event.reason = reason;
fireEvent(change, event);
final List<String> args = new ArrayList<String>();
args.add(changeAbandonedHook.getAbsolutePath());
args.add("--change");
args.add(change.getKey().get());
args.add(event.change);
args.add("--project");
args.add(change.getProject().get());
args.add(event.project);
args.add("--branch");
args.add(change.getDest().getShortName());
args.add(event.branch);
args.add("--abandoner");
args.add(getDisplayName(account));
args.add("--reason");
@@ -221,6 +357,36 @@ public class ChangeHookRunner {
runHook(getRepo(change), args);
}
private void fireEvent(final Change change, final ChangeEvent event) {
for (ChangeListenerHolder holder : listeners.values()) {
if (isVisibleTo(change, holder.user)) {
holder.listener.onChangeEvent(event);
}
}
}
private boolean isVisibleTo(Change change, IdentifiedUser user) {
final ProjectState pe = projectCache.get(change.getProject());
if (pe == null) {
return false;
}
final ProjectControl pc = pe.controlFor(user);
return pc.controlFor(change).isVisible();
}
/**
* Create an AuthorAttribute for the given account suitable for serialization to JSON.
*
* @param account
* @return object suitable for serialization to JSON
*/
private AuthorAttribute getAuthorAttribute(final Account account) {
AuthorAttribute author = new AuthorAttribute();
author.name = account.getFullName();
author.email = account.getPreferredEmail();
return author;
}
/**
* Get the display name for the given account.
*

View File

@@ -0,0 +1,21 @@
// Copyright (C) 2010 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.common;
import com.google.gerrit.common.ChangeHookRunner.ChangeEvent;
public interface ChangeListener {
public void onChangeEvent(ChangeEvent event);
}