package org.sonar.server.batch;
+import org.sonarqube.ws.WsBatch.WsProjectResponse.FileData.Builder;
+
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
+
import org.sonar.api.server.ws.Request;
import org.sonar.api.server.ws.Response;
import org.sonar.api.server.ws.WebService;
import org.sonar.batch.protocol.input.FileData;
import org.sonar.batch.protocol.input.ProjectRepositories;
import org.sonarqube.ws.WsBatch.WsProjectResponse;
-
import static org.sonar.server.ws.WsUtils.writeProtobuf;
public class ProjectAction implements BatchWsAction {
}
private static WsProjectResponse.FileData toFileDataResponse(FileData fileData) {
- return WsProjectResponse.FileData.newBuilder()
- .setHash(fileData.hash())
- .setRevision(fileData.revision())
- .build();
- }
+ Builder fileDataBuilder = WsProjectResponse.FileData.newBuilder();
+ if (fileData.hash() != null) {
+ fileDataBuilder.setHash(fileData.hash());
+ }
+ if (fileData.revision() != null) {
+ fileDataBuilder.setRevision(fileData.revision());
+ }
+ return fileDataBuilder.build();
+ }
}
public class DefaultProjectRepositoriesLoader implements ProjectRepositoriesLoader {
private static final Logger LOG = LoggerFactory.getLogger(DefaultProjectRepositoriesLoader.class);
- private static final String BATCH_PROJECT_URL = "/batch/project";
+ private static final String BATCH_PROJECT_URL = "/batch/project.protobuf";
private final WSLoader loader;
public DefaultProjectRepositoriesLoader(WSLoader loader) {
import java.util.List;
public class DefaultQualityProfileLoader implements QualityProfileLoader {
- private static final String WS_URL = "/qualityprofiles/search";
+ private static final String WS_URL = "/api/qualityprofiles/search.protobuf";
private WSLoader wsLoader;
@Override
public boolean load(String componentKey, Function<ServerIssue, Void> consumer) {
- WSLoaderResult<InputStream> result = wsLoader.loadStream("/batch/issues?key=" + BatchUtils.encodeForUrl(componentKey));
+ WSLoaderResult<InputStream> result = wsLoader.loadStream("/batch/issues.protobuf?key=" + BatchUtils.encodeForUrl(componentKey));
parseIssues(result.get(), consumer);
return result.isFromCache();
}
ActiveRulesBuilder builder = new ActiveRulesBuilder();
for (Rule activeRule : loadedRules) {
- NewActiveRule newActiveRule = builder.create(RuleKey.of(activeRule.getRepo(), activeRule.getKey()));
+ NewActiveRule newActiveRule = builder.create(RuleKey.parse(activeRule.getKey()));
newActiveRule.setName(activeRule.getName());
newActiveRule.setSeverity(activeRule.getSeverity());
newActiveRule.setLanguage(activeRule.getLang());
import java.util.List;
public class DefaultActiveRulesLoader implements ActiveRulesLoader {
- private static final String RULES_SEARCH_URL = "/api/rules/search?f=repo,name,severity,lang,internalKey,templateKey";
+ private static final String RULES_SEARCH_URL = "/api/rules/search.protobuf?f=repo,name,severity,lang,internalKey,templateKey,params&activation=true";
private final WSLoader wsLoader;
import java.util.List;
public class DefaultRulesLoader implements RulesLoader {
- private static final String RULES_SEARCH_URL = "/api/rules/list";
+ private static final String RULES_SEARCH_URL = "/api/rules/list.protobuf";
private final WSLoader wsLoader;
import javax.annotation.Nullable;
-import org.sonar.batch.cache.ProjectCacheStatus;
import org.sonarqube.ws.Rules.ListResponse.Rule;
import org.sonar.batch.bootstrapper.IssueListener;
import org.sonar.api.server.rule.RulesDefinition.Repository;
org.sonarqube.ws.Rules.Rule.Builder builder = org.sonarqube.ws.Rules.Rule.newBuilder();
builder.setRepo(repositoryKey);
- builder.setKey(ruleKey);
+ if (internalKey != null) {
+ builder.setInternalKey(internalKey);
+ }
+ builder.setKey(repositoryKey + ":" + ruleKey);
builder.setName(name);
if (templateRuleKey != null) {
if (languag != null) {
builder.setLang(languag);
}
- if (internalKey != null) {
- builder.setInternalKey(internalKey);
- }
if (severity != null) {
builder.setSeverity(severity);
}
@Nullable String internalKey, @Nullable String languag, String paramKey, String paramValue) {
org.sonarqube.ws.Rules.Rule.Builder builder = org.sonarqube.ws.Rules.Rule.newBuilder();
builder.setRepo(repositoryKey);
- builder.setKey(ruleKey);
+ builder.setKey(repositoryKey + ":" + ruleKey);
if (templateRuleKey != null) {
builder.setTemplateKey(templateRuleKey);
}
@Test
public void passIssuesModeParameter() {
loader.load(PROJECT_KEY, false, null);
- verify(wsLoader).loadStream("/batch/project?key=foo%3F");
+ verify(wsLoader).loadStream("/batch/project.protobuf?key=foo%3F");
loader.load(PROJECT_KEY, true, null);
- verify(wsLoader).loadStream("/batch/project?key=foo%3F&issues=true");
+ verify(wsLoader).loadStream("/batch/project.protobuf?key=foo%3F&issues=true");
}
@Test
@Test
public void passAndEncodeProjectKeyParameter() {
loader.load(PROJECT_KEY, false, null);
- verify(wsLoader).loadStream("/batch/project?key=foo%3F");
+ verify(wsLoader).loadStream("/batch/project.protobuf?key=foo%3F");
}
private InputStream mockData() throws IOException {
when(ws.loadStream(anyString())).thenReturn(result);
List<QualityProfile> loaded = qpLoader.load("foo#2", "my-profile#2", null);
- verify(ws).loadStream("/qualityprofiles/search?projectKey=foo%232&profileName=my-profile%232");
+ verify(ws).loadStream("/api/qualityprofiles/search.protobuf?projectKey=foo%232&profileName=my-profile%232");
verifyNoMoreInteractions(ws);
assertThat(loaded).hasSize(1);
}
when(ws.loadStream(anyString())).thenReturn(new WSLoaderResult<InputStream>(is, false));
List<QualityProfile> loaded = qpLoader.loadDefault(null);
- verify(ws).loadStream("/qualityprofiles/search?defaults=true");
+ verify(ws).loadStream("/api/qualityprofiles/search.protobuf?defaults=true");
verifyNoMoreInteractions(ws);
assertThat(loaded).hasSize(1);
}
.writeDelimitedTo(bos);
InputStream is = new ByteArrayInputStream(bos.toByteArray());
- when(wsLoader.loadStream("/batch/issues?key=foo")).thenReturn(new WSLoaderResult<>(is, true));
+ when(wsLoader.loadStream("/batch/issues.protobuf?key=foo")).thenReturn(new WSLoaderResult<>(is, true));
final List<ServerIssue> result = new ArrayList<>();
loader.load("foo", new Function<BatchInput.ServerIssue, Void>() {
public void testError() throws IOException {
InputStream is = mock(InputStream.class);
when(is.read()).thenThrow(IOException.class);
- when(wsLoader.loadStream("/batch/issues?key=foo")).thenReturn(new WSLoaderResult<InputStream>(is, true));
+ when(wsLoader.loadStream("/batch/issues.protobuf?key=foo")).thenReturn(new WSLoaderResult<InputStream>(is, true));
loader.load("foo", mock(Function.class));
}
}
}
private static Rule mockRule(String name) {
- return Rule.newBuilder().setName(name).setRepo(name).setKey(name).build();
+ return Rule.newBuilder().setName(name).setRepo(name).setKey(name + ":" + name).build();
}
}
Collection<Rule> activeRules = loader.load("java-sonar-way-26368", null);
assertThat(activeRules).hasSize(100);
- verify(ws).loadStream("/api/rules/search?f=repo,name,severity,lang,internalKey,templateKey&qprofile=java-sonar-way-26368");
+ verify(ws).loadStream("/api/rules/search.protobuf?f=repo,name,severity,lang,internalKey,templateKey,params&activation=true&qprofile=java-sonar-way-26368");
verifyNoMoreInteractions(ws);
}
try {
lock();
Path path = getCacheCopy(key);
+ if (path == null) {
+ return null;
+ }
return new DeleteFileOnCloseInputStream(new FileInputStream(path.toFile()), path);
} finally {
*/
package org.sonar.home.cache;
+import org.apache.commons.io.IOUtils;
+
import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
assertThat(new String(Files.readAllBytes(lockFile), StandardCharsets.UTF_8)).isEqualTo("test");
}
+ @Test
+ public void testStream() throws IOException {
+ cache.put("id", "test".getBytes());
+ InputStream stream = cache.getStream("id");
+ assertThat(IOUtils.toString(stream)).isEqualTo("test");
+
+ assertThat(cache.getStream("non existing")).isNull();
+ }
+
@Test
public void testClear() throws Exception {
Path lockFile = cache.getDirectory().resolve("lock");
@Override
public ActiveRule find(RuleKey ruleKey) {
- return activeRulesByRepositoryAndKey.containsKey(ruleKey.repository()) ? activeRulesByRepositoryAndKey.get(ruleKey.repository()).get(ruleKey.rule()) : null;
+ Map<String, ActiveRule> map = activeRulesByRepositoryAndKey.get(ruleKey.repository());
+ if(map != null) {
+ return map.get(ruleKey.rule());
+ }
+ return null;
}
@Override