private void setDefault(DbSession session, QualityProfileDto profile) {
   PropertyDto property =
       new PropertyDto()
           .setKey(PROFILE_PROPERTY_PREFIX + profile.getLanguage())
           .setValue(profile.getName());
   db.propertiesDao().setProperty(property, session);
 }
 private void doDelete(DbSession session, QualityProfileDto profile) {
   db.activeRuleDao().deleteByProfileKey(session, profile.getKey());
   db.qualityProfileDao().delete(session, profile);
   db.propertiesDao()
       .deleteProjectProperties(
           PROFILE_PROPERTY_PREFIX + profile.getLanguage(), profile.getName(), session);
 }
Esempio n. 3
0
  private List<ActiveRuleChange> cascadeDeactivation(
      ActiveRuleKey key, DbSession dbSession, boolean isCascade, boolean force) {
    List<ActiveRuleChange> changes = Lists.newArrayList();
    RuleActivatorContext context = contextFactory.create(key, dbSession);
    ActiveRuleChange change;
    if (context.activeRule() == null) {
      return changes;
    }
    if (!force && !isCascade && context.activeRule().getInheritance() != null) {
      throw new IllegalStateException("Cannot deactivate inherited rule '" + key.ruleKey() + "'");
    }
    change = ActiveRuleChange.createFor(ActiveRuleChange.Type.DEACTIVATED, key);
    changes.add(change);
    persist(change, context, dbSession);

    // get all inherited profiles
    List<QualityProfileDto> profiles =
        db.qualityProfileDao().findByParentKey(dbSession, key.qProfile());

    for (QualityProfileDto profile : profiles) {
      ActiveRuleKey activeRuleKey = ActiveRuleKey.of(profile.getKey(), key.ruleKey());
      changes.addAll(cascadeDeactivation(activeRuleKey, dbSession, true, force));
    }

    if (!changes.isEmpty()) {
      log.write(dbSession, Activity.Type.ACTIVE_RULE, changes);
      previewCache.reportGlobalModification();
    }

    return changes;
  }
  public void renameProfile(int profileId, String newName, UserSession userSession) {
    checkPermission(userSession);
    DbSession session = myBatis.openSession(false);
    try {
      QualityProfileDto profileDto = findNotNull(profileId, session);
      String oldName = profileDto.getName();

      QProfile profile = QProfile.from(profileDto);
      if (!oldName.equals(newName)) {
        checkNotAlreadyExists(newName, profile.language(), session);
      }
      profileDto.setName(newName);
      dao.update(session, profileDto);

      List<QProfile> children = profileLookup.children(profile, session);
      for (QProfile child : children) {
        dao.update(session, child.setParent(newName).toDto());
      }
      propertiesDao.updateProperties(
          PROFILE_PROPERTY_PREFIX + profile.language(), oldName, newName, session);

      session.commit();
    } finally {
      MyBatis.closeQuietly(session);
    }
  }
Esempio n. 5
0
  void setParent(
      DbSession dbSession, QualityProfileKey key, @Nullable QualityProfileKey parentKey) {
    QualityProfileDto profile = db.qualityProfileDao().getNonNullByKey(dbSession, key);
    if (parentKey == null) {
      // unset if parent is defined, else nothing to do
      removeParent(dbSession, profile);

    } else if (profile.getParentKey() == null || !profile.getParentKey().equals(parentKey)) {
      QualityProfileDto parentProfile =
          db.qualityProfileDao().getNonNullByKey(dbSession, parentKey);
      if (isDescendant(dbSession, profile, parentProfile)) {
        throw new BadRequestException(
            String.format(
                "Descendant profile '%s' can not be selected as parent of '%s'", parentKey, key));
      }
      removeParent(dbSession, profile);

      // set new parent
      profile.setParent(parentKey.name());
      db.qualityProfileDao().update(dbSession, profile);
      for (ActiveRuleDto parentActiveRule :
          db.activeRuleDao().findByProfileKey(dbSession, parentKey)) {
        RuleActivation activation =
            new RuleActivation(ActiveRuleKey.of(key, parentActiveRule.getKey().ruleKey()));
        activate(dbSession, activation);
      }
    }
  }
 public QProfile get(String language, String name) {
   QualityProfileDto dto = qualityProfileDao.getByNameAndLanguage(name, language);
   if (dto == null) {
     return null;
   }
   return new org.sonar.batch.protocol.input.QProfile(
       dto.getKey(),
       dto.getName(),
       dto.getLanguage(),
       UtcDateUtils.parseDateTime(dto.getRulesUpdatedAt()));
 }
