|
| 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 | +} |
0 commit comments