]> source.dussan.org Git - sonarqube.git/commitdiff
Merge remote-tracking branch 'origin/branch-4.5'
authorJulien HENRY <julien.henry@sonarsource.com>
Tue, 14 Oct 2014 12:31:54 +0000 (14:31 +0200)
committerJulien HENRY <julien.henry@sonarsource.com>
Tue, 14 Oct 2014 12:31:54 +0000 (14:31 +0200)
Conflicts:
sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java
sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java
sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java

1  2 
sonar-batch/src/main/java/org/sonar/batch/bootstrap/ServerClient.java
sonar-batch/src/main/java/org/sonar/batch/referential/DefaultProjectReferentialsLoader.java
sonar-batch/src/main/java/org/sonar/batch/scan/LastSnapshots.java
sonar-batch/src/main/java/org/sonar/batch/scan/filesystem/ModuleFileSystemInitializer.java
sonar-batch/src/test/java/org/sonar/batch/mediumtest/fs/FileSystemMediumTest.java
sonar-batch/src/test/java/org/sonar/batch/scan/LastSnapshotsTest.java
sonar-plugin-api/src/main/java/org/sonar/api/batch/SonarIndex.java

index b86dd4e1cf7f91f8b8e5efdeb31001785e2246da,7ff216c42805d9c843e74b2bcc5803809050b523..f2519a186ebfa84509c736c4bf1bded147ee3850
@@@ -35,21 -25,8 +35,19 @@@ import org.sonar.api.utils.KeyValueForm
  import org.sonar.batch.bootstrap.AnalysisMode;
  import org.sonar.batch.bootstrap.ServerClient;
  import org.sonar.batch.bootstrap.TaskProperties;
 +import org.sonar.batch.protocol.input.FileData;
  import org.sonar.batch.protocol.input.ProjectReferentials;
  import org.sonar.batch.rule.ModuleQProfiles;
