Exemple #1
0
 Set<Dependency> getDependenciesBetweenProjects() {
   Set<Dependency> result = Sets.newLinkedHashSet();
   for (Dependency dependency : dependencies) {
     if (ResourceUtils.isSet(dependency.getFrom()) || ResourceUtils.isSet(dependency.getTo())) {
       result.add(dependency);
     }
   }
   return result;
 }
  @Override
  public void decorate(Resource resource, DecoratorContext context) {
    if (ResourceUtils.isPersistable(resource) && !ResourceUtils.isUnitTestFile(resource)) {
      Long developmentCost = getDevelopmentCost(context);
      context.saveMeasure(
          new Measure(CoreMetrics.DEVELOPMENT_COST, Long.toString(developmentCost)));

      long debt = getMeasureValue(context, CoreMetrics.TECHNICAL_DEBT);
      double density = computeDensity(debt, developmentCost);
      context.saveMeasure(CoreMetrics.SQALE_DEBT_RATIO, 100.0 * density);

      SqaleRatingGrid ratingGrid = new SqaleRatingGrid(sqaleRatingSettings.getRatingGrid());
      context.saveMeasure(createRatingMeasure(ratingGrid.getRatingForDensity(density)));
    }
  }
Exemple #3
0
  /** Keep only project stuff */
  public void clear() {
    Iterator<Map.Entry<Resource, Bucket>> it = buckets.entrySet().iterator();
    while (it.hasNext()) {
      Map.Entry<Resource, Bucket> entry = it.next();
      Resource resource = entry.getKey();
      if (!ResourceUtils.isSet(resource)) {
        entry.getValue().clear();
        it.remove();
      }
    }

    // store dependencies
    for (Dependency dep : dependencies) {
      dependencyPersister.saveDependency(currentProject, dep);
    }

    // Keep only inter module dependencies
    Set<Dependency> projectDependencies = getDependenciesBetweenProjects();
    dependencies.clear();
    incomingDependenciesByResource.clear();
    outgoingDependenciesByResource.clear();
    for (Dependency projectDependency : projectDependencies) {
      projectDependency.setId(null);
      registerDependency(projectDependency);
    }
  }
 @VisibleForTesting
 static boolean shouldPersistMeasure(@Nullable Resource resource, @Nullable Measure measure) {
   if (resource == null || measure == null) {
     return false;
   }
   return measure.getPersistenceMode().useDatabase()
       && !(ResourceUtils.isEntity(resource) && measure.isBestValue())
       && isMeasureNotEmpty(measure);
 }
 private void saveOnRequirement(
     DecoratorContext context, Map<TechnicalDebtRequirement, Double> requirementCosts) {
   for (Map.Entry<TechnicalDebtRequirement, Double> entry : requirementCosts.entrySet()) {
     saveCost(
         context,
         entry.getKey().toCharacteristic(),
         entry.getValue(),
         ResourceUtils.isEntity(context.getResource()));
   }
 }
Exemple #6
0
 /** Keep only project stuff */
 public void clear() {
   Iterator<Map.Entry<Resource, Bucket>> it = buckets.entrySet().iterator();
   while (it.hasNext()) {
     Map.Entry<Resource, Bucket> entry = it.next();
     Resource resource = entry.getKey();
     if (!ResourceUtils.isSet(resource)) {
       entry.getValue().clear();
       it.remove();
     }
   }
 }
 @Override
 public void decorate(Resource resource, DecoratorContext context) {
   if (!ResourceUtils.isPersistable(resource)) {
     return;
   }
   File path = null;
   if (ResourceUtils.isProject(resource)) {
     path = fs.baseDir();
   } else if (resource.getPath() != null) {
     path = new File(fs.baseDir(), resource.getPath());
   }
   ResourceNode resourceNode = new ResourceNode(resource, path, fs.sourceCharset());
   RESOURCE_BY_KEY.put(resource.getEffectiveKey(), resourceNode);
   for (DecoratorContext childContext : context.getChildren()) {
     Resource child = childContext.getResource();
     ResourceNode childNode = RESOURCE_BY_KEY.get(child.getEffectiveKey());
     if (childNode != null) {
       resourceNode.addChild(childNode);
     }
   }
 }
Exemple #8
0
  private Bucket doIndex(Resource resource, @Nullable Resource parentReference) {
    Bucket bucket = getBucket(resource);
    if (bucket != null) {
      return bucket;
    }

    if (StringUtils.isBlank(resource.getKey())) {
      LOG.warn("Unable to index a resource without key " + resource);
      return null;
    }

    Resource parent = null;
    if (!ResourceUtils.isLibrary(resource)) {
      // a library has no parent
      parent = (Resource) ObjectUtils.defaultIfNull(parentReference, currentProject);
    }

    Bucket parentBucket = getBucket(parent);
    if (parentBucket == null && parent != null) {
      LOG.warn("Resource ignored, parent is not indexed: " + resource);
      return null;
    }

    if (ResourceUtils.isProject(resource)
        || /* For technical projects */ ResourceUtils.isRootProject(resource)) {
      resource.setEffectiveKey(resource.getKey());
    } else {
      resource.setEffectiveKey(ComponentKeys.createEffectiveKey(currentProject, resource));
    }
    bucket = new Bucket(resource).setParent(parentBucket);
    addBucket(resource, bucket);

    Resource parentResource = parentBucket != null ? parentBucket.getResource() : null;
    resourceCache.add(resource, parentResource);

    return bucket;
  }
Exemple #9
0
  private Bucket doIndex(Resource resource, Resource parentReference) {
    Bucket bucket = buckets.get(resource);
    if (bucket != null) {
      return bucket;
    }

    checkLock(resource);

    Resource parent = null;
    if (!ResourceUtils.isLibrary(resource)) {
      // a library has no parent
      parent = (Resource) ObjectUtils.defaultIfNull(parentReference, currentProject);
    }

    Bucket parentBucket = getBucket(parent, true);
    if (parentBucket == null && parent != null) {
      LOG.warn("Resource ignored, parent is not indexed: " + resource);
      return null;
    }

    resource.setEffectiveKey(createUID(currentProject, resource));
    bucket = new Bucket(resource).setParent(parentBucket);
    buckets.put(resource, bucket);

    boolean excluded = checkExclusion(resource, parentBucket);
    if (!excluded) {
      Snapshot snapshot =
          persistence.saveResource(
              currentProject, resource, (parentBucket != null ? parentBucket.getResource() : null));
      if (ResourceUtils.isPersistable(resource)
          && !Qualifiers.LIBRARY.equals(resource.getQualifier())) {
        graph.addComponent(resource, snapshot);
      }
    }

    return bucket;
  }
 /** {@inheritDoc} */
 @SuppressWarnings("rawtypes")
 public void decorate(Resource resource, DecoratorContext context) {
   if (ResourceUtils.isFile(resource) && context.getMeasure(testMetric) == null) {
     if (isExcludedFromCoverage(resource)) {
       return;
     }
     // for LINES_TO_COVER and UNCOVERED_LINES, we use NCLOC and STATEMENTS as an approximation
     Measure ncloc = context.getMeasure(CoreMetrics.NCLOC);
     if (ncloc != null) {
       Measure sts = context.getMeasure(CoreMetrics.STATEMENTS);
       double lines = Math.min(ncloc.getValue(), sts.getValue());
       if (lines > 0d) {
         LOG.debug(
             "Coverage metrics have not been set on '{}': default values will be inserted.",
             resource.getName());
         handleUncoveredResource(context, lines);
       }
     }
   }
 }
 @VisibleForTesting
 void saveCost(
     DecoratorContext context,
     org.sonar.api.qualitymodel.Characteristic characteristic,
     Double value,
     boolean inMemory) {
   // we need the value on projects (root or module) even if value==0 in order to display correctly
   // the SQALE history chart (see SQALE-122)
   // BUT we don't want to save zero-values for non top-characteristics (see SQALE-147)
   if (value > 0.0
       || (ResourceUtils.isProject(context.getResource())
           && characteristic.getDepth() == org.sonar.api.qualitymodel.Characteristic.ROOT_DEPTH)) {
     Measure measure = new Measure(CoreMetrics.TECHNICAL_DEBT);
     measure.setCharacteristic(characteristic);
     measure.setValue(value, DECIMALS_PRECISION);
     if (inMemory) {
       measure.setPersistenceMode(PersistenceMode.MEMORY);
     }
     context.saveMeasure(measure);
   }
 }
Exemple #12
0
  /** Keep only project stuff */
  public void clear() {
    Iterator<Map.Entry<Resource, Bucket>> it = buckets.entrySet().iterator();
    while (it.hasNext()) {
      Map.Entry<Resource, Bucket> entry = it.next();
      Resource resource = entry.getKey();
      if (!ResourceUtils.isSet(resource)) {
        entry.getValue().clear();
        it.remove();
      }
    }

    Set<Dependency> projectDependencies = getDependenciesBetweenProjects();
    dependencies.clear();
    incomingDependenciesByResource.clear();
    outgoingDependenciesByResource.clear();
    for (Dependency projectDependency : projectDependencies) {
      projectDependency.setId(null);
      registerDependency(projectDependency);
    }

    lock.unlock();
  }
  /** {@inheritDoc} */
  public void decorate(Resource resource, DecoratorContext context) {

    if (ResourceUtils.isProject(resource)) {

      Collection<Measure> childMeasures =
          context.getChildrenMeasures(CodesizeMetrics.CODE_COUNTERS);

      if (!childMeasures.isEmpty()) {
        Measure projectMeasure = context.getMeasure(CodesizeMetrics.CODE_COUNTERS);
        final PropertiesBuilder<String, Integer> counters =
            new PropertiesBuilder<String, Integer>(CodesizeMetrics.CODE_COUNTERS);
        if (projectMeasure != null) {
          Map<String, Integer> map = KeyValueFormat.parseStringInt(projectMeasure.getData());
          counters.addAll(map);
        }

        for (Measure childMeasure : childMeasures) {
          Map<String, Integer> childcounters =
              KeyValueFormat.parseStringInt(childMeasure.getData());
          for (Entry<String, Integer> entry : childcounters.entrySet()) {
            String key = entry.getKey();
            Integer value = entry.getValue();
            if (counters.getProps().containsKey(key)) {
              counters.add(key, value + counters.getProps().get(key));
            } else {
              counters.add(key, value);
            }
          }
        }

        if (projectMeasure != null) {
          projectMeasure.setData(counters.buildData());
        } else {
          context.saveMeasure(counters.build());
        }
      }
    }
  }
 private boolean shouldDecorateResource(Resource resource, DecoratorContext context) {
   return ResourceUtils.isProject(resource)
       && context.getMeasure(CoreMetrics.DEPENDENCY_MATRIX) == null;
 }
 @Override
 public boolean shouldDecorateResource(Resource resource) {
   return !ResourceUtils.isUnitTestClass(resource);
 }
 public void decorate(Resource resource, DecoratorContext context) {
   if (ResourceUtils.isProject(resource)) {
     persistConfiguration();
   }
 }
Exemple #17
0
 private void checkLock(Resource resource) {
   if (lock.isLocked() && !ResourceUtils.isLibrary(resource) && lock.isFailWhenLocked()) {
     throw new SonarException("Index is locked, resource can not be indexed: " + resource);
   }
 }
 public boolean shouldExecuteOnProject(Project project) {
   return profile != null
       && profile.getAlerts() != null
       && profile.getAlerts().size() > 0
       && ResourceUtils.isRootProject(project);
 }
 private boolean shouldDecorateResource(Resource resource) {
   return ResourceUtils.isRootProject(resource);
 }
 protected boolean shouldDecorate(final Resource resource, final DecoratorContext context) {
   return context.getMeasure(getTargetMetric()) == null
       && !ResourceUtils.isUnitTestClass(resource);
 }