@@ -22,30 +22,71 @@ public class DiggerClient {
22
22
23
23
public static final long DEFAULT_BUILD_TIMEOUT = 60 * 1000 ;
24
24
25
- private final JenkinsServer jenkins ;
25
+ private JenkinsServer jenkinsServer ;
26
26
27
- public DiggerClient (JenkinsAuth auth ) throws URISyntaxException {
28
- this .jenkins = new JenkinsServer (new URI (auth .getUrl ()), auth .getUser (), auth .getPassword ());
27
+ private CreateJobService createJobService ;
28
+ private TriggerBuildService triggerBuildService ;
29
+
30
+ private DiggerClient () {
29
31
}
30
32
31
33
/**
32
- * Create client using provided url and credentials
34
+ * Create a client with defaults using provided url and credentials.
35
+ * <p>
36
+ * This client will use the defaults for the services. This is perfectly fine for majorith of the cases.
33
37
*
34
38
* @param url Jenkins url
35
39
* @param user Jenkins user
36
40
* @param password Jenkins password
37
41
* @return client instance
38
42
* @throws DiggerClientException if something goes wrong
39
43
*/
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." );
44
+ public static DiggerClient createDefaultWithAuth (String url , String user , String password ) throws DiggerClientException {
45
+ return DiggerClient .builder ()
46
+ .createJobService (new CreateJobService ())
47
+ .triggerBuildService (new TriggerBuildService (TriggerBuildService .DEFAULT_FIRST_CHECK_DELAY , TriggerBuildService .DEFAULT_POLL_PERIOD ))
48
+ .withAuth (url , user , password )
49
+ .build ();
50
+ }
51
+
52
+ public static DiggerClientBuilder builder () {
53
+ return new DiggerClientBuilder ();
54
+ }
55
+
56
+ public static class DiggerClientBuilder {
57
+ private JenkinsAuth auth ;
58
+ private CreateJobService createJobService ;
59
+ private TriggerBuildService triggerBuildService ;
60
+
61
+ public DiggerClientBuilder withAuth (String url , String user , String password ) {
62
+ this .auth = new JenkinsAuth (url , user , password );
63
+ return this ;
64
+ }
65
+
66
+ public DiggerClientBuilder createJobService (CreateJobService createJobService ) {
67
+ this .createJobService = createJobService ;
68
+ return this ;
69
+ }
70
+
71
+ public DiggerClientBuilder triggerBuildService (TriggerBuildService triggerBuildService ) {
72
+ this .triggerBuildService = triggerBuildService ;
73
+ return this ;
74
+ }
75
+
76
+ public DiggerClient build () throws DiggerClientException {
77
+ final DiggerClient client = new DiggerClient ();
78
+ try {
79
+ client .jenkinsServer = new JenkinsServer (new URI (auth .getUrl ()), auth .getUser (), auth .getPassword ());
80
+ client .createJobService = this .createJobService ;
81
+ client .triggerBuildService = this .triggerBuildService ;
82
+ return client ;
83
+ } catch (URISyntaxException e ) {
84
+ throw new DiggerClientException ("Invalid jenkins url format." );
85
+ }
46
86
}
47
87
}
48
88
89
+
49
90
/**
50
91
* Create new Digger job on Jenkins platform
51
92
*
@@ -55,9 +96,8 @@ public static DiggerClient from(String url, String user, String password) throws
55
96
* @throws DiggerClientException if something goes wrong
56
97
*/
57
98
public void createJob (String name , String gitRepo , String gitBranch ) throws DiggerClientException {
58
- CreateJobService service = new CreateJobService (this .jenkins );
59
99
try {
60
- service .create (name , gitRepo , gitBranch );
100
+ createJobService .create (this . jenkinsServer , name , gitRepo , gitBranch );
61
101
} catch (Throwable e ) {
62
102
throw new DiggerClientException (e );
63
103
}
@@ -72,22 +112,21 @@ public void createJob(String name, String gitRepo, String gitBranch) throws Digg
72
112
* 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
113
* after the given timeout period, a {@code BuildStatus} is returned with state {@link BuildStatus.State#TIMED_OUT}.
74
114
* <p>
75
- * Please note that timeout period is never meant to be very precise. It has the resolution of {@link TriggerBuildService#POLL_PERIOD } because
115
+ * Please note that timeout period is never meant to be very precise. It has the resolution of {@link TriggerBuildService#DEFAULT_POLL_PERIOD } because
76
116
* timeout is checked before every pull.
77
117
* <p>
78
118
* Similarly, {@link BuildStatus.State#CANCELLED_IN_QUEUE} is returned if the build is cancelled on Jenkins side and
79
119
* {@link BuildStatus.State#STUCK_IN_QUEUE} is returned if the build is stuck.
80
120
*
81
121
* @param jobName name of the job to trigger the build
82
122
* @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 }
123
+ * Should be larger than {@link TriggerBuildService#DEFAULT_FIRST_CHECK_DELAY }
84
124
* @return the build status
85
125
* @throws DiggerClientException if connection problems occur during connecting to Jenkins
86
126
*/
87
127
public BuildStatus build (String jobName , long timeout ) throws DiggerClientException {
88
- final TriggerBuildService triggerBuildService = new TriggerBuildService (jenkins );
89
128
try {
90
- return triggerBuildService .build (jobName , timeout );
129
+ return triggerBuildService .build (this . jenkinsServer , jobName , timeout );
91
130
} catch (IOException e ) {
92
131
LOG .debug ("Exception while connecting to Jenkins" , e );
93
132
throw new DiggerClientException (e );
0 commit comments