Search in sources :

Example 56 with ReviewDb

use of com.google.gerrit.reviewdb.server.ReviewDb in project gerrit by GerritCodeReview.

the class RebaseUtil method parseBase.

Base parseBase(RevisionResource rsrc, String base) throws OrmException {
    ReviewDb db = dbProvider.get();
    // Try parsing the base as a ref string.
    PatchSet.Id basePatchSetId = PatchSet.Id.fromRef(base);
    if (basePatchSetId != null) {
        Change.Id baseChangeId = basePatchSetId.getParentKey();
        ChangeControl baseCtl = controlFor(rsrc, baseChangeId);
        if (baseCtl != null) {
            return Base.create(controlFor(rsrc, basePatchSetId.getParentKey()), psUtil.get(db, baseCtl.getNotes(), basePatchSetId));
        }
    }
    // Try parsing base as a change number (assume current patch set).
    Integer baseChangeId = Ints.tryParse(base);
    if (baseChangeId != null) {
        ChangeControl baseCtl = controlFor(rsrc, new Change.Id(baseChangeId));
        if (baseCtl != null) {
            return Base.create(baseCtl, psUtil.current(db, baseCtl.getNotes()));
        }
    }
    // Try parsing as SHA-1.
    Base ret = null;
    for (ChangeData cd : queryProvider.get().byProjectCommit(rsrc.getProject(), base)) {
        for (PatchSet ps : cd.patchSets()) {
            if (!ps.getRevision().matches(base)) {
                continue;
            }
            if (ret == null || ret.patchSet().getId().get() < ps.getId().get()) {
                ret = Base.create(rsrc.getControl().getProjectControl().controlFor(cd.notes()), ps);
            }
        }
    }
    return ret;
}
Also used : ChangeControl(com.google.gerrit.server.project.ChangeControl) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) ChangeData(com.google.gerrit.server.query.change.ChangeData) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 57 with ReviewDb

use of com.google.gerrit.reviewdb.server.ReviewDb in project gerrit by GerritCodeReview.

the class DraftCommentNotes method rebuildAndOpen.

private LoadHandle rebuildAndOpen(Repository repo) throws NoSuchChangeException, IOException {
    Timer1.Context timer = args.metrics.autoRebuildLatency.start(CHANGES);
    try {
        Change.Id cid = getChangeId();
        ReviewDb db = args.db.get();
        ChangeRebuilder rebuilder = args.rebuilder.get();
        NoteDbUpdateManager.Result r;
        try (NoteDbUpdateManager manager = rebuilder.stage(db, cid)) {
            if (manager == null) {
                // May be null in tests.
                return super.openHandle(repo);
            }
            r = manager.stageAndApplyDelta(change);
            try {
                rebuilder.execute(db, cid, manager);
                repo.scanForRepoChanges();
            } catch (OrmException | IOException e) {
                // See ChangeNotes#rebuildAndOpen.
                log.debug("Rebuilding change {} via drafts failed: {}", getChangeId(), e.getMessage());
                args.metrics.autoRebuildFailureCount.increment(CHANGES);
                checkNotNull(r.staged());
                return LoadHandle.create(ChangeNotesCommit.newStagedRevWalk(repo, r.staged().allUsersObjects()), draftsId(r));
            }
        }
        return LoadHandle.create(ChangeNotesCommit.newRevWalk(repo), draftsId(r));
    } catch (NoSuchChangeException e) {
        return super.openHandle(repo);
    } catch (OrmException e) {
        throw new IOException(e);
    } finally {
        log.debug("Rebuilt change {} in {} in {} ms via drafts", getChangeId(), change != null ? "project " + change.getProject() : "unknown project", TimeUnit.MILLISECONDS.convert(timer.stop(), TimeUnit.NANOSECONDS));
    }
}
Also used : NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) OrmException(com.google.gwtorm.server.OrmException) Change(com.google.gerrit.reviewdb.client.Change) IOException(java.io.IOException) ChangeRebuilder(com.google.gerrit.server.notedb.rebuild.ChangeRebuilder) Timer1(com.google.gerrit.metrics.Timer1) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Example 58 with ReviewDb

use of com.google.gerrit.reviewdb.server.ReviewDb in project gerrit by GerritCodeReview.

the class NoteDbModule method configure.

