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());
}
}
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());
}
}
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;
}
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));
}
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;
}
Aggregations