forked from jenkinsci/java-client-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJenkinsServer.java
More file actions
995 lines (910 loc) · 34.4 KB
/
JenkinsServer.java
File metadata and controls
995 lines (910 loc) · 34.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
/*
* Copyright (c) 2013 Cosmin Stejerean, Karl Heinz Marbaise, and contributors.
*
* Distributed under the MIT license: http://opensource.org/licenses/MIT
*/
package com.offbytwo.jenkins;
import java.io.IOException;
import java.net.URI;
import java.util.List;
import java.util.Map;
import javax.xml.bind.JAXBException;
import org.apache.http.HttpStatus;
import org.apache.http.client.HttpResponseException;
import org.apache.http.entity.ContentType;
import org.dom4j.DocumentException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Function;
import com.google.common.base.Optional;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Maps;
import com.offbytwo.jenkins.client.JenkinsHttpClient;
import com.offbytwo.jenkins.client.JenkinsHttpConnection;
import com.offbytwo.jenkins.client.util.EncodingUtils;
import com.offbytwo.jenkins.client.util.UrlUtils;
import com.offbytwo.jenkins.helper.JenkinsVersion;
import com.offbytwo.jenkins.model.Build;
import com.offbytwo.jenkins.model.Computer;
import com.offbytwo.jenkins.model.ComputerSet;
import com.offbytwo.jenkins.model.FolderJob;
import com.offbytwo.jenkins.model.Job;
import com.offbytwo.jenkins.model.JobConfiguration;
import com.offbytwo.jenkins.model.JobWithDetails;
import com.offbytwo.jenkins.model.LabelWithDetails;
import com.offbytwo.jenkins.model.MainView;
import com.offbytwo.jenkins.model.MavenJobWithDetails;
import com.offbytwo.jenkins.model.PluginManager;
import com.offbytwo.jenkins.model.Queue;
import com.offbytwo.jenkins.model.QueueItem;
import com.offbytwo.jenkins.model.QueueReference;
import com.offbytwo.jenkins.model.View;
import java.io.Closeable;
/**
* The main starting point for interacting with a Jenkins server.
*/
public class JenkinsServer implements Closeable {
private final Logger LOGGER = LoggerFactory.getLogger(getClass());
/**
* The transport client instance to use.
*/
private final JenkinsHttpConnection client;
/**
* Create a new Jenkins server reference given only the server address
*
* @param serverUri address of jenkins server (ex.
* http://localhost:8080/jenkins)
*/
public JenkinsServer(URI serverUri) {
this(new JenkinsHttpClient(serverUri));
}
/**
* Create a new Jenkins server reference given the address and credentials
*
* @param serverUri address of jenkins server (ex.
* http://localhost:8080/jenkins)
* @param username username to use when connecting
* @param passwordOrToken password (not recommended) or token (recommended)
*/
public JenkinsServer(URI serverUri, String username, String passwordOrToken) {
this(new JenkinsHttpClient(serverUri, username, passwordOrToken));
}
/**
* Create a new Jenkins server directly from an HTTP client (ADVANCED)
*
* @param client Specialized client to use.
*/
public JenkinsServer(final JenkinsHttpConnection client) {
this.client = client;
}
/**
* Get the current status of the Jenkins end-point by pinging it.
*
* @return true if Jenkins is up and running, false otherwise
*/
public boolean isRunning() {
try {
client.get("/");
return true;
} catch (IOException e) {
LOGGER.debug("isRunning()", e);
return false;
}
}
/**
* @return {@link JenkinsVersion}
*/
public JenkinsVersion getVersion() {
if (!client.isJenkinsVersionSet()) {
// Force a request to get at least once
// HttpHeader. The header contains the version
// information.
isRunning();
}
JenkinsVersion jv = new JenkinsVersion(client.getJenkinsVersion());
return jv;
}
/**
* Get a list of all the defined jobs on the server (at the summary level)
*
* @return list of defined jobs (summary level, for details @see Job#details
* @throws IOException in case of an error.
*/
public Map<String, Job> getJobs() throws IOException {
return getJobs(null, null);
}
/**
* Get a list of all the defined jobs on the server (in the given folder)
*
* @param folder {@link FolderJob}
* @return list of defined jobs (summary level, for details @see Job#details
* @throws IOException in case of an error.
*/
public Map<String, Job> getJobs(FolderJob folder) throws IOException {
return getJobs(folder, null);
}
/**
* Get a list of all the defined jobs on the server (at the specified view
* level)
*
* @param view The view to get jobs from.
* @return list of defined jobs (view level, for details @see Job#details
* @throws IOException in case of an error.
*/
public Map<String, Job> getJobs(String view) throws IOException {
return getJobs(null, view);
}
/**
* Get a list of all the defined jobs on the server (at the specified view
* level and in the specified folder)
*
* @param folder {@link FolderJob}
* @param view The view to use.
* @return list of defined jobs (view level, for details @see Job#details
* @throws IOException in case of an error.
*/
public Map<String, Job> getJobs(FolderJob folder, String view) throws IOException {
String path = UrlUtils.toBaseUrl(folder);
Class<? extends MainView> viewClass = MainView.class;
if (view != null) {
path = path + "view/" + EncodingUtils.encode(view) + "/";
viewClass = View.class;
}
List<Job> jobs = client.get(path, viewClass).getJobs();
return Maps.uniqueIndex(jobs, new Function<Job, String>() {
@Override
public String apply(Job job) {
job.setClient(client);
return job.getName();
}
});
}
/**
* Get a list of all the defined views on the server (at the summary level)
*
* @return list of defined views
* @throws IOException in case of an error.
*/
public Map<String, View> getViews() throws IOException {
return getViews(null);
}
/**
* Get a list of all the defined views on the server (at the summary level
* and in the given folder)
*
* @param folder {@link FolderJob}
* @return list of defined views
* @throws IOException in case of an error.
*/
public Map<String, View> getViews(FolderJob folder) throws IOException {
// This is much better than using &depth=2
// http://localhost:8080/api/json?pretty&tree=views[name,url,jobs[name,url]]
List<View> views = client.get(UrlUtils.toBaseUrl(folder) + "?tree=views[name,url,jobs[name,url]]", MainView.class).getViews();
return Maps.uniqueIndex(views, new Function<View, String>() {
@Override
public String apply(View view) {
view.setClient(client);
// TODO: Think about the following? Does there exists a
// simpler/more elegant method?
for (Job job : view.getJobs()) {
job.setClient(client);
}
for (View item : view.getViews()) {
item.setClient(client);
}
return view.getName();
}
});
}
/**
* Get a single view object from the server
*
* @param name name of the view in Jenkins
* @return the view object
* @throws IOException in case of an error.
*/
public View getView(String name) throws IOException {
return getView(null, name);
}
/**
* Get a single view object from the given folder
*
* @param folder The name of the folder.
* @param name name of the view in Jenkins
* @return the view object
* @throws IOException in case of an error.
*/
public View getView(FolderJob folder, String name) throws IOException {
try {
View resultView = client.get(UrlUtils.toViewBaseUrl(folder, name) + "/", View.class);
resultView.setClient(client);
// TODO: Think about the following? Does there exists a simpler/more
// elegant method?
for (Job job : resultView.getJobs()) {
job.setClient(client);
}
for (View view : resultView.getViews()) {
view.setClient(client);
}
return resultView;
} catch (HttpResponseException e) {
LOGGER.debug("getView(folder={}, name={}) status={}", folder, name, e.getStatusCode());
if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
// TODO: Think hard about this.
return null;
}
throw e;
}
}
/**
* Get a single Job from the server.
*
* @param jobName name of the job to get details of.
* @return A single Job, null if not present
* @throws IOException in case of an error.
*/
public JobWithDetails getJob(String jobName) throws IOException {
return getJob(null, UrlUtils.toFullJobPath(jobName));
}
/**
* Get a single Job from the given folder.
*
* @param folder {@link FolderJob}
* @param jobName name of the job to get details of.
* @return A single Job, null if not present
* @throws IOException in case of an error.
*/
public JobWithDetails getJob(FolderJob folder, String jobName) throws IOException {
try {
JobWithDetails job = client.get(UrlUtils.toJobBaseUrl(folder, jobName), JobWithDetails.class);
job.setClient(client);
return job;
} catch (HttpResponseException e) {
LOGGER.debug("getJob(folder={}, jobName={}) status={}", folder, jobName, e.getStatusCode());
if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
// TODO: Think hard about this.
return null;
}
throw e;
}
}
public MavenJobWithDetails getMavenJob(String jobName) throws IOException {
return getMavenJob(null, UrlUtils.toFullJobPath(jobName));
}
public MavenJobWithDetails getMavenJob(FolderJob folder, String jobName) throws IOException {
try {
MavenJobWithDetails job = client.get(UrlUtils.toJobBaseUrl(folder, jobName), MavenJobWithDetails.class);
job.setClient(client);
return job;
} catch (HttpResponseException e) {
LOGGER.debug("getMavenJob(jobName={}) status={}", jobName, e.getStatusCode());
if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
return null;
}
throw e;
}
}
public Optional<FolderJob> getFolderJob(Job job) throws IOException {
try {
FolderJob folder = client.get(job.getUrl(), FolderJob.class);
if (!folder.isFolder()) {
return Optional.absent();
}
folder.setClient(client);
return Optional.of(folder);
} catch (HttpResponseException e) {
LOGGER.debug("getForlderJob(job={}) status={}", job, e.getStatusCode());
if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
// TODO: Check if this is a good idea ? What about
// Optional.absent() ?
return null;
}
throw e;
}
}
/**
* Create a job on the server using the provided xml
*
* @param jobName name of the job to be created.
* @param jobXml the <code>config.xml</code> which should be used to create
* the job.
* @throws IOException in case of an error.
*/
public JenkinsServer createJob(String jobName, String jobXml) throws IOException {
return createJob(null, jobName, jobXml, false);
}
/**
* Create a job on the server using the provided xml
*
* @param jobName name of the job to be created.
* @param jobXml the <code>config.xml</code> which should be used to create
* the job.
* @param crumbFlag <code>true</code> to add <b>crumbIssuer</b>
* <code>false</code> otherwise.
* @throws IOException in case of an error.
*/
public JenkinsServer createJob(String jobName, String jobXml, Boolean crumbFlag) throws IOException {
return createJob(null, jobName, jobXml, crumbFlag);
}
/**
* Create a job on the server using the provided xml and in the provided
* folder
*
* @param folder {@link FolderJob}
* @param jobName name of the job to be created.
* @param jobXml the <code>config.xml</code> which should be used to create
* the job.
* @throws IOException in case of an error.
*/
public JenkinsServer createJob(FolderJob folder, String jobName, String jobXml) throws IOException {
return createJob(folder, jobName, jobXml, false);
}
/**
* Create a job on the server using the provided xml and in the provided
* folder
*
* @param folder {@link FolderJob}
* @param jobName name of the job to be created.
* @param jobXml the <code>config.xml</code> which should be used to create
* the job.
* @param crumbFlag <code>true</code> to add <b>crumbIssuer</b>
* <code>false</code> otherwise.
* @throws IOException in case of an error.
*/
public JenkinsServer createJob(FolderJob folder, String jobName, String jobXml, Boolean crumbFlag)
throws IOException {
client.post_xml(UrlUtils.toBaseUrl(folder) + "createItem?name=" + EncodingUtils.formParameter(jobName), jobXml, crumbFlag);
return this;
}
/**
* Create a view on the server using the provided xml
*
* @param viewName name of the view to be created.
* @param viewXml The configuration for the view.
* @throws IOException in case of an error.
*/
public JenkinsServer createView(String viewName, String viewXml) throws IOException {
return createView(null, viewName, viewXml, false);
}
/**
* Create a view on the server using the provided xml.
*
* @param viewName name of the view to be created.
* @param viewXml The configuration for the view.
* @param crumbFlag <code>true</code> to add <b>crumbIssuer</b>
* <code>false</code> otherwise.
* @throws IOException in case of an error.
*/
public JenkinsServer createView(String viewName, String viewXml, Boolean crumbFlag) throws IOException {
return createView(null, viewName, viewXml, crumbFlag);
}
/**
* Create a view on the server using the provided xml and in the provided
* folder.
*
* @param folder {@link FolderJob}
* @param viewName name of the view to be created.
* @param viewXml The configuration for the view.
* @throws IOException in case of an error.
*/
public JenkinsServer createView(FolderJob folder, String viewName, String viewXml) throws IOException {
return createView(folder, viewName, viewXml, false);
}
/**
* Create a view on the server using the provided xml and in the provided
* folder.
*
* @param folder the folder.
* @param viewName the view name.
* @param viewXml the view xml.
* @param crumbFlag <code>true</code> to add <b>crumbIssuer</b>
* <code>false</code> otherwise.
* @throws IOException in case of an error.
*/
public JenkinsServer createView(FolderJob folder, String viewName, String viewXml, Boolean crumbFlag)
throws IOException {
client.post_xml(UrlUtils.toBaseUrl(folder) + "createView?name=" + EncodingUtils.formParameter(viewName), viewXml,
crumbFlag);
return this;
}
/**
* Create a folder on the server (in the root)
*
* @param folderName name of the folder.
* @throws IOException in case of an error.
*/
public JenkinsServer createFolder(String folderName) throws IOException {
return createFolder(null, folderName, false);
}
/**
* Create a folder on the server (in the root)
*
* @param folderName name of the folder.
* @param crumbFlag <code>true</code> to add <b>crumbIssuer</b>
* <code>false</code> otherwise.
* @throws IOException in case of an error.
*/
public JenkinsServer createFolder(String folderName, Boolean crumbFlag) throws IOException {
return createFolder(null, folderName, crumbFlag);
}
/**
* Create a job on the server (in the given folder)
*
* @param folder {@link FolderJob}
* @param jobName name of the job.
* @throws IOException in case of an error.
*/
public JenkinsServer createFolder(FolderJob folder, String jobName) throws IOException {
return createFolder(folder, jobName, false);
}
/**
* Create a job on the server (in the given folder)
*
* @param folder {@link FolderJob}
* @param jobName name of the job.
* @param crumbFlag <code>true</code> to add <b>crumbIssuer</b>
* <code>false</code> otherwise.
* @throws IOException in case of an error.
*/
public JenkinsServer createFolder(FolderJob folder, String jobName, Boolean crumbFlag) throws IOException {
// https://gist.github.com/stuart-warren/7786892 was slightly helpful
// here
ImmutableMap<String, String> params = ImmutableMap.of("mode", "com.cloudbees.hudson.plugins.folder.Folder",
"name", EncodingUtils.formParameter(jobName), "from", "", "Submit", "OK");
client.post_form(UrlUtils.toBaseUrl(folder) + "createItem?", params, crumbFlag);
return this;
}
/**
* Get the xml description of an existing job
*
* @param jobName name of the job.
* @return the new job object
* @throws IOException in case of an error.
*/
public String getJobXml(String jobName) throws IOException {
return getJobXml(null, jobName);
}
/**
* Get the xml description of an existing job.
*
* @param jobName name of the job.
* @param folder {@link FolderJob}
* @return the new job object
* @throws IOException in case of an error.
*/
public String getJobXml(FolderJob folder, String jobName) throws IOException {
return client.get(UrlUtils.toJobBaseUrl(folder, jobName) + "/config.xml");
}
/**
* Get the description of an existing Label
*
* @param labelName name of the label.
* @return {@link LabelWithDetails}
* @throws IOException in case of an error.
*/
public LabelWithDetails getLabel(String labelName) throws IOException {
return client.get("/label/" + EncodingUtils.encode(labelName), LabelWithDetails.class);
}
/**
* Get a list of all the computers on the server (at the summary level)
*
* @return map of defined computers (summary level, for details @see
* Computer#details
* @throws IOException in case of an error.
*/
public Map<String, Computer> getComputers() throws IOException {
List<Computer> computers = client.get("computer/", Computer.class).getComputers();
return Maps.uniqueIndex(computers, new Function<Computer, String>() {
@Override
public String apply(Computer computer) {
computer.setClient(client);
return computer.getDisplayName().toLowerCase();
}
});
}
/**
* The ComputerSet class will give informations like
* {@link ComputerSet#getBusyExecutors()} or the
* {@link ComputerSet#getTotalExecutors()}.
*
* @return {@link ComputerSet}
* @throws IOException in case of an error.
*/
public ComputerSet getComputerSet() throws IOException {
return client.get("computer/?depth=2", ComputerSet.class);
}
/**
* This will give you back the {@link PluginManager}.
*
* @return {@link PluginManager}
* @throws IOException in case of a failure.
*/
public PluginManager getPluginManager() throws IOException {
return client.get("pluginManager/?depth=2", PluginManager.class);
}
/**
* Update the xml description of an existing view
*
* @param viewName name of the view.
* @param viewXml the view configuration.
* @throws IOException in case of an error.
*/
public JenkinsServer updateView(String viewName, String viewXml) throws IOException {
return this.updateView(viewName, viewXml, true);
}
public JenkinsServer updateView(String viewName, String viewXml, boolean crumbFlag) throws IOException {
client.post_xml("/view/" + EncodingUtils.encode(viewName) + "/config.xml", viewXml, crumbFlag);
return this;
}
public JenkinsServer updateView(FolderJob folder, String viewName, String viewXml) throws IOException {
client.post_xml(UrlUtils.toBaseUrl(folder) + "view/" + EncodingUtils.encode(viewName) + "/config.xml", viewXml, true);
return this;
}
public JenkinsServer updateView(FolderJob folder, String viewName, String viewXml, boolean crumbFlag)
throws IOException {
client.post_xml(UrlUtils.toBaseUrl(folder) + "view/" + EncodingUtils.encode(viewName) + "/config.xml", viewXml, crumbFlag);
return this;
}
/**
* Update the xml description of an existing job
*
* @param jobName name of the job to be updated.
* @param jobXml the configuration to be used for updating.
* @throws IOException in case of an error.
*/
public JenkinsServer updateJob(String jobName, String jobXml) throws IOException {
return this.updateJob(jobName, jobXml, true);
}
/**
* Update the xml description of an existing job
*
* @param jobName name of the job to be updated.
* @param jobXml the configuration to be used for updating.
* @param crumbFlag true/false.
* @throws IOException in case of an error.
*/
public JenkinsServer updateJob(String jobName, String jobXml, boolean crumbFlag) throws IOException {
return updateJob(null, jobName, jobXml, crumbFlag);
}
/**
* Update the xml description of an existing job
*
* @param folder the folder.
* @param jobName the name of the job.
* @param jobXml the job xml configuration.
* @param crumbFlag true/false.
* @throws IOException in case of an error.
*/
public JenkinsServer updateJob(FolderJob folder, String jobName, String jobXml, boolean crumbFlag)
throws IOException {
client.post_xml(UrlUtils.toJobBaseUrl(folder, jobName) + "/config.xml", jobXml, crumbFlag);
return this;
}
/**
* @param jobName name of the job.
* @param name name of the formParameter.
* @param description of the parameters.
* @param defaultValue the defaultValue for the parameters.
* @throws IOException in case of an error.
* @throws JAXBException in case of an error.
* @throws DocumentException in case of an error.
*/
public JenkinsServer addStringParam(String jobName, String name, String description, String defaultValue)
throws IOException, JAXBException, DocumentException {
String jobXml = this.getJobXml(jobName);
JobConfiguration jobConf = new JobConfiguration(jobXml);
jobXml = jobConf.addStringParam(name, description, defaultValue).asXml();
return this.updateJob(jobName, jobXml);
}
/**
* Sends the Quiet Down (Prepare for shutdown) message
*
* @throws IOException in case of an error.
*/
public JenkinsServer quietDown() throws IOException {
try {
client.get("/quietDown/");
} catch (org.apache.http.client.ClientProtocolException e) {
LOGGER.error("quietDown()", e);
}
return this;
}
/**
* Cancels the Quiet Down (Prepare for shutdown) message
*
* @throws IOException in case of an error.
*/
public JenkinsServer cancelQuietDown() throws IOException {
try {
client.post("/cancelQuietDown/");
} catch (org.apache.http.client.ClientProtocolException e) {
LOGGER.error("cancelQuietDown()", e);
}
return this;
}
/**
* Delete a job from Jenkins within a folder.
*
* @param folder The folder where the given job is located.
* @param jobName The job which should be deleted.
*
* @throws IOException in case of an error.
*/
public JenkinsServer deleteJob(FolderJob folder, String jobName) throws IOException {
return deleteJob(folder, jobName, false);
}
/**
* Delete a job from Jenkins within a folder.
*
* @param folder The folder where the given job is located.
* @param jobName The job which should be deleted.
* @param crumbFlag The crumbFlag
* @throws IOException in case of problems.
*/
public JenkinsServer deleteJob(FolderJob folder, String jobName, boolean crumbFlag) throws IOException {
client.post(UrlUtils.toJobBaseUrl(folder, jobName) + "/doDelete", crumbFlag);
return this;
}
/**
* Delete a job from jenkins
*
* @param jobName The name of the job which should be deleted.
* @throws IOException in case of an error.
*/
public JenkinsServer deleteJob(String jobName) throws IOException {
return deleteJob(jobName, false);
}
/**
* Delete a job from Jenkins.
*
* @param jobName The name of the job to be deleted.
* @param crumbFlag <code>true</code> to add <b>crumbIssuer</b>
* <code>false</code> otherwise.
* @throws IOException In case of an failure.
*/
public JenkinsServer deleteJob(String jobName, boolean crumbFlag) throws IOException {
client.post("/job/" + EncodingUtils.encode(jobName) + "/doDelete", crumbFlag);
return this;
}
/**
* Disable a job from jenkins
*
* @param jobName The name of the job which should be disabled.
* @throws IOException in case of an error.
*/
public JenkinsServer disableJob(String jobName) throws IOException {
return disableJob(jobName, false);
}
/**
* Disable a job from Jenkins.
*
* @param jobName The name of the job to be deleted.
* @param crumbFlag <code>true</code> to add <b>crumbIssuer</b>
* <code>false</code> otherwise.
* @throws IOException In case of an failure.
*/
public JenkinsServer disableJob(String jobName, boolean crumbFlag) throws IOException {
client.post("/job/" + EncodingUtils.encode(jobName) + "/disable", crumbFlag);
return this;
}
/**
* Enable a job from jenkins
*
* @param jobName name of the job which should be enabled.
* @throws IOException In case of an failure.
*/
public JenkinsServer enableJob(String jobName) throws IOException {
return enableJob(jobName, false);
}
/**
* Enable a job from Jenkins.
*
* @param jobName The name of the job to be deleted.
* @param crumbFlag <code>true</code> to add <b>crumbIssuer</b>
* <code>false</code> otherwise.
* @throws IOException In case of an failure.
*/
public JenkinsServer enableJob(String jobName, boolean crumbFlag) throws IOException {
client.post("/job/" + EncodingUtils.encode(jobName) + "/enable", crumbFlag);
return this;
}
/**
* Runs the provided groovy script on the server and returns the result.
*
* This is similar to running groovy scripts using the script console.
*
* In the instance where your script causes an exception, the server still
* returns a 200 status, so detecting errors is very challenging. It is
* recommended to use heuristics to check your return string for stack
* traces by detecting strings like "groovy.lang.(something)Exception".
*
* @param script The script to be executed.
* @return results The results of the script.
* @throws IOException in case of an error.
*/
public String runScript(String script) throws IOException {
return runScript(script, false);
}
/**
* Runs the provided groovy script on the server and returns the result.
*
* This is similar to running groovy scripts using the script console.
*
* In the instance where your script causes an exception, the server still
* returns a 200 status, so detecting errors is very challenging. It is
* recommended to use heuristics to check your return string for stack
* traces by detecting strings like "groovy.lang.(something)Exception".
*
* @param script The script to run.
* @param crumbFlag <code>true</code> to add <b>crumbIssuer</b>
* <code>false</code> otherwise.
* @return results The results of the run of the script.
* @throws IOException in case of an error.
*/
public String runScript(String script, boolean crumbFlag) throws IOException {
return client.post_text("/scriptText", "script=" + script, ContentType.APPLICATION_FORM_URLENCODED, crumbFlag);
}
public Queue getQueue() throws IOException {
// TODO: Check if using depth=1 is a good idea?
return client.get("queue/?depth=1", Queue.class);
}
public QueueItem getQueueItem(QueueReference ref) throws IOException {
try {
String url = ref.getQueueItemUrlPart();
// "/queue/item/" + id
QueueItem queueItem = client.get(url, QueueItem.class);
queueItem.setClient(client);
return queueItem;
} catch (HttpResponseException e) {
if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
return null;
}
throw e;
}
}
public Build getBuild(QueueItem q) throws IOException {
// http://ci.seitenbau.net/job/rainer-ansible-build/51/
try {
String url = q.getExecutable().getUrl();
// "/queue/item/" + id
Build job = client.get(url, Build.class);
job.setClient(client);
return job;
} catch (HttpResponseException e) {
if (e.getStatusCode() == HttpStatus.SC_NOT_FOUND) {
return null;
}
throw e;
}
}
/**
* Rename a job
*
* @param oldJobName existing job name.
* @param newJobName The new job name.
* @throws IOException In case of a failure.
*/
public JenkinsServer renameJob(String oldJobName, String newJobName) throws IOException {
return renameJob(null, oldJobName, newJobName, false);
}
/**
* Rename a job
*
* @param oldJobName existing job name.
* @param newJobName The new job name.
* @param crumbFlag <code>true</code> to add <b>crumbIssuer</b>
* <code>false</code> otherwise.
* @throws IOException In case of a failure.
*/
public JenkinsServer renameJob(String oldJobName, String newJobName, Boolean crumbFlag) throws IOException {
renameJob(null, oldJobName, newJobName, crumbFlag);
return this;
}
/**
* Rename a job
*
* @param folder {@link FolderJob}
* @param oldJobName existing job name.
* @param newJobName The new job name.
* @throws IOException In case of a failure.
*/
public JenkinsServer renameJob(FolderJob folder, String oldJobName, String newJobName) throws IOException {
return renameJob(folder, oldJobName, newJobName, false);
}
/**
* Rename a job
*
* @param folder {@link FolderJob}
* @param oldJobName existing job name.
* @param newJobName The new job name.
* @param crumbFlag <code>true</code> to add <b>crumbIssuer</b>
* <code>false</code> otherwise.
* @throws IOException In case of a failure.
*/
public JenkinsServer renameJob(FolderJob folder, String oldJobName, String newJobName, Boolean crumbFlag)
throws IOException {
client.post(UrlUtils.toJobBaseUrl(folder, oldJobName)
+ "/doRename?newName=" + EncodingUtils.formParameter(newJobName),
crumbFlag);
return this;
}
/**
* Closes underlying resources.
* Closed instances should no longer be used
* Closing an already closed instance has no side effects
*/
@Override
public void close() {
client.close();
}
/**
* Restart Jenkins without waiting for any existing build to complete
*
* @param crumbFlag
* <code>true</code> to add <b>crumbIssuer</b> <code>false</code>
* otherwise.
* @throws IOException
* in case of an error.
*/
public JenkinsServer restart(Boolean crumbFlag) throws IOException {
try {
client.post("/restart", crumbFlag);
} catch (org.apache.http.client.ClientProtocolException e) {
LOGGER.error("restart()", e);
}
return this;
}
/**
* safeRestart: Puts Jenkins into the quiet mode, wait for existing builds
* to be completed, and then restart Jenkins
*
* @param crumbFlag
* <code>true</code> to add <b>crumbIssuer</b> <code>false</code>
* otherwise.
* @throws IOException
* in case of an error.
*/
public JenkinsServer safeRestart(Boolean crumbFlag) throws IOException {
try {
client.post("/safeRestart", crumbFlag);
} catch (org.apache.http.client.ClientProtocolException e) {
LOGGER.error("safeRestart()", e);
}
return this;
}
/**
* Shutdown Jenkins without waiting for any existing build to complete
*
* @param crumbFlag
* <code>true</code> to add <b>crumbIssuer</b> <code>false</code>
* otherwise.
* @throws IOException
* in case of an error.
*/
public JenkinsServer exit(Boolean crumbFlag) throws IOException {
try {
client.post("/exit", crumbFlag);
} catch (org.apache.http.client.ClientProtocolException e) {
LOGGER.error("exit()", e);
}
return this;
}
/**
* safeExit: Puts Jenkins into the quiet mode, wait for existing builds to
* be completed, and then shut down Jenkins
*
* @param crumbFlag
* <code>true</code> to add <b>crumbIssuer</b> <code>false</code>
* otherwise.
* @throws IOException
* in case of an error.
*/
public JenkinsServer safeExit(Boolean crumbFlag) throws IOException {
try {
client.post("/safeExit", crumbFlag);
} catch (org.apache.http.client.ClientProtocolException e) {
LOGGER.error("safeExit()", e);
}
return this;
}
}