1
1
package com .redhat .digkins ;
2
2
3
3
import com .offbytwo .jenkins .JenkinsServer ;
4
+ import com .redhat .digkins .model .BuildStatus ;
4
5
import com .redhat .digkins .services .CreateJobService ;
5
- import com .redhat .digkins .util . JenkinsAuth ;
6
+ import com .redhat .digkins .services . TriggerBuildService ;
6
7
import com .redhat .digkins .util .DiggerClientException ;
8
+ import com .redhat .digkins .util .JenkinsAuth ;
9
+ import org .slf4j .Logger ;
10
+ import org .slf4j .LoggerFactory ;
7
11
12
+ import java .io .IOException ;
8
13
import java .net .URI ;
9
14
import java .net .URISyntaxException ;
10
15
11
16
/**
12
- * Digger Java Client
13
- * <p>
14
- * Interact with digger jenkins api!
17
+ * Digger Java Client interact with Digger Jenkins api.
15
18
*/
16
19
public class DiggerClient {
17
20
21
+ private static final Logger LOG = LoggerFactory .getLogger (DiggerClient .class );
22
+
23
+ public static final long DEFAULT_BUILD_TIMEOUT = 60 * 1000 ;
24
+
18
25
private final JenkinsServer jenkins ;
19
26
20
27
public DiggerClient (JenkinsAuth auth ) throws URISyntaxException {
21
28
this .jenkins = new JenkinsServer (new URI (auth .getUrl ()), auth .getUser (), auth .getPassword ());
22
29
}
23
30
24
31
/**
25
- * Create new digger job on jenkins platform
32
+ * Create client using provided url and credentials
33
+ *
34
+ * @param url Jenkins url
35
+ * @param user Jenkins user
36
+ * @param password Jenkins password
37
+ * @return client instance
38
+ * @throws DiggerClientException if something goes wrong
39
+ */
40
+ public static DiggerClient from (String url , String user , String password ) throws DiggerClientException {
41
+ try {
42
+ JenkinsAuth jenkinsAuth = new JenkinsAuth (url , user , password );
43
+ return new DiggerClient (jenkinsAuth );
44
+ } catch (URISyntaxException e ) {
45
+ throw new DiggerClientException ("Invalid jenkins url format." );
46
+ }
47
+ }
48
+
49
+ /**
50
+ * Create new Digger job on Jenkins platform
26
51
*
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)
52
+ * @param name job name that can be used later to reference job
53
+ * @param gitRepo git repository url (full git repository url. e.g [email protected] :wtrocki/helloworld-android-gradle.git
54
+ * @param gitBranch git repository branch (default branch used to checkout source code)
55
+ * @throws DiggerClientException if something goes wrong
30
56
*/
31
57
public void createJob (String name , String gitRepo , String gitBranch ) throws DiggerClientException {
32
58
CreateJobService service = new CreateJobService (this .jenkins );
@@ -38,19 +64,54 @@ public void createJob(String name, String gitRepo, String gitBranch) throws Digg
38
64
}
39
65
40
66
/**
41
- * Create client using provided url and credentials
67
+ * Triggers a build for the given job and waits until it leaves the queue and actually starts.
68
+ * <p>
69
+ * Jenkins puts the build requests in a queue and once there is a slave available, it starts building
70
+ * it and a build number is assigned to the build.
71
+ * <p>
72
+ * This method will block until there is a build number, or the given timeout period is passed. If the build is still in the queue
73
+ * after the given timeout period, a {@code BuildStatus} is returned with state {@link BuildStatus.State#TIMED_OUT}.
74
+ * <p>
75
+ * Please note that timeout period is never meant to be very precise. It has the resolution of {@link TriggerBuildService#POLL_PERIOD} because
76
+ * timeout is checked before every pull.
77
+ * <p>
78
+ * Similarly, {@link BuildStatus.State#CANCELLED_IN_QUEUE} is returned if the build is cancelled on Jenkins side and
79
+ * {@link BuildStatus.State#STUCK_IN_QUEUE} is returned if the build is stuck.
42
80
*
43
- * @param url - jenkins url
44
- * @param user - jenkins user
45
- * @param password - jenkins password
46
- * @return client instance
81
+ * @param jobName name of the job to trigger the build
82
+ * @param timeout how many milliseconds should this call block before returning {@link BuildStatus.State#TIMED_OUT}.
83
+ * Should be larger than {@link TriggerBuildService#FIRST_CHECK_DELAY}
84
+ * @return the build status
85
+ * @throws DiggerClientException if connection problems occur during connecting to Jenkins
47
86
*/
48
- public static DiggerClient from (String url , String user , String password ) throws DiggerClientException {
87
+ public BuildStatus build (String jobName , long timeout ) throws DiggerClientException {
88
+ final TriggerBuildService triggerBuildService = new TriggerBuildService (jenkins );
49
89
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." );
90
+ return triggerBuildService .build (jobName , timeout );
91
+ } catch (IOException e ) {
92
+ LOG .debug ("Exception while connecting to Jenkins" , e );
93
+ throw new DiggerClientException (e );
94
+ } catch (InterruptedException e ) {
95
+ LOG .debug ("Exception while waiting on Jenkins" , e );
96
+ throw new DiggerClientException (e );
97
+ } catch (Throwable e ) {
98
+ LOG .debug ("Exception while triggering a build" , e );
99
+ throw new DiggerClientException (e );
54
100
}
55
101
}
102
+
103
+ /**
104
+ * Triggers a build for the given job and waits until it leaves the queue and actually starts.
105
+ * <p>
106
+ * Calls {@link #build(String, long)} with a default timeout of {@link #DEFAULT_BUILD_TIMEOUT}.
107
+ *
108
+ * @param jobName name of the job
109
+ * @return the build status
110
+ * @throws DiggerClientException if connection problems occur during connecting to Jenkins
111
+ * @see #build(String, long)
112
+ */
113
+ public BuildStatus build (String jobName ) throws DiggerClientException {
114
+ return this .build (jobName , DEFAULT_BUILD_TIMEOUT );
115
+ }
116
+
56
117
}
0 commit comments