Esempio n. 7
0
 /** Does not commit */
 private void removeParent(DbSession dbSession, QualityProfileDto profileDto) {
   if (profileDto.getParent() != null) {
     profileDto.setParent(null);
     db.qualityProfileDao().update(dbSession, profileDto);
     for (ActiveRuleDto activeRule :
         db.activeRuleDao().findByProfileKey(dbSession, profileDto.getKey())) {
       if (ActiveRuleDto.INHERITED.equals(activeRule.getInheritance())) {
         deactivate(dbSession, activeRule.getKey(), true);
       } else if (ActiveRuleDto.OVERRIDES.equals(activeRule.getInheritance())) {
         activeRule.setInheritance(null);
         db.activeRuleDao().update(dbSession, activeRule);
       }
     }
   }
 }
 /**
  * Session is NOT committed. Profiles marked as "default" for a language can't be deleted, except
  * if the parameter <code>force</code> is true.
  */
 void delete(DbSession session, String key, boolean force) {
   QualityProfileDto profile = db.qualityProfileDao().getNonNullByKey(session, key);
   List<QualityProfileDto> descendants = db.qualityProfileDao().findDescendants(session, key);
   if (!force) {
     QualityProfileDto defaultProfile = getDefault(session, profile.getLanguage());
     checkNotDefault(defaultProfile, profile);
     for (QualityProfileDto descendant : descendants) {
       checkNotDefault(defaultProfile, descendant);
     }
   }
   // delete bottom-up
   for (QualityProfileDto descendant : Lists.reverse(descendants)) {
     doDelete(session, descendant);
   }
   doDelete(session, profile);
 }
Esempio n. 9
0
 boolean isDescendant(
     DbSession dbSession,
     QualityProfileDto childProfile,
     @Nullable QualityProfileDto parentProfile) {
   QualityProfileDto currentParent = parentProfile;
   while (currentParent != null) {
     if (childProfile.getName().equals(currentParent.getName())) {
       return true;
     }
     if (currentParent.getParent() != null) {
       currentParent = db.qualityProfileDao().getByKey(dbSession, currentParent.getParentKey());
     } else {
       currentParent = null;
     }
   }
   return false;
 }
 void verifyForActivation() {
   if (RuleStatus.REMOVED == rule.getStatus()) {
     throw new BadRequestException("Rule was removed: " + rule.getKey());
   }
   if (rule.isTemplate()) {
     throw new BadRequestException(
         "Rule template can't be activated on a Quality profile: " + rule.getKey());
   }
   if (rule.getKey().isManual()) {
     throw new BadRequestException(
         "Manual rule can't be activated on a Quality profile: " + rule.getKey());
   }
   if (!profile.getLanguage().equals(rule.getLanguage())) {
     throw new BadRequestException(
         String.format(
             "Rule %s and profile %s have different languages", rule.getKey(), profile.getKey()));
   }
 }
Esempio n. 11
0
  @Test
  public void search_by_profile() throws InterruptedException {
    QualityProfileDto qualityProfileDto1 = QProfileTesting.newXooP1();
    QualityProfileDto qualityProfileDto2 = QProfileTesting.newXooP2();
    db.qualityProfileDao().insert(dbSession, qualityProfileDto1, qualityProfileDto2);

    RuleDto rule1 = RuleTesting.newXooX1();
    RuleDto rule2 = RuleTesting.newXooX2();
    RuleDto rule3 = RuleTesting.newXooX3();
    dao.insert(dbSession, rule1, rule2, rule3);

    db.activeRuleDao()
        .insert(
            dbSession,
            ActiveRuleDto.createFor(qualityProfileDto1, rule1).setSeverity("BLOCKER"),
            ActiveRuleDto.createFor(qualityProfileDto2, rule1).setSeverity("BLOCKER"),
            ActiveRuleDto.createFor(qualityProfileDto1, rule2).setSeverity("BLOCKER"));
    dbSession.commit();
    dbSession.clearCache();

    // 1. get all active rules.
    Result<Rule> result = index.search(new RuleQuery().setActivation(true), new QueryContext());
    assertThat(result.getHits()).hasSize(2);

    // 2. get all inactive rules.
    result = index.search(new RuleQuery().setActivation(false), new QueryContext());
    assertThat(result.getHits()).hasSize(1);
    assertThat(result.getHits().get(0).name()).isEqualTo(rule3.getName());

    // 3. get all rules not active on profile
    index.search(
        new RuleQuery().setActivation(false).setQProfileKey(qualityProfileDto2.getKey()),
        new QueryContext());
    // TODO
    assertThat(result.getHits()).hasSize(1);

    // 4. get all active rules on profile
    result =
        index.search(
            new RuleQuery().setActivation(true).setQProfileKey(qualityProfileDto2.getKey()),
            new QueryContext());
    assertThat(result.getHits()).hasSize(1);
    assertThat(result.getHits().get(0).name()).isEqualTo(rule1.getName());
  }
