Trigger GitReferenceUpdated event when external IDs are updated

When external IDs are updated the refs/meta/external-ids branch in the
All-Users repository is updated. For this update we must trigger a
GitReferenceUpdated event, so that e.g. this ref update gets replicated
to Gerrit slaves.

PostGpgKeys and GpgKeys are now injected as provider into
GpgApiAdapterImpl to avoid a circular Guice dependency:
GitReferenceUpdated needs EventUtil which needs ChangeJson.Factory.
ChangeJson uses GpgApiAdapter to find out if push certificates are
enabled and if yes to include the push certicates of the patch set
uploaders into RevisionInfo. This doesn't require PostGpgKeys and
GpgKeys. If PostGpgKeys would be injected without provider we would need
ExternalIdsUpdate.User to instantiate it which now needs
GitReferenceUpdated. Hence without injecting PostGpgKeys into
GpgApiAdapterImpl as a provider we would have a dependency circle.

Change-Id: Ia9c71d26669f24f20c659d82ae97ecfca6ab61c3
Signed-off-by: Edwin Kempin <ekempin@google.com>
This commit is contained in:
Edwin Kempin
2017-10-05 11:39:09 +02:00
parent 8dac226947
commit 02e281d9d3
8 changed files with 269 additions and 30 deletions

View File

