Search in sources :

Example 6 with ValidationRuleSet

use of org.commonjava.indy.promote.model.ValidationRuleSet 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 7 with ValidationRuleSet

use of org.commonjava.indy.promote.model.ValidationRuleSet in project indy by Commonjava.

the class ValidationRuleParser method parseRuleSet.

public ValidationRuleSet parseRuleSet(final String spec, final String scriptName) throws PromotionValidationException {
    if (spec == null) {
        return null;
    }
    Logger logger = LoggerFactory.getLogger(getClass());
    logger.debug("Parsing rule-set from: {} with content:\n{}\n", scriptName, spec);
    try {
        ValidationRuleSet rs = objectMapper.readValue(spec, ValidationRuleSet.class);
        rs.setName(scriptName);
        return rs;
    } catch (final IOException e) {
        throw new PromotionValidationException("[PROMOTE] Cannot load validation rule-set from: {} as an instance of: {}. Reason: {}", e, scriptName, ValidationRule.class.getSimpleName(), e.getMessage());
    }
}
Also used : IOException(java.io.IOException) Logger(org.slf4j.Logger) ValidationRuleSet(org.commonjava.indy.promote.model.ValidationRuleSet)

Example 8 with ValidationRuleSet

use of org.commonjava.indy.promote.model.ValidationRuleSet in project indy by Commonjava.

the class PromoteValidationsManager method parseRules.

public synchronized void parseRules() throws PromotionValidationException {
    if (!config.isEnabled()) {
        this.enabled = false;
        this.ruleMappings = Collections.emptyMap();
        logger.info("Promotion is disabled.");
        return;
    }
    final Map<String, ValidationRuleMapping> ruleMappings = new HashMap<>();
    DataFile dataDir = ffManager.getDataFile(config.getBasedir(), RULES_DIR);
    logger.info("Scanning {} for promotion validation rules...", dataDir);
    if (dataDir.exists()) {
        final DataFile[] scripts = dataDir.listFiles((pathname) -> {
            logger.info("Checking for promote validation rule script in: {}", pathname);
            return pathname.getName().endsWith(".groovy");
        });
        if (scripts != null && scripts.length > 0) {
            for (final DataFile script : scripts) {
                logger.info("Reading promotion validation rule from: {}", script);
                final ValidationRuleMapping rule = ruleParser.parseRule(script);
                if (rule != null) {
                    ruleMappings.put(rule.getName(), rule);
                }
            }
        } else {
            logger.warn("No rule script file was defined for promotion: no rule script found in {} directory", RULES_DIR);
        }
    } else {
        logger.warn("No rule script file was defined for promotion: {} directory not exists", RULES_DIR);
    }
    this.ruleMappings = ruleMappings;
    Map<String, ValidationRuleSet> ruleSets = new HashMap<>();
    dataDir = ffManager.getDataFile(config.getBasedir(), RULES_SETS_DIR);
    logger.info("Scanning {} for promotion validation rule-set mappings...", dataDir);
    if (dataDir.exists()) {
        final DataFile[] scripts = dataDir.listFiles((pathname) -> {
            logger.info("Checking for promotion rule-set in: {}", pathname);
            return pathname.getName().endsWith(".json");
        });
        if (scripts != null && scripts.length > 0) {
            for (final DataFile script : scripts) {
                logger.info("Reading promotion validation rule-set from: {}", script);
                final ValidationRuleSet set = ruleParser.parseRuleSet(script);
                if (set != null) {
                    ruleSets.put(script.getName(), set);
                }
            }
        } else {
            logger.warn("No rule-set json file was defined for promotion: no json file found in {} directory", RULES_SETS_DIR);
        }
    } else {
        logger.warn("No rule-set json file was defined for promotion: {} directory not exists", RULES_SETS_DIR);
    }
    this.ruleSets = ruleSets;
    this.enabled = true;
}
Also used : DataFile(org.commonjava.indy.subsys.datafile.DataFile) HashMap(java.util.HashMap) ValidationRuleMapping(org.commonjava.indy.promote.validate.model.ValidationRuleMapping) ValidationRuleSet(org.commonjava.indy.promote.model.ValidationRuleSet)

Example 9 with ValidationRuleSet

use of org.commonjava.indy.promote.model.ValidationRuleSet in project indy by Commonjava.

the class PromoteValidationsManagerTest method testRuleSetParseAndMatchOnStoreKey.

@Test
public void testRuleSetParseAndMatchOnStoreKey() throws Exception {
    DataFile dataFile = fileManager.getDataFile("promote/rule-sets/test.json");
    dataFile.writeString("{\"name\":\"test\",\"storeKeyPattern\":\".*\"}", new ChangeSummary(ChangeSummary.SYSTEM_USER, "writing test data"));
    promoteValidations = new PromoteValidationsManager(fileManager, config, parser);
    ValidationRuleSet ruleSet = promoteValidations.getRuleSetMatching(new StoreKey(StoreType.hosted, "repo"));
    assertThat(ruleSet, notNullValue());
    assertThat(ruleSet.matchesKey("hosted:repo"), equalTo(true));
}
Also used : DataFile(org.commonjava.indy.subsys.datafile.DataFile) ChangeSummary(org.commonjava.indy.audit.ChangeSummary) ValidationRuleSet(org.commonjava.indy.promote.model.ValidationRuleSet) StoreKey(org.commonjava.indy.model.core.StoreKey) Test(org.junit.Test)

Example 10 with ValidationRuleSet

use of org.commonjava.indy.promote.model.ValidationRuleSet in project indy by Commonjava.

the class ProjectArtifactsRuleTest method getRuleSet.

protected ValidationRuleSet getRuleSet() {
    ValidationRuleSet ruleSet = new ValidationRuleSet();
    ruleSet.setName("test");
    ruleSet.setStoreKeyPattern("group:target");
    ruleSet.setRuleNames(Collections.singletonList(getRuleScriptFile()));
    ruleSet.setValidationParameters(Collections.singletonMap("classifierAndTypeSet", "sources:jar,javadoc:jar"));
    return ruleSet;
}
Also used : ValidationRuleSet(org.commonjava.indy.promote.model.ValidationRuleSet)

Aggregations

ValidationRuleSet (org.commonjava.indy.promote.model.ValidationRuleSet)16 HashMap (java.util.HashMap)2 ChangeSummary (org.commonjava.indy.audit.ChangeSummary)2 StoreKey (org.commonjava.indy.model.core.StoreKey)2 ValidationRuleMapping (org.commonjava.indy.promote.validate.model.ValidationRuleMapping)2 DataFile (org.commonjava.indy.subsys.datafile.DataFile)2 Test (org.junit.Test)2 Logger (org.slf4j.Logger)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 File (java.io.File)1 IOException (java.io.IOException)1 IndyDataException (org.commonjava.indy.data.IndyDataException)1 ArtifactStore (org.commonjava.indy.model.core.ArtifactStore)1 ValidationRequest (org.commonjava.indy.promote.validate.model.ValidationRequest)1 EventMetadata (org.commonjava.maven.galley.event.EventMetadata)1