Search in sources :

Example 26 with ChangeSummary

use of org.commonjava.indy.audit.ChangeSummary in project indy by Commonjava.

the class GitManager method getChangelog.

public List<ChangeSummary> getChangelog(final File f, final int start, final int length) throws GitSubsystemException {
    return lockAnd(me -> {
        if (length == 0) {
            return Collections.emptyList();
        }
        try {
            final ObjectId oid = repo.resolve(Constants.HEAD);
            final PlotWalk pw = new PlotWalk(repo);
            final RevCommit rc = pw.parseCommit(oid);
            toChangeSummary(rc);
            pw.markStart(rc);
            final String filepath = relativize(f);
            logger.info("Getting changelog for: {} (start: {}, length: {})", filepath, start, length);
            if (!isEmpty(filepath) && !filepath.equals("/")) {
                pw.setTreeFilter(AndTreeFilter.create(PathFilter.create(filepath), TreeFilter.ANY_DIFF));
            } else {
                pw.setTreeFilter(TreeFilter.ANY_DIFF);
            }
            final List<ChangeSummary> changelogs = new ArrayList<ChangeSummary>();
            int count = 0;
            final int stop = length > 0 ? length + 1 : 0;
            RevCommit commit = null;
            while ((commit = pw.next()) != null && (stop < 1 || count < stop)) {
                if (count < start) {
                    count++;
                    continue;
                }
                changelogs.add(toChangeSummary(commit));
                count++;
            }
            if (length < -1) {
                final int remove = (-1 * length) - 1;
                for (int i = 0; i < remove; i++) {
                    changelogs.remove(changelogs.size() - 1);
                }
            }
            return changelogs;
        } catch (RevisionSyntaxException | IOException e) {
            throw new GitSubsystemException("Failed to resolve HEAD commit for: %s. Reason: %s", e, f, e.getMessage());
        }
    });
}
Also used : ObjectId(org.eclipse.jgit.lib.ObjectId) RevisionSyntaxException(org.eclipse.jgit.errors.RevisionSyntaxException) ArrayList(java.util.ArrayList) JoinString(org.commonjava.maven.atlas.ident.util.JoinString) IOException(java.io.IOException) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) PlotWalk(org.eclipse.jgit.revplot.PlotWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 27 with ChangeSummary

use of org.commonjava.indy.audit.ChangeSummary in project indy by Commonjava.

the class PromotionValidator method validate.

public void validate(PromoteRequest request, ValidationResult result, String baseUrl) throws PromotionValidationException {
    ValidationRuleSet set = validationsManager.getRuleSetMatching(request.getTargetKey());
    Logger logger = LoggerFactory.getLogger(getClass());
    if (set != null) {
        logger.debug("Running validation rule-set for promotion: {}", set.getName());
        result.setRuleSet(set.getName());
        List<String> ruleNames = set.getRuleNames();
        if (ruleNames != null && !ruleNames.isEmpty()) {
            final ArtifactStore store = getRequestStore(request, baseUrl);
            try {
                final ValidationRequest req = new ValidationRequest(request, set, validationTools, store);
                for (String ruleRef : ruleNames) {
                    String ruleName = // flatten in case some path fragment leaks in...
                    new File(ruleRef).getName();
                    ValidationRuleMapping rule = validationsManager.getRuleMappingNamed(ruleName);
                    if (rule != null) {
                        try {
                            logger.debug("Running promotion validation rule: {}", rule.getName());
                            String error = rule.getRule().validate(req);
                            if (StringUtils.isNotEmpty(error)) {
                                logger.debug("{} failed", rule.getName());
                                result.addValidatorError(rule.getName(), error);
                            } else {
                                logger.debug("{} succeeded", rule.getName());
                            }
                        } catch (Exception e) {
                            if (e instanceof PromotionValidationException) {
                                throw (PromotionValidationException) e;
                            }
                            throw new PromotionValidationException("Failed to run validation rule: {} for request: {}. Reason: {}", e, rule.getName(), request, e);
                        }
                    }
                }
            } finally {
                if (needTempRepo(request)) {
                    try {
                        final String changeSum = String.format("Removes the temp remote repo [%s] after promote operation.", store);
                        storeDataMgr.deleteArtifactStore(store.getKey(), new ChangeSummary(ChangeSummary.SYSTEM_USER, changeSum), new EventMetadata().set(ContentManager.SUPPRESS_EVENTS, true));
                        logger.info("Promotion temporary repo {} has been deleted for {}", store.getKey(), request.getSource());
                    } catch (IndyDataException e) {
                        logger.warn("StoreDataManager can not remove artifact stores correctly.", e);
                    }
                }
            }
        }
    } else {
        logger.info("No validation rule-sets are defined for: {}", request.getTargetKey());
    }
}
Also used : ValidationRequest(org.commonjava.indy.promote.validate.model.ValidationRequest) Logger(org.slf4j.Logger) IndyDataException(org.commonjava.indy.data.IndyDataException) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) IndyDataException(org.commonjava.indy.data.IndyDataException) ArtifactStore(org.commonjava.indy.model.core.ArtifactStore) ValidationRuleMapping(org.commonjava.indy.promote.validate.model.ValidationRuleMapping) ValidationRuleSet(org.commonjava.indy.promote.model.ValidationRuleSet) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) File(java.io.File)

