Skip to content

Commit

Permalink
Merge pull request #134 from TouK/saas_api_key
Browse files Browse the repository at this point in the history
Saas api key
  • Loading branch information
SpOOnman committed Mar 3, 2016
2 parents 96da183 + 6f5e65c commit 6835c14
Show file tree
Hide file tree
Showing 13 changed files with 77 additions and 213 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jdk:

before_install:
- sudo pip install codecov

after_success:
- codecov
- python <(curl -s https://raw.githubusercontent.com/TouK/sputnik-ci/master/sputnik-ci.py)
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ dependencies {
testCompile 'org.mockito:mockito-core:1.9.5'
testCompile 'org.assertj:assertj-core:1.5.0'
testCompile 'com.googlecode.catch-exception:catch-exception:1.2.0'
testCompile('com.github.tomakehurst:wiremock:1.46') {
testCompile('com.github.tomakehurst:wiremock:1.57') {
exclude group: 'log4j'
}
}
Expand Down
172 changes: 0 additions & 172 deletions checkstyle.xml

This file was deleted.

21 changes: 0 additions & 21 deletions sputnik.properties

This file was deleted.

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 @@ -9,7 +9,8 @@ public enum CliOption implements ConfigurationOption {
CONF("cli.conf", "Configuration properties file", null),
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);
PULL_REQUEST_ID("cli.pullRequestId", "Stash pull request id", null),
API_KEY("cli.apiKey", "Optional API key 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 @@ -23,6 +23,7 @@ private Options createOptions() {
localOptions.addOption(buildOption(CliOption.REVISION_ID, true, false));

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

return localOptions;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpHost;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpRequestBase;
Expand Down Expand Up @@ -51,6 +52,9 @@ public CloseableHttpResponse logAndExecute(@NotNull HttpRequestBase request) thr

@NotNull
public String consumeAndLogEntity(@NotNull CloseableHttpResponse response) throws IOException {
if (!isSuccessful(response)) {
throw new HttpException(response);
}
if (response.getEntity() == null) {
log.debug("Entity {}: no entity", REQUEST_COUNTER);
return StringUtils.EMPTY;
Expand All @@ -59,4 +63,8 @@ public String consumeAndLogEntity(@NotNull CloseableHttpResponse response) throw
log.info("Entity {}: {}", REQUEST_COUNTER, content);
return content;
}

private boolean isSuccessful(HttpResponse response) {
return response.getStatusLine().getStatusCode() / 100 == 2;
}
}
20 changes: 20 additions & 0 deletions src/main/java/pl/touk/sputnik/connector/http/HttpException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package pl.touk.sputnik.connector.http;

import org.apache.http.HttpResponse;

public class HttpException extends RuntimeException {

private final HttpResponse response;

public HttpException(HttpResponse response) {
this.response = response;
}

public int getStatusCode() {
return response.getStatusLine().getStatusCode();
}

public String getMessage() {
return "Response status [" + getStatusCode() + "]";
}
}
20 changes: 16 additions & 4 deletions src/main/java/pl/touk/sputnik/connector/saas/SaasConnector.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package pl.touk.sputnik.connector.saas;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.BasicNameValuePair;
import org.jetbrains.annotations.NotNull;
import pl.touk.sputnik.connector.Connector;
import pl.touk.sputnik.connector.github.GithubPatchset;
Expand All @@ -17,11 +20,14 @@
import java.util.List;

@AllArgsConstructor
@Slf4j
public class SaasConnector implements Connector {

private HttpConnector httpConnector;
private GithubPatchset githubPatchset;
private String apiKey;

private static final String API_KEY_PARAM = "key";
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";

Expand All @@ -32,16 +38,17 @@ public List<String> getReviewFiles() {
@NotNull
@Override
public String listFiles() throws URISyntaxException, IOException {
URI uri = httpConnector.buildUri(createUrl(githubPatchset, FILES_URL_FORMAT));
CloseableHttpResponse httpResponse = httpConnector.logAndExecute(new HttpGet(uri));
URI uri = httpConnector.buildUri(createUrl(githubPatchset, FILES_URL_FORMAT), apiKeyParam());
HttpGet request = new HttpGet(uri);
CloseableHttpResponse httpResponse = httpConnector.logAndExecute(request);
return httpConnector.consumeAndLogEntity(httpResponse);
}

@NotNull
@Override
public String sendReview(String violationsAsJson) throws URISyntaxException, IOException {
System.out.println(violationsAsJson);
URI uri = httpConnector.buildUri(createUrl(githubPatchset, VIOLATIONS_URL_FORMAT));
log.info("Sending violations: {}", violationsAsJson);
URI uri = httpConnector.buildUri(createUrl(githubPatchset, VIOLATIONS_URL_FORMAT), apiKeyParam());
HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(new StringEntity(violationsAsJson, ContentType.APPLICATION_JSON));
CloseableHttpResponse httpResponse = httpConnector.logAndExecute(httpPost);
Expand All @@ -51,4 +58,9 @@ public String sendReview(String violationsAsJson) throws URISyntaxException, IOE
private String createUrl(GithubPatchset patchset, String formatUrl) {
return String.format(formatUrl, patchset.getProjectPath(), patchset.getPullRequestId());
}

@NotNull
private NameValuePair apiKeyParam() {
return new BasicNameValuePair(API_KEY_PARAM, apiKey);
}
}
5 changes: 5 additions & 0 deletions src/main/java/pl/touk/sputnik/connector/saas/SaasFacade.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import pl.touk.sputnik.configuration.GeneralOptionNotSupportedException;
import pl.touk.sputnik.connector.ConnectorFacade;
import pl.touk.sputnik.connector.Connectors;
import pl.touk.sputnik.connector.http.HttpException;
import pl.touk.sputnik.connector.saas.json.FileViolation;
import pl.touk.sputnik.connector.saas.json.Violation;
import pl.touk.sputnik.review.Comment;
Expand Down Expand Up @@ -40,6 +41,8 @@ public List<ReviewFile> listFiles() {
reviewFiles.add(new ReviewFile(filename));
}
return reviewFiles;
} catch (HttpException ex) {
throw new SaasException("Error when listing files, check your api key", ex);
} catch (URISyntaxException | IOException ex) {
throw new SaasException("Error when listing files", ex);
}
Expand All @@ -58,6 +61,8 @@ public void publish(Review review) {
String request = gson.toJson(fileViolations);
try {
saasConnector.sendReview(request);
} catch (HttpException ex) {
throw new SaasException("Error when listing files, check your api key", ex);
} catch (URISyntaxException | IOException ex) {
throw new SaasException("Error while publishing review", ex);
}
Expand Down
Loading

0 comments on commit 6835c14

Please sign in to comment.