- import java.io.UnsupportedEncodingException;
- import java.net.URLEncoder;
 +import org.sonar.core.source.SnapshotDataTypes;
 +import org.sonar.core.source.db.SnapshotDataDao;
 +import org.sonar.core.source.db.SnapshotDataDto;
 +
 +import javax.persistence.Query;
 +
 +import java.util.Arrays;
 +import java.util.Collection;
 +import java.util.List;
 +import java.util.Map;
  
  public class DefaultProjectReferentialsLoader implements ProjectReferentialsLoader {
  
      if (taskProperties.properties().containsKey(ModuleQProfiles.SONAR_PROFILE_PROP)) {
        LOG.warn("Ability to set quality profile from command line using '" + ModuleQProfiles.SONAR_PROFILE_PROP
          + "' is deprecated and will be dropped in a future SonarQube version. Please configure quality profile used by your project on SonarQube server.");
-       try {
-         url += "&profile=" + URLEncoder.encode(taskProperties.properties().get(ModuleQProfiles.SONAR_PROFILE_PROP), "UTF-8");
-       } catch (UnsupportedEncodingException e) {
-         throw new IllegalStateException("Unable to encode URL", e);
-       }
+       url += "&profile=" + ServerClient.encodeForUrl(taskProperties.properties().get(ModuleQProfiles.SONAR_PROFILE_PROP));
      }
      url += "&preview=" + analysisMode.isPreview();
 -    return ProjectReferentials.fromJson(serverClient.request(url));
 +    ProjectReferentials ref = ProjectReferentials.fromJson(serverClient.request(url));
 +
 +    for (ProjectDefinition module : reactor.getProjects()) {
 +
 +      for (Map.Entry<String, String> hashByPaths : hashByRelativePath(module.getKeyWithBranch()).entrySet()) {
 +        String path = hashByPaths.getKey();
 +        String hash = hashByPaths.getValue();
 +        String lastCommits = null;
 +        String revisions = null;
 +        String authors = null;
 +        List<Object[]> measuresByKey = query(projectKey + ":" + path, CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE_KEY, CoreMetrics.SCM_REVISIONS_BY_LINE_KEY,
 +          CoreMetrics.SCM_AUTHORS_BY_LINE_KEY);
 +        for (Object[] measureByKey : measuresByKey) {
 +          if (measureByKey[0].equals(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE_KEY)) {
 +            lastCommits = ((MeasureModel) measureByKey[1]).getData(CoreMetrics.SCM_LAST_COMMIT_DATETIMES_BY_LINE);
 +          } else if (measureByKey[0].equals(CoreMetrics.SCM_REVISIONS_BY_LINE_KEY)) {
 +            revisions = ((MeasureModel) measureByKey[1]).getData(CoreMetrics.SCM_REVISIONS_BY_LINE);
 +          } else if (measureByKey[0].equals(CoreMetrics.SCM_AUTHORS_BY_LINE_KEY)) {
 +            authors = ((MeasureModel) measureByKey[1]).getData(CoreMetrics.SCM_AUTHORS_BY_LINE);
 +          }
 +        }
 +        ref.addFileData(module.getKeyWithBranch(), path, new FileData(hash, lastCommits, revisions, authors));
 +      }
 +    }
 +    return ref;
 +  }
 +
 +  public Map<String, String> hashByRelativePath(String projectKey) {
 +    Map<String, String> map = Maps.newHashMap();
 +    Collection<SnapshotDataDto> selectSnapshotData = dao.selectSnapshotDataByComponentKey(
 +      projectKey,
 +      Arrays.asList(SnapshotDataTypes.FILE_HASHES)
 +      );
 +    if (!selectSnapshotData.isEmpty()) {
 +      SnapshotDataDto snapshotDataDto = selectSnapshotData.iterator().next();
 +      String data = snapshotDataDto.getData();
 +      map = KeyValueFormat.parse(data);
 +    }
 +    return map;
 +  }
 +
 +  public List<Object[]> query(String resourceKey, String... metricKeys) {
 +    StringBuilder sb = new StringBuilder();
 +    Map<String, Object> params = Maps.newHashMap();
 +
 +    sb.append("SELECT met.key, m");
 +    sb.append(" FROM ")
 +      .append(MeasureModel.class.getSimpleName())
 +      .append(" m, ")
 +      .append(Metric.class.getSimpleName())
 +      .append(" met, ")
 +      .append(ResourceModel.class.getSimpleName())
 +      .append(" r, ")
 +      .append(Snapshot.class.getSimpleName())
 +      .append(" s WHERE met.id=m.metricId AND m.snapshotId=s.id AND s.resourceId=r.id AND r.key=:kee AND s.status=:status AND s.qualifier<>:lib");
 +    params.put("kee", resourceKey);
 +    params.put("status", Snapshot.STATUS_PROCESSED);
 +    params.put("lib", Qualifiers.LIBRARY);
 +
 +    sb.append(" AND m.characteristicId IS NULL");
 +    sb.append(" AND m.personId IS NULL");
 +    sb.append(" AND m.ruleId IS NULL AND m.rulePriority IS NULL");
 +    if (metricKeys.length > 0) {
 +      sb.append(" AND met.key IN (:metricKeys) ");
 +      params.put("metricKeys", Arrays.asList(metricKeys));
 +    }
 +    sb.append(" AND s.last=true ");
 +    sb.append(" ORDER BY s.createdAt ");
 +
 +    Query jpaQuery = session.createQuery(sb.toString());
 +
 +    for (Map.Entry<String, Object> entry : params.entrySet()) {
 +      jpaQuery.setParameter(entry.getKey(), entry.getValue());
 +    }
 +    return jpaQuery.getResultList();
    }
  }
