Search in sources :

Example 51 with MetaDataUpdate

use of com.google.gerrit.server.git.meta.MetaDataUpdate in project gerrit by GerritCodeReview.

the class AccountIT method stalenessChecker.

@Test
// Instants
@SuppressWarnings("JdkObsolete")
public void stalenessChecker() throws Exception {
    // Newly created account is not stale.
    AccountInfo accountInfo = gApi.accounts().create(name("foo")).get();
    Account.Id accountId = Account.id(accountInfo._accountId);
    assertThat(stalenessChecker.check(accountId).isStale()).isFalse();
    // Manually updating the user ref makes the index document stale.
    String userRef = RefNames.refsUsers(accountId);
    try (Repository repo = repoManager.openRepository(allUsers);
        ObjectInserter oi = repo.newObjectInserter();
        RevWalk rw = new RevWalk(repo)) {
        RevCommit commit = rw.parseCommit(repo.exactRef(userRef).getObjectId());
        PersonIdent ident = new PersonIdent(serverIdent.get(), Date.from(TimeUtil.now()));
        CommitBuilder cb = new CommitBuilder();
        cb.setTreeId(commit.getTree());
        cb.setCommitter(ident);
        cb.setAuthor(ident);
        cb.setMessage(commit.getFullMessage());
        ObjectId emptyCommit = oi.insert(cb);
        oi.flush();
        RefUpdate updateRef = repo.updateRef(userRef);
        updateRef.setExpectedOldObjectId(commit.toObjectId());
        updateRef.setNewObjectId(emptyCommit);
        assertThat(updateRef.forceUpdate()).isEqualTo(RefUpdate.Result.FORCED);
    }
    assertStaleAccountAndReindex(accountId);
    // stale.
    try (Repository repo = repoManager.openRepository(allUsers)) {
        ExternalIdNotes extIdNotes = ExternalIdNotes.load(allUsers, repo, externalIdFactory, authConfig.isUserNameCaseInsensitiveMigrationMode());
        ExternalId.Key key = externalIdKeyFactory.create("foo", "foo");
        extIdNotes.insert(externalIdFactory.create(key, accountId));
        try (MetaDataUpdate update = metaDataUpdateFactory.create(allUsers)) {
            extIdNotes.commit(update);
        }
        assertStaleAccountAndReindex(accountId);
        extIdNotes = ExternalIdNotes.load(allUsers, repo, externalIdFactory, authConfig.isUserNameCaseInsensitiveMigrationMode());
        extIdNotes.upsert(externalIdFactory.createWithEmail(key, accountId, "foo@example.com"));
        try (MetaDataUpdate update = metaDataUpdateFactory.create(allUsers)) {
            extIdNotes.commit(update);
        }
        assertStaleAccountAndReindex(accountId);
        extIdNotes = ExternalIdNotes.load(allUsers, repo, externalIdFactory, authConfig.isUserNameCaseInsensitiveMigrationMode());
        extIdNotes.delete(accountId, key);
        try (MetaDataUpdate update = metaDataUpdateFactory.create(allUsers)) {
            extIdNotes.commit(update);
        }
        assertStaleAccountAndReindex(accountId);
    }
    // Manually delete account
    try (Repository repo = repoManager.openRepository(allUsers);
        RevWalk rw = new RevWalk(repo)) {
        RevCommit commit = rw.parseCommit(repo.exactRef(userRef).getObjectId());
        RefUpdate updateRef = repo.updateRef(userRef);
        updateRef.setExpectedOldObjectId(commit.toObjectId());
        updateRef.setNewObjectId(ObjectId.zeroId());
        updateRef.setForceUpdate(true);
        assertThat(updateRef.delete()).isEqualTo(RefUpdate.Result.FORCED);
    }
    assertStaleAccountAndReindex(accountId);
}
Also used : TestAccount(com.google.gerrit.acceptance.TestAccount) Account(com.google.gerrit.entities.Account) ObjectId(org.eclipse.jgit.lib.ObjectId) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) PublicKeyStore.keyToString(com.google.gerrit.gpg.PublicKeyStore.keyToString) RevWalk(org.eclipse.jgit.revwalk.RevWalk) TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) InMemoryRepository(org.eclipse.jgit.internal.storage.dfs.InMemoryRepository) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) PersonIdent(org.eclipse.jgit.lib.PersonIdent) ExternalIdNotes(com.google.gerrit.server.account.externalids.ExternalIdNotes) AccountInfo(com.google.gerrit.extensions.common.AccountInfo) RevCommit(org.eclipse.jgit.revwalk.RevCommit) RefUpdate(org.eclipse.jgit.lib.RefUpdate) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 52 with MetaDataUpdate

use of com.google.gerrit.server.git.meta.MetaDataUpdate in project gerrit by GerritCodeReview.

the class AbstractDaemonTest method setUseSignedOffBy.

protected void setUseSignedOffBy(InheritableBoolean value) throws Exception {
    try (MetaDataUpdate md = metaDataUpdateFactory.create(project)) {
        ProjectConfig config = projectConfigFactory.read(md);
        config.updateProject(p -> p.setBooleanConfig(BooleanProjectConfig.USE_SIGNED_OFF_BY, value));
        config.commit(md);
        projectCache.evictAndReindex(config.getProject());
    }
}
Also used : BooleanProjectConfig(com.google.gerrit.entities.BooleanProjectConfig) ProjectConfig(com.google.gerrit.server.project.ProjectConfig) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 53 with MetaDataUpdate

use of com.google.gerrit.server.git.meta.MetaDataUpdate in project gerrit by GerritCodeReview.

the class LocalUsernamesToLowerCase method run.

