@Test public void create_ad_hoc_rule_from_scanner_report() { OrganizationDto organization = db.organizations().insert(); NewAdHocRule addHocRule = new NewAdHocRule(ScannerReport.AdHocRule.newBuilder() .setEngineId("eslint") .setRuleId("no-cond-assign") .setName("No condition assigned") .setDescription("A description") .setSeverity(Constants.Severity.BLOCKER) .setType(ScannerReport.IssueType.BUG) .build()); RuleDto rule = underTest.persistAndIndex(dbSession, addHocRule, organization); assertThat(rule).isNotNull(); assertThat(rule.isExternal()).isTrue(); assertThat(rule.isAdHoc()).isTrue(); assertThat(rule.getId()).isGreaterThan(0); assertThat(rule.getKey()).isEqualTo(RuleKey.of("external_eslint", "no-cond-assign")); assertThat(rule.getName()).isEqualTo("eslint:no-cond-assign"); assertThat(rule.getDescription()).isNull(); assertThat(rule.getSeverity()).isNull(); assertThat(rule.getType()).isEqualTo(0); assertThat(rule.getMetadata().getAdHocName()).isEqualTo("No condition assigned"); assertThat(rule.getMetadata().getAdHocDescription()).isEqualTo("A description"); assertThat(rule.getMetadata().getAdHocSeverity()).isEqualTo(Severity.BLOCKER); assertThat(rule.getMetadata().getAdHocType()).isEqualTo(RuleType.BUG.getDbConstant()); }
private void updateAdHocRules() { adHocRuleEditor.setText(""); StringBuilder builder = new StringBuilder(); try (CloseableIterator<ScannerReport.AdHocRule> adHocRuleCloseableIterator = reader.readAdHocRules()) { while (adHocRuleCloseableIterator.hasNext()) { builder.append(adHocRuleCloseableIterator.next().toString()).append("\n"); } adHocRuleEditor.setText(builder.toString()); } }
public void appendAdHocRule(ScannerReport.AdHocRule adHocRule) { File file = fileStructure.adHocRules(); try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true))) { adHocRule.writeDelimitedTo(out); } catch (Exception e) { throw ContextException.of("Unable to write ad hoc rule", e).addContext("file", file); } }
@Override public void store(DefaultAdHocRule adHocRule) { ScannerReportWriter writer = reportPublisher.getWriter(); final ScannerReport.AdHocRule.Builder builder = ScannerReport.AdHocRule.newBuilder(); builder.setEngineId(adHocRule.engineId()); builder.setRuleId(adHocRule.ruleId()); builder.setName(adHocRule.name()); String description = adHocRule.description(); if (description != null) { builder.setDescription(description); } builder.setSeverity(Constants.Severity.valueOf(adHocRule.severity().name())); builder.setType(ScannerReport.IssueType.valueOf(adHocRule.type().name())); writer.appendAdHocRule(builder.build()); }
private void updateAdHocRules() { adHocRuleEditor.setText(""); StringBuilder builder = new StringBuilder(); try (CloseableIterator<ScannerReport.AdHocRule> adHocRuleCloseableIterator = reader.readAdHocRules()) { while (adHocRuleCloseableIterator.hasNext()) { builder.append(adHocRuleCloseableIterator.next().toString()).append("\n"); } adHocRuleEditor.setText(builder.toString()); } }
public void appendAdHocRule(ScannerReport.AdHocRule adHocRule) { File file = fileStructure.adHocRules(); try (OutputStream out = new BufferedOutputStream(new FileOutputStream(file, true))) { adHocRule.writeDelimitedTo(out); } catch (Exception e) { throw ContextException.of("Unable to write ad hoc rule", e).addContext("file", file); } }
public CloseableIterator<ScannerReport.AdHocRule> readAdHocRules() { File file = fileStructure.adHocRules(); if (!fileExists(file)) { return emptyCloseableIterator(); } return Protobuf.readStream(file, ScannerReport.AdHocRule.parser()); }
while (reportAdHocRule.hasNext()) { ScannerReport.AdHocRule adHocRule = reportAdHocRule.next(); adHocRuleMap.put(RuleKey.of(RuleKey.EXTERNAL_RULE_REPO_PREFIX + adHocRule.getEngineId(), adHocRule.getRuleId()), adHocRule);
@Test public void update_metadata_only() { OrganizationDto organization = db.organizations().insert(); NewAdHocRule addHocRule = new NewAdHocRule(ScannerReport.AdHocRule.newBuilder() .setEngineId("eslint") .setRuleId("no-cond-assign") RuleDto rule = underTest.persistAndIndex(dbSession, addHocRule, organization); long creationDate = rule.getCreatedAt(); NewAdHocRule addHocRuleUpdated = new NewAdHocRule(ScannerReport.AdHocRule.newBuilder() .setEngineId("eslint") .setRuleId("no-cond-assign")
RuleMetadataDto ruleMetadata = db.rules().insertOrUpdateMetadata(rule, organization); RuleDto ruleUpdated = underTest.persistAndIndex(dbSession, new NewAdHocRule(ScannerReport.AdHocRule.newBuilder() .setEngineId("eslint") .setRuleId(rule.getKey().rule())
@Test public void write_adhoc_rule() { // write data ScannerReport.AdHocRule rule = ScannerReport.AdHocRule.newBuilder() .setEngineId("eslint") .setRuleId("123") .setName("Foo") .setDescription("Description") .setSeverity(Constants.Severity.BLOCKER) .setType(ScannerReport.IssueType.BUG) .build(); underTest.appendAdHocRule(rule); File file = underTest.getFileStructure().adHocRules(); assertThat(file).exists().isFile(); try (CloseableIterator<ScannerReport.AdHocRule> read = Protobuf.readStream(file, ScannerReport.AdHocRule.parser())) { assertThat(Iterators.size(read)).isEqualTo(1); } }
@Override public void store(DefaultAdHocRule adHocRule) { ScannerReportWriter writer = reportPublisher.getWriter(); final ScannerReport.AdHocRule.Builder builder = ScannerReport.AdHocRule.newBuilder(); builder.setEngineId(adHocRule.engineId()); builder.setRuleId(adHocRule.ruleId()); builder.setName(adHocRule.name()); String description = adHocRule.description(); if (description != null) { builder.setDescription(description); } builder.setSeverity(Constants.Severity.valueOf(adHocRule.severity().name())); builder.setType(ScannerReport.IssueType.valueOf(adHocRule.type().name())); writer.appendAdHocRule(builder.build()); }
public NewAdHocRule(ScannerReport.AdHocRule ruleFromScannerReport) { Preconditions.checkArgument(isNotBlank(ruleFromScannerReport.getEngineId()), "'engine id' not expected to be null for an ad hoc rule"); Preconditions.checkArgument(isNotBlank(ruleFromScannerReport.getRuleId()), "'rule id' not expected to be null for an ad hoc rule"); Preconditions.checkArgument(isNotBlank(ruleFromScannerReport.getName()), "'name' not expected to be null for an ad hoc rule"); Preconditions.checkArgument(ruleFromScannerReport.getSeverity() != Constants.Severity.UNSET_SEVERITY , "'severity' not expected to be null for an ad hoc rule"); Preconditions.checkArgument(ruleFromScannerReport.getType() != ScannerReport.IssueType.UNSET, "'issue type' not expected to be null for an ad hoc rule"); this.key = RuleKey.of(RuleKey.EXTERNAL_RULE_REPO_PREFIX + ruleFromScannerReport.getEngineId(), ruleFromScannerReport.getRuleId()); this.engineId = ruleFromScannerReport.getEngineId(); this.ruleId = ruleFromScannerReport.getRuleId(); this.name = ruleFromScannerReport.getName(); this.description = trimToNull(ruleFromScannerReport.getDescription()); this.severity = ruleFromScannerReport.getSeverity().name(); this.ruleType = RuleType.valueOf(ruleFromScannerReport.getType().name()); this.hasDetails = true; }
public CloseableIterator<ScannerReport.AdHocRule> readAdHocRules() { File file = fileStructure.adHocRules(); if (!fileExists(file)) { return emptyCloseableIterator(); } return Protobuf.readStream(file, ScannerReport.AdHocRule.parser()); }