@@ -38,6 +38,7 @@ import static org.junit.Assert.fail;
import com.google.common.collect.FluentIterable; import com.google.common.collect.FluentIterable;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.common.collect.ImmutableSetMultimap; import com.google.common.collect.ImmutableSetMultimap;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
@@ -67,6 +68,7 @@ import com.google.gerrit.extensions.common.ChangeInfo;
import com.google.gerrit.extensions.common.GpgKeyInfo; import com.google.gerrit.extensions.common.GpgKeyInfo;
import com.google.gerrit.extensions.common.SshKeyInfo; import com.google.gerrit.extensions.common.SshKeyInfo;
import com.google.gerrit.extensions.events.AccountIndexedListener; import com.google.gerrit.extensions.events.AccountIndexedListener;
import com.google.gerrit.extensions.events.GitReferenceUpdatedListener;
import com.google.gerrit.extensions.registration.DynamicSet; import com.google.gerrit.extensions.registration.DynamicSet;
import com.google.gerrit.extensions.registration.RegistrationHandle; import com.google.gerrit.extensions.registration.RegistrationHandle;
import com.google.gerrit.extensions.restapi.AuthException; import com.google.gerrit.extensions.restapi.AuthException;
@@ -79,6 +81,7 @@ import com.google.gerrit.gpg.PublicKeyStore;
import com.google.gerrit.gpg.testutil.TestKey; import com.google.gerrit.gpg.testutil.TestKey;
import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountGroup; import com.google.gerrit.reviewdb.client.AccountGroup;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.client.RefNames; import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.server.Sequences; import com.google.gerrit.server.Sequences;
import com.google.gerrit.server.account.AccountConfig; import com.google.gerrit.server.account.AccountConfig;
@@ -106,6 +109,7 @@ import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.EnumSet; import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.List; import java.util.List;
@@ -155,6 +159,8 @@ public class AccountIT extends AbstractDaemonTest {
@Inject private DynamicSet<AccountIndexedListener> accountIndexedListeners; @Inject private DynamicSet<AccountIndexedListener> accountIndexedListeners;
@Inject private DynamicSet<GitReferenceUpdatedListener> refUpdateListeners;
@Inject private Sequences seq; @Inject private Sequences seq;
@Inject private Provider<InternalAccountQuery> accountQueryProvider; @Inject private Provider<InternalAccountQuery> accountQueryProvider;
@@ -163,6 +169,8 @@ public class AccountIT extends AbstractDaemonTest {
private AccountIndexedCounter accountIndexedCounter; private AccountIndexedCounter accountIndexedCounter;
private RegistrationHandle accountIndexEventCounterHandle; private RegistrationHandle accountIndexEventCounterHandle;
private RefUpdateCounter refUpdateCounter;
private RegistrationHandle refUpdateCounterHandle;
private ExternalIdsUpdate externalIdsUpdate; private ExternalIdsUpdate externalIdsUpdate;
private List<ExternalId> savedExternalIds; private List<ExternalId> savedExternalIds;
@@ -179,6 +187,19 @@ public class AccountIT extends AbstractDaemonTest {
} }
} }
@Before
public void addRefUpdateCounter() {
refUpdateCounter = new RefUpdateCounter();
refUpdateCounterHandle = refUpdateListeners.add(refUpdateCounter);
}
@After
public void removeRefUpdateCounter() {
if (refUpdateCounterHandle != null) {
refUpdateCounterHandle.remove();
}
}
@Before @Before
public void saveExternalIds() throws Exception { public void saveExternalIds() throws Exception {
externalIdsUpdate = externalIdsUpdateFactory.create(); externalIdsUpdate = externalIdsUpdateFactory.create();
@@ -228,16 +249,25 @@ public class AccountIT extends AbstractDaemonTest {
@Test @Test
public void create() throws Exception { public void create() throws Exception {
create(2); // account creation + external ID creation Account.Id accountId = create(2); // account creation + external ID creation
refUpdateCounter.assertRefUpdateFor(
RefUpdateCounter.projectRef(allUsers, RefNames.refsUsers(accountId)),
RefUpdateCounter.projectRef(allUsers, RefNames.REFS_EXTERNAL_IDS));
} }
@Test @Test
@UseSsh @UseSsh
public void createWithSshKeys() throws Exception { public void createWithSshKeys() throws Exception {
create(3); // account creation + external ID creation + adding SSH keys Account.Id accountId = create(3); // account creation + external ID creation + adding SSH keys
refUpdateCounter.assertRefUpdateFor(
ImmutableMap.of(
RefUpdateCounter.projectRef(allUsers, RefNames.refsUsers(accountId)),
2,
RefUpdateCounter.projectRef(allUsers, RefNames.REFS_EXTERNAL_IDS),
1));
} }
private void create(int expectedAccountReindexCalls) throws Exception { private Account.Id create(int expectedAccountReindexCalls) throws Exception {
String name = "foo"; String name = "foo";
TestAccount foo = accountCreator.create(name); TestAccount foo = accountCreator.create(name);
AccountInfo info = gApi.accounts().id(foo.id.get()).get(); AccountInfo info = gApi.accounts().id(foo.id.get()).get();
@@ -245,6 +275,7 @@ public class AccountIT extends AbstractDaemonTest {
assertThat(info.name).isEqualTo(name); assertThat(info.name).isEqualTo(name);
accountIndexedCounter.assertReindexOf(foo, expectedAccountReindexCalls); accountIndexedCounter.assertReindexOf(foo, expectedAccountReindexCalls);
assertUserBranch(foo.getId(), name, null); assertUserBranch(foo.getId(), name, null);
return foo.getId();
} }
@Test @Test
@@ -1899,4 +1930,45 @@ public class AccountIT extends AbstractDaemonTest {
assertThat(countsByAccount).isEmpty(); assertThat(countsByAccount).isEmpty();
} }
} }
private static class RefUpdateCounter implements GitReferenceUpdatedListener {
private final AtomicLongMap<String> countsByProjectRefs = AtomicLongMap.create();
static String projectRef(Project.NameKey project, String ref) {
return projectRef(project.get(), ref);
}
static String projectRef(String project, String ref) {
return project + ":" + ref;
}
@Override
public void onGitReferenceUpdated(Event event) {
countsByProjectRefs.incrementAndGet(projectRef(event.getProjectName(), event.getRefName()));
}
void clear() {
countsByProjectRefs.clear();
}
long getCount(String projectRef) {
return countsByProjectRefs.get(projectRef);
}
void assertRefUpdateFor(String... projectRefs) {
Map<String, Integer> expectedRefUpdateCounts = new HashMap<>();
for (String projectRef : projectRefs) {
expectedRefUpdateCounts.put(projectRef, 1);
}
assertRefUpdateFor(expectedRefUpdateCounts);
}
void assertRefUpdateFor(Map<String, Integer> expectedProjectRefUpdateCounts) {
for (Map.Entry<String, Integer> e : expectedProjectRefUpdateCounts.entrySet()) {
assertThat(getCount(e.getKey())).isEqualTo(e.getValue());
}
assertThat(countsByProjectRefs).hasSize(expectedProjectRefUpdateCounts.size());
clear();
}
}
} }

View File

