private List<RuleDto> loadDtos(List<RuleKey> ruleKeys) {
   DbSession dbSession = dbClient.openSession(false);
   try {
     return dbClient.ruleDao().selectByKeys(dbSession, ruleKeys);
   } finally {
     dbClient.closeSession(dbSession);
   }
 }
 private RuleDto createRule(String lang, String id) {
   RuleDto rule =
       RuleTesting.newDto(RuleKey.of("blah", id))
           .setLanguage(lang)
           .setSeverity(Severity.BLOCKER)
           .setStatus(RuleStatus.READY);
   db.ruleDao().insert(session, rule);
   return rule;
 }
 /** Used in issues_controller.rb and in manual_rules_controller.rb and in SQALE */
 @CheckForNull
 public RuleDto findByKey(String ruleKey) {
   DbSession dbSession = dbClient.openSession(false);
   try {
     return dbClient.ruleDao().selectByKey(dbSession, RuleKey.parse(ruleKey)).orNull();
   } finally {
     dbClient.closeSession(dbSession);
   }
 }
Beispiel #4
0
  private long insertNewProjectInDbAndReturnSnapshotId(int id) {
    String suffix = String.valueOf(id);
    ComponentDto project =
        ComponentTesting.newProjectDto("project-uuid-" + suffix).setKey("project-key-" + suffix);
    RuleDto rule = RuleTesting.newDto(RuleKey.of("sonarqube", "rule-" + suffix));
    dbClient.ruleDao().insert(dbSession, rule);
    IssueDto issue = IssueTesting.newDto(rule, project, project).setKee("issue-key-" + suffix);
    dbClient.componentDao().insert(dbSession, project);
    SnapshotDto snapshot =
        dbClient.snapshotDao().insert(dbSession, SnapshotTesting.newAnalysis(project));
    dbClient.issueDao().insert(dbSession, issue);
    dbSession.commit();

    return snapshot.getId();
  }
  public boolean updateRule(
      RuleDto ruleDto,
      @Nullable String newFunction,
      @Nullable String newCoefficient,
      @Nullable String newOffset,
      DbSession session) {
    boolean needUpdate = false;

    // A sub-characteristic and a remediation function is given -> update rule debt
    if (newFunction != null) {
      // New values are the same as the default values -> set overridden values to null
      if (isRuleDebtSameAsDefaultValues(ruleDto, newFunction, newCoefficient, newOffset)) {
        ruleDto.setRemediationFunction(null);
        ruleDto.setRemediationGapMultiplier(null);
        ruleDto.setRemediationBaseEffort(null);
        needUpdate = true;

        // New values are not the same as the overridden values -> update overridden values with new
        // values
      } else if (!isRuleDebtSameAsOverriddenValues(
          ruleDto, newFunction, newCoefficient, newOffset)) {
        DefaultDebtRemediationFunction debtRemediationFunction =
            new DefaultDebtRemediationFunction(
                DebtRemediationFunction.Type.valueOf(newFunction), newCoefficient, newOffset);
        ruleDto.setRemediationFunction(debtRemediationFunction.type().name());
        ruleDto.setRemediationGapMultiplier(debtRemediationFunction.gapMultiplier());
        ruleDto.setRemediationBaseEffort(debtRemediationFunction.baseEffort());
        needUpdate = true;
      }

      // No sub-characteristic is given -> disable rule debt if not already disabled
    } else {
      // Rule characteristic is not already disabled -> update it
      ruleDto.setRemediationFunction(null);
      ruleDto.setRemediationGapMultiplier(null);
      ruleDto.setRemediationBaseEffort(null);
      needUpdate = true;
    }

    if (needUpdate) {
      dbClient.ruleDao().update(session, ruleDto);
    }
    return needUpdate;
  }
 public void updateRule(RuleChange ruleChange, UserSession userSession) {
   checkPermission(userSession);
   DbSession session = dbClient.openSession(false);
   try {
     RuleDto ruleDto = dbClient.ruleDao().selectOrFailByKey(session, ruleChange.ruleKey());
     boolean needUpdate =
         updateRule(
             ruleDto,
             ruleChange.debtRemediationFunction(),
             ruleChange.debtRemediationCoefficient(),
             ruleChange.debtRemediationOffset(),
             session);
     if (needUpdate) {
       ruleIndexer.index();
       session.commit();
     }
   } catch (IllegalArgumentException e) {
     throw new BadRequestException(e.getMessage());
   } finally {
     MyBatis.closeQuietly(session);
   }
 }