Skip to content

Commit

Permalink
Update orchestrator version (#2160)
Browse files Browse the repository at this point in the history
(cherry picked from commit 3e70ea3)
  • Loading branch information
1 parent 5799edc commit ef71e7b
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,10 @@
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.sonar.wsclient.issue.IssueQuery;
import org.sonarqube.ws.client.issues.SearchRequest;

import static com.sonar.javascript.it.plugin.CustomRulesTest.newWsClient;
import static java.util.Collections.singletonList;
import static org.assertj.core.api.Assertions.assertThat;

public class NoSonarTest {
Expand Down Expand Up @@ -54,8 +56,9 @@ public static void startServer() {

@Test
public void test() {
assertThat(orchestrator.getServer().wsClient().issueClient().find(IssueQuery.create().componentRoots("nosonar-project").severities("INFO").rules("javascript:S1116")).list())
.hasSize(1);
SearchRequest request = new SearchRequest();
request.setComponentKeys(singletonList("nosonar-project")).setSeverities(singletonList("INFO")).setRules(singletonList("javascript:S1116"));
assertThat(newWsClient(orchestrator).issues().search(request).getIssuesList()).hasSize(1);
}

}
6 changes: 6 additions & 0 deletions its/ruling/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@
<dependency>
<groupId>org.sonarsource.orchestrator</groupId>
<artifactId>sonar-orchestrator</artifactId>
<version>${sonar-orchestrator.version}</version>
</dependency>
<dependency>
<groupId>org.sonarsource.sonarqube</groupId>
<artifactId>sonar-ws</artifactId>
<version>7.9.1</version>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,9 @@
*/
package org.sonar.javascript.it;

import com.google.common.collect.ImmutableMap;
import com.google.gson.Gson;
import com.sonar.orchestrator.Orchestrator;
import com.sonar.orchestrator.build.SonarScanner;
import com.sonar.orchestrator.container.Server;
import com.sonar.orchestrator.locator.FileLocation;
import com.sonar.orchestrator.locator.Location;
import com.sonar.orchestrator.locator.MavenLocation;
Expand All @@ -35,10 +34,10 @@
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.apache.commons.lang.StringUtils;
import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
Expand All @@ -48,7 +47,13 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.sonar.api.utils.System2;
import org.sonar.wsclient.SonarClient;
import org.sonarqube.ws.Qualityprofiles;
import org.sonarqube.ws.client.HttpConnector;
import org.sonarqube.ws.client.WsClient;
import org.sonarqube.ws.client.WsClientFactories;
import org.sonarqube.ws.client.qualityprofiles.ActivateRuleRequest;
import org.sonarqube.ws.client.qualityprofiles.SearchRequest;
import org.sonarqube.ws.client.rules.CreateRequest;
import org.sonarsource.analyzer.commons.ProfileGenerator;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -201,34 +206,46 @@ private static boolean isUserHome(File dir) throws IOException {
}

private static void instantiateTemplateRule(String language, String qualityProfile, String ruleTemplateKey, String instantiationKey, String params) {
SonarClient sonarClient = orchestrator.getServer().adminWsClient();
String keyPrefix = "ts".equals(language) ? "typescript:" : "javascript:";
sonarClient.post("/api/rules/create", ImmutableMap.<String, Object>builder()
.put("name", instantiationKey)
.put("markdown_description", instantiationKey)
.put("severity", "INFO")
.put("status", "READY")
.put("template_key", keyPrefix + ruleTemplateKey)
.put("custom_key", instantiationKey)
.put("prevent_reactivation", "true")
.put("params", "name=\"" + instantiationKey + "\";key=\"" + instantiationKey + "\";markdown_description=\"" + instantiationKey + "\";" + params)
.build());
String post = sonarClient.get("api/qualityprofiles/search", "language", language, "qualityProfile", qualityProfile);

Map rulesProfile = new Gson().fromJson(post, Map.class);
String profileKey = ((List<Map>) rulesProfile.get("profiles")).stream()
.findFirst().map(profileDescription -> (String) profileDescription.get("key")).orElse(null);

if (profileKey != null) {
String response = sonarClient.post("api/qualityprofiles/activate_rule", ImmutableMap.of(
"profile_key", profileKey,
"rule_key", keyPrefix + instantiationKey,
"severity", "INFO",
"params", ""));
LOG.warn(response);
newAdminWsClient(orchestrator)
.rules()
.create(new CreateRequest()
.setName(instantiationKey)
.setMarkdownDescription(instantiationKey)
.setSeverity("INFO")
.setStatus("READY")
.setTemplateKey(keyPrefix + ruleTemplateKey)
.setCustomKey(instantiationKey)
.setPreventReactivation("true")
.setParams(Arrays.asList(("name=\"" + instantiationKey + "\";key=\"" + instantiationKey + "\";markdown_description=\"" + instantiationKey + "\";" + params).split(";", 0))));


String profileKey = newAdminWsClient(orchestrator).qualityprofiles()
.search(new SearchRequest().setLanguage(language))
.getProfilesList().stream()
.filter(qp -> qualityProfile.equals(qp.getName()))
.map(Qualityprofiles.SearchWsResponse.QualityProfile::getKey)
.findFirst()
.orElse(null);

if (!StringUtils.isEmpty(profileKey)) {
newAdminWsClient(orchestrator).qualityprofiles()
.activateRule(new ActivateRuleRequest()
.setKey(profileKey)
.setRule(keyPrefix + instantiationKey)
.setSeverity("INFO")
.setParams(Collections.emptyList()));
LOG.warn(String.format("Successfully activated template rule '%s'", keyPrefix + instantiationKey));
} else {
throw new IllegalStateException("Could not retrieve profile key : Template rule " + ruleTemplateKey + " has not been activated");
}
}

static WsClient newAdminWsClient(Orchestrator orchestrator) {
return WsClientFactories.getDefault().newClient(HttpConnector.newBuilder()
.credentials(Server.ADMIN_LOGIN, Server.ADMIN_PASSWORD)
.url(orchestrator.getServer().getUrl())
.build());
}

}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
<mockito.version>2.21.0</mockito.version>
<slf4j.version>1.7.21</slf4j.version>
<sonar.version>7.9</sonar.version>
<sonar-orchestrator.version>3.26.0.2111</sonar-orchestrator.version>
<sonar-orchestrator.version>3.30.0.2630</sonar-orchestrator.version>
<sonarlint.version>4.3.1.2486</sonarlint.version>
<sslr.version>1.23</sslr.version>
<sslr-squid-bridge.version>2.7.0.377</sslr-squid-bridge.version>
Expand Down

0 comments on commit ef71e7b

Please sign in to comment.