From 9ba5525492f46a5749389cb8dac18368e22b72b0 Mon Sep 17 00:00:00 2001 From: Karl Heinz Marbaise Date: Sat, 26 Nov 2016 20:03:08 +0100 Subject: [PATCH] Fixed #203 o added methods getJobXml and updateJob with folder parameters. Patch applied of RainerW --- ReleaseNotes.md | 14 ++ .../com/offbytwo/jenkins/JenkinsServer.java | 130 ++++++++++-------- 2 files changed, 87 insertions(+), 57 deletions(-) diff --git a/ReleaseNotes.md b/ReleaseNotes.md index 1f18f5e0..7a2e806e 100644 --- a/ReleaseNotes.md +++ b/ReleaseNotes.md @@ -14,6 +14,18 @@ ### API Changes + * [Fixed Issue 203][issue-203] with [pull request #204][pull-204] + of RainerW + + Added methods getJobXml and updateJob with folder parameter. + +```java +public class JenkinsServer { + String getJobXml(FolderJob folder, String jobName); + void updateJob(FolderJob folder, String jobName, String jobXml, boolean crumbFlag); +} +``` + * [Fixed Issue 207][issue-207] Added @@ -705,11 +717,13 @@ TestReport testReport = mavenJob.getLastSuccessfulBuild().getTestReport(); [issue-200]: https://github.com/jenkinsci/java-client-api/issues/200 [issue-201]: https://github.com/jenkinsci/java-client-api/issues/201 [issue-202]: https://github.com/jenkinsci/java-client-api/issues/202 +[issue-203]: https://github.com/jenkinsci/java-client-api/issues/203 [issue-207]: https://github.com/jenkinsci/java-client-api/issues/207 [pull-123]: https://github.com/jenkinsci/java-client-api/pull/123 [pull-149]: https://github.com/jenkinsci/java-client-api/pull/149 [pull-158]: https://github.com/jenkinsci/java-client-api/pull/158 [pull-163]: https://github.com/jenkinsci/java-client-api/pull/163 +[pull-204]: https://github.com/jenkinsci/java-client-api/pull/204 [jissue-35002]: https://issues.jenkins-ci.org/browse/JENKINS-35002 [jissue-35108]: https://issues.jenkins-ci.org/browse/JENKINS-35108 [jissue-38787]: https://issues.jenkins-ci.org/browse/JENKINS-38787 diff --git a/jenkins-client/src/main/java/com/offbytwo/jenkins/JenkinsServer.java b/jenkins-client/src/main/java/com/offbytwo/jenkins/JenkinsServer.java index 6018bbce..79793b99 100644 --- a/jenkins-client/src/main/java/com/offbytwo/jenkins/JenkinsServer.java +++ b/jenkins-client/src/main/java/com/offbytwo/jenkins/JenkinsServer.java @@ -150,10 +150,7 @@ public Map getJobs(String view) throws IOException { * @throws IOException */ public Map getJobs(FolderJob folder, String view) throws IOException { - String path = "/"; - if (folder != null) { - path = folder.getUrl(); - } + String path = toBaseUrl(folder); Class viewClass = MainView.class; if (view != null) { path = path + "view/" + EncodingUtils.encode(view) + "/"; @@ -187,11 +184,7 @@ public Map getViews() throws IOException { * @throws IOException */ public Map getViews(FolderJob folder) throws IOException { - String path = "/"; - if (folder != null) { - path = folder.getUrl(); - } - List views = client.get(path + "?depth=1", MainView.class).getViews(); + List views = client.get(toBaseUrl(folder) + "?depth=1", MainView.class).getViews(); return Maps.uniqueIndex(views, new Function() { @Override public String apply(View view) { @@ -235,13 +228,8 @@ public View getView(String name) throws IOException { * @throws IOException */ public View getView(FolderJob folder, String name) throws IOException { - String path = "/"; - if (folder != null) { - path = folder.getUrl(); - } - try { - View resultView = client.get(path + "view/" + EncodingUtils.encode(name) + "/", View.class); + View resultView = client.get(toViewBaseUrl(folder,name) + "/", View.class); resultView.setClient(client); // TODO: Think about the following? Does there exists a simpler/more @@ -280,12 +268,8 @@ public JobWithDetails getJob(String jobName) throws IOException { * @throws IOException */ public JobWithDetails getJob(FolderJob folder, String jobName) throws IOException { - String path = "/"; - if (folder != null) { - path = folder.getUrl(); - } try { - JobWithDetails job = client.get(path + "job/" + EncodingUtils.encode(jobName), JobWithDetails.class); + JobWithDetails job = client.get(toJobBaseUrl(folder,jobName), JobWithDetails.class); job.setClient(client); return job; @@ -304,12 +288,8 @@ public MavenJobWithDetails getMavenJob(String jobName) throws IOException { } public MavenJobWithDetails getMavenJob(FolderJob folder, String jobName) throws IOException { - String path = "/"; - if (folder != null) { - path = folder.getUrl(); - } try { - MavenJobWithDetails job = client.get(path + "job/" + EncodingUtils.encode(jobName), + MavenJobWithDetails job = client.get(toJobBaseUrl(folder, jobName), MavenJobWithDetails.class); job.setClient(client); @@ -382,11 +362,7 @@ public void createJob(FolderJob folder, String jobName, String jobXml) throws IO * @throws IOException */ public void createJob(FolderJob folder, String jobName, String jobXml, Boolean crumbFlag) throws IOException { - String path = "/"; - if (folder != null) { - path = folder.getUrl(); - } - client.post_xml(path + "createItem?name=" + EncodingUtils.encodeParam(jobName), jobXml, crumbFlag); + client.post_xml( toBaseUrl(folder) + "createItem?name=" + EncodingUtils.encodeParam(jobName), jobXml, crumbFlag); } /** @@ -428,11 +404,7 @@ public void createView(FolderJob folder, String viewName, String viewXml) throws * @throws IOException */ public void createView(FolderJob folder, String viewName, String viewXml, Boolean crumbFlag) throws IOException { - String path = "/"; - if (folder != null) { - path = folder.getUrl(); - } - client.post_xml(path + "createView?name=" + EncodingUtils.encodeParam(viewName), viewXml, crumbFlag); + client.post_xml(toBaseUrl(folder) + "createView?name=" + EncodingUtils.encodeParam(viewName), viewXml, crumbFlag); } /** @@ -468,15 +440,11 @@ public void createFolder(FolderJob folder, String jobName) throws IOException { * @throws IOException */ public void createFolder(FolderJob folder, String jobName, Boolean crumbFlag) throws IOException { - String path = "/"; - if (folder != null) { - path = folder.getUrl(); - } // https://gist.github.com/stuart-warren/7786892 was slightly helpful // here ImmutableMap params = ImmutableMap.of("mode", "com.cloudbees.hudson.plugins.folder.Folder", "name", EncodingUtils.encodeParam(jobName), "from", "", "Submit", "OK"); - client.post_form(path + "createItem?", params, crumbFlag); + client.post_form(toBaseUrl(folder) + "createItem?", params, crumbFlag); } /** @@ -486,7 +454,18 @@ public void createFolder(FolderJob folder, String jobName, Boolean crumbFlag) th * @throws IOException */ public String getJobXml(String jobName) throws IOException { - return client.get("/job/" + EncodingUtils.encode(jobName) + "/config.xml"); + return getJobXml(null,jobName); + } + + /** + * Get the xml description of an existing job + * + * @return the new job object + * @throws IOException + */ + public String getJobXml(FolderJob folder, String jobName) throws IOException + { + return client.get(toJobBaseUrl(folder, jobName) + "/config.xml"); } /** @@ -557,15 +536,29 @@ public void updateView(String viewName, String viewXml, boolean crumbFlag) throw /** * Update the xml description of an existing job * - * @return the new job object * @throws IOException */ public void updateJob(String jobName, String jobXml) throws IOException { this.updateJob(jobName, jobXml, true); } + /** + * Update the xml description of an existing job + * + * @throws IOException + */ public void updateJob(String jobName, String jobXml, boolean crumbFlag) throws IOException { - client.post_xml("/job/" + EncodingUtils.encode(jobName) + "/config.xml", jobXml, crumbFlag); + updateJob(null,jobName,jobXml,crumbFlag); + } + + /** + * Update the xml description of an existing job + * + * @throws IOException + */ + public void updateJob(FolderJob folder, String jobName, String jobXml, boolean crumbFlag) throws IOException + { + client.post_xml(toJobBaseUrl(folder, jobName) + "/config.xml", jobXml, crumbFlag); } public void addStringParam(String jobName, String name, String description, String defaultValue) @@ -630,11 +623,7 @@ public void deleteJob(FolderJob folder, String jobName) throws IOException { * in case of problems. */ public void deleteJob(FolderJob folder, String jobName, boolean crumbFlag) throws IOException { - String path = "/"; - if (folder != null) { - path = folder.getUrl(); - } - client.post(path + "/job/" + EncodingUtils.encode(jobName) + "/doDelete", crumbFlag); + client.post(toJobBaseUrl(folder, jobName) + "/doDelete", crumbFlag); } /* @@ -825,14 +814,41 @@ public void renameJob(FolderJob folder, String oldJobName, String newJobName) th */ public void renameJob(FolderJob folder, String oldJobName, String newJobName, Boolean crumbFlag) throws IOException { - - String path = "/"; - if (folder != null) { - path = folder.getUrl(); - } - client.post(path + "job/" + EncodingUtils.encode(oldJobName) + "/doRename?newName=" - + EncodingUtils.encodeParam(newJobName), crumbFlag); - - } + client.post( toJobBaseUrl(folder, oldJobName) + "/doRename?newName=" + EncodingUtils.encodeParam(newJobName), crumbFlag); + } + + /** + * Helper to create a base url in case a folder is given + * @param folder the folder or {@code null} + * @return + */ + private String toBaseUrl(FolderJob folder) + { + String path = "/"; + if (folder != null) { + path = folder.getUrl(); + } + return path; + } + + /** + * Helper to create the base url for a job, with or without a given folder + * @param folder the folder or {@code null} + * @return + */ + private String toJobBaseUrl(FolderJob folder, String jobName) + { + return toBaseUrl(folder) + "job/" + EncodingUtils.encode(jobName); + } + + /** + * Helper to create the base url for a view, with or without a given folder + * @param folder the folder or {@code null} + * @return + */ + private String toViewBaseUrl(FolderJob folder, String name) + { + return toBaseUrl(folder) + "view/" + EncodingUtils.encode(name); + } }