Example #1
0
  BulkChangeResult bulkActivate(
      RuleQuery ruleQuery, QualityProfileKey profileKey, @Nullable String severity) {
    BulkChangeResult result = new BulkChangeResult();
    RuleIndex ruleIndex = index.get(RuleIndex.class);
    DbSession dbSession = db.openSession(false);
    try {
      Result<Rule> ruleSearchResult =
          ruleIndex.search(ruleQuery, new QueryOptions().setScroll(true));
      Iterator<Rule> rules = ruleSearchResult.scroll();
      while (rules.hasNext()) {
        Rule rule = rules.next();
        try {
          ActiveRuleKey key = ActiveRuleKey.of(profileKey, rule.key());
          RuleActivation activation = new RuleActivation(key);
          activation.setSeverity(severity);
          List<ActiveRuleChange> changes = activate(dbSession, activation);
          result.addChanges(changes);
          result.incrementSucceeded();

        } catch (BadRequestException e) {
          // other exceptions stop the bulk activation
          result.incrementFailed();
          // TODO result.addMessage
        }
      }
      dbSession.commit();
    } finally {
      dbSession.close();
    }
    return result;
  }
  private void writeTags(Rule rule, JsonWriter json) {
    json.name("tags").beginArray();
    for (String adminTag : rule.adminTags()) {
      json.value(adminTag);
    }
    json.endArray();

    json.name("sysTags").beginArray();
    for (String systemTag : rule.systemTags()) {
      json.value(systemTag);
    }
    json.endArray();
  }
  @Test
  public void search_protected_chars() throws InterruptedException {
    String nameWithProtectedChars = "ja#va&sc\"r:ipt";

    RuleDto ruleDto = RuleTesting.newXooX1().setName(nameWithProtectedChars);
    dao.insert(dbSession, ruleDto);
    dbSession.commit();

    Rule rule = index.getByKey(RuleTesting.XOO_X1);
    assertThat(rule.name()).isEqualTo(nameWithProtectedChars);

    RuleQuery protectedCharsQuery = new RuleQuery().setQueryText(nameWithProtectedChars);
    List<Rule> results = index.search(protectedCharsQuery, new QueryContext()).getHits();
    assertThat(results).hasSize(1);
    assertThat(results.get(0).key()).isEqualTo(RuleTesting.XOO_X1);
  }
 void completeShow(Rule rule, JsonWriter json) {
   json.name("actives").beginArray();
   for (ActiveRule activeRule : loader.findActiveRulesByRule(rule.key())) {
     writeActiveRule(activeRule, json);
   }
   json.endArray();
 }
 private void addNote(Rule rule, JsonWriter json) {
   RuleNote ruleNote = rule.ruleNote();
   if (ruleNote != null && !Strings.isNullOrEmpty(ruleNote.data())) {
     json.prop("noteRaw", ruleNote.data())
         .prop("noteHtml", Markdown.convertToHtml(ruleNote.data()));
   }
 }
