Skip to content

Commit

Permalink
Merge pull request #149 from pjagielski/saas_jenkins
Browse files Browse the repository at this point in the history
SCM provider support
  • Loading branch information
pjagielski committed May 11, 2016
2 parents 39008a5 + 5932729 commit eb27fa5
Show file tree
Hide file tree
Showing 12 changed files with 94 additions and 48 deletions.
5 changes: 3 additions & 2 deletions src/main/java/pl/touk/sputnik/configuration/CliOption.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ 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),
BUILD_ID("cli.buildId", "Optional build id for using Sputnik for Github", null);
API_KEY("cli.apiKey", "Optional API key for using Sputnik as a service", null),
BUILD_ID("cli.buildId", "Optional build id for using Sputnik as a service", null),
PROVIDER("cli.provider", "Optional SCM provider (GitHub, GitLab) for using Sputnik as a service", null);

private String key;
private String description;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,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));
localOptions.addOption(buildOption(CliOption.PROVIDER, true, false));

return localOptions;
}
Expand Down
26 changes: 26 additions & 0 deletions src/main/java/pl/touk/sputnik/configuration/Provider.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package pl.touk.sputnik.configuration;

import org.apache.commons.lang3.StringUtils;

public enum Provider {

GITHUB("github"), GITLAB("gitlab");

private final String name;

Provider(String name) {
this.name = name;
}

public String getName() {
return name;
}

public static Provider from(String name) {
if (StringUtils.isNoneBlank(name)) {
return Provider.valueOf(name.toUpperCase());
} else {
return null;
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package pl.touk.sputnik.connector.github;
package pl.touk.sputnik.connector;

import lombok.AllArgsConstructor;
import lombok.Data;
import pl.touk.sputnik.configuration.Provider;

@Data
@AllArgsConstructor
public class GithubPatchset {
public class Patchset {
private final Integer pullRequestId;
private final String projectPath;
private final Provider provider;
}
34 changes: 34 additions & 0 deletions src/main/java/pl/touk/sputnik/connector/PatchsetBuilder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package pl.touk.sputnik.connector;

import org.apache.commons.lang3.math.NumberUtils;
import org.jetbrains.annotations.NotNull;
import pl.touk.sputnik.configuration.CliOption;
import pl.touk.sputnik.configuration.Configuration;
import pl.touk.sputnik.configuration.GeneralOption;
import pl.touk.sputnik.configuration.Provider;

import static org.apache.commons.lang3.Validate.isTrue;
import static org.apache.commons.lang3.Validate.notBlank;
import static org.apache.commons.lang3.Validate.notNull;

public final class PatchsetBuilder {

private PatchsetBuilder() { }

@NotNull
public static Patchset build(Configuration configuration) {
String pullRequestId = configuration.getProperty(CliOption.PULL_REQUEST_ID);
String project = configuration.getProperty(GeneralOption.PROJECT);
String repository = configuration.getProperty(GeneralOption.REPOSITORY);
Provider provider = Provider.from(configuration.getProperty(CliOption.PROVIDER));

notBlank(pullRequestId, "You must provide non blank pull request id");
isTrue(NumberUtils.isNumber(pullRequestId), "Integer value as pull request id required");
notBlank(project, "You must provide non blank project key");
notBlank(repository, "You must provide non blank repository slug");
notNull(provider, "You must provide non blank SCM provider");

return new Patchset(Integer.parseInt(pullRequestId), String.format("%s/%s", project, repository), provider);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import pl.touk.sputnik.configuration.GeneralOptionNotSupportedException;
import pl.touk.sputnik.connector.ConnectorFacade;
import pl.touk.sputnik.connector.Connectors;
import pl.touk.sputnik.connector.Patchset;
import pl.touk.sputnik.review.Review;
import pl.touk.sputnik.review.ReviewFile;

Expand All @@ -24,7 +25,7 @@ public class GithubFacade implements ConnectorFacade {

private final Repo repo;

private final GithubPatchset githubPatchset;
private final Patchset patchset;

@Override
public Connectors name() {
Expand Down Expand Up @@ -66,6 +67,6 @@ public void publish(Review review) {
}

private Pull getPull() {
return repo.pulls().get(githubPatchset.getPullRequestId());
return repo.pulls().get(patchset.getPullRequestId());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,16 @@
import org.jetbrains.annotations.NotNull;
import pl.touk.sputnik.configuration.Configuration;
import pl.touk.sputnik.configuration.GeneralOption;
import pl.touk.sputnik.connector.Patchset;
import pl.touk.sputnik.connector.PatchsetBuilder;

@Slf4j
public class GithubFacadeBuilder {

@NotNull
public GithubFacade build(Configuration configuration) {

GithubPatchset githubPatchset = GithubPatchsetBuilder.build(configuration);
Patchset patchset = PatchsetBuilder.build(configuration);

String oAuthKey = configuration.getProperty(GeneralOption.GITHUB_API_KEY);
Github github = new RtGithub(
Expand All @@ -25,7 +27,7 @@ public GithubFacade build(Configuration configuration) {
.through(RetryWire.class)
);

Repo repo = github.repos().get(new Coordinates.Simple(githubPatchset.getProjectPath()));
return new GithubFacade(repo, githubPatchset);
Repo repo = github.repos().get(new Coordinates.Simple(patchset.getProjectPath()));
return new GithubFacade(repo, patchset);
}
}

This file was deleted.

16 changes: 8 additions & 8 deletions src/main/java/pl/touk/sputnik/connector/saas/SaasConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.apache.http.message.BasicNameValuePair;
import org.jetbrains.annotations.NotNull;
import pl.touk.sputnik.connector.Connector;
import pl.touk.sputnik.connector.github.GithubPatchset;
import pl.touk.sputnik.connector.Patchset;
import pl.touk.sputnik.connector.http.HttpConnector;

import java.io.IOException;
Expand All @@ -26,19 +26,19 @@
public class SaasConnector implements Connector {

private HttpConnector httpConnector;
private GithubPatchset githubPatchset;
private Patchset patchset;
private String apiKey;
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";
private static final String FILES_URL_FORMAT = "/api/%s/%s/pulls/%d/files";
private static final String VIOLATIONS_URL_FORMAT = "/api/%s/%s/pulls/%d/violations";

@NotNull
@Override
public String listFiles() throws URISyntaxException, IOException {
URI uri = httpConnector.buildUri(createUrl(githubPatchset, FILES_URL_FORMAT), params());
URI uri = httpConnector.buildUri(createUrl(patchset, FILES_URL_FORMAT), params());
HttpGet request = new HttpGet(uri);
CloseableHttpResponse httpResponse = httpConnector.logAndExecute(request);
return httpConnector.consumeAndLogEntity(httpResponse);
Expand All @@ -48,15 +48,15 @@ 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), params());
URI uri = httpConnector.buildUri(createUrl(patchset, VIOLATIONS_URL_FORMAT), params());
HttpPost httpPost = new HttpPost(uri);
httpPost.setEntity(new StringEntity(violationsAsJson, ContentType.APPLICATION_JSON));
CloseableHttpResponse httpResponse = httpConnector.logAndExecute(httpPost);
return httpConnector.consumeAndLogEntity(httpResponse);
}

private String createUrl(GithubPatchset patchset, String formatUrl) {
return String.format(formatUrl, patchset.getProjectPath(), patchset.getPullRequestId());
private String createUrl(Patchset patchset, String formatUrl) {
return String.format(formatUrl, patchset.getProvider().getName(), patchset.getProjectPath(), patchset.getPullRequestId());
}

@NotNull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import pl.touk.sputnik.configuration.CliOption;
import pl.touk.sputnik.configuration.Configuration;
import pl.touk.sputnik.connector.ConnectorDetails;
import pl.touk.sputnik.connector.github.GithubPatchsetBuilder;
import pl.touk.sputnik.connector.PatchsetBuilder;
import pl.touk.sputnik.connector.http.HttpConnector;
import pl.touk.sputnik.connector.http.HttpHelper;

Expand All @@ -28,7 +28,7 @@ public SaasFacade build(Configuration configuration) {

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

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
import org.mockito.runners.MockitoJUnitRunner;
import pl.touk.sputnik.configuration.Configuration;
import pl.touk.sputnik.configuration.ConfigurationSetup;
import pl.touk.sputnik.configuration.Provider;
import pl.touk.sputnik.connector.FacadeConfigUtil;
import pl.touk.sputnik.connector.Patchset;
import pl.touk.sputnik.review.*;

import javax.json.Json;
Expand Down Expand Up @@ -64,7 +66,7 @@ public void setUp() throws IOException {
when(pull.repo().git().commits()).thenReturn(commits);

config = new ConfigurationSetup().setUp(FacadeConfigUtil.getHttpConfig("github"), GITHUB_PATCHSET_MAP);
githubFacade = new GithubFacade(repo, new GithubPatchset(SOME_PULL_REQUEST_ID, projectPath()));
githubFacade = new GithubFacade(repo, new Patchset(SOME_PULL_REQUEST_ID, projectPath(), Provider.GITHUB));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.assertj.core.api.Assertions.assertThat;
import static pl.touk.sputnik.configuration.Provider.GITHUB;

public class SaasFacadeTest extends HttpConnectorEnv {

Expand All @@ -28,6 +29,7 @@ public class SaasFacadeTest extends HttpConnectorEnv {
private static final Map<String, String> GITHUB_PATCHSET_MAP = ImmutableMap.of(
"cli.pullRequestId", SOME_PULL_REQUEST_ID.toString(),
"cli.apiKey", SOME_API_KEY,
"cli.provider", GITHUB.getName(),
"connector.repository", SOME_REPOSITORY,
"connector.project", SOME_PROJECT
);
Expand Down Expand Up @@ -77,6 +79,7 @@ public void shouldThrowOnWrongApiKey() throws Exception {
SaasFacade saasFacade = buildFacade(ImmutableMap.of(
"cli.pullRequestId", SOME_PULL_REQUEST_ID.toString(),
"cli.apiKey", "WRONG_API_KEY",
"cli.provider", GITHUB.getName(),
"connector.repository", SOME_REPOSITORY,
"connector.project", SOME_PROJECT
));
Expand All @@ -92,6 +95,7 @@ public void shouldThrowOnWrongApiKey() throws Exception {
public void shouldHandleEmptyApiKey() throws Exception {
SaasFacade saasFacade = buildFacade(ImmutableMap.of(
"cli.pullRequestId", SOME_PULL_REQUEST_ID.toString(),
"cli.provider", GITHUB.getName(),
"connector.repository", SOME_REPOSITORY,
"connector.project", SOME_PROJECT
));
Expand All @@ -110,6 +114,7 @@ public void shouldSendBuildIdIfProvided() throws Exception {
SaasFacade saasFacade = buildFacade(ImmutableMap.of(
"cli.pullRequestId", SOME_PULL_REQUEST_ID.toString(),
"cli.buildId", "11223344",
"cli.provider", GITHUB.getName(),
"connector.repository", SOME_REPOSITORY,
"connector.project", SOME_PROJECT
));
Expand Down

0 comments on commit eb27fa5

Please sign in to comment.