@Override
public void configure() {
    factory(ChangeUpdate.Factory.class);
    factory(ChangeDraftUpdate.Factory.class);
    factory(DeleteCommentRewriter.Factory.class);
    factory(DraftCommentNotes.Factory.class);
    factory(RobotCommentUpdate.Factory.class);
    factory(RobotCommentNotes.Factory.class);
    factory(NoteDbUpdateManager.Factory.class);
    if (!useTestBindings) {
        install(ChangeNotesCache.module());
        if (cfg.getBoolean("noteDb", null, "testRebuilderWrapper", false)) {
            // Yes, another variety of test bindings with a different way of
            // configuring it.
            bind(ChangeRebuilder.class).to(TestChangeRebuilderWrapper.class);
        } else {
            bind(ChangeRebuilder.class).to(ChangeRebuilderImpl.class);
        }
    } else {
        bind(ChangeRebuilder.class).toInstance(new ChangeRebuilder(null) {

            @Override
            public Result rebuild(ReviewDb db, Change.Id changeId) {
                return null;
            }

            @Override
            public Result rebuildEvenIfReadOnly(ReviewDb db, Id changeId) {
                return null;
            }

            @Override
            public Result rebuild(NoteDbUpdateManager manager, ChangeBundle bundle) {
                return null;
            }

            @Override
            public NoteDbUpdateManager stage(ReviewDb db, Change.Id changeId) {
                return null;
            }

            @Override
            public Result execute(ReviewDb db, Change.Id changeId, NoteDbUpdateManager manager) {
                return null;
            }

            @Override
            public void buildUpdates(NoteDbUpdateManager manager, ChangeBundle bundle) {
            // Do nothing.
            }

            @Override
            public void rebuildReviewDb(ReviewDb db, Project.NameKey project, Id changeId) {
            // Do nothing.
            }
        });
        bind(new TypeLiteral<Cache<ChangeNotesCache.Key, ChangeNotesState>>() {
        }).annotatedWith(Names.named(ChangeNotesCache.CACHE_NAME)).toInstance(CacheBuilder.newBuilder().<ChangeNotesCache.Key, ChangeNotesState>build());
    }
}
Also used : Change(com.google.gerrit.reviewdb.client.Change) Result(com.google.gerrit.server.notedb.NoteDbUpdateManager.Result) Project(com.google.gerrit.reviewdb.client.Project) Id(com.google.gerrit.reviewdb.client.Change.Id) Id(com.google.gerrit.reviewdb.client.Change.Id) ChangeRebuilder(com.google.gerrit.server.notedb.rebuild.ChangeRebuilder) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) Cache(com.google.common.cache.Cache)

Example 59 with ReviewDb

use of com.google.gerrit.reviewdb.server.ReviewDb in project gerrit by GerritCodeReview.

the class AbstractChangeNotesTest method setUp.