Example 28 with ChangeSummary

use of org.commonjava.indy.audit.ChangeSummary in project indy by Commonjava.

the class PromotionManagerTest method rollback_PurgeSource_PushTwoArtifactsToHostedRepo_PromoteSuccessThenRollback_VerifyContentInSource.

@Test
public void rollback_PurgeSource_PushTwoArtifactsToHostedRepo_PromoteSuccessThenRollback_VerifyContentInSource() throws Exception {
    final HostedRepository source = new HostedRepository(MAVEN_PKG_KEY, "source");
    storeManager.storeArtifactStore(source, new ChangeSummary(ChangeSummary.SYSTEM_USER, "test setup"), false, true, new EventMetadata());
    final String first = "/first/path";
    final String second = "/second/path";
    contentManager.store(source, first, new ByteArrayInputStream("This is a test".getBytes()), TransferOperation.UPLOAD, new EventMetadata());
    contentManager.store(source, second, new ByteArrayInputStream("This is a test".getBytes()), TransferOperation.UPLOAD, new EventMetadata());
    final HostedRepository target = new HostedRepository(MAVEN_PKG_KEY, "target");
    storeManager.storeArtifactStore(target, new ChangeSummary(ChangeSummary.SYSTEM_USER, "test setup"), false, true, new EventMetadata());
    PathsPromoteResult result = manager.promotePaths(new PathsPromoteRequest(source.getKey(), target.getKey()).setPurgeSource(true), FAKE_BASE_URL);
    assertThat(result.getRequest().getSource(), equalTo(source.getKey()));
    assertThat(result.getRequest().getTarget(), equalTo(target.getKey()));
    Set<String> pending = result.getPendingPaths();
    assertThat(pending == null || pending.isEmpty(), equalTo(true));
    Set<String> completed = result.getCompletedPaths();
    assertThat(completed, notNullValue());
    assertThat(completed.size(), equalTo(2));
    assertThat(result.getError(), nullValue());
    result = manager.rollbackPathsPromote(result);
    assertThat(result.getRequest().getSource(), equalTo(source.getKey()));
    assertThat(result.getRequest().getTarget(), equalTo(target.getKey()));
    completed = result.getCompletedPaths();
    assertThat(completed == null || completed.isEmpty(), equalTo(true));
    pending = result.getPendingPaths();
    assertThat(pending, notNullValue());
    assertThat(pending.size(), equalTo(2));
    assertThat(result.getError(), nullValue());
    Transfer ref = downloadManager.getStorageReference(target, first);
    assertThat(ref.exists(), equalTo(false));
    ref = downloadManager.getStorageReference(target, second);
    assertThat(ref.exists(), equalTo(false));
    ref = downloadManager.getStorageReference(source, first);
    assertThat(ref.exists(), equalTo(true));
    ref = downloadManager.getStorageReference(source, second);
    assertThat(ref.exists(), equalTo(true));
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PathsPromoteResult(org.commonjava.indy.promote.model.PathsPromoteResult) Transfer(org.commonjava.maven.galley.model.Transfer) PathsPromoteRequest(org.commonjava.indy.promote.model.PathsPromoteRequest) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) HostedRepository(org.commonjava.indy.model.core.HostedRepository) EventMetadata(org.commonjava.maven.galley.event.EventMetadata) Test(org.junit.Test)