Esempio n. 12
0
  private List<ActiveRuleChange> cascadeActivation(DbSession session, RuleActivation activation) {
    List<ActiveRuleChange> changes = Lists.newArrayList();

    // get all inherited profiles
    List<QualityProfileDto> profiles =
        db.qualityProfileDao().findByParentKey(session, activation.getKey().qProfile());

    for (QualityProfileDto profile : profiles) {
      ActiveRuleKey activeRuleKey =
          ActiveRuleKey.of(profile.getKey(), activation.getKey().ruleKey());
      changes.addAll(
          this.activate(
              session,
              new RuleActivation(activeRuleKey)
                  .isCascade(true)
                  .setParameters(activation.getParameters())
                  .setSeverity(activation.getSeverity())));
    }
    return changes;
  }
Esempio n. 13
0
 public boolean rename(String key, String newName) {
   Verifications.check(StringUtils.isNotBlank(newName), "Name must be set");
   Verifications.check(
       newName.length() < 100, String.format("Name is too long (>%d characters)", 100));
   DbSession dbSession = db.openSession(false);
   try {
     QualityProfileDto profile = db.qualityProfileDao().getNonNullByKey(dbSession, key);
     if (!StringUtils.equals(newName, profile.getName())) {
       if (db.qualityProfileDao().getByNameAndLanguage(newName, profile.getLanguage(), dbSession)
           != null) {
         throw new BadRequestException("Quality profile already exists: " + newName);
       }
       String previousName = profile.getName();
       profile.setName(newName);
       db.qualityProfileDao().update(dbSession, profile);
       db.propertiesDao()
           .updateProperties(
               PROFILE_PROPERTY_PREFIX + profile.getLanguage(), previousName, newName, dbSession);
       dbSession.commit();
       return true;
     }
     return false;
   } finally {
     dbSession.close();
   }
 }
Esempio n. 14
0
  private QualityProfileDto doCreate(DbSession dbSession, QProfileName name) {
    if (StringUtils.isEmpty(name.getName())) {
      throw new BadRequestException("quality_profiles.profile_name_cant_be_blank");
    }
    Date now = new Date();
    for (int i = 0; i < 20; i++) {
      String key =
          Slug.slugify(
              String.format(
                  "%s %s %s",
                  name.getLanguage(), name.getName(), RandomStringUtils.randomNumeric(5)));
      QualityProfileDto dto =
          QualityProfileDto.createFor(key)
              .setName(name.getName())
              .setLanguage(name.getLanguage())
              .setRulesUpdatedAtAsDate(now);
      if (db.qualityProfileDao().getByKey(dbSession, dto.getKey()) == null) {
        db.qualityProfileDao().insert(dbSession, dto);
        return dto;
      }
    }

    throw new IllegalStateException("Failed to create an unique quality profile for " + name);
  }
 ActiveRuleKey activeRuleKey() {
   return ActiveRuleKey.of(profile.getKee(), rule.getKey());
 }
