Skip to content

Commit

Permalink
added build id optional param
Browse files Browse the repository at this point in the history
  • Loading branch information
pjagielski committed Mar 23, 2016
1 parent 9acc523 commit 640074d
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 23 deletions.
3 changes: 2 additions & 1 deletion src/main/java/pl/touk/sputnik/configuration/CliOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ public enum CliOption implements ConfigurationOption {
CHANGE_ID("cli.changeId", "Gerrit change id", null),
REVISION_ID("cli.revisionId", "Gerrit revision id", null),
PULL_REQUEST_ID("cli.pullRequestId", "Stash pull request id", null),
API_KEY("cli.apiKey", "Optional API key for using Sputnik for Github", null);
API_KEY("cli.apiKey", "Optional API key for using Sputnik for Github", null),
BUILD_ID("cli.buildId", "Optional build id for using Sputnik for Github", null);

private String key;
private String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ private Options createOptions() {

localOptions.addOption(buildOption(CliOption.PULL_REQUEST_ID, true, false));
localOptions.addOption(buildOption(CliOption.API_KEY, true, false));
localOptions.addOption(buildOption(CliOption.BUILD_ID, true, false));

return localOptions;
}
Expand Down
26 changes: 15 additions & 11 deletions src/main/java/pl/touk/sputnik/connector/saas/SaasConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
Expand All @@ -17,6 +18,8 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

@AllArgsConstructor
@Slf4j
Expand All @@ -25,19 +28,17 @@ public class SaasConnector implements Connector {
private HttpConnector httpConnector;
private GithubPatchset githubPatchset;
private String apiKey;

public SaasConnector(HttpConnector httpConnector, GithubPatchset githubPatchset) {
this(httpConnector, githubPatchset, null);
}
private String buildId;

private static final String API_KEY_PARAM = "key";
private static final String BUILD_ID_PARAM = "build_id";
private static final String FILES_URL_FORMAT = "/api/github/%s/pulls/%d/files";
private static final String VIOLATIONS_URL_FORMAT = "/api/github/%s/pulls/%d/violations";

@NotNull
@Override
public String listFiles() throws URISyntaxException, IOException {
URI uri = httpConnector.buildUri(createUrl(githubPatchset, FILES_URL_FORMAT), apiKeyParam());
URI uri = httpConnector.buildUri(createUrl(githubPatchset, FILES_URL_FORMAT), params());
HttpGet request = new HttpGet(uri);
CloseableHttpResponse httpResponse = httpConnector.logAndExecute(request);
return httpConnector.consumeAndLogEntity(httpResponse);
Expand All @@ -47,7 +48,7 @@ public String listFiles() throws URISyntaxException, IOException {
@Override
public String sendReview(String violationsAsJson) throws URISyntaxException, IOException {
log.info("Sending violations: {}", violationsAsJson);
URI uri = httpConnector.buildUri(createUrl(githubPatchset, VIOLATIONS_URL_FORMAT), apiKeyParam());
URI uri = httpConnector.buildUri(createUrl(githubPatchset, VIOLATIONS_URL_FORMAT), params());
HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(new StringEntity(violationsAsJson, ContentType.APPLICATION_JSON));
CloseableHttpResponse httpResponse = httpConnector.logAndExecute(httpPost);
Expand All @@ -59,11 +60,14 @@ private String createUrl(GithubPatchset patchset, String formatUrl) {
}

@NotNull
private NameValuePair[] apiKeyParam() {
if (apiKey != null) {
return new BasicNameValuePair[]{new BasicNameValuePair(API_KEY_PARAM, apiKey)};
} else {
return new BasicNameValuePair[]{};
private NameValuePair[] params() {
List<NameValuePair> params = new ArrayList<>();
if (StringUtils.isNotBlank(apiKey)) {
params.add(new BasicNameValuePair(API_KEY_PARAM, apiKey));
}
if (StringUtils.isNotBlank(buildId)) {
params.add(new BasicNameValuePair(BUILD_ID_PARAM, buildId));
}
return params.toArray(new NameValuePair[params.size()]);
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package pl.touk.sputnik.connector.saas;

import com.google.gson.Gson;
import org.apache.commons.lang.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.auth.BasicScheme;
Expand All @@ -10,7 +9,6 @@
import pl.touk.sputnik.configuration.CliOption;
import pl.touk.sputnik.configuration.Configuration;
import pl.touk.sputnik.connector.ConnectorDetails;
import pl.touk.sputnik.connector.github.GithubPatchset;
import pl.touk.sputnik.connector.github.GithubPatchsetBuilder;
import pl.touk.sputnik.connector.http.HttpConnector;
import pl.touk.sputnik.connector.http.HttpHelper;
Expand All @@ -23,18 +21,14 @@ public class SaasFacadeBuilder {
public SaasFacade build(Configuration configuration) {
ConnectorDetails connectorDetails = new ConnectorDetails(configuration);
String apiKey = configuration.getProperty(CliOption.API_KEY);
String buildId = configuration.getProperty(CliOption.BUILD_ID);
HttpHost httpHost = httpHelper.buildHttpHost(connectorDetails);
HttpClientContext httpClientContext = httpHelper.buildClientContext(httpHost, new BasicScheme());
CloseableHttpClient closeableHttpClient = httpHelper.buildClient(httpHost, connectorDetails);

HttpConnector httpConnector = new HttpConnector(closeableHttpClient, httpClientContext, connectorDetails.getPath());
GithubPatchset patchset = GithubPatchsetBuilder.build(configuration);

SaasConnector saasConnector = StringUtils.isNotBlank(apiKey) ?
new SaasConnector(httpConnector, patchset, apiKey) :
new SaasConnector(httpConnector, patchset);

return new SaasFacade(saasConnector, new Gson());
return new SaasFacade(new SaasConnector(
new HttpConnector(closeableHttpClient, httpClientContext, connectorDetails.getPath()),
GithubPatchsetBuilder.build(configuration), apiKey, buildId), new Gson());
}

}
20 changes: 19 additions & 1 deletion src/test/java/pl/touk/sputnik/connector/saas/SaasFacadeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,27 @@ public void shouldHandleEmptyApiKey() throws Exception {
assertThat(files).extracting("reviewFilename").containsOnly("src/main/java/TestFile.java", "src/main/java/TestFile2.java");
}

@Test
public void shouldSendBuildIdIfProvided() throws Exception {
SaasFacade saasFacade = buildFacade(ImmutableMap.of(
"cli.pullRequestId", SOME_PULL_REQUEST_ID.toString(),
"cli.buildId", "11223344",
"connector.repository", SOME_REPOSITORY,
"connector.project", SOME_PROJECT
));

stubGet(urlEqualTo(String.format(
"%s/api/github/%s/%s/pulls/%s/files?build_id=%s",
FacadeConfigUtil.PATH, SOME_PROJECT, SOME_REPOSITORY, SOME_PULL_REQUEST_ID, "11223344")), "/json/saas-files.json");

List<ReviewFile> files = saasFacade.listFiles();

assertThat(files).extracting("reviewFilename").containsOnly("src/main/java/TestFile.java", "src/main/java/TestFile2.java");
}

protected SaasFacade buildFacade(Map<String, String> configMap) {
Configuration config = new ConfigurationSetup().setUp(FacadeConfigUtil.getHttpConfig("saas"), configMap);
return new SaasFacadeBuilder().build(config);
}

}

0 comments on commit 640074d

Please sign in to comment.