Example #6
0
 BulkChangeResult bulkDeactivate(RuleQuery ruleQuery, QualityProfileKey profile) {
   DbSession dbSession = db.openSession(false);
   try {
     RuleIndex ruleIndex = index.get(RuleIndex.class);
     BulkChangeResult result = new BulkChangeResult();
     Result<Rule> ruleSearchResult =
         ruleIndex.search(ruleQuery, new QueryOptions().setScroll(true));
     Iterator<Rule> rules = ruleSearchResult.scroll();
     while (rules.hasNext()) {
       Rule rule = rules.next();
       ActiveRuleKey key = ActiveRuleKey.of(profile, rule.key());
       List<ActiveRuleChange> changes = deactivate(dbSession, key);
       result.addChanges(changes);
       result.incrementSucceeded();
     }
     dbSession.commit();
     return result;
   } finally {
     dbSession.close();
   }
 }
 private void writeRule(Rule rule, JsonWriter json) {
   json.prop("key", rule.ruleKey().toString())
       .prop("name", rule.name())
       .prop("description", rule.description());
   addNote(rule, json);
   addDate(rule.createdAt(), "createdAt", json);
   addFormattedDate(rule.createdAt(), "fCreatedAt", json);
   addDate(rule.updatedAt(), "updatedAt", json);
   addFormattedDate(rule.updatedAt(), "fUpdatedAt", json);
 }
  @Test
  public void select_doc_fields_to_return() {
    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("javascript", "S001")));
    dbSession.commit();

    QueryContext options = new QueryContext();
    options.addFieldsToReturn(
        RuleNormalizer.RuleField.LANGUAGE.field(), RuleNormalizer.RuleField.STATUS.field());
    Result<Rule> results = index.search(new RuleQuery(), options);
    assertThat(results.getHits()).hasSize(1);

    Rule hit = Iterables.getFirst(results.getHits(), null);
    assertThat(hit.language()).isEqualTo("js");
    assertThat(hit.status()).isEqualTo(RuleStatus.READY);

    try {
      hit.htmlDescription();
      fail();
    } catch (IllegalStateException e) {
      assertThat(e).hasMessage("Field htmlDesc not specified in query options");
    }
  }
  @Test
  public void return_all_doc_fields_by_default() {
    dao.insert(dbSession, RuleTesting.newDto(RuleKey.of("javascript", "S001")));
    dbSession.commit();

    QueryContext options = new QueryContext().setFieldsToReturn(null);
    Result<Rule> results = index.search(new RuleQuery(), options);
    assertThat(results.getHits()).hasSize(1);
    Rule hit = Iterables.getFirst(results.getHits(), null);
    assertThat(hit.key()).isNotNull();
    assertThat(hit.htmlDescription()).isNotNull();
    assertThat(hit.name()).isNotNull();

    options = new QueryContext().setFieldsToReturn(Collections.<String>emptyList());
    results = index.search(new RuleQuery(), options);
    assertThat(results.getHits()).hasSize(1);
    hit = Iterables.getFirst(results.getHits(), null);
    assertThat(hit.key()).isNotNull();
    assertThat(hit.htmlDescription()).isNotNull();
    assertThat(hit.name()).isNotNull();
  }
  @Test
  public void getByKey() throws InterruptedException {
    RuleDto ruleDto = RuleTesting.newDto(RuleKey.of("javascript", "S001"));
    dao.insert(dbSession, ruleDto);
    dbSession.commit();

    Rule rule = index.getByKey(RuleKey.of("javascript", "S001"));

    assertThat(rule.htmlDescription()).isEqualTo(ruleDto.getDescription());
    assertThat(rule.key()).isEqualTo(ruleDto.getKey());

    // TODO
    // assertThat(rule.debtSubCharacteristicKey())
    // .isEqualTo(ruleDto.getDefaultSubCharacteristicId().toString());
    assertThat(rule.debtRemediationFunction().type().name())
        .isEqualTo(ruleDto.getRemediationFunction());

    assertThat(Sets.newHashSet(rule.tags())).isEqualTo(ruleDto.getTags());
    assertThat(Sets.newHashSet(rule.systemTags())).isEqualTo(ruleDto.getSystemTags());
  }
  void completeSearch(RuleQuery query, Collection<Rule> rules, JsonWriter json) {
    json.name("actives").beginObject();

    String profileKey = query.getQProfileKey();
    if (profileKey != null) {
      // Load details of active rules on the selected profile
      for (Rule rule : rules) {
        ActiveRule activeRule = loader.getActiveRule(ActiveRuleKey.of(profileKey, rule.key()));
        if (activeRule != null) {
          writeActiveRules(rule.key(), Arrays.asList(activeRule), json);
        }
      }
    } else {
      // Load details of all active rules
      for (Rule rule : rules) {
        writeActiveRules(rule.key(), loader.findActiveRulesByRule(rule.key()), json);
      }
    }
    json.endObject();
  }
 private Rule create(String repoKey, String key, String name, String description) {
   Rule mockRule = mock(Rule.class);
   when(mockRule.adminTags()).thenReturn(ImmutableList.of("admin1", "admin2"));
   return mockRule;
 }