@@ -55,6 +55,7 @@ import com.google.gerrit.server.account.externalids.ExternalIds;
import com.google.gerrit.server.account.externalids.ExternalIdsUpdate; import com.google.gerrit.server.account.externalids.ExternalIdsUpdate;
import com.google.gerrit.server.account.externalids.ExternalIdsUpdate.RefsMetaExternalIdsUpdate; import com.google.gerrit.server.account.externalids.ExternalIdsUpdate.RefsMetaExternalIdsUpdate;
import com.google.gerrit.server.config.AllUsersName; import com.google.gerrit.server.config.AllUsersName;
import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
import com.google.gerrit.server.git.LockFailureException; import com.google.gerrit.server.git.LockFailureException;
import com.google.gson.reflect.TypeToken; import com.google.gson.reflect.TypeToken;
import com.google.gwtorm.server.OrmDuplicateKeyException; import com.google.gwtorm.server.OrmDuplicateKeyException;
@@ -579,7 +580,17 @@ public class ExternalIdIT extends AbstractDaemonTest {
noteMap.set(noteId, dataBlob); noteMap.set(noteId, dataBlob);
ExternalIdsUpdate.commit( ExternalIdsUpdate.commit(
repo, rw, ins, rev, noteMap, "Add external ID", admin.getIdent(), admin.getIdent()); allUsers,
repo,
rw,
ins,
rev,
noteMap,
"Add external ID",
admin.getIdent(),
admin.getIdent(),
null,
GitReferenceUpdated.DISABLED);
return noteId.getName(); return noteId.getName();
} }
} }
@@ -600,7 +611,17 @@ public class ExternalIdIT extends AbstractDaemonTest {
noteMap.set(noteId, dataBlob); noteMap.set(noteId, dataBlob);
ExternalIdsUpdate.commit( ExternalIdsUpdate.commit(
repo, rw, ins, rev, noteMap, "Add external ID", admin.getIdent(), admin.getIdent()); allUsers,
repo,
rw,
ins,
rev,
noteMap,
"Add external ID",
admin.getIdent(),
admin.getIdent(),
null,
GitReferenceUpdated.DISABLED);
return noteId.getName(); return noteId.getName();
} }
} }
@@ -617,7 +638,17 @@ public class ExternalIdIT extends AbstractDaemonTest {
noteMap.set(noteId, dataBlob); noteMap.set(noteId, dataBlob);
ExternalIdsUpdate.commit( ExternalIdsUpdate.commit(
repo, rw, ins, rev, noteMap, "Add external ID", admin.getIdent(), admin.getIdent()); allUsers,
repo,
rw,
ins,
rev,
noteMap,
"Add external ID",
admin.getIdent(),
admin.getIdent(),
null,
GitReferenceUpdated.DISABLED);
return noteId.getName(); return noteId.getName();
} }
} }
@@ -634,7 +665,17 @@ public class ExternalIdIT extends AbstractDaemonTest {
noteMap.set(noteId, dataBlob); noteMap.set(noteId, dataBlob);
ExternalIdsUpdate.commit( ExternalIdsUpdate.commit(
repo, rw, ins, rev, noteMap, "Add external ID", admin.getIdent(), admin.getIdent()); allUsers,
repo,
rw,
ins,
rev,
noteMap,
"Add external ID",
admin.getIdent(),
admin.getIdent(),
null,
GitReferenceUpdated.DISABLED);
return noteId.getName(); return noteId.getName();
} }
} }
@@ -690,6 +731,8 @@ public class ExternalIdIT extends AbstractDaemonTest {
new DisabledExternalIdCache(), new DisabledExternalIdCache(),
serverIdent.get(), serverIdent.get(),
serverIdent.get(), serverIdent.get(),
null,
GitReferenceUpdated.DISABLED,
() -> { () -> {
if (!doneBgUpdate.getAndSet(true)) { if (!doneBgUpdate.getAndSet(true)) {
try { try {
@@ -726,6 +769,8 @@ public class ExternalIdIT extends AbstractDaemonTest {
new DisabledExternalIdCache(), new DisabledExternalIdCache(),
serverIdent.get(), serverIdent.get(),
serverIdent.get(), serverIdent.get(),
null,
GitReferenceUpdated.DISABLED,
() -> { () -> {
try { try {
extIdsUpdate extIdsUpdate
@@ -824,7 +869,17 @@ public class ExternalIdIT extends AbstractDaemonTest {
NoteMap noteMap = ExternalIdReader.readNoteMap(rw, rev); NoteMap noteMap = ExternalIdReader.readNoteMap(rw, rev);
ExternalIdsUpdate.insert(rw, ins, noteMap, extId); ExternalIdsUpdate.insert(rw, ins, noteMap, extId);
ExternalIdsUpdate.commit( ExternalIdsUpdate.commit(
repo, rw, ins, rev, noteMap, "insert new ID", serverIdent.get(), serverIdent.get()); allUsers,
repo,
rw,
ins,
rev,
noteMap,
"insert new ID",
serverIdent.get(),
serverIdent.get(),
null,
GitReferenceUpdated.DISABLED);
} }
} }
@@ -839,6 +894,7 @@ public class ExternalIdIT extends AbstractDaemonTest {
} }
ExternalIdsUpdate.commit( ExternalIdsUpdate.commit(
allUsers,
testRepo.getRepository(), testRepo.getRepository(),
testRepo.getRevWalk(), testRepo.getRevWalk(),
ins, ins,
@@ -846,7 +902,9 @@ public class ExternalIdIT extends AbstractDaemonTest {
noteMap, noteMap,
"Add external ID", "Add external ID",
admin.getIdent(), admin.getIdent(),
admin.getIdent()); admin.getIdent(),
null,
GitReferenceUpdated.DISABLED);
} }
} }

View File

@@ -29,6 +29,7 @@ import com.google.gerrit.server.account.AccountResource;
import com.google.gerrit.server.api.accounts.GpgApiAdapter; import com.google.gerrit.server.api.accounts.GpgApiAdapter;
import com.google.gwtorm.server.OrmException; import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject; import com.google.inject.Inject;
import com.google.inject.Provider;
import java.io.IOException; import java.io.IOException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@@ -38,15 +39,15 @@ import org.eclipse.jgit.transport.PushCertificate;
import org.eclipse.jgit.transport.PushCertificateParser; import org.eclipse.jgit.transport.PushCertificateParser;
public class GpgApiAdapterImpl implements GpgApiAdapter { public class GpgApiAdapterImpl implements GpgApiAdapter {
private final PostGpgKeys postGpgKeys; private final Provider<PostGpgKeys> postGpgKeys;
private final GpgKeys gpgKeys; private final Provider<GpgKeys> gpgKeys;
private final GpgKeyApiImpl.Factory gpgKeyApiFactory; private final GpgKeyApiImpl.Factory gpgKeyApiFactory;
private final GerritPushCertificateChecker.Factory pushCertCheckerFactory; private final GerritPushCertificateChecker.Factory pushCertCheckerFactory;
@Inject @Inject
GpgApiAdapterImpl( GpgApiAdapterImpl(
PostGpgKeys postGpgKeys, Provider<PostGpgKeys> postGpgKeys,
GpgKeys gpgKeys, Provider<GpgKeys> gpgKeys,
GpgKeyApiImpl.Factory gpgKeyApiFactory, GpgKeyApiImpl.Factory gpgKeyApiFactory,
GerritPushCertificateChecker.Factory pushCertCheckerFactory) { GerritPushCertificateChecker.Factory pushCertCheckerFactory) {
this.postGpgKeys = postGpgKeys; this.postGpgKeys = postGpgKeys;
@@ -64,7 +65,7 @@ public class GpgApiAdapterImpl implements GpgApiAdapter {
public Map<String, GpgKeyInfo> listGpgKeys(AccountResource account) public Map<String, GpgKeyInfo> listGpgKeys(AccountResource account)
throws RestApiException, GpgException { throws RestApiException, GpgException {
try { try {
return gpgKeys.list().apply(account); return gpgKeys.get().list().apply(account);
} catch (OrmException | PGPException | IOException e) { } catch (OrmException | PGPException | IOException e) {
throw new GpgException(e); throw new GpgException(e);
} }
@@ -78,7 +79,7 @@ public class GpgApiAdapterImpl implements GpgApiAdapter {
in.add = add; in.add = add;
in.delete = delete; in.delete = delete;
try { try {
return postGpgKeys.apply(account, in); return postGpgKeys.get().apply(account, in);
} catch (PGPException | OrmException | IOException | ConfigInvalidException e) { } catch (PGPException | OrmException | IOException | ConfigInvalidException e) {
throw new GpgException(e); throw new GpgException(e);
} }
@@ -88,7 +89,7 @@ public class GpgApiAdapterImpl implements GpgApiAdapter {
public GpgKeyApi gpgKey(AccountResource account, IdString idStr) public GpgKeyApi gpgKey(AccountResource account, IdString idStr)
throws RestApiException, GpgException { throws RestApiException, GpgException {
try { try {
return gpgKeyApiFactory.create(gpgKeys.parse(account, idStr)); return gpgKeyApiFactory.create(gpgKeys.get().parse(account, idStr));
} catch (PGPException | OrmException | IOException e) { } catch (PGPException | OrmException | IOException e) {
throw new GpgException(e); throw new GpgException(e);
} }

View File

@@ -16,11 +16,13 @@ package com.google.gerrit.pgm.init;
import com.google.gerrit.pgm.init.api.AllUsersNameOnInitProvider; import com.google.gerrit.pgm.init.api.AllUsersNameOnInitProvider;
import com.google.gerrit.pgm.init.api.InitFlags; import com.google.gerrit.pgm.init.api.InitFlags;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.server.GerritPersonIdentProvider; import com.google.gerrit.server.GerritPersonIdentProvider;
import com.google.gerrit.server.account.externalids.ExternalId; import com.google.gerrit.server.account.externalids.ExternalId;
import com.google.gerrit.server.account.externalids.ExternalIdReader; import com.google.gerrit.server.account.externalids.ExternalIdReader;
import com.google.gerrit.server.account.externalids.ExternalIdsUpdate; import com.google.gerrit.server.account.externalids.ExternalIdsUpdate;
import com.google.gerrit.server.config.SitePaths; import com.google.gerrit.server.config.SitePaths;
import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
import com.google.gwtorm.server.OrmException; import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject; import com.google.inject.Inject;
import java.io.File; import java.io.File;
@@ -67,7 +69,17 @@ public class ExternalIdsOnInit {
PersonIdent serverIdent = new GerritPersonIdentProvider(flags.cfg).get(); PersonIdent serverIdent = new GerritPersonIdentProvider(flags.cfg).get();
ExternalIdsUpdate.commit( ExternalIdsUpdate.commit(
repo, rw, ins, rev, noteMap, commitMessage, serverIdent, serverIdent); new Project.NameKey(allUsers),
repo,
rw,
ins,
rev,
noteMap,
commitMessage,
serverIdent,
serverIdent,
null,
GitReferenceUpdated.DISABLED);
} }
} }
} }

View File

@@ -17,6 +17,7 @@ package com.google.gerrit.server.account.externalids;
import com.google.common.collect.ImmutableSet; import com.google.common.collect.ImmutableSet;
import com.google.gerrit.server.GerritPersonIdent; import com.google.gerrit.server.GerritPersonIdent;
import com.google.gerrit.server.config.AllUsersName; import com.google.gerrit.server.config.AllUsersName;
import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
import com.google.gerrit.server.git.GitRepositoryManager; import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gwtorm.server.OrmException; import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject; import com.google.inject.Inject;
@@ -41,6 +42,7 @@ import org.eclipse.jgit.revwalk.RevWalk;
*/ */
public class ExternalIdsBatchUpdate { public class ExternalIdsBatchUpdate {
private final GitRepositoryManager repoManager; private final GitRepositoryManager repoManager;
private final GitReferenceUpdated gitRefUpdated;
private final AllUsersName allUsersName; private final AllUsersName allUsersName;
private final PersonIdent serverIdent; private final PersonIdent serverIdent;
private final ExternalIdCache externalIdCache; private final ExternalIdCache externalIdCache;
@@ -50,10 +52,12 @@ public class ExternalIdsBatchUpdate {
@Inject @Inject
public ExternalIdsBatchUpdate( public ExternalIdsBatchUpdate(
GitRepositoryManager repoManager, GitRepositoryManager repoManager,
GitReferenceUpdated gitRefUpdated,
AllUsersName allUsersName, AllUsersName allUsersName,
@GerritPersonIdent PersonIdent serverIdent, @GerritPersonIdent PersonIdent serverIdent,
ExternalIdCache externalIdCache) { ExternalIdCache externalIdCache) {
this.repoManager = repoManager; this.repoManager = repoManager;
this.gitRefUpdated = gitRefUpdated;
this.allUsersName = allUsersName; this.allUsersName = allUsersName;
this.serverIdent = serverIdent; this.serverIdent = serverIdent;
this.externalIdCache = externalIdCache; this.externalIdCache = externalIdCache;
@@ -105,7 +109,17 @@ public class ExternalIdsBatchUpdate {
ObjectId newRev = ObjectId newRev =
ExternalIdsUpdate.commit( ExternalIdsUpdate.commit(
repo, rw, ins, rev, noteMap, commitMessage, serverIdent, serverIdent); allUsersName,
repo,
rw,
ins,
rev,
noteMap,
commitMessage,
serverIdent,
serverIdent,
null,
gitRefUpdated);
externalIdCache.onReplace(rev, newRev, toDelete, toAdd); externalIdCache.onReplace(rev, newRev, toDelete, toAdd);
} }

View File

@@ -41,11 +41,13 @@ import com.google.gerrit.metrics.Counter0;
import com.google.gerrit.metrics.Description; import com.google.gerrit.metrics.Description;
import com.google.gerrit.metrics.MetricMaker; import com.google.gerrit.metrics.MetricMaker;
import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.client.RefNames; import com.google.gerrit.reviewdb.client.RefNames;
import com.google.gerrit.server.GerritPersonIdent; import com.google.gerrit.server.GerritPersonIdent;
import com.google.gerrit.server.IdentifiedUser; import com.google.gerrit.server.IdentifiedUser;
import com.google.gerrit.server.account.AccountCache; import com.google.gerrit.server.account.AccountCache;
import com.google.gerrit.server.config.AllUsersName; import com.google.gerrit.server.config.AllUsersName;
import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
import com.google.gerrit.server.git.GitRepositoryManager; import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gerrit.server.git.LockFailureException; import com.google.gerrit.server.git.LockFailureException;
import com.google.gwtorm.server.OrmDuplicateKeyException; import com.google.gwtorm.server.OrmDuplicateKeyException;
@@ -111,6 +113,7 @@ public class ExternalIdsUpdate {
private final ExternalIds externalIds; private final ExternalIds externalIds;
private final ExternalIdCache externalIdCache; private final ExternalIdCache externalIdCache;
private final Provider<PersonIdent> serverIdent; private final Provider<PersonIdent> serverIdent;
private final GitReferenceUpdated gitRefUpdated;
@Inject @Inject
public Server( public Server(
@@ -120,7 +123,8 @@ public class ExternalIdsUpdate {
MetricMaker metricMaker, MetricMaker metricMaker,
ExternalIds externalIds, ExternalIds externalIds,
ExternalIdCache externalIdCache, ExternalIdCache externalIdCache,
@GerritPersonIdent Provider<PersonIdent> serverIdent) { @GerritPersonIdent Provider<PersonIdent> serverIdent,
GitReferenceUpdated gitRefUpdated) {
this.repoManager = repoManager; this.repoManager = repoManager;
this.accountCache = accountCache; this.accountCache = accountCache;
this.allUsersName = allUsersName; this.allUsersName = allUsersName;
@@ -128,12 +132,22 @@ public class ExternalIdsUpdate {
this.externalIds = externalIds; this.externalIds = externalIds;
this.externalIdCache = externalIdCache; this.externalIdCache = externalIdCache;
this.serverIdent = serverIdent; this.serverIdent = serverIdent;
this.gitRefUpdated = gitRefUpdated;
} }
public ExternalIdsUpdate create() { public ExternalIdsUpdate create() {
PersonIdent i = serverIdent.get(); PersonIdent i = serverIdent.get();
return new ExternalIdsUpdate( return new ExternalIdsUpdate(
repoManager, accountCache, allUsersName, metricMaker, externalIds, externalIdCache, i, i); repoManager,
accountCache,
allUsersName,
metricMaker,
externalIds,
externalIdCache,
i,
i,
null,
gitRefUpdated);
} }
} }
@@ -154,6 +168,7 @@ public class ExternalIdsUpdate {
private final ExternalIds externalIds; private final ExternalIds externalIds;
private final ExternalIdCache externalIdCache; private final ExternalIdCache externalIdCache;
private final Provider<PersonIdent> serverIdent; private final Provider<PersonIdent> serverIdent;
private final GitReferenceUpdated gitRefUpdated;
@Inject @Inject
public ServerNoReindex( public ServerNoReindex(
@@ -162,19 +177,30 @@ public class ExternalIdsUpdate {
MetricMaker metricMaker, MetricMaker metricMaker,
ExternalIds externalIds, ExternalIds externalIds,
ExternalIdCache externalIdCache, ExternalIdCache externalIdCache,
@GerritPersonIdent Provider<PersonIdent> serverIdent) { @GerritPersonIdent Provider<PersonIdent> serverIdent,
GitReferenceUpdated gitRefUpdated) {
this.repoManager = repoManager; this.repoManager = repoManager;
this.allUsersName = allUsersName; this.allUsersName = allUsersName;
this.metricMaker = metricMaker; this.metricMaker = metricMaker;
this.externalIds = externalIds; this.externalIds = externalIds;
this.externalIdCache = externalIdCache; this.externalIdCache = externalIdCache;
this.serverIdent = serverIdent; this.serverIdent = serverIdent;
this.gitRefUpdated = gitRefUpdated;
} }
public ExternalIdsUpdate create() { public ExternalIdsUpdate create() {
PersonIdent i = serverIdent.get(); PersonIdent i = serverIdent.get();
return new ExternalIdsUpdate( return new ExternalIdsUpdate(
repoManager, null, allUsersName, metricMaker, externalIds, externalIdCache, i, i); repoManager,
null,
allUsersName,
metricMaker,
externalIds,
externalIdCache,
i,
i,
null,
gitRefUpdated);
} }
} }
@@ -194,6 +220,7 @@ public class ExternalIdsUpdate {
private final ExternalIdCache externalIdCache; private final ExternalIdCache externalIdCache;
private final Provider<PersonIdent> serverIdent; private final Provider<PersonIdent> serverIdent;
private final Provider<IdentifiedUser> identifiedUser; private final Provider<IdentifiedUser> identifiedUser;
private final GitReferenceUpdated gitRefUpdated;
@Inject @Inject
public User( public User(
@@ -204,7 +231,8 @@ public class ExternalIdsUpdate {
ExternalIds externalIds, ExternalIds externalIds,
ExternalIdCache externalIdCache, ExternalIdCache externalIdCache,
@GerritPersonIdent Provider<PersonIdent> serverIdent, @GerritPersonIdent Provider<PersonIdent> serverIdent,
Provider<IdentifiedUser> identifiedUser) { Provider<IdentifiedUser> identifiedUser,
GitReferenceUpdated gitRefUpdated) {
this.repoManager = repoManager; this.repoManager = repoManager;
this.accountCache = accountCache; this.accountCache = accountCache;
this.allUsersName = allUsersName; this.allUsersName = allUsersName;
@@ -213,9 +241,11 @@ public class ExternalIdsUpdate {
this.externalIdCache = externalIdCache; this.externalIdCache = externalIdCache;
this.serverIdent = serverIdent; this.serverIdent = serverIdent;
this.identifiedUser = identifiedUser; this.identifiedUser = identifiedUser;
this.gitRefUpdated = gitRefUpdated;
} }
public ExternalIdsUpdate create() { public ExternalIdsUpdate create() {
IdentifiedUser user = identifiedUser.get();
PersonIdent i = serverIdent.get(); PersonIdent i = serverIdent.get();
return new ExternalIdsUpdate( return new ExternalIdsUpdate(
repoManager, repoManager,
@@ -224,8 +254,10 @@ public class ExternalIdsUpdate {
metricMaker, metricMaker,
externalIds, externalIds,
externalIdCache, externalIdCache,
createPersonIdent(i, identifiedUser.get()), createPersonIdent(i, user),
i); i,
user,
gitRefUpdated);
} }
private PersonIdent createPersonIdent(PersonIdent ident, IdentifiedUser user) { private PersonIdent createPersonIdent(PersonIdent ident, IdentifiedUser user) {
@@ -253,6 +285,8 @@ public class ExternalIdsUpdate {
private final ExternalIdCache externalIdCache; private final ExternalIdCache externalIdCache;
private final PersonIdent committerIdent; private final PersonIdent committerIdent;
private final PersonIdent authorIdent; private final PersonIdent authorIdent;
@Nullable private final IdentifiedUser currentUser;
private final GitReferenceUpdated gitRefUpdated;
private final Runnable afterReadRevision; private final Runnable afterReadRevision;
private final Retryer<RefsMetaExternalIdsUpdate> retryer; private final Retryer<RefsMetaExternalIdsUpdate> retryer;
private final Counter0 updateCount; private final Counter0 updateCount;
@@ -265,7 +299,9 @@ public class ExternalIdsUpdate {
ExternalIds externalIds, ExternalIds externalIds,
ExternalIdCache externalIdCache, ExternalIdCache externalIdCache,
PersonIdent committerIdent, PersonIdent committerIdent,
PersonIdent authorIdent) { PersonIdent authorIdent,
@Nullable IdentifiedUser currentUser,
GitReferenceUpdated gitRefUpdated) {
this( this(
repoManager, repoManager,
accountCache, accountCache,
@@ -275,6 +311,8 @@ public class ExternalIdsUpdate {
externalIdCache, externalIdCache,
committerIdent, committerIdent,
authorIdent, authorIdent,
currentUser,
gitRefUpdated,
Runnables.doNothing(), Runnables.doNothing(),
RETRYER); RETRYER);
} }
@@ -289,6 +327,8 @@ public class ExternalIdsUpdate {
ExternalIdCache externalIdCache, ExternalIdCache externalIdCache,
PersonIdent committerIdent, PersonIdent committerIdent,
PersonIdent authorIdent, PersonIdent authorIdent,
@Nullable IdentifiedUser currentUser,
GitReferenceUpdated gitRefUpdated,
Runnable afterReadRevision, Runnable afterReadRevision,
Retryer<RefsMetaExternalIdsUpdate> retryer) { Retryer<RefsMetaExternalIdsUpdate> retryer) {
this.repoManager = checkNotNull(repoManager, "repoManager"); this.repoManager = checkNotNull(repoManager, "repoManager");
@@ -298,6 +338,8 @@ public class ExternalIdsUpdate {
this.externalIds = checkNotNull(externalIds, "externalIds"); this.externalIds = checkNotNull(externalIds, "externalIds");
this.externalIdCache = checkNotNull(externalIdCache, "externalIdCache"); this.externalIdCache = checkNotNull(externalIdCache, "externalIdCache");
this.authorIdent = checkNotNull(authorIdent, "authorIdent"); this.authorIdent = checkNotNull(authorIdent, "authorIdent");
this.currentUser = currentUser;
this.gitRefUpdated = checkNotNull(gitRefUpdated, "gitRefUpdated");
this.afterReadRevision = checkNotNull(afterReadRevision, "afterReadRevision"); this.afterReadRevision = checkNotNull(afterReadRevision, "afterReadRevision");
this.retryer = checkNotNull(retryer, "retryer"); this.retryer = checkNotNull(retryer, "retryer");
this.updateCount = this.updateCount =
@@ -732,13 +774,26 @@ public class ExternalIdsUpdate {
NoteMap noteMap, NoteMap noteMap,
UpdatedExternalIds updatedExtIds) UpdatedExternalIds updatedExtIds)
throws IOException { throws IOException {
ObjectId newRev = commit(repo, rw, ins, rev, noteMap, COMMIT_MSG, committerIdent, authorIdent); ObjectId newRev =
commit(
allUsersName,
repo,
rw,
ins,
rev,
noteMap,
COMMIT_MSG,
committerIdent,
authorIdent,
currentUser,
gitRefUpdated);
updateCount.increment(); updateCount.increment();
return RefsMetaExternalIdsUpdate.create(rev, newRev, updatedExtIds); return RefsMetaExternalIdsUpdate.create(rev, newRev, updatedExtIds);
} }
/** Commits updates to the external IDs. */ /** Commits updates to the external IDs. */
public static ObjectId commit( public static ObjectId commit(
Project.NameKey project,
Repository repo, Repository repo,
RevWalk rw, RevWalk rw,
ObjectInserter ins, ObjectInserter ins,
@@ -746,7 +801,9 @@ public class ExternalIdsUpdate {
NoteMap noteMap, NoteMap noteMap,
String commitMessage, String commitMessage,
PersonIdent committerIdent, PersonIdent committerIdent,
PersonIdent authorIdent) PersonIdent authorIdent,
@Nullable IdentifiedUser user,
GitReferenceUpdated gitRefUpdated)
throws IOException { throws IOException {
CommitBuilder cb = new CommitBuilder(); CommitBuilder cb = new CommitBuilder();
cb.setMessage(commitMessage); cb.setMessage(commitMessage);
@@ -793,6 +850,7 @@ public class ExternalIdsUpdate {
default: default:
throw new IOException("Updating external IDs failed with " + res); throw new IOException("Updating external IDs failed with " + res);
} }
gitRefUpdated.fire(project, u, user != null ? user.getAccount() : null);
return rw.parseCommit(commitId); return rw.parseCommit(commitId);
} }

View File

@@ -21,6 +21,7 @@ import com.google.gerrit.server.account.externalids.ExternalId;
import com.google.gerrit.server.account.externalids.ExternalIdReader; import com.google.gerrit.server.account.externalids.ExternalIdReader;
import com.google.gerrit.server.account.externalids.ExternalIdsUpdate; import com.google.gerrit.server.account.externalids.ExternalIdsUpdate;
import com.google.gerrit.server.config.AllUsersName; import com.google.gerrit.server.config.AllUsersName;
import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
import com.google.gerrit.server.git.GitRepositoryManager; import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gwtorm.jdbc.JdbcSchema; import com.google.gwtorm.jdbc.JdbcSchema;
import com.google.gwtorm.server.OrmException; import com.google.gwtorm.server.OrmException;
@@ -93,7 +94,18 @@ public class Schema_144 extends SchemaVersion {
ExternalIdsUpdate.upsert(rw, ins, noteMap, extId); ExternalIdsUpdate.upsert(rw, ins, noteMap, extId);
} }
ExternalIdsUpdate.commit(repo, rw, ins, rev, noteMap, COMMIT_MSG, serverIdent, serverIdent); ExternalIdsUpdate.commit(
allUsersName,
repo,
rw,
ins,
rev,
noteMap,
COMMIT_MSG,
serverIdent,
serverIdent,
null,
GitReferenceUpdated.DISABLED);
} }
} catch (IOException | ConfigInvalidException e) { } catch (IOException | ConfigInvalidException e) {
throw new OrmException("Failed to migrate external IDs to NoteDb", e); throw new OrmException("Failed to migrate external IDs to NoteDb", e);

View File

@@ -23,6 +23,7 @@ import com.google.gerrit.server.account.externalids.ExternalId;
import com.google.gerrit.server.account.externalids.ExternalIdReader; import com.google.gerrit.server.account.externalids.ExternalIdReader;
import com.google.gerrit.server.account.externalids.ExternalIdsUpdate; import com.google.gerrit.server.account.externalids.ExternalIdsUpdate;
import com.google.gerrit.server.config.AllUsersName; import com.google.gerrit.server.config.AllUsersName;
import com.google.gerrit.server.extensions.events.GitReferenceUpdated;
import com.google.gerrit.server.git.GitRepositoryManager; import com.google.gerrit.server.git.GitRepositoryManager;
import com.google.gwtorm.server.OrmException; import com.google.gwtorm.server.OrmException;
import com.google.inject.Inject; import com.google.inject.Inject;
@@ -84,7 +85,18 @@ public class Schema_148 extends SchemaVersion {
} }
} }
if (dirty) { if (dirty) {
ExternalIdsUpdate.commit(repo, rw, ins, rev, noteMap, COMMIT_MSG, serverUser, serverUser); ExternalIdsUpdate.commit(
allUsersName,
repo,
rw,
ins,
rev,
noteMap,
COMMIT_MSG,
serverUser,
serverUser,
null,
GitReferenceUpdated.DISABLED);
} }
} catch (IOException e) { } catch (IOException e) {
throw new OrmException("Failed to update external IDs", e); throw new OrmException("Failed to update external IDs", e);