Skip to content
This repository has been archived by the owner on Oct 30, 2019. It is now read-only.

Commit

Permalink
Make Slack optional
Browse files Browse the repository at this point in the history
  • Loading branch information
jhaber committed Apr 19, 2016
1 parent 9cfd0b4 commit 238e2fd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.hubspot.blazar.config;


import org.hibernate.validator.constraints.NotEmpty;

import com.fasterxml.jackson.annotation.JsonProperty;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ public void configure(Binder binder) {

binder.install(new BlazarDataModule());
binder.install(new BlazarSingularityModule(getConfiguration()));
binder.install(new BuildVisitorModule());
binder.install(new BuildVisitorModule(getConfiguration()));
binder.install(new DiscoveryModule());

binder.bind(IllegalArgumentExceptionMapper.class);
Expand All @@ -90,7 +90,11 @@ public void configure(Binder binder) {
binder.bind(RepositoryBuildResource.class);
binder.bind(BuildHistoryResource.class);
binder.bind(InstantMessageResource.class);
binder.bind(UserFeedbackResource.class);

if (getConfiguration().getSlackConfiguration().isPresent()) {
binder.bind(SlackClient.class);
binder.bind(UserFeedbackResource.class);
}

binder.bind(DataSourceFactory.class).toInstance(getConfiguration().getDatabaseConfiguration());
binder.bind(PropertyFilteringMessageBodyWriter.class)
Expand Down Expand Up @@ -146,12 +150,6 @@ public HttpClient provideHttpClient(ObjectMapper objectMapper) {
return new NingHttpClient(HttpConfig.newBuilder().setMaxRetries(5).setObjectMapper(objectMapper).build());
}

@Provides
@Singleton
public SlackClient providesSlackClient(AsyncHttpClient asyncHttpClient, BlazarConfiguration blazarConfiguration, ObjectMapper objectMapper, HttpClient httpClient) {
return new SlackClient(asyncHttpClient, blazarConfiguration, objectMapper, httpClient);
}

public static GitHub toGitHub(String host, GitHubConfiguration gitHubConfig) {
final String endpoint;
if ("github.com".equals(host) || "api.github.com".equals(host)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import com.hubspot.horizon.HttpRequest.Method;
import com.hubspot.horizon.HttpResponse;


@Singleton
public class SlackClient {
private static final Logger LOG = LoggerFactory.getLogger(SlackClient.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,14 @@
import com.google.inject.multibindings.Multibinder;
import com.hubspot.blazar.base.visitor.ModuleBuildVisitor;
import com.hubspot.blazar.base.visitor.RepositoryBuildVisitor;
import com.hubspot.blazar.config.BlazarConfiguration;

public class BuildVisitorModule implements Module {
private final BlazarConfiguration configuration;

public BuildVisitorModule(BlazarConfiguration configuration) {
this.configuration = configuration;
}

@Override
public void configure(Binder binder) {
Expand All @@ -24,11 +30,15 @@ public void configure(Binder binder) {
repositoryBuildVisitors.addBinding().to(CompletedRepositoryBuildVisitor.class);
// update GitHub status
repositoryBuildVisitors.addBinding().to(GitHubStatusVisitor.class);
// send Slack notifications
repositoryBuildVisitors.addBinding().to(SlackNotificationVisitor.class);
// monitor repo build state changes
repositoryBuildVisitors.addBinding().to(MetricBuildVisitor.class);

if (configuration.getSlackConfiguration().isPresent()) {
// send Slack notifications
repositoryBuildVisitors.addBinding().to(SlackNotificationVisitor.class);

}

Multibinder<ModuleBuildVisitor> moduleBuildVisitors = Multibinder.newSetBinder(binder, ModuleBuildVisitor.class);

// launch the queued build if nothing upstream
Expand All @@ -43,9 +53,12 @@ public void configure(Binder binder) {
moduleBuildVisitors.addBinding().to(DownstreamModuleBuildCanceller.class);
// complete the repository build once all of the module builds have finished
moduleBuildVisitors.addBinding().to(RepositoryBuildCompleter.class);
// send Slack notifications
moduleBuildVisitors.addBinding().to(SlackNotificationVisitor.class);
// monitor module build state changes
moduleBuildVisitors.addBinding().to(MetricBuildVisitor.class);

if (configuration.getSlackConfiguration().isPresent()) {
// send Slack notifications
moduleBuildVisitors.addBinding().to(SlackNotificationVisitor.class);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.hubspot.blazar.resources;


import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.Set;

Expand All @@ -28,12 +28,13 @@
@Consumes(MediaType.APPLICATION_JSON)
public class InstantMessageResource {

private final SlackClient slackClient;
private InstantMessageConfigurationService instantMessageConfigurationService;
@Inject(optional = true)
private SlackClient slackClient;

private final InstantMessageConfigurationService instantMessageConfigurationService;

@Inject
public InstantMessageResource(SlackClient slackClient, InstantMessageConfigurationService instantMessageConfigurationService) {
this.slackClient = slackClient;
public InstantMessageResource(InstantMessageConfigurationService instantMessageConfigurationService) {
this.instantMessageConfigurationService = instantMessageConfigurationService;
}

Expand Down Expand Up @@ -79,6 +80,6 @@ public Set<InstantMessageConfiguration> getAllWithBranchId(@PathParam("branchId"
@GET
@Path("/slack/list-channels")
public List<SlackChannel> getChannels() throws IOException {
return slackClient.getSlackChannels();
return slackClient == null ? Collections.<SlackChannel>emptyList() : slackClient.getSlackChannels();
}
}

0 comments on commit 238e2fd

Please sign in to comment.