Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ Build a default client:
Build a customized client:
```
DiggerClient client = DiggerClient.builder()
.createJobService(new CreateJobService())
.triggerBuildService(new TriggerBuildService(10000, 100))
.jobService(new JobService())
.triggerBuildService(new BuildService(10000, 100))
.artifactsService(artifactsService)
.withAuth("https://digger.com", "admin", "password")
.build();
```
Expand Down
27 changes: 18 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,21 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>groupId</groupId>
<groupId>org.jboss.aerogear</groupId>
<artifactId>jenkins-client</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>

<properties>
<junit.version>4.11</junit.version>
<mockito-core.version>1.9.5</mockito-core.version>
<jenkins.client.version>0.3.7-SNAPSHOT</jenkins.client.version>
<jtwig.templates.version>5.65</jtwig.templates.version>
<slf4j.api.version>1.7.21</slf4j.api.version>
<slf4j-log4j12.version>1.7.21</slf4j-log4j12.version>
<assertj-core.version>3.6.1</assertj-core.version>
</properties>

<licenses>
<license>
<name>Apache License, Version 2.0</name>
Expand All @@ -22,42 +32,41 @@
<dependency>
<groupId>com.offbytwo.jenkins</groupId>
<artifactId>jenkins-client</artifactId>
<version>0.3.7-SNAPSHOT</version>
<version>${jenkins.client.version}</version>
</dependency>
<dependency>
<groupId>org.jtwig</groupId>
<artifactId>jtwig-core</artifactId>
<version>5.65</version>
<version>${jtwig.templates.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
<version>${slf4j.api.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
<version>${slf4j-log4j12.version}</version>
</dependency>

<!--Testing-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>1.10.19</version>
<version>${mockito-core.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
<version>3.6.1</version>
<version>${assertj-core.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Expand Down
62 changes: 52 additions & 10 deletions src/main/java/com/redhat/digkins/DiggerClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import com.offbytwo.jenkins.JenkinsServer;
import com.redhat.digkins.model.BuildStatus;
import com.redhat.digkins.services.CreateJobService;
import com.redhat.digkins.services.ArtifactsService;
import com.redhat.digkins.services.JobService;
import com.redhat.digkins.services.TriggerBuildService;
import com.redhat.digkins.util.DiggerClientException;
import com.redhat.digkins.util.JenkinsAuth;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;

Expand All @@ -24,8 +27,9 @@ public class DiggerClient {

private JenkinsServer jenkinsServer;

private CreateJobService createJobService;
private JobService jobService;
private TriggerBuildService triggerBuildService;
private ArtifactsService artifactsService;

private DiggerClient() {
}
Expand All @@ -42,29 +46,33 @@ private DiggerClient() {
* @throws DiggerClientException if something goes wrong
*/
public static DiggerClient createDefaultWithAuth(String url, String user, String password) throws DiggerClientException {
TriggerBuildService triggerBuildService = new TriggerBuildService(TriggerBuildService.DEFAULT_FIRST_CHECK_DELAY, TriggerBuildService.DEFAULT_POLL_PERIOD);
JobService jobService = new JobService();
ArtifactsService artifactsService = new ArtifactsService();
return DiggerClient.builder()
.createJobService(new CreateJobService())
.triggerBuildService(new TriggerBuildService(TriggerBuildService.DEFAULT_FIRST_CHECK_DELAY, TriggerBuildService.DEFAULT_POLL_PERIOD))
.createJobService(jobService)
.triggerBuildService(triggerBuildService)
.artifactsService(artifactsService)
.withAuth(url, user, password)
.build();
}

public static DiggerClientBuilder builder() {
return new DiggerClientBuilder();
}

public static class DiggerClientBuilder {
private JenkinsAuth auth;
private CreateJobService createJobService;
private JobService jobService;
private TriggerBuildService triggerBuildService;
private ArtifactsService artifactsService;

public DiggerClientBuilder withAuth(String url, String user, String password) {
this.auth = new JenkinsAuth(url, user, password);
return this;
}

public DiggerClientBuilder createJobService(CreateJobService createJobService) {
this.createJobService = createJobService;
public DiggerClientBuilder createJobService(JobService jobService) {
this.jobService = jobService;
return this;
}

Expand All @@ -73,12 +81,18 @@ public DiggerClientBuilder triggerBuildService(TriggerBuildService triggerBuildS
return this;
}

public DiggerClientBuilder artifactsService(ArtifactsService artifactsService) {
this.artifactsService = artifactsService;
return this;
}

public DiggerClient build() throws DiggerClientException {
final DiggerClient client = new DiggerClient();
try {
client.jenkinsServer = new JenkinsServer(new URI(auth.getUrl()), auth.getUser(), auth.getPassword());
client.createJobService = this.createJobService;
client.jobService = this.jobService;
client.triggerBuildService = this.triggerBuildService;
client.artifactsService = this.artifactsService;
return client;
} catch (URISyntaxException e) {
throw new DiggerClientException("Invalid jenkins url format.");
Expand All @@ -97,7 +111,7 @@ public DiggerClient build() throws DiggerClientException {
*/
public void createJob(String name, String gitRepo, String gitBranch) throws DiggerClientException {
try {
createJobService.create(this.jenkinsServer, name, gitRepo, gitBranch);
jobService.create(this.jenkinsServer, name, gitRepo, gitBranch);
} catch (Throwable e) {
throw new DiggerClientException(e);
}
Expand Down Expand Up @@ -153,4 +167,32 @@ public BuildStatus build(String jobName) throws DiggerClientException {
return this.build(jobName, DEFAULT_BUILD_TIMEOUT);
}

/**
* Fetch artifacts urls for specific job and build number
*
* @param jobName name of the job
* @param buildNumber job build number
* @param artifactName - name of the artifact to fetch - can be regexp
* @return InputStream with file contents
* @throws DiggerClientException - when problem with fetching artifacts from jenkins
*/
public InputStream fetchArtifact(String jobName, int buildNumber, String artifactName) throws DiggerClientException {
return artifactsService.streamArtifact(jenkinsServer,jobName, buildNumber, artifactName);
}

/**
* Save artifact for specified location for specific job, build number and artifact name.
* If name would be an regular expression method would return stream for the first match.
*
* @param jobName name of the job
* @param buildNumber job build number
* @param artifactName name of the artifact to fetch - can be regexp for example *.apk
* @param outputFile file (location) used to save artifact
*
* @throws DiggerClientException when problem with fetching artifacts from jenkins
* @throws IOException when one of the files cannot be saved
*/
public void saveArtifact(String jobName, int buildNumber, String artifactName, File outputFile) throws DiggerClientException, IOException {
artifactsService.saveArtifact(jenkinsServer,jobName, buildNumber, artifactName,outputFile);
}
}
36 changes: 36 additions & 0 deletions src/main/java/com/redhat/digkins/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.redhat.digkins;

import com.redhat.digkins.services.JobService;
import com.redhat.digkins.services.TriggerBuildService;
import com.redhat.digkins.util.DiggerClientException;
import org.apache.commons.io.IOUtils;

import java.io.*;

/**
*/
public class Main {

public static void main(String[] args) throws DiggerClientException, IOException {
DiggerClient client = DiggerClient.builder()
.createJobService(new JobService())
.triggerBuildService(new TriggerBuildService(10000, 100))
.withAuth("https://jenkins-digger2.osm3.feedhenry.net", "admin", "Vu8ysYH5f2dJiLgL")
.build();
//client.createJob("wtr-java-tests2", "https://github.com/wtrocki/helloworld-android-gradle", "master");
//client.build("wtr-java-tests2");
InputStream inputStream = client.fetchArtifact("wtr-java-tests2", 1, "app-debug.apk");
if(inputStream != null){
File targetFile = new File("artifact.tmp");
OutputStream outStream = new FileOutputStream(targetFile);

byte[] buffer = new byte[8 * 1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outStream);
}
}
}
94 changes: 94 additions & 0 deletions src/main/java/com/redhat/digkins/services/ArtifactsService.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
package com.redhat.digkins.services;

import com.offbytwo.jenkins.JenkinsServer;
import com.offbytwo.jenkins.model.Artifact;
import com.offbytwo.jenkins.model.Build;
import com.offbytwo.jenkins.model.BuildWithDetails;
import com.offbytwo.jenkins.model.JobWithDetails;
import com.redhat.digkins.util.DiggerClientException;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.net.URISyntaxException;
import java.util.List;

/**
* Service used to retrieve artifacts
*/
public class ArtifactsService {

/**
*/
public ArtifactsService() {
}

private static final Logger LOG = LoggerFactory.getLogger(ArtifactsService.class);
private static int DEFAULT_BUFFER = 8 * 1024;

/**
* Save artifact for specified location for specific job, build number and artifact name.
* If name would be an regular expression method would return stream for the first match.
*
* @param jobName name of the job
* @param buildNumber job build number
* @param artifactName name of the artifact to fetch - can be regexp for example *.apk
* @param outputFile file (location) used to save artifact
* @throws DiggerClientException when problem with fetching artifacts from jenkins
* @throws IOException when one of the files cannot be saved
*/
public void saveArtifact(JenkinsServer jenkins, String jobName, int buildNumber, String artifactName, File outputFile) throws DiggerClientException, IOException {
InputStream inputStream = streamArtifact(jenkins, jobName, buildNumber, artifactName);
if (inputStream != null) {
OutputStream outStream = new FileOutputStream(outputFile);
byte[] buffer = new byte[DEFAULT_BUFFER];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
IOUtils.closeQuietly(inputStream);
IOUtils.closeQuietly(outStream);
} else {
throw new DiggerClientException("Cannot fetch artifacts from jenkins");
}
}

/**
* Get artifact inputstream for specific job, build number and artifact name.
* If name would be an regular expression method would return stream for the first match.
*
* @param jobName name of the job
* @param buildNumber job build number
* @param artifactName name of the artifact to fetch - can be regexp for example *.apk
* @return InputStream with file contents that can be saved or piped to socket
* @throws DiggerClientException when problem with fetching artifacts from jenkins
*/
public InputStream streamArtifact(JenkinsServer jenkins, String jobName, int buildNumber, String artifactName) throws DiggerClientException {
try {
JobWithDetails job = jenkins.getJob(jobName);
if (job == null) {
LOG.error("Cannot fetch job from jenkins {0}", jobName);
throw new DiggerClientException("Cannot fetch job from jenkins");
}
Build build = job.getBuildByNumber(buildNumber);
BuildWithDetails buildWithDetails = build.details();
List<Artifact> artifacts = buildWithDetails.getArtifacts();
for (Artifact artifact : artifacts) {
if (artifact.getFileName().matches(artifactName)) {
LOG.debug("Streaming artifact {0}", artifactName);
return buildWithDetails.downloadArtifact(artifact);
}
}
} catch (URISyntaxException e) {
LOG.error("Invalid job name {0}", jobName, e);
throw new DiggerClientException(e);
} catch (IOException e) {
LOG.error("Problem when fetching artifacts for {0} {1} {2}", jobName, buildNumber, artifactName, e);
throw new DiggerClientException(e);
}
LOG.debug("Cannot find build for ", jobName, buildNumber, artifactName);
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@
/**
* Create digger job on jenkins platform
*/
public class CreateJobService {
public class JobService {

private final static String GIT_REPO_URL = "GIT_REPO_URL";
private final static String GIT_REPO_BRANCH = "GIT_REPO_BRANCH";

private static final String JOB_TEMPLATE_PATH = "templates/job.xml";

/**
Expand All @@ -29,5 +28,4 @@ public void create(JenkinsServer jenkinsServer, String name, String gitRepo, Str
JtwigModel model = JtwigModel.newModel().with(GIT_REPO_URL, gitRepo).with(GIT_REPO_BRANCH, gitBranch);
jenkinsServer.createJob(name, template.render(model));
}

}
Loading