Skip to content
This repository has been archived by the owner on Jan 7, 2021. It is now read-only.

Commit

Permalink
add sonarqube version to http useragent
Browse files Browse the repository at this point in the history
  • Loading branch information
t-8ch committed Nov 9, 2016
1 parent 250ac06 commit 0fce732
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ public void executeOn(Project project, SensorContext context) {

StashCredentials stashCredentials = stashRequestFacade.getCredentials();

try (StashClient stashClient = new StashClient(stashURL, stashCredentials, stashTimeout)) {
try (StashClient stashClient = new StashClient(stashURL, stashCredentials, stashTimeout, config.getSonarQubeVersion())) {

StashUser stashUser = stashRequestFacade.getSonarQubeReviewer(stashCredentials.getLogin(), stashClient);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.sonar.plugins.stash;

import org.sonar.api.BatchComponent;
import org.sonar.api.CoreProperties;
import org.sonar.api.batch.InstantiationStrategy;
import org.sonar.api.config.Settings;

Expand Down Expand Up @@ -64,4 +65,8 @@ public boolean resetComments() {
public String getTaskIssueSeverityThreshold() {
return settings.getString(StashPlugin.STASH_TASK_SEVERITY_THRESHOLD);
}

public String getSonarQubeVersion() {
return settings.getString(CoreProperties.SERVER_VERSION);
}
}
18 changes: 10 additions & 8 deletions src/main/java/org/sonar/plugins/stash/client/StashClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,11 @@ public class StashClient implements AutoCloseable {

private static final ContentType JSON = new ContentType("application", "json", null);

public StashClient(String url, StashCredentials credentials, int stashTimeout) {
public StashClient(String url, StashCredentials credentials, int stashTimeout, String sonarQubeVersion) {
this.baseUrl = url;
this.credentials = credentials;
this.stashTimeout = stashTimeout;
this.httpClient = createHttpClient();
this.httpClient = createHttpClient(sonarQubeVersion);
}

public void postCommentOnPullRequest(String project, String repository, String pullRequestId, String report)
Expand Down Expand Up @@ -339,23 +339,25 @@ private static String formatStashApiError(Response response) throws StashClientE

// We can't test this, as the manifest can only be loaded when deployed from a JAR-archive.
// During unit testing this is not the case
public static String getUserAgent() {
private static String getUserAgent(String sonarQubeVersion) {
PluginInfo info = PluginUtils.infoForPluginClass(StashPlugin.class);
String name;
String version;
String sonarQubeVersion;
name = version = sonarQubeVersion = "unknown";
name = version = "unknown";
if (info != null) {
name = info.getName();
version = info.getVersion();
// FIXME: add SonarQube version
}
return MessageFormat.format("SonarQube/{0} {1}/{2} {3}",
sonarQubeVersion, name, version, AsyncHttpClientConfigDefaults.defaultUserAgent());
sonarQubeVersion == null ? "unknown" : sonarQubeVersion,
name,
version,
AsyncHttpClientConfigDefaults.defaultUserAgent());
}

AsyncHttpClient createHttpClient(){
AsyncHttpClient createHttpClient(String sonarQubeVersion){
return new AsyncHttpClient(
new AsyncHttpClientConfig.Builder().setUserAgent(getUserAgent()).build());
new AsyncHttpClientConfig.Builder().setUserAgent(getUserAgent(sonarQubeVersion)).build());
}
}
10 changes: 2 additions & 8 deletions src/test/java/org/sonar/plugins/stash/CompleteITCase.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,15 +84,9 @@ public void basicTest() throws Exception {
wireMock.verify(WireMock.getRequestedFor(WireMock.urlPathMatching(".*" + stashUser + "$")));
wireMock.verify(WireMock.getRequestedFor(WireMock.urlPathMatching(".*diff$")));
wireMock.verify(WireMock.postRequestedFor(WireMock.urlPathMatching(".*comments$")));
}

@Test
public void testUserAgent() throws Exception {
String jsonUser = "{\"name\":\"SonarQube\", \"email\":\"sq@email.com\", \"id\":1, \"slug\":\"sonarqube\"}";
wireMock.stubFor(WireMock.any(WireMock.anyUrl()).willReturn(WireMock.aResponse().withHeader("Content-Type", "application/json").withBody(jsonUser)));
StashClient client = new StashClient("http://127.0.0.1:" + wireMock.port(), new StashCredentials("some", "thing"), 300);
client.getUser("sonarqube");
wireMock.verify(WireMock.getRequestedFor(WireMock.anyUrl()).withHeader("User-Agent", WireMock.containing(" Stash/")));
wireMock.verify(WireMock.getRequestedFor(WireMock.anyUrl()).withHeader("User-Agent", WireMock.matching("Stash/[0-9]+")));
wireMock.verify(WireMock.getRequestedFor(WireMock.anyUrl()).withHeader("User-Agent", WireMock.matching("SonarQube/[0-9]+")));
}

private String repoPath(String project, String repo, String... parts) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ public void setUp() throws Exception {
primeWireMock();
client = new StashClient("http://127.0.0.1:" + wireMock.port(),
new StashCredentials("login", "password"),
timeout);
timeout,
"dummyVersion");
}

@Test
Expand Down

0 comments on commit 0fce732

Please sign in to comment.