Skip to content

Commit 891c47f

Browse files
committed
AGDIGGER-58 - Digger java client backbone
1 parent e11ea50 commit 891c47f

File tree

8 files changed

+278
-0
lines changed

8 files changed

+278
-0
lines changed

.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
target/
2+
pom.xml.tag
3+
pom.xml.releaseBackup
4+
pom.xml.versionsBackup
5+
pom.xml.next
6+
release.properties
7+
dependency-reduced-pom.xml
8+
buildNumber.properties
9+
.mvn/timing.properties
10+
.idea
11+
jenkins-client.iml
12+
# Exclude maven wrapper
13+
!/.mvn/wrapper/maven-wrapper.jar

README.md

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
## Digger Java client
2+
3+
A java integration library for AeroGear Digger
4+
5+
## Usage
6+
7+
Create job:
8+
9+
```
10+
DiggerClient client = DiggerClient.from("https://digger.com", "admin", "password");
11+
client.createJob("java-client-job1","https://github.com/wtrocki/helloworld-android-gradle","master");
12+
```
13+
14+
## Requirements
15+
16+
Client works with Java6 and above.
17+
18+
## Building
19+
20+
`mvn clean package`

pom.xml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>groupId</groupId>
8+
<artifactId>jenkins-client</artifactId>
9+
<version>1.0.0</version>
10+
<packaging>jar</packaging>
11+
12+
<licenses>
13+
<license>
14+
<name>Apache License, Version 2.0</name>
15+
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
16+
<distribution>repo</distribution>
17+
</license>
18+
</licenses>
19+
20+
<dependencies>
21+
<dependency>
22+
<groupId>com.offbytwo.jenkins</groupId>
23+
<artifactId>jenkins-client</artifactId>
24+
<version>0.3.6</version>
25+
</dependency>
26+
<dependency>
27+
<groupId>org.jtwig</groupId>
28+
<artifactId>jtwig-core</artifactId>
29+
<version>5.65</version>
30+
</dependency>
31+
</dependencies>
32+
<repositories>
33+
<repository>
34+
<id>jcenter</id>
35+
<url>https://jcenter.bintray.com/</url>
36+
</repository>
37+
</repositories>
38+
<build>
39+
<plugins>
40+
<plugin>
41+
<groupId>org.apache.maven.plugins</groupId>
42+
<artifactId>maven-compiler-plugin</artifactId>
43+
<version>3.6.0</version>
44+
<configuration>
45+
<source>1.6</source>
46+
<target>1.6</target>
47+
</configuration>
48+
</plugin>
49+
</plugins>
50+
</build>
51+
</project>
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
package com.redhat.digkins;
2+
3+
import com.offbytwo.jenkins.JenkinsServer;
4+
import com.redhat.digkins.services.CreateJobService;
5+
import com.redhat.digkins.util.JenkinsAuth;
6+
import com.redhat.digkins.util.DiggerClientException;
7+
8+
import java.net.URI;
9+
import java.net.URISyntaxException;
10+
11+
/**
12+
* Digger Java Client
13+
* <p>
14+
* Interact with digger jenkins api!
15+
*/
16+
public class DiggerClient {
17+
18+
private final JenkinsServer jenkins;
19+
20+
public DiggerClient(JenkinsAuth auth) throws URISyntaxException {
21+
this.jenkins = new JenkinsServer(new URI(auth.getUrl()), auth.getUser(), auth.getPassword());
22+
}
23+
24+
/**
25+
* Create new digger job on jenkins platform
26+
*
27+
* @param name - job name that can be used later to reference job
28+
* @param gitRepo - git repository url (full git repository url. e.g [email protected]:wtrocki/helloworld-android-gradle.git
29+
* @param gitBranch - git repository branch (default branch used to checkout source code)
30+
*/
31+
public void createJob(String name, String gitRepo, String gitBranch) throws DiggerClientException {
32+
CreateJobService service = new CreateJobService(this.jenkins);
33+
try {
34+
service.create(name, gitRepo, gitBranch);
35+
} catch (Throwable e) {
36+
throw new DiggerClientException(e);
37+
}
38+
}
39+
40+
/**
41+
* Create client using provided url and credentials
42+
*
43+
* @param url - jenkins url
44+
* @param user - jenkins user
45+
* @param password - jenkins password
46+
* @return client instance
47+
*/
48+
public static DiggerClient from(String url, String user, String password) throws DiggerClientException {
49+
try {
50+
JenkinsAuth jenkinsAuth = new JenkinsAuth(url, user, password);
51+
return new DiggerClient(jenkinsAuth);
52+
} catch (URISyntaxException e) {
53+
throw new DiggerClientException("Invalid jenkins url format.");
54+
}
55+
}
56+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.redhat.digkins.services;
2+
3+
import com.offbytwo.jenkins.JenkinsServer;
4+
import org.apache.commons.io.FileUtils;
5+
import org.jtwig.JtwigModel;
6+
import org.jtwig.JtwigTemplate;
7+
8+
import java.io.File;
9+
import java.io.IOException;
10+
11+
/**
12+
* Create digger job on jenkins platform
13+
*/
14+
public class CreateJobService {
15+
16+
private JenkinsServer jenkins;
17+
18+
private final static String GIT_REPO_URL = "GIT_REPO_URL", GIT_REPO_BRANCH = "GIT_REPO_BRANCH";
19+
20+
/**
21+
* @param jenkins - jenkins api instance
22+
*/
23+
public CreateJobService(JenkinsServer jenkins) {
24+
this.jenkins = jenkins;
25+
}
26+
27+
/**
28+
* Create new digger job on jenkins platform
29+
*
30+
* @param name - job name that can be used later to reference job
31+
* @param gitRepo - git repository url (full git repository url. e.g [email protected]:digger/helloworld.git
32+
* @param gitBranch - git repository branch (default branch used to checkout source code)
33+
*/
34+
public void create(String name, String gitRepo, String gitBranch) throws IOException {
35+
JtwigTemplate template = JtwigTemplate.classpathTemplate("templates/job.xml");
36+
JtwigModel model = JtwigModel.newModel().with(GIT_REPO_URL, gitRepo).with(GIT_REPO_BRANCH, gitBranch);
37+
jenkins.createJob(name, template.render(model));
38+
}
39+
40+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.redhat.digkins.util;
2+
3+
import java.util.TreeMap;
4+
5+
/**
6+
* Represents internal client exception
7+
*/
8+
public class DiggerClientException extends Exception {
9+
10+
public DiggerClientException() {
11+
super();
12+
}
13+
14+
public DiggerClientException(String message) {
15+
super(message);
16+
}
17+
18+
public DiggerClientException(String message, Throwable cause) {
19+
super(message, cause);
20+
}
21+
22+
public DiggerClientException(Throwable cause) {
23+
super(cause);
24+
}
25+
26+
@Override
27+
public Throwable fillInStackTrace() {
28+
return null;
29+
}
30+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.redhat.digkins.util;
2+
3+
/**
4+
* Jenkins authentication object.
5+
*/
6+
public class JenkinsAuth {
7+
8+
private String url, user, password;
9+
10+
public JenkinsAuth(String url, String user, String password) {
11+
this.url = url;
12+
this.user = user;
13+
this.password = password;
14+
}
15+
16+
public String getUrl() {
17+
return url;
18+
}
19+
20+
public void setUrl(String url) {
21+
this.url = url;
22+
}
23+
24+
public String getUser() {
25+
return user;
26+
}
27+
28+
public void setUser(String user) {
29+
this.user = user;
30+
}
31+
32+
public String getPassword() {
33+
return password;
34+
}
35+
36+
public void setPassword(String password) {
37+
this.password = password;
38+
}
39+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<flow-definition plugin="[email protected]">
2+
<description/>
3+
<keepDependencies>false</keepDependencies>
4+
<properties>
5+
<org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
6+
<triggers/>
7+
</org.jenkinsci.plugins.workflow.job.properties.PipelineTriggersJobProperty>
8+
</properties>
9+
<definition class="org.jenkinsci.plugins.workflow.cps.CpsScmFlowDefinition" plugin="[email protected]">
10+
<scm class="hudson.plugins.git.GitSCM" plugin="[email protected]">
11+
<configVersion>2</configVersion>
12+
<userRemoteConfigs>
13+
<hudson.plugins.git.UserRemoteConfig>
14+
<url>{{GIT_REPO_URL}}</url>
15+
</hudson.plugins.git.UserRemoteConfig>
16+
</userRemoteConfigs>
17+
<branches>
18+
<hudson.plugins.git.BranchSpec>
19+
<name>{{GIT_REPO_BRANCH}}</name>
20+
</hudson.plugins.git.BranchSpec>
21+
</branches>
22+
<doGenerateSubmoduleConfigurations>false</doGenerateSubmoduleConfigurations>
23+
<submoduleCfg class="list"/>
24+
<extensions/>
25+
</scm>
26+
<scriptPath>Jenkinsfile</scriptPath>
27+
</definition>
28+
<triggers/>
29+
</flow-definition>

0 commit comments

Comments
 (0)