@Before
public void setUp() throws Exception {
    setTimeForTesting();
    serverIdent = new PersonIdent("Gerrit Server", "noreply@gerrit.com", TimeUtil.nowTs(), TZ);
    project = new Project.NameKey("test-project");
    repoManager = new InMemoryRepositoryManager();
    repo = repoManager.createRepository(project);
    tr = new TestRepository<>(repo);
    rw = tr.getRevWalk();
    accountCache = new FakeAccountCache();
    Account co = new Account(new Account.Id(1), TimeUtil.nowTs());
    co.setFullName("Change Owner");
    co.setPreferredEmail("change@owner.com");
    accountCache.put(co);
    Account ou = new Account(new Account.Id(2), TimeUtil.nowTs());
    ou.setFullName("Other Account");
    ou.setPreferredEmail("other@account.com");
    accountCache.put(ou);
    injector = Guice.createInjector(new FactoryModule() {

        @Override
        public void configure() {
            install(new GitModule());
            install(NoteDbModule.forTest(testConfig));
            bind(AllUsersName.class).toProvider(AllUsersNameProvider.class);
            bind(String.class).annotatedWith(GerritServerId.class).toInstance("gerrit");
            bind(NotesMigration.class).toInstance(MIGRATION);
            bind(GitRepositoryManager.class).toInstance(repoManager);
            bind(ProjectCache.class).toProvider(Providers.<ProjectCache>of(null));
            bind(CapabilityControl.Factory.class).toProvider(Providers.<CapabilityControl.Factory>of(null));
            bind(Config.class).annotatedWith(GerritServerConfig.class).toInstance(testConfig);
            bind(String.class).annotatedWith(AnonymousCowardName.class).toProvider(AnonymousCowardNameProvider.class);
            bind(String.class).annotatedWith(CanonicalWebUrl.class).toInstance("http://localhost:8080/");
            bind(Boolean.class).annotatedWith(DisableReverseDnsLookup.class).toInstance(Boolean.FALSE);
            bind(Realm.class).to(FakeRealm.class);
            bind(GroupBackend.class).to(SystemGroupBackend.class).in(SINGLETON);
            bind(AccountCache.class).toInstance(accountCache);
            bind(PersonIdent.class).annotatedWith(GerritPersonIdent.class).toInstance(serverIdent);
            bind(GitReferenceUpdated.class).toInstance(GitReferenceUpdated.DISABLED);
            bind(MetricMaker.class).to(DisabledMetricMaker.class);
            bind(ReviewDb.class).toProvider(Providers.<ReviewDb>of(null));
        }
    });
    injector.injectMembers(this);
    repoManager.createRepository(allUsers);
    changeOwner = userFactory.create(co.getId());
    otherUser = userFactory.create(ou.getId());
    otherUserId = otherUser.getAccountId();
    internalUser = new InternalUser(null);
}
Also used : MetricMaker(com.google.gerrit.metrics.MetricMaker) DisabledMetricMaker(com.google.gerrit.metrics.DisabledMetricMaker) Account(com.google.gerrit.reviewdb.client.Account) InMemoryRepositoryManager(com.google.gerrit.testutil.InMemoryRepositoryManager) CapabilityControl(com.google.gerrit.server.account.CapabilityControl) InternalUser(com.google.gerrit.server.InternalUser) GerritServerId(com.google.gerrit.server.config.GerritServerId) DisableReverseDnsLookup(com.google.gerrit.server.config.DisableReverseDnsLookup) GitReferenceUpdated(com.google.gerrit.server.extensions.events.GitReferenceUpdated) FakeAccountCache(com.google.gerrit.testutil.FakeAccountCache) SystemGroupBackend(com.google.gerrit.server.group.SystemGroupBackend) FakeRealm(com.google.gerrit.server.account.FakeRealm) Realm(com.google.gerrit.server.account.Realm) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) AnonymousCowardName(com.google.gerrit.server.config.AnonymousCowardName) CanonicalWebUrl(com.google.gerrit.server.config.CanonicalWebUrl) FactoryModule(com.google.gerrit.extensions.config.FactoryModule) GitModule(com.google.gerrit.server.git.GitModule) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) TestNotesMigration(com.google.gerrit.testutil.TestNotesMigration) FakeAccountCache(com.google.gerrit.testutil.FakeAccountCache) AccountCache(com.google.gerrit.server.account.AccountCache) Project(com.google.gerrit.reviewdb.client.Project) ProjectCache(com.google.gerrit.server.project.ProjectCache) PersonIdent(org.eclipse.jgit.lib.PersonIdent) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) AllUsersName(com.google.gerrit.server.config.AllUsersName) Before(org.junit.Before)

Example 60 with ReviewDb

use of com.google.gerrit.reviewdb.server.ReviewDb in project gerrit by GerritCodeReview.

the class CommentsUtil method changesWithDraftsByAuthor.

// To be used only by HasDraftByLegacyPredicate.
@Deprecated
public List<Change.Id> changesWithDraftsByAuthor(ReviewDb db, Account.Id author) throws OrmException {
    if (!migration.readChanges()) {
        return FluentIterable.from(db.patchComments().draftByAuthor(author)).transform(plc -> plc.getPatchSetId().getParentKey()).toList();
    }
    List<Change.Id> changes = new ArrayList<>();
    try (Repository repo = repoManager.openRepository(allUsers)) {
        for (String refName : repo.getRefDatabase().getRefs(RefNames.REFS_DRAFT_COMMENTS).keySet()) {
            Account.Id accountId = Account.Id.fromRefSuffix(refName);
            Change.Id changeId = Change.Id.fromRefPart(refName);
            if (accountId == null || changeId == null) {
                continue;
            }
            changes.add(changeId);
        }
    } catch (IOException e) {
        throw new OrmException(e);
    }
    return changes;
}
Also used : AllUsersName(com.google.gerrit.server.config.AllUsersName) PatchLineComment(com.google.gerrit.reviewdb.client.PatchLineComment) OrmException(com.google.gwtorm.server.OrmException) CommentInfo(com.google.gerrit.extensions.common.CommentInfo) Inject(com.google.inject.Inject) ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) PrimaryStorage(com.google.gerrit.server.notedb.NoteDbChangeState.PrimaryStorage) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Comment(com.google.gerrit.reviewdb.client.Comment) BatchUpdateReviewDb(com.google.gerrit.server.update.BatchUpdateReviewDb) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) FluentIterable(com.google.common.collect.FluentIterable) Map(java.util.Map) Side(com.google.gerrit.extensions.client.Side) Collectors.toSet(java.util.stream.Collectors.toSet) Predicate(java.util.function.Predicate) Collection(java.util.Collection) Streams(com.google.common.collect.Streams) ComparisonChain(com.google.common.collect.ComparisonChain) NullProgressMonitor(org.eclipse.jgit.lib.NullProgressMonitor) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate) Status(com.google.gerrit.reviewdb.client.PatchLineComment.Status) RefNames(com.google.gerrit.reviewdb.client.RefNames) Ref(org.eclipse.jgit.lib.Ref) Optional(java.util.Optional) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) NotesMigration(com.google.gerrit.server.notedb.NotesMigration) RobotComment(com.google.gerrit.reviewdb.client.RobotComment) Singleton(com.google.inject.Singleton) PatchListNotAvailableException(com.google.gerrit.server.patch.PatchListNotAvailableException) Iterables(com.google.common.collect.Iterables) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb) GerritServerId(com.google.gerrit.server.config.GerritServerId) Change(com.google.gerrit.reviewdb.client.Change) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) Patch(com.google.gerrit.reviewdb.client.Patch) ArrayList(java.util.ArrayList) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) ResultSet(com.google.gwtorm.server.ResultSet) Account(com.google.gerrit.reviewdb.client.Account) ChangeContext(com.google.gerrit.server.update.ChangeContext) PatchListCache(com.google.gerrit.server.patch.PatchListCache) ReviewDbUtil(com.google.gerrit.reviewdb.server.ReviewDbUtil) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) IOException(java.io.IOException) ObjectId(org.eclipse.jgit.lib.ObjectId) Collectors.toList(java.util.stream.Collectors.toList) ChangeUpdate(com.google.gerrit.server.notedb.ChangeUpdate) Ordering(com.google.common.collect.Ordering) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) PUBLISHED(com.google.gerrit.reviewdb.client.PatchLineComment.Status.PUBLISHED) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Collections(java.util.Collections) Repository(org.eclipse.jgit.lib.Repository) Account(com.google.gerrit.reviewdb.client.Account) Repository(org.eclipse.jgit.lib.Repository) OrmException(com.google.gwtorm.server.OrmException) ArrayList(java.util.ArrayList) GerritServerId(com.google.gerrit.server.config.GerritServerId) ObjectId(org.eclipse.jgit.lib.ObjectId) Change(com.google.gerrit.reviewdb.client.Change) IOException(java.io.IOException)

Aggregations

ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)63 OrmException (com.google.gwtorm.server.OrmException)25 Change (com.google.gerrit.reviewdb.client.Change)21 Account (com.google.gerrit.reviewdb.client.Account)17 ArrayList (java.util.ArrayList)14 IOException (java.io.IOException)12 GitRepositoryManager (com.google.gerrit.server.git.GitRepositoryManager)10 Repository (org.eclipse.jgit.lib.Repository)9 Project (com.google.gerrit.reviewdb.client.Project)8 GerritPersonIdent (com.google.gerrit.server.GerritPersonIdent)8 Provider (com.google.inject.Provider)8 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)7 Inject (com.google.inject.Inject)7 AllUsersName (com.google.gerrit.server.config.AllUsersName)6 ChangeControl (com.google.gerrit.server.project.ChangeControl)6 PersonIdent (org.eclipse.jgit.lib.PersonIdent)6 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)5 AccountGroupMember (com.google.gerrit.reviewdb.client.AccountGroupMember)5 GerritServerConfig (com.google.gerrit.server.config.GerritServerConfig)5 GitReferenceUpdated (com.google.gerrit.server.extensions.events.GitReferenceUpdated)5