@Override
public int run() throws Exception {
    Injector dbInjector = createDbInjector();
    manager.add(dbInjector, dbInjector.createChildInjector(NoteDbSchemaVersionCheck.module()));
    manager.start();
    dbInjector.createChildInjector(new FactoryModule() {

        @Override
        protected void configure() {
            bind(GitReferenceUpdated.class).toInstance(GitReferenceUpdated.DISABLED);
            factory(MetaDataUpdate.InternalFactory.class);
            // The LocalUsernamesToLowerCase program needs to access all external IDs only
            // once to update them. After the update they are not accessed again. Hence the
            // LocalUsernamesToLowerCase program doesn't benefit from caching external IDs and
            // the external ID cache can be disabled.
            install(DisabledExternalIdCache.module());
        }
    }).injectMembers(this);
    Collection<ExternalId> todo = externalIds.all();
    monitor.beginTask("Converting local usernames", todo.size());
    try (Repository repo = repoManager.openRepository(allUsersName)) {
        ExternalIdNotes extIdNotes = externalIdNotesFactory.load(repo);
        for (ExternalId extId : todo) {
            convertLocalUserToLowerCase(extIdNotes, extId);
            monitor.update(1);
        }
        try (MetaDataUpdate metaDataUpdate = metaDataUpdateServerFactory.get().create(allUsersName)) {
            metaDataUpdate.setMessage("Convert local usernames to lower case");
            extIdNotes.commit(metaDataUpdate);
        }
    }
    monitor.endTask();
    int exitCode = reindexAccounts();
    manager.stop();
    return exitCode;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) Injector(com.google.inject.Injector) FactoryModule(com.google.gerrit.extensions.config.FactoryModule) ExternalIdNotes(com.google.gerrit.server.account.externalids.ExternalIdNotes) ExternalId(com.google.gerrit.server.account.externalids.ExternalId) GitReferenceUpdated(com.google.gerrit.server.extensions.events.GitReferenceUpdated) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 54 with MetaDataUpdate

use of com.google.gerrit.server.git.meta.MetaDataUpdate in project gerrit by GerritCodeReview.

the class GroupsOnInit method createMetaDataUpdate.

private MetaDataUpdate createMetaDataUpdate(Repository repository, PersonIdent personIdent) {
    MetaDataUpdate metaDataUpdate = new MetaDataUpdate(GitReferenceUpdated.DISABLED, allUsers, repository);
    metaDataUpdate.getCommitBuilder().setAuthor(personIdent);
    metaDataUpdate.getCommitBuilder().setCommitter(personIdent);
    return metaDataUpdate;
}
Also used : MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Example 55 with MetaDataUpdate

use of com.google.gerrit.server.git.meta.MetaDataUpdate in project gerrit by GerritCodeReview.

the class ExternalIdsOnInit method insert.

public synchronized void insert(String commitMessage, Collection<ExternalId> extIds) throws IOException, ConfigInvalidException {
    File path = getPath();
    if (path != null) {
        try (Repository allUsersRepo = new FileRepository(path)) {
            ExternalIdNotes extIdNotes = ExternalIdNotes.load(allUsers, allUsersRepo, externalIdFactory, authConfig.isUserNameCaseInsensitiveMigrationMode());
            extIdNotes.insert(extIds);
            try (MetaDataUpdate metaDataUpdate = new MetaDataUpdate(GitReferenceUpdated.DISABLED, allUsers, allUsersRepo)) {
                PersonIdent serverIdent = new GerritPersonIdentProvider(flags.cfg).get();
                metaDataUpdate.getCommitBuilder().setAuthor(serverIdent);
                metaDataUpdate.getCommitBuilder().setCommitter(serverIdent);
                metaDataUpdate.getCommitBuilder().setMessage(commitMessage);
                extIdNotes.commit(metaDataUpdate);
            }
        }
    }
}
Also used : FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) FileRepository(org.eclipse.jgit.internal.storage.file.FileRepository) Repository(org.eclipse.jgit.lib.Repository) PersonIdent(org.eclipse.jgit.lib.PersonIdent) ExternalIdNotes(com.google.gerrit.server.account.externalids.ExternalIdNotes) GerritPersonIdentProvider(com.google.gerrit.server.GerritPersonIdentProvider) File(java.io.File) MetaDataUpdate(com.google.gerrit.server.git.meta.MetaDataUpdate)

Aggregations

MetaDataUpdate (com.google.gerrit.server.git.meta.MetaDataUpdate)84 Test (org.junit.Test)39 Repository (org.eclipse.jgit.lib.Repository)36 ExternalIdNotes (com.google.gerrit.server.account.externalids.ExternalIdNotes)28 ProjectConfig (com.google.gerrit.server.project.ProjectConfig)26 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)22 TestRepository (org.eclipse.jgit.junit.TestRepository)17 GerritConfig (com.google.gerrit.acceptance.config.GerritConfig)15 InMemoryRepository (org.eclipse.jgit.internal.storage.dfs.InMemoryRepository)15 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)14 Account (com.google.gerrit.entities.Account)13 ExternalId (com.google.gerrit.server.account.externalids.ExternalId)12 PersonIdent (org.eclipse.jgit.lib.PersonIdent)11 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)7 Project (com.google.gerrit.entities.Project)6 LightweightPluginDaemonTest (com.google.gerrit.acceptance.LightweightPluginDaemonTest)5 TestAccount (com.google.gerrit.acceptance.TestAccount)5 AccessSection (com.google.gerrit.entities.AccessSection)5 GroupReference (com.google.gerrit.entities.GroupReference)5 LabelType (com.google.gerrit.entities.LabelType)5