Search in sources :

Example 1 with ChangeEvent

use of org.commonjava.auditquery.history.ChangeEvent in project indy by Commonjava.

the class RepoChangelogStoreTest method test.

@Test
public void test() throws Exception {
    HostedRepository repo = new HostedRepository(MAVEN_PKG_KEY, newName());
    final StoreKey hostedKey = repo.getKey();
    repo = client.stores().create(repo, name.getMethodName(), HostedRepository.class);
    repo.setAllowReleases(!repo.isAllowReleases());
    client.stores().update(repo, name.getMethodName());
    repo.setReadonly(true);
    client.stores().update(repo, name.getMethodName());
    List<ChangeEvent> logs = client.module(IndyRepoChangelogClientModule.class).getByStoreKey(repo.getKey());
    final AtomicInteger createCount = new AtomicInteger(0);
    final AtomicInteger updateCount = new AtomicInteger(0);
    logs.forEach(c -> {
        assertThat(c.getStoreKey(), equalTo(hostedKey.toString()));
        if (c.getChangeType() == ChangeType.CREATE) {
            createCount.getAndIncrement();
        }
        if (c.getChangeType() == ChangeType.UPDATE) {
            updateCount.getAndIncrement();
        }
    });
    assertThat(createCount.get(), equalTo(1));
    assertThat(updateCount.get(), equalTo(2));
    logs = client.module(IndyRepoChangelogClientModule.class).getAll();
    final AtomicInteger updateCount2 = new AtomicInteger(0);
    final AtomicInteger testRepoCount = new AtomicInteger(0);
    logs.forEach(c -> {
        if (c.getChangeType() == ChangeType.UPDATE) {
            updateCount2.getAndIncrement();
        }
        if (c.getStoreKey().equals(hostedKey.toString())) {
            testRepoCount.getAndIncrement();
        }
    });
    assertThat(updateCount2.get(), equalTo(2));
    assertThat(testRepoCount.get(), equalTo(3));
}
Also used : IndyRepoChangelogClientModule(org.commonjava.indy.changelog.client.IndyRepoChangelogClientModule) ChangeEvent(org.commonjava.auditquery.history.ChangeEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) StoreKey(org.commonjava.indy.model.core.StoreKey) HostedRepository(org.commonjava.indy.model.core.HostedRepository) AbstractIndyFunctionalTest(org.commonjava.indy.ftest.core.AbstractIndyFunctionalTest) Test(org.junit.Test)

Example 2 with ChangeEvent

use of org.commonjava.auditquery.history.ChangeEvent in project indy by Commonjava.

the class RepoChangelogStoreDisableTest method test.

@Test
public void test() throws Exception {
    HostedRepository repo = new HostedRepository(MAVEN_PKG_KEY, newName());
    final StoreKey hostedKey = repo.getKey();
    repo = client.stores().create(repo, name.getMethodName(), HostedRepository.class);
    repo.setAllowReleases(!repo.isAllowReleases());
    client.stores().update(repo, name.getMethodName());
    repo.setReadonly(true);
    client.stores().update(repo, name.getMethodName());
    IndyRepoChangelogClientModule repoChangelogClientModule = client.module(IndyRepoChangelogClientModule.class);
    List<ChangeEvent> logs = null;
    try {
        logs = repoChangelogClientModule.getByStoreKey(repo.getKey());
    } catch (IndyClientException e) {
        assertThat(e.getStatusCode(), equalTo(404));
    }
    assertNotNull(logs);
    assertTrue(logs.isEmpty());
    try {
        repoChangelogClientModule.getAll();
    } catch (IndyClientException e) {
        assertThat(e.getStatusCode(), equalTo(404));
    }
    assertNotNull(logs);
    assertTrue(logs.isEmpty());
}
Also used : IndyRepoChangelogClientModule(org.commonjava.indy.changelog.client.IndyRepoChangelogClientModule) ChangeEvent(org.commonjava.auditquery.history.ChangeEvent) IndyClientException(org.commonjava.indy.client.core.IndyClientException) StoreKey(org.commonjava.indy.model.core.StoreKey) HostedRepository(org.commonjava.indy.model.core.HostedRepository) AbstractIndyFunctionalTest(org.commonjava.indy.ftest.core.AbstractIndyFunctionalTest) Test(org.junit.Test)

Example 3 with ChangeEvent

use of org.commonjava.auditquery.history.ChangeEvent in project indy by Commonjava.

the class RepoChangeHandler method handleRepoChange.

private void handleRepoChange(DiffStoreFetcher fetcher, boolean isDelete) {
    if (!config.isEnabled()) {
        logger.info("Repository changelog module is not enabled. Will ignore all change logs for propagation.");
        return;
    }
    ChangeSummary changeSummary = fetcher.getSummary();
    Collection<ArtifactStore> stores = fetcher.getStores();
    String user = ChangeSummary.SYSTEM_USER;
    String summary = "";
    String version = "";
    Date timeStamp = new Date();
    if (changeSummary != null) {
        if (changeSummary.getUser() != null) {
            user = changeSummary.getUser();
        }
        if (changeSummary.getSummary() != null) {
            summary = changeSummary.getSummary();
        }
        if (changeSummary.getRevisionId() != null) {
            version = changeSummary.getRevisionId();
        } else {
            // FIXME: we need version not null here to let it be a key, not sure if timestamp is good here
            version = String.format("%s", timeStamp.getTime());
        }
    }
    for (ArtifactStore store : stores) {
        try {
            ArtifactStore origin = fetcher.getOriginal(store);
            ArtifactStore changed = fetcher.getChanged(store);
            String patchString = diffRepoChanges(changed, origin);
            ChangeEvent changeLog = new ChangeEvent();
            changeLog.setEventId(UUID.randomUUID().toString().replace("-", ""));
            changeLog.setStoreKey(store.getKey().toString());
            changeLog.setChangeTime(new Date());
            changeLog.setDiffContent(patchString);
            if (isDelete) {
                changeLog.setChangeType(ChangeType.DELETE);
            } else {
                changeLog.setChangeType(origin == null ? ChangeType.CREATE : ChangeType.UPDATE);
            }
            changeLog.setUser(user);
            changeLog.setSummary(summary);
            changeLog.setVersion(version);
            String key = changeLog.getStoreKey() + "_" + changeLog.getVersion();
            repoChangelogCache.put(key, changeLog);
        } catch (JsonProcessingException | DiffException e) {
            String error = String.format("Something wrong happened when doing repo change log generation for store %s", store.getKey());
            logger.error(error, e);
        }
    }
}
Also used : ChangeEvent(org.commonjava.auditquery.history.ChangeEvent) DiffException(com.github.difflib.algorithm.DiffException) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) Date(java.util.Date)

Aggregations

ChangeEvent (org.commonjava.auditquery.history.ChangeEvent)3 IndyRepoChangelogClientModule (org.commonjava.indy.changelog.client.IndyRepoChangelogClientModule)2 AbstractIndyFunctionalTest (org.commonjava.indy.ftest.core.AbstractIndyFunctionalTest)2 HostedRepository (org.commonjava.indy.model.core.HostedRepository)2 StoreKey (org.commonjava.indy.model.core.StoreKey)2 Test (org.junit.Test)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 DiffException (com.github.difflib.algorithm.DiffException)1 Date (java.util.Date)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 ChangeSummary (org.commonjava.indy.audit.ChangeSummary)1 IndyClientException (org.commonjava.indy.client.core.IndyClientException)1 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)1