Skip to content

Commit

Permalink
implementation of ability to plug in custom CI providers using the Se…
Browse files Browse the repository at this point in the history
…rviceLoader
  • Loading branch information
boxheed committed Feb 24, 2015
1 parent 65bd351 commit 0351db3
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,22 @@ package nebula.plugin.info.ci

import nebula.plugin.info.InfoBrokerPlugin
import nebula.plugin.info.InfoCollectorPlugin

import org.gradle.api.Plugin
import org.gradle.api.Project
import org.gradle.api.internal.IConventionAware

class ContinuousIntegrationInfoPlugin implements Plugin<Project>, InfoCollectorPlugin {
protected Project project
List<ContinuousIntegrationInfoProvider> providers


ContinuousIntegrationInfoProvider selectedProvider
ContinuousIntegrationInfoExtension extension

@Override
void apply(Project project) {
this.project = project

providers = [new JenkinsProvider(), new UnknownContinuousIntegrationProvider()]
selectedProvider = findProvider()

extension = project.extensions.create('ciinfo', ContinuousIntegrationInfoExtension)
Expand All @@ -37,11 +38,6 @@ class ContinuousIntegrationInfoPlugin implements Plugin<Project>, InfoCollectorP
}

ContinuousIntegrationInfoProvider findProvider() {
def provider = providers.find { it.supports(project) }
if (provider) {
return provider
} else {
throw new IllegalStateException('Unable to find a SCM provider, even the Unknown provider')
}
return new ContinuousIntegrationInfoProviderResolver().findProvider(project)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package nebula.plugin.info.ci;

import java.util.Collection;
import java.util.LinkedList;
import java.util.ServiceLoader;

public class ContinuousIntegrationInfoProviderResolver {

private static ServiceLoader<ContinuousIntegrationInfoProvider> continuousIntegrationInfoProviderServiceLoader = ServiceLoader
.load(ContinuousIntegrationInfoProvider.class);

def all () {
return continuousIntegrationInfoProviderServiceLoader.asList() << new UnknownContinuousIntegrationProvider();
}

ContinuousIntegrationInfoProvider findProvider(project) {

def provider = continuousIntegrationInfoProviderServiceLoader.find { it.supports(project) }

if (provider) {
return provider
} else {
return new UnknownContinuousIntegrationProvider()
}
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
nebula.plugin.info.ci.JenkinsProvider
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package nebula.plugin.info.ci

import nebula.plugin.info.InfoBrokerPlugin
import nebula.plugin.info.InfoBrokerPlugin.ManifestEntry
import nebula.plugin.info.basic.BasicInfoPlugin
import nebula.test.ProjectSpec

import org.gradle.api.NamedDomainObjectContainer

import spock.lang.Ignore

import java.util.concurrent.atomic.AtomicBoolean

class ContinuousIntegrationInfoProviderResolverSpec extends ProjectSpec {

def 'get all configured info providers'() {
when:
def resolver = new ContinuousIntegrationInfoProviderResolver()

then:
def providers = resolver.all()
providers != null
providers.size() == 2
providers[0].getClass().equals(JenkinsProvider.class)
providers[1].getClass().equals(UnknownContinuousIntegrationProvider.class)
}

def 'get Jenkins provider if running on Jenkins'() {
when:
def onJenkins = System.getenv('BUILD_NUMBER') && System.getenv('JOB_NAME')
def resolver = new ContinuousIntegrationInfoProviderResolver()

then:
def provider = resolver.findProvider(project)
if(onJenkins) {
provider.getClass().equals(JenkinsProvider.class)
} else {
provider.getClass().equals(UnknownContinuousIntegrationProvider.class)
}
}
}

0 comments on commit 0351db3

Please sign in to comment.