Example 29 with ChangeSummary

use of org.commonjava.indy.audit.ChangeSummary in project indy by Commonjava.

the class RevisionsManagerTest method commitTwoDifferentFilesAndRetrieveChangelogForOneOfThem_LimitToOldestEvent.

@Test
public void commitTwoDifferentFilesAndRetrieveChangelogForOneOfThem_LimitToOldestEvent() throws Exception {
    lcEvents.fireStarted();
    final DataFile f1 = dfManager.getDataFile("test/foo.txt");
    f1.writeString("this is a test", "UTF-8", new ChangeSummary("test-user", "test for first file."));
    final DataFile f2 = dfManager.getDataFile("test/bar.txt");
    final String testSummary = "test for second file.";
    f2.writeString("this is a test", "UTF-8", new ChangeSummary("test-user", testSummary));
    f2.writeString("this is another test", "UTF-8", new ChangeSummary("test-user", "test (2) for second file."));
    listener.waitForEvents(3);
    final List<ChangeSummary> changeLog = revManager.getDataChangeLog(f2.getPath(), 1, 1);
    assertThat(changeLog, notNullValue());
    assertThat(changeLog.size(), equalTo(1));
    final ChangeSummary summary = changeLog.get(0);
    assertThat(summary.getSummary().startsWith(testSummary), equalTo(true));
}
Also used : DataFile(org.commonjava.indy.subsys.datafile.DataFile) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) Test(org.junit.Test)

Example 30 with ChangeSummary

use of org.commonjava.indy.audit.ChangeSummary in project indy by Commonjava.

the class RevisionsManagerTest method commitOneFileTwice_NoChangeSecondTime_RetrieveOneChangelog.

@Test
public void commitOneFileTwice_NoChangeSecondTime_RetrieveOneChangelog() throws Exception {
    lcEvents.fireStarted();
    revManager.setup();
    final DataFile f1 = dfManager.getDataFile("test/foo.txt");
    f1.writeString("this is a test", "UTF-8", new ChangeSummary("test-user", "test for first write of file."));
    List<DataFileEvent> events = listener.waitForEvents(1);
    System.out.println("Got events:\n  " + join(events, "\n  "));
    f1.writeString("this is a test", "UTF-8", new ChangeSummary("test-user", "test for second write of file."));
    events = listener.waitForEvents(1);
    System.out.println("Got events:\n  " + join(events, "\n  "));
    final List<ChangeSummary> changeLog = revManager.getDataChangeLog(f1.getPath(), 0, -1);
    assertThat(changeLog, notNullValue());
    assertThat(changeLog.size(), equalTo(1));
    assertThat(changeLog.get(0).getSummary().contains("test for first write of file."), equalTo(true));
}
Also used : DataFile(org.commonjava.indy.subsys.datafile.DataFile) DataFileEvent(org.commonjava.indy.subsys.datafile.change.DataFileEvent) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) Test(org.junit.Test)

Aggregations

ChangeSummary (org.commonjava.indy.audit.ChangeSummary)68 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)37 Test (org.junit.Test)29 IndyDataException (org.commonjava.indy.data.IndyDataException)24 DataFile (org.commonjava.indy.subsys.datafile.DataFile)21 RemoteRepository (org.commonjava.indy.model.core.RemoteRepository)17 StoreKey (org.commonjava.indy.model.core.StoreKey)14 IOException (java.io.IOException)11 HostedRepository (org.commonjava.indy.model.core.HostedRepository)11 Group (org.commonjava.indy.model.core.Group)10 File (java.io.File)8 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)8 PathsPromoteRequest (org.commonjava.indy.promote.model.PathsPromoteRequest)8 Transfer (org.commonjava.maven.galley.model.Transfer)8 Logger (org.slf4j.Logger)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 PathsPromoteResult (org.commonjava.indy.promote.model.PathsPromoteResult)7 ArrayList (java.util.ArrayList)6 IndyLifecycleException (org.commonjava.indy.action.IndyLifecycleException)5 BMRules (org.jboss.byteman.contrib.bmunit.BMRules)5