Esempio n. 16
0
  @Test
  public void search_by_profile_and_inheritance() throws InterruptedException {
    QualityProfileDto qualityProfileDto1 = QProfileTesting.newXooP1();
    QualityProfileDto qualityProfileDto2 =
        QProfileTesting.newXooP2().setParentKee(QProfileTesting.XOO_P1_KEY);
    db.qualityProfileDao().insert(dbSession, qualityProfileDto1, qualityProfileDto2);

    RuleDto rule1 = RuleTesting.newDto(RuleKey.of("xoo", "S001"));
    RuleDto rule2 = RuleTesting.newDto(RuleKey.of("xoo", "S002"));
    RuleDto rule3 = RuleTesting.newDto(RuleKey.of("xoo", "S003"));
    RuleDto rule4 = RuleTesting.newDto(RuleKey.of("xoo", "S004"));
    dao.insert(dbSession, rule1, rule2, rule3, rule4);

    db.activeRuleDao()
        .insert(
            dbSession,
            ActiveRuleDto.createFor(qualityProfileDto1, rule1).setSeverity("BLOCKER"),
            ActiveRuleDto.createFor(qualityProfileDto1, rule2).setSeverity("BLOCKER"),
            ActiveRuleDto.createFor(qualityProfileDto1, rule3).setSeverity("BLOCKER"),
            ActiveRuleDto.createFor(qualityProfileDto2, rule1)
                .setSeverity("MINOR")
                .setInheritance(ActiveRule.Inheritance.INHERITED.name()),
            ActiveRuleDto.createFor(qualityProfileDto2, rule2)
                .setSeverity("BLOCKER")
                .setInheritance(ActiveRule.Inheritance.OVERRIDES.name()),
            ActiveRuleDto.createFor(qualityProfileDto2, rule3)
                .setSeverity("BLOCKER")
                .setInheritance(ActiveRule.Inheritance.INHERITED.name()));

    dbSession.commit();

    // 0. get all rules
    Result<Rule> result = index.search(new RuleQuery(), new QueryContext());
    assertThat(result.getHits()).hasSize(4);

    // 1. get all active rules
    result = index.search(new RuleQuery().setActivation(true), new QueryContext());
    assertThat(result.getHits()).hasSize(3);

    // 2. get all inactive rules.
    result = index.search(new RuleQuery().setActivation(false), new QueryContext());
    assertThat(result.getHits()).hasSize(1);
    assertThat(result.getHits().get(0).name()).isEqualTo(rule4.getName());

    // 3. get Inherited Rules on profile1
    result =
        index.search(
            new RuleQuery()
                .setActivation(true)
                .setQProfileKey(qualityProfileDto1.getKey())
                .setInheritance(ImmutableSet.of(ActiveRule.Inheritance.INHERITED.name())),
            new QueryContext());
    assertThat(result.getHits()).hasSize(0);

    // 4. get Inherited Rules on profile2
    result =
        index.search(
            new RuleQuery()
                .setActivation(true)
                .setQProfileKey(qualityProfileDto2.getKey())
                .setInheritance(ImmutableSet.of(ActiveRule.Inheritance.INHERITED.name())),
            new QueryContext());
    assertThat(result.getHits()).hasSize(2);

    // 5. get Overridden Rules on profile1
    result =
        index.search(
            new RuleQuery()
                .setActivation(true)
                .setQProfileKey(qualityProfileDto1.getKey())
                .setInheritance(ImmutableSet.of(ActiveRule.Inheritance.OVERRIDES.name())),
            new QueryContext());
    assertThat(result.getHits()).hasSize(0);

    // 6. get Overridden Rules on profile2
    result =
        index.search(
            new RuleQuery()
                .setActivation(true)
                .setQProfileKey(qualityProfileDto2.getKey())
                .setInheritance(ImmutableSet.of(ActiveRule.Inheritance.OVERRIDES.name())),
            new QueryContext());
    assertThat(result.getHits()).hasSize(1);

    // 7. get Inherited AND Overridden Rules on profile1
    result =
        index.search(
            new RuleQuery()
                .setActivation(true)
                .setQProfileKey(qualityProfileDto1.getKey())
                .setInheritance(
                    ImmutableSet.of(
                        ActiveRule.Inheritance.INHERITED.name(),
                        ActiveRule.Inheritance.OVERRIDES.name())),
            new QueryContext());
    assertThat(result.getHits()).hasSize(0);

    // 8. get Inherited AND Overridden Rules on profile2
    result =
        index.search(
            new RuleQuery()
                .setActivation(true)
                .setQProfileKey(qualityProfileDto2.getKey())
                .setInheritance(
                    ImmutableSet.of(
                        ActiveRule.Inheritance.INHERITED.name(),
                        ActiveRule.Inheritance.OVERRIDES.name())),
            new QueryContext());
    assertThat(result.getHits()).hasSize(3);
  }
Esempio n. 17
0
 private void checkNotDefault(@Nullable QualityProfileDto defaultProfile, QualityProfileDto p) {
   if (defaultProfile != null && defaultProfile.getKey().equals(p.getKey())) {
     throw new BadRequestException(
         "The profile marked as default can not be deleted: " + p.getKey());
   }
 }