Search in sources :

Example 1 with ChangeIndexedListener

use of com.google.gerrit.extensions.events.ChangeIndexedListener in project gerrit by GerritCodeReview.

the class ProjectIT method reindexChangesOfProject.

@Test
public void reindexChangesOfProject() throws Exception {
    Change.Id changeId1 = createChange().getChange().getId();
    Change.Id changeId2 = createChange().getChange().getId();
    ChangeIndexedListener changeIndexedListener = mock(ChangeIndexedListener.class);
    try (Registration registration = extensionRegistry.newRegistration().add(changeIndexedListener)) {
        gApi.projects().name(project.get()).indexChanges();
        verify(changeIndexedListener, times(1)).onChangeScheduledForIndexing(project.get(), changeId1.get());
        verify(changeIndexedListener, times(1)).onChangeScheduledForIndexing(project.get(), changeId2.get());
    }
}
Also used : Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) ChangeIndexedListener(com.google.gerrit.extensions.events.ChangeIndexedListener) Change(com.google.gerrit.entities.Change) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 2 with ChangeIndexedListener

use of com.google.gerrit.extensions.events.ChangeIndexedListener in project gerrit by GerritCodeReview.

the class AbstractSubmit method submitSchedulesOpenChangesOfSameBranchForReindexing.

@Test
@GerritConfig(name = "change.mergeabilityComputationBehavior", value = "API_REF_UPDATED_AND_CHANGE_REINDEX")
public void submitSchedulesOpenChangesOfSameBranchForReindexing() throws Throwable {
    // Create a merged change.
    PushOneCommit push = pushFactory.create(admin.newIdent(), testRepo, "Merged Change", "foo.txt", "foo");
    PushOneCommit.Result mergedChange = push.to("refs/for/master");
    mergedChange.assertOkStatus();
    approve(mergedChange.getChangeId());
    submit(mergedChange.getChangeId());
    // Create some open changes.
    PushOneCommit.Result change1 = createChange();
    PushOneCommit.Result change2 = createChange();
    PushOneCommit.Result change3 = createChange();
    // Create a branch with one open change.
    BranchInput in = new BranchInput();
    in.revision = projectOperations.project(project).getHead("master").name();
    gApi.projects().name(project.get()).branch("dev").create(in);
    PushOneCommit.Result changeOtherBranch = createChange("refs/for/dev");
    ChangeIndexedListener changeIndexedListener = mock(ChangeIndexedListener.class);
    try (Registration registration = extensionRegistry.newRegistration().add(changeIndexedListener)) {
        // submit a change, this should trigger asynchronous reindexing of the open changes on the
        // same branch
        approve(change1.getChangeId());
        submit(change1.getChangeId());
        assertThat(gApi.changes().id(change1.getChangeId()).get().status).isEqualTo(ChangeStatus.MERGED);
        // on submit the change that is submitted gets reindexed synchronously
        verify(changeIndexedListener, atLeast(1)).onChangeScheduledForIndexing(project.get(), change1.getChange().getId().get());
        verify(changeIndexedListener, atLeast(1)).onChangeIndexed(project.get(), change1.getChange().getId().get());
        // the open changes on the same branch get reindexed asynchronously
        verify(changeIndexedListener, times(1)).onChangeScheduledForIndexing(project.get(), change2.getChange().getId().get());
        verify(changeIndexedListener, times(1)).onChangeScheduledForIndexing(project.get(), change3.getChange().getId().get());
        // merged changes don't get reindexed
        verify(changeIndexedListener, times(0)).onChangeScheduledForIndexing(project.get(), mergedChange.getChange().getId().get());
        // open changes on other branches don't get reindexed
        verify(changeIndexedListener, times(0)).onChangeScheduledForIndexing(project.get(), changeOtherBranch.getChange().getId().get());
    }
}
Also used : Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) ChangeIndexedListener(com.google.gerrit.extensions.events.ChangeIndexedListener) BranchInput(com.google.gerrit.extensions.api.projects.BranchInput) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 3 with ChangeIndexedListener

use of com.google.gerrit.extensions.events.ChangeIndexedListener in project gerrit by GerritCodeReview.

the class RevisionIT method mergeable.

@Test
@GerritConfig(name = "change.mergeabilityComputationBehavior", value = "API_REF_UPDATED_AND_CHANGE_REINDEX")
public void mergeable() throws Exception {
    ObjectId initial = repo().exactRef(HEAD).getLeaf().getObjectId();
    PushOneCommit push1 = pushFactory.create(admin.newIdent(), testRepo, PushOneCommit.SUBJECT, PushOneCommit.FILE_NAME, "push 1 content");
    PushOneCommit.Result r1 = push1.to("refs/for/master");
    assertMergeable(r1.getChangeId(), true);
    merge(r1);
    // Reset client HEAD to initial so the new change is a merge conflict.
    testRepo.reset(initial);
    PushOneCommit push2 = pushFactory.create(admin.newIdent(), testRepo, PushOneCommit.SUBJECT, PushOneCommit.FILE_NAME, "new contents");
    PushOneCommit.Result r2 = push2.to("refs/for/master");
    Change.Id id2 = r2.getChange().getId();
    assertMergeable(r2.getChangeId(), false);
    // Search shows change is not mergeable.
    Callable<List<ChangeInfo>> search = () -> gApi.changes().query("is:mergeable change:" + r2.getChangeId()).get();
    assertThat(search.call()).isEmpty();
    // will not reindex any open changes.
    try (Repository repo = repoManager.openRepository(project);
        TestRepository<Repository> tr = new TestRepository<>(repo)) {
        String ref = "refs/heads/master";
        assertThat(repo.exactRef(ref).getObjectId()).isEqualTo(r1.getCommit());
        tr.update(ref, tr.getRevWalk().parseCommit(initial));
        tr.branch(ref).commit().message("Side update").add(PushOneCommit.FILE_NAME, "new contents").create();
    }
    // Search shows change is still not mergeable.
    assertThat(search.call()).isEmpty();
    // Using the API returns the correct value, and reindexes as well.
    CountDownLatch reindexed = new CountDownLatch(1);
    ChangeIndexedListener listener = new ChangeIndexedListener() {

        @Override
        public void onChangeIndexed(String projectName, int id) {
            if (id == id2.get()) {
                reindexed.countDown();
            }
        }

        @Override
        public void onChangeDeleted(int id) {
        }
    };
    try (Registration registration = extensionRegistry.newRegistration().add(listener)) {
        assertMergeable(r2.getChangeId(), true);
        reindexed.await();
    }
    List<ChangeInfo> changes = search.call();
    assertThat(changes).hasSize(1);
    assertThat(changes.get(0).changeId).isEqualTo(r2.getChangeId());
    assertThat(changes.get(0).mergeable).isEqualTo(Boolean.TRUE);
}
Also used : TestRepository(org.eclipse.jgit.junit.TestRepository) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) ObjectId(org.eclipse.jgit.lib.ObjectId) Change(com.google.gerrit.entities.Change) CountDownLatch(java.util.concurrent.CountDownLatch) PushOneCommit(com.google.gerrit.acceptance.PushOneCommit) TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) Registration(com.google.gerrit.acceptance.ExtensionRegistry.Registration) ChangeIndexedListener(com.google.gerrit.extensions.events.ChangeIndexedListener) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) GerritConfig(com.google.gerrit.acceptance.config.GerritConfig) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Aggregations

AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)3 Registration (com.google.gerrit.acceptance.ExtensionRegistry.Registration)3 ChangeIndexedListener (com.google.gerrit.extensions.events.ChangeIndexedListener)3 Test (org.junit.Test)3 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)2 GerritConfig (com.google.gerrit.acceptance.config.GerritConfig)2 Change (com.google.gerrit.entities.Change)2 ImmutableList (com.google.common.collect.ImmutableList)1 BranchInput (com.google.gerrit.extensions.api.projects.BranchInput)1 ChangeInfo (com.google.gerrit.extensions.common.ChangeInfo)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Collectors.toList (java.util.stream.Collectors.toList)1 TestRepository (org.eclipse.jgit.junit.TestRepository)1 ObjectId (org.eclipse.jgit.lib.ObjectId)1 Repository (org.eclipse.jgit.lib.Repository)1