Added support for ref-update events
Whenever a ref is updated via either a direct push to a branch or a Gerrit change submission, Gerrit will now send a new "ref-updated" event to the event stream. This can be used by external agents for things like automatic submodule ref updating, replication, build bot hooks, etc. Change-Id: Ice9d65db8fd662d53df53ff6b61d811815c9f2f6
This commit is contained in:
committed by
Shawn O. Pearce
parent
ec15511690
commit
6cc1190d8f
@@ -19,6 +19,7 @@ import com.google.gerrit.common.data.ApprovalTypes;
|
||||
import com.google.gerrit.reviewdb.Account;
|
||||
import com.google.gerrit.reviewdb.ApprovalCategory;
|
||||
import com.google.gerrit.reviewdb.ApprovalCategoryValue;
|
||||
import com.google.gerrit.reviewdb.Branch;
|
||||
import com.google.gerrit.reviewdb.Change;
|
||||
import com.google.gerrit.reviewdb.PatchSet;
|
||||
import com.google.gerrit.reviewdb.Project;
|
||||
@@ -35,6 +36,7 @@ import com.google.gerrit.server.events.ChangeRestoreEvent;
|
||||
import com.google.gerrit.server.events.CommentAddedEvent;
|
||||
import com.google.gerrit.server.events.EventFactory;
|
||||
import com.google.gerrit.server.events.PatchSetCreatedEvent;
|
||||
import com.google.gerrit.server.events.RefUpdatedEvent;
|
||||
import com.google.gerrit.server.git.GitRepositoryManager;
|
||||
import com.google.gerrit.server.git.WorkQueue;
|
||||
import com.google.gerrit.server.project.ProjectCache;
|
||||
@@ -45,6 +47,8 @@ import com.google.inject.Singleton;
|
||||
|
||||
import org.eclipse.jgit.errors.RepositoryNotFoundException;
|
||||
import org.eclipse.jgit.lib.Config;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.RefUpdate;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@@ -96,6 +100,9 @@ public class ChangeHookRunner {
|
||||
/** Filename of the change abandoned hook. */
|
||||
private final File changeRestoredHook;
|
||||
|
||||
/** Filename of the ref updated hook. */
|
||||
private final File refUpdatedHook;
|
||||
|
||||
/** Repository Manager. */
|
||||
private final GitRepositoryManager repoManager;
|
||||
|
||||
@@ -141,6 +148,7 @@ public class ChangeHookRunner {
|
||||
changeMergedHook = sitePath.resolve(new File(hooksPath, getValue(config, "hooks", "changeMergedHook", "change-merged")).getPath());
|
||||
changeAbandonedHook = sitePath.resolve(new File(hooksPath, getValue(config, "hooks", "changeAbandonedHook", "change-abandoned")).getPath());
|
||||
changeRestoredHook = sitePath.resolve(new File(hooksPath, getValue(config, "hooks", "changeRestoredHook", "change-restored")).getPath());
|
||||
refUpdatedHook = sitePath.resolve(new File(hooksPath, getValue(config, "hooks", "refUpdatedHook", "ref-updated")).getPath());
|
||||
}
|
||||
|
||||
public void addChangeListener(ChangeListener listener, IdentifiedUser user) {
|
||||
@@ -172,7 +180,16 @@ public class ChangeHookRunner {
|
||||
* @return Repository or null.
|
||||
*/
|
||||
private Repository openRepository(final Change change) {
|
||||
Project.NameKey name = change.getProject();
|
||||
return openRepository(change.getProject());
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Repository for the given project name, or null on error.
|
||||
*
|
||||
* @param name Project to get repo for,
|
||||
* @return Repository or null.
|
||||
*/
|
||||
private Repository openRepository(final Project.NameKey name) {
|
||||
try {
|
||||
return repoManager.openRepository(name.get());
|
||||
} catch (RepositoryNotFoundException err) {
|
||||
@@ -335,6 +352,44 @@ public class ChangeHookRunner {
|
||||
runHook(openRepository(change), changeRestoredHook, args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire the Ref Updated Hook
|
||||
* @param project The project the ref update occured on
|
||||
* @param refUpdate An actual RefUpdate object
|
||||
* @param account The gerrit user who moved the ref
|
||||
*/
|
||||
public void doRefUpdatedHook(final Branch.NameKey refName, final RefUpdate refUpdate, final Account account) {
|
||||
doRefUpdatedHook(refName, refUpdate.getOldObjectId(), refUpdate.getNewObjectId(), account);
|
||||
}
|
||||
|
||||
/**
|
||||
* Fire the Ref Updated Hook
|
||||
* @param refName The Branch.NameKey of the ref that was updated
|
||||
* @param oldId The ref's old id
|
||||
* @param newId The ref's new id
|
||||
* @param account The gerrit user who moved the ref
|
||||
*/
|
||||
public void doRefUpdatedHook(final Branch.NameKey refName, final ObjectId oldId, final ObjectId newId, final Account account) {
|
||||
final RefUpdatedEvent event = new RefUpdatedEvent();
|
||||
|
||||
if (account != null) {
|
||||
event.submitter = eventFactory.asAccountAttribute(account);
|
||||
}
|
||||
event.refUpdate = eventFactory.asRefUpdateAttribute(oldId, newId, refName);
|
||||
fireEvent(refName, event);
|
||||
|
||||
final List<String> args = new ArrayList<String>();
|
||||
addArg(args, "--oldrev", event.refUpdate.oldRev);
|
||||
addArg(args, "--newrev", event.refUpdate.newRev);
|
||||
addArg(args, "--refname", event.refUpdate.refName);
|
||||
addArg(args, "--project", event.refUpdate.project);
|
||||
if (account != null) {
|
||||
addArg(args, "--submitter", getDisplayName(account));
|
||||
}
|
||||
|
||||
runHook(openRepository(refName.getParentKey()), refUpdatedHook, args);
|
||||
}
|
||||
|
||||
private void fireEvent(final Change change, final ChangeEvent event) {
|
||||
for (ChangeListenerHolder holder : listeners.values()) {
|
||||
if (isVisibleTo(change, holder.user)) {
|
||||
@@ -343,6 +398,14 @@ public class ChangeHookRunner {
|
||||
}
|
||||
}
|
||||
|
||||
private void fireEvent(Branch.NameKey branchName, final ChangeEvent event) {
|
||||
for (ChangeListenerHolder holder : listeners.values()) {
|
||||
if (isVisibleTo(branchName, holder.user)) {
|
||||
holder.listener.onChangeEvent(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isVisibleTo(Change change, IdentifiedUser user) {
|
||||
final ProjectState pe = projectCache.get(change.getProject());
|
||||
if (pe == null) {
|
||||
@@ -352,6 +415,15 @@ public class ChangeHookRunner {
|
||||
return pc.controlFor(change).isVisible();
|
||||
}
|
||||
|
||||
private boolean isVisibleTo(Branch.NameKey branchName, IdentifiedUser user) {
|
||||
final ProjectState pe = projectCache.get(branchName.getParentKey());
|
||||
if (pe == null) {
|
||||
return false;
|
||||
}
|
||||
final ProjectControl pc = pe.controlFor(user);
|
||||
return pc.controlForRef(branchName).isVisible();
|
||||
}
|
||||
|
||||
/**
|
||||
* Create an ApprovalAttribute for the given approval suitable for serialization to JSON.
|
||||
* @param approval
|
||||
|
||||
@@ -17,6 +17,7 @@ package com.google.gerrit.server.events;
|
||||
import com.google.gerrit.common.data.ApprovalType;
|
||||
import com.google.gerrit.common.data.ApprovalTypes;
|
||||
import com.google.gerrit.reviewdb.Account;
|
||||
import com.google.gerrit.reviewdb.Branch;
|
||||
import com.google.gerrit.reviewdb.Change;
|
||||
import com.google.gerrit.reviewdb.PatchSet;
|
||||
import com.google.gerrit.reviewdb.PatchSetApproval;
|
||||
@@ -28,6 +29,8 @@ import com.google.inject.Provider;
|
||||
import com.google.inject.Singleton;
|
||||
import com.google.inject.internal.Nullable;
|
||||
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
@@ -67,6 +70,23 @@ public class EventFactory {
|
||||
return a;
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a RefUpdateAttribute for the given old ObjectId, new ObjectId, and
|
||||
* branch that is suitable for serialization to JSON.
|
||||
*
|
||||
* @param refUpdate
|
||||
* @param refName
|
||||
* @return object suitable for serialization to JSON
|
||||
*/
|
||||
public RefUpdateAttribute asRefUpdateAttribute(final ObjectId oldId, final ObjectId newId, final Branch.NameKey refName) {
|
||||
RefUpdateAttribute ru = new RefUpdateAttribute();
|
||||
ru.newRev = newId != null ? newId.getName() : ObjectId.zeroId().getName();
|
||||
ru.oldRev = oldId != null ? oldId.getName() : ObjectId.zeroId().getName();
|
||||
ru.project = refName.getParentKey().get();
|
||||
ru.refName = refName.getShortName();
|
||||
return ru;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extend the existing ChangeAttribute with additional fields.
|
||||
*
|
||||
|
||||
@@ -0,0 +1,22 @@
|
||||
// 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.server.events;
|
||||
|
||||
public class RefUpdateAttribute {
|
||||
public String oldRev;
|
||||
public String newRev;
|
||||
public String refName;
|
||||
public String project;
|
||||
}
|
||||
@@ -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.server.events;
|
||||
|
||||
public class RefUpdatedEvent extends ChangeEvent {
|
||||
public final String type = "ref-updated";
|
||||
public AccountAttribute submitter;
|
||||
public RefUpdateAttribute refUpdate;
|
||||
}
|
||||
@@ -881,6 +881,13 @@ public class MergeOp {
|
||||
case FAST_FORWARD:
|
||||
replication.scheduleUpdate(destBranch.getParentKey(), branchUpdate
|
||||
.getName());
|
||||
|
||||
Account account = null;
|
||||
final PatchSetApproval submitter = getSubmitter(mergeTip.patchsetId);
|
||||
if (submitter != null) {
|
||||
account = accountCache.get(submitter.getAccountId()).getAccount();
|
||||
}
|
||||
hooks.doRefUpdatedHook(destBranch, branchUpdate, account);
|
||||
break;
|
||||
|
||||
default:
|
||||
|
||||
@@ -302,6 +302,8 @@ public class ReceiveCommits implements PreReceiveHook, PostReceiveHook {
|
||||
// Change refs are scheduled when they are created.
|
||||
//
|
||||
replication.scheduleUpdate(project.getNameKey(), c.getRefName());
|
||||
Branch.NameKey destBranch = new Branch.NameKey(project.getNameKey(), c.getRefName());
|
||||
hooks.doRefUpdatedHook(destBranch, c.getOldId(), c.getNewId(), currentUser.getAccount());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user