8
8
9
9
import java .io .IOException ;
10
10
import java .net .URI ;
11
- import java .util .Arrays ;
12
11
import java .util .List ;
13
12
import java .util .Map ;
14
13
27
26
import com .google .common .collect .Maps ;
28
27
import com .offbytwo .jenkins .client .JenkinsHttpClient ;
29
28
import com .offbytwo .jenkins .client .util .EncodingUtils ;
29
+ import com .offbytwo .jenkins .client .util .UrlUtils ;
30
30
import com .offbytwo .jenkins .helper .JenkinsVersion ;
31
31
import com .offbytwo .jenkins .model .Build ;
32
32
import com .offbytwo .jenkins .model .Computer ;
@@ -155,7 +155,7 @@ public Map<String, Job> getJobs(String view) throws IOException {
155
155
* @throws IOException in case of an error.
156
156
*/
157
157
public Map <String , Job > getJobs (FolderJob folder , String view ) throws IOException {
158
- String path = toBaseUrl (folder );
158
+ String path = UrlUtils . toBaseUrl (folder );
159
159
Class <? extends MainView > viewClass = MainView .class ;
160
160
if (view != null ) {
161
161
path = path + "view/" + EncodingUtils .encode (view ) + "/" ;
@@ -192,7 +192,7 @@ public Map<String, View> getViews() throws IOException {
192
192
public Map <String , View > getViews (FolderJob folder ) throws IOException {
193
193
// This is much better than using &depth=2
194
194
// http://localhost:8080/api/json?pretty&tree=views[name,url,jobs[name,url]]
195
- List <View > views = client .get (toBaseUrl (folder ) + "?tree=views[name,url,jobs[name,url]]" , MainView .class ).getViews ();
195
+ List <View > views = client .get (UrlUtils . toBaseUrl (folder ) + "?tree=views[name,url,jobs[name,url]]" , MainView .class ).getViews ();
196
196
return Maps .uniqueIndex (views , new Function <View , String >() {
197
197
@ Override
198
198
public String apply (View view ) {
@@ -232,7 +232,7 @@ public View getView(String name) throws IOException {
232
232
*/
233
233
public View getView (FolderJob folder , String name ) throws IOException {
234
234
try {
235
- View resultView = client .get (toViewBaseUrl (folder , name ) + "/" , View .class );
235
+ View resultView = client .get (UrlUtils . toViewBaseUrl (folder , name ) + "/" , View .class );
236
236
resultView .setClient (client );
237
237
238
238
// TODO: Think about the following? Does there exists a simpler/more
@@ -262,7 +262,7 @@ public View getView(FolderJob folder, String name) throws IOException {
262
262
* @throws IOException in case of an error.
263
263
*/
264
264
public JobWithDetails getJob (String jobName ) throws IOException {
265
- return getJob (null , parseFullName (jobName ));
265
+ return getJob (null , UrlUtils . toFullJobPath (jobName ));
266
266
}
267
267
268
268
/**
@@ -275,7 +275,7 @@ public JobWithDetails getJob(String jobName) throws IOException {
275
275
*/
276
276
public JobWithDetails getJob (FolderJob folder , String jobName ) throws IOException {
277
277
try {
278
- JobWithDetails job = client .get (toJobBaseUrl (folder , jobName ), JobWithDetails .class );
278
+ JobWithDetails job = client .get (UrlUtils . toJobBaseUrl (folder , jobName ), JobWithDetails .class );
279
279
job .setClient (client );
280
280
281
281
return job ;
@@ -290,12 +290,12 @@ public JobWithDetails getJob(FolderJob folder, String jobName) throws IOExceptio
290
290
}
291
291
292
292
public MavenJobWithDetails getMavenJob (String jobName ) throws IOException {
293
- return getMavenJob (null , parseFullName (jobName ));
293
+ return getMavenJob (null , UrlUtils . toFullJobPath (jobName ));
294
294
}
295
295
296
296
public MavenJobWithDetails getMavenJob (FolderJob folder , String jobName ) throws IOException {
297
297
try {
298
- MavenJobWithDetails job = client .get (toJobBaseUrl (folder , jobName ), MavenJobWithDetails .class );
298
+ MavenJobWithDetails job = client .get (UrlUtils . toJobBaseUrl (folder , jobName ), MavenJobWithDetails .class );
299
299
job .setClient (client );
300
300
301
301
return job ;
@@ -381,7 +381,7 @@ public void createJob(FolderJob folder, String jobName, String jobXml) throws IO
381
381
* @throws IOException in case of an error.
382
382
*/
383
383
public void createJob (FolderJob folder , String jobName , String jobXml , Boolean crumbFlag ) throws IOException {
384
- client .post_xml (toBaseUrl (folder ) + "createItem?name=" + EncodingUtils .encodeParam (jobName ), jobXml , crumbFlag );
384
+ client .post_xml (UrlUtils . toBaseUrl (folder ) + "createItem?name=" + EncodingUtils .encodeParam (jobName ), jobXml , crumbFlag );
385
385
}
386
386
387
387
/**
@@ -433,7 +433,7 @@ public void createView(FolderJob folder, String viewName, String viewXml) throws
433
433
* @throws IOException in case of an error.
434
434
*/
435
435
public void createView (FolderJob folder , String viewName , String viewXml , Boolean crumbFlag ) throws IOException {
436
- client .post_xml (toBaseUrl (folder ) + "createView?name=" + EncodingUtils .encodeParam (viewName ), viewXml ,
436
+ client .post_xml (UrlUtils . toBaseUrl (folder ) + "createView?name=" + EncodingUtils .encodeParam (viewName ), viewXml ,
437
437
crumbFlag );
438
438
}
439
439
@@ -484,7 +484,7 @@ public void createFolder(FolderJob folder, String jobName, Boolean crumbFlag) th
484
484
// here
485
485
ImmutableMap <String , String > params = ImmutableMap .of ("mode" , "com.cloudbees.hudson.plugins.folder.Folder" ,
486
486
"name" , EncodingUtils .encodeParam (jobName ), "from" , "" , "Submit" , "OK" );
487
- client .post_form (toBaseUrl (folder ) + "createItem?" , params , crumbFlag );
487
+ client .post_form (UrlUtils . toBaseUrl (folder ) + "createItem?" , params , crumbFlag );
488
488
}
489
489
490
490
/**
@@ -507,7 +507,7 @@ public String getJobXml(String jobName) throws IOException {
507
507
* @throws IOException in case of an error.
508
508
*/
509
509
public String getJobXml (FolderJob folder , String jobName ) throws IOException {
510
- return client .get (toJobBaseUrl (folder , jobName ) + "/config.xml" );
510
+ return client .get (UrlUtils . toJobBaseUrl (folder , jobName ) + "/config.xml" );
511
511
}
512
512
513
513
/**
@@ -577,11 +577,11 @@ public void updateView(String viewName, String viewXml, boolean crumbFlag) throw
577
577
}
578
578
579
579
public void updateView (FolderJob folder , String viewName , String viewXml ) throws IOException {
580
- client .post_xml (toBaseUrl (folder ) + "view/" + EncodingUtils .encode (viewName ) + "/config.xml" , viewXml , true );
580
+ client .post_xml (UrlUtils . toBaseUrl (folder ) + "view/" + EncodingUtils .encode (viewName ) + "/config.xml" , viewXml , true );
581
581
}
582
582
583
583
public void updateView (FolderJob folder , String viewName , String viewXml , boolean crumbFlag ) throws IOException {
584
- client .post_xml (toBaseUrl (folder ) + "view/" + EncodingUtils .encode (viewName ) + "/config.xml" , viewXml , crumbFlag );
584
+ client .post_xml (UrlUtils . toBaseUrl (folder ) + "view/" + EncodingUtils .encode (viewName ) + "/config.xml" , viewXml , crumbFlag );
585
585
}
586
586
587
587
/**
@@ -617,7 +617,7 @@ public void updateJob(String jobName, String jobXml, boolean crumbFlag) throws I
617
617
* @throws IOException in case of an error.
618
618
*/
619
619
public void updateJob (FolderJob folder , String jobName , String jobXml , boolean crumbFlag ) throws IOException {
620
- client .post_xml (toJobBaseUrl (folder , jobName ) + "/config.xml" , jobXml , crumbFlag );
620
+ client .post_xml (UrlUtils . toJobBaseUrl (folder , jobName ) + "/config.xml" , jobXml , crumbFlag );
621
621
}
622
622
623
623
/**
@@ -685,7 +685,7 @@ public void deleteJob(FolderJob folder, String jobName) throws IOException {
685
685
* @throws IOException in case of problems.
686
686
*/
687
687
public void deleteJob (FolderJob folder , String jobName , boolean crumbFlag ) throws IOException {
688
- client .post (toJobBaseUrl (folder , jobName ) + "/doDelete" , crumbFlag );
688
+ client .post (UrlUtils . toJobBaseUrl (folder , jobName ) + "/doDelete" , crumbFlag );
689
689
}
690
690
691
691
/**
@@ -878,80 +878,10 @@ public void renameJob(FolderJob folder, String oldJobName, String newJobName) th
878
878
*/
879
879
public void renameJob (FolderJob folder , String oldJobName , String newJobName , Boolean crumbFlag )
880
880
throws IOException {
881
- client .post (toJobBaseUrl (folder , oldJobName ) + "/doRename?newName=" + EncodingUtils .encodeParam (newJobName ),
882
- crumbFlag );
883
- }
884
-
885
- /**
886
- * Helper to create a base url in case a folder is given
887
- *
888
- * @param folder the folder or {@code null}
889
- * @return The created base url.
890
- */
891
- private String toBaseUrl (FolderJob folder ) {
892
- String path = "/" ;
893
- if (folder != null ) {
894
- path = folder .getUrl ();
895
- }
896
- return path ;
897
- }
898
-
899
- /**
900
- * Helper to create the base url for a job, with or without a given folder
901
- *
902
- * @param folder the folder or {@code null}
903
- * @param jobName the name of the job.
904
- * @return converted base url.
905
- */
906
- private String toJobBaseUrl (FolderJob folder , String jobName ) {
907
- String jobBaseUrl = toBaseUrl (folder ) + "job/" ;
908
-
909
- String [] jobNameParts = jobName .split ("/" );
910
- for (int i = 0 ; i < jobNameParts .length ; i ++) {
911
- jobBaseUrl += EncodingUtils .encode (jobNameParts [i ]);
912
-
913
- if (i != jobNameParts .length - 1 ) {
914
- jobBaseUrl += "/" ;
915
- }
916
- }
917
-
918
- return jobBaseUrl ;
881
+ client .post (UrlUtils .toJobBaseUrl (folder , oldJobName )
882
+ + "/doRename?newName=" + EncodingUtils .encodeParam (newJobName ),
883
+ crumbFlag );
919
884
}
920
885
921
- /**
922
- * Helper to create the base url for a view, with or without a given folder
923
- *
924
- * @param folder the folder or {@code null}
925
- * @param name the of the view.
926
- * @return converted view url.
927
- */
928
- private String toViewBaseUrl (FolderJob folder , String name ) {
929
- return toBaseUrl (folder ) + "view/" + EncodingUtils .encode (name );
930
- }
931
886
932
- /**
933
- * Parses the provided job name for folders to get the full path for the job.
934
- * @param jobName the fullName of the job.
935
- * @return the path of the job including folders if present.
936
- */
937
- private String parseFullName (String jobName )
938
- {
939
- if (!jobName .contains ("/" )) {
940
- return jobName ;
941
- }
942
-
943
- List <String > foldersAndJob = Arrays .asList (jobName .split ("/" ));
944
-
945
- String foldersAndJobName = "" ;
946
-
947
- for (int i = 0 ; i < foldersAndJob .size (); i ++) {
948
- foldersAndJobName += foldersAndJob .get (i );
949
-
950
- if (i != foldersAndJob .size () -1 ) {
951
- foldersAndJobName += "/job/" ;
952
- }
953
- }
954
-
955
- return foldersAndJobName ;
956
- }
957
887
}
0 commit comments