index 980129c03118f59383a74ba81c6a99287809b04c,7bea9e46216ef0014fbb11b6a0c7d878d09597ca..9f04b206fb3787f3ae9bf00b4d07200c1d77d53b
@@@ -59,10 -55,11 +59,11 @@@ public class LastSnapshots implements B
      return StringUtils.defaultString(source, "");
    }
  
 -  @CheckForNull
    private String loadSourceFromWs(Resource resource) {
 +    TimeProfiler profiler = new TimeProfiler(LOG).start("Load previous source code of: " + resource.getEffectiveKey()).setLevelToDebug();
      try {
-       return server.request("/api/sources?resource=" + resource.getEffectiveKey() + "&format=txt", "GET", false, analysisMode.getPreviewReadTimeoutSec() * 1000);
+       return server
 -        .request("/api/sources?resource=" + ServerClient.encodeForUrl(resource.getEffectiveKey()) + "&format=txt", false, analysisMode.getPreviewReadTimeoutSec() * 1000);
++        .request("/api/sources?resource=" + ServerClient.encodeForUrl(resource.getEffectiveKey()) + "&format=txt", "GET", false, analysisMode.getPreviewReadTimeoutSec() * 1000);
      } catch (HttpDownloader.HttpException he) {
        if (he.getResponseCode() == 404) {
          return "";
index 145332981d4990e087a687dbcaf157c76e7babca,1c3746408abd91df643054b0383546f79aabd809..66acae88fa34ea3813aeff2d2a493c99f37fe7e9
@@@ -88,7 -91,21 +91,21 @@@ public class LastSnapshotsTest 
  
      String source = lastSnapshots.getSource(newFile());
      assertThat(source).isEqualTo("downloaded source of Bar.c");
-     verify(server).request("/api/sources?resource=myproject:org/foo/Bar.c&format=txt", "GET", false, 30 * 1000);
 -    verify(server).request("/api/sources?resource=myproject%3Aorg%2Ffoo%2FBar.c&format=txt", false, 30 * 1000);
++    verify(server).request("/api/sources?resource=myproject%3Aorg%2Ffoo%2FBar.c&format=txt", "GET", false, 30 * 1000);
+   }
+   @Test
+   public void should_download_source_with_space_from_ws_if_preview_mode() {
+     db.prepareDbUnit(getClass(), "last_snapshot.xml");
+     ServerClient server = mock(ServerClient.class);
 -    when(server.request(anyString(), eq(false), eq(30 * 1000))).thenReturn("downloaded source of Foo Bar.c");
++    when(server.request(anyString(), eq("GET"), eq(false), eq(30 * 1000))).thenReturn("downloaded source of Foo Bar.c");
+     when(mode.isPreview()).thenReturn(true);
+     LastSnapshots lastSnapshots = new LastSnapshots(mode, new SnapshotSourceDao(db.myBatis()), server);
+     String source = lastSnapshots.getSource(newFile());
+     assertThat(source).isEqualTo("downloaded source of Foo Bar.c");
 -    verify(server).request("/api/sources?resource=myproject%3Aorg%2Ffoo%2FBar.c&format=txt", false, 30 * 1000);
++    verify(server).request("/api/sources?resource=myproject%3Aorg%2Ffoo%2FBar.c&format=txt", "GET", false, 30 * 1000);
    }
  
    @Test
      when(mode.isPreview()).thenReturn(true);
      LastSnapshots lastSnapshots = new LastSnapshots(mode, new SnapshotSourceDao(db.myBatis()), server);
  
-     String source = lastSnapshots.getSource(newFile());
+     String source = lastSnapshots.getSource(newFileWithSpace());
      assertThat(source).isEqualTo("");
-     verify(server).request("/api/sources?resource=myproject:org/foo/Bar.c&format=txt", "GET", false, 30 * 1000);
 -    verify(server).request("/api/sources?resource=myproject%3Aorg%2Ffoo%2FFoo+Bar.c&format=txt", false, 30 * 1000);
++    verify(server).request("/api/sources?resource=myproject%3Aorg%2Ffoo%2FFoo+Bar.c&format=txt", "GET", false, 30 * 1000